#include "parse_tool.h"
|
|
#include <sstream>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <algorithm>
|
|
#include <cassert>
|
|
#include <utility>
|
|
#include <filesystem>
|
|
|
|
parse_tool::parse_tool(std::string tool, tool_env env)
|
|
{
|
|
std::stringstream generator(tool);
|
|
while(generator.good())
|
|
{
|
|
std::string line;
|
|
std::getline(generator,line);
|
|
assert(!line.empty());
|
|
if((*line.begin())==';') break;
|
|
auto env_start = std::find(line.begin(),line.end(),'[');
|
|
auto env_end_pos = line.rfind(']');
|
|
env_end_pos = env_end_pos >= line.size() ? line.size() : env_end_pos;
|
|
auto env_end = line.begin()+env_end_pos;
|
|
std::string operand{line.begin(), env_start};
|
|
|
|
auto lenv = parse_toolenv(env, env_start, env_end);
|
|
|
|
auto mtool = mktool(operand, lenv);
|
|
assert(mtool.has_value());
|
|
if(pipeline.size()!=0)
|
|
{
|
|
pipeline[pipeline.size()-1]->init(mtool);
|
|
}
|
|
pipeline.push_back(mtool.value());
|
|
}
|
|
init(next);
|
|
}
|
|
|
|
void parse_tool::init(std::optional<std::shared_ptr<tool>> next)
|
|
{
|
|
pipeline[pipeline.size()-1]->init(next);
|
|
}
|
|
|
|
void parse_tool::execute()
|
|
{
|
|
auto it = pipeline.rbegin();
|
|
for(;it!=pipeline.rend();it++)
|
|
{
|
|
if((*it)->has_queue())
|
|
{
|
|
(*it)->execute();
|
|
return;
|
|
}
|
|
}
|
|
pipeline[0]->enqueue(queue.front());
|
|
queue.pop_front();
|
|
}
|
|
|
|
std::string mod_open(tool_env env)
|
|
{
|
|
auto fname = env["file"];
|
|
if(!std::filesystem::exists(fname))
|
|
{
|
|
assert(env.count("_path"));
|
|
assert(env.count("module"));
|
|
std::vector<std::filesystem::path> path;
|
|
std::stringstream curr;
|
|
for(auto it : env["_path"])
|
|
{
|
|
if(it == '\0')
|
|
{
|
|
path.push_back(curr.str());
|
|
curr = std::stringstream{};
|
|
}
|
|
else
|
|
{
|
|
curr << it;
|
|
}
|
|
}
|
|
|
|
if(curr.str().size())
|
|
{
|
|
path.push_back(curr.str());
|
|
}
|
|
|
|
for(auto dir : path)
|
|
{
|
|
auto file = (dir/std::filesystem::path{env["module"]}).replace_extension("r9");
|
|
|
|
if(!std::filesystem::exists(file))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
fname = file;
|
|
}
|
|
}
|
|
assert(std::filesystem::exists(fname));
|
|
std::ifstream input(fname);
|
|
std::stringstream sstr;
|
|
while(input >> sstr.rdbuf());
|
|
|
|
return sstr.str();
|
|
}
|
|
|
|
pipe::pipe(tool_env env)
|
|
: parse_tool(mod_open(env), env)
|
|
{}
|