|
|
- #pragma once
- #include <map>
- #include <string>
- #include <sstream>
- #include <optional>
- #include <memory>
-
- using tool_env = std::map<std::string, std::string>;
-
- class tool
- {
- public:
- virtual void init(std::optional<std::shared_ptr<tool>> next) = 0;
- virtual void execute() = 0;
- virtual void enqueue(std::string) = 0;
- virtual bool has_queue() = 0;
- };
-
- inline std::string escape(std::string v)
- {
- std::stringstream sstr;
- for(char c : v)
- {
- switch(c)
- {
- case '\\':
- sstr<<"\\\\";
- break;
- case '\n':
- sstr<<"\\n";
- break;
- case '\t':
- sstr<<"\\t";
- break;
- case ':':
- sstr<<"\\:";
- break;
- case 0:
- sstr<<"\\0";
- break;
- default:
- sstr<<c;
- break;
- }
- }
- return sstr.str();
- }
-
-
- inline std::string unescape(std::string v)
- {
- std::stringstream sstr;
- std::stringstream orig{v};
- char point;
- while(orig.good())
- {
- orig >> point;
- if(point!='\\')
- {
- sstr << point;
- }
- else
- {
- orig >> point;
- switch(point)
- {
- case '\\':
- sstr<<"\\";
- break;
- case 'n':
- sstr<<"\n";
- break;
- case 't':
- sstr<<"\t";
- break;
- case '0':
- sstr<<"\0";
- break;
- case ':':
- sstr<<":";
- break;
- default:
- sstr<<point;
- break;
- }
- }
- }
- return sstr.str();
- }
-
- std::optional<std::shared_ptr<tool> > mktool(std::string toolname, tool_env env);
|