|
|
- #include "tool.h"
- #include <deque>
- #include <iostream>
- #include <fstream>
-
- class identity : public tool
- {
- std::optional<std::shared_ptr<tool>> n;
- tool_env me;
- std::deque<std::string> queue;
- public:
- identity(tool_env t)
- : me{t}
- {}
-
- virtual void init(std::optional<std::shared_ptr<tool>> next)
- {
- n = next;
- }
- virtual void execute()
- {
- if(has_queue())
- {
- if(n.has_value())
- n.value()->enqueue(queue.front());
- queue.pop_front();
- }
- }
- virtual void enqueue(std::string v)
- {
- queue.push_back(v);
- }
- virtual bool has_queue()
- {
- return queue.size()!=0;
- }
- };
-
- class echo : public tool
- {
- std::optional<std::shared_ptr<tool>> n;
- tool_env me;
- std::deque<std::string> queue;
- std::optional<std::unique_ptr<std::ofstream> > outlet = std::nullopt;
- public:
- echo(tool_env t)
- : me{t}
- {}
-
- virtual void init(std::optional<std::shared_ptr<tool>> next)
- {
- n = next;
- if(me.find("file")!=me.end())
- {
- outlet = std::make_unique<std::ofstream>(me["file"]);
- }
- }
- virtual void execute()
- {
- if(has_queue())
- {
- if(outlet.has_value())
- {
- (*outlet.value())<<escape(queue.front()) << std::endl;
- }
- else
- {
- std::cout << escape(queue.front()) << std::endl;
- }
-
- if(n.has_value())
- n.value()->enqueue(queue.front());
- queue.pop_front();
- }
- }
- virtual void enqueue(std::string v)
- {
- queue.push_back(v);
- }
- virtual bool has_queue()
- {
- return queue.size()!=0;
- }
- };
|