|
|
- #pragma once
- #include "tool.h"
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <array>
- #include <string_view>
- #include <string>
-
- class file_lines : public tool
- {
- std::optional<std::shared_ptr<tool>> n;
- tool_env me;
- std::ifstream source;
- bool has_buffered = false;
- std::string next;
- std::stringstream buffer;
- public:
- file_lines(std::ifstream&& stream, tool_env t)
- : me{t}
- {
- source = std::move(stream);
- }
-
- file_lines(tool_env t)
- : me{t}
- {
- source.open(t["file"]);
- }
-
- virtual void init(std::optional<std::shared_ptr<tool>> next)
- {
- n = next;
- }
- /*
- virtual void execute()
- {
- if(has_queue())
- {
- if(n.has_value() && next!="\0")
- n.value()->enqueue(next);
- has_buffered = false;
- }
- }
-
- virtual void enqueue(std::string v)
- {}
-
- virtual bool has_queue()
- {
- if(has_buffered)
- {
- return true;
- }
- else
- {
- next = "";
- std::array<char, 4096> buff;
- if(source.good() && buffer.rdbuf()->in_avail() <= buff.size())
- {
- if(me["reload"]!="true")
- {
- source.readsome(&buff.front(), buff.size());
- size_t sz = source.gcount();
- std::string_view data{&buff.front(),sz};
- buffer<<data;
- }
- else if(buffer.rdbuf()->in_avail()==0)
- {
- source.clear();
- source.seekg(0, std::ios::beg);
-
- while(source >> buffer.rdbuf());
- }
- }
- {
- std::string_view data;
- do{
- buffer.getline(&buff.front(), buff.size());
- size_t sz = buffer.gcount();
- data = std::string_view{&buff.front(),sz};
- next+=data.size();
- }
- while(data.size() == buff.size());
- if(next.empty())
- {
- return false;
- }
- has_buffered = true;
- return true;
- }
-
- }
- }*/
- virtual void execute()
- {
- if(has_queue())
- {
- if(n.has_value())
- n.value()->enqueue(next);
- has_buffered = false;
- }
- }
-
- virtual void enqueue(std::string v)
- {}
-
- virtual bool has_queue()
- {
- if(has_buffered) return true;
- if(source.good() && !source.eof())
- {
- std::getline(source, next);
- has_buffered=true;
- return true;
- }else if(me["reload"]=="true"){
- source.clear();
- source.seekg(0, std::ios::beg);
- std::getline(source, next);
- has_buffered=true;
- return true;
- }
- return false;
- }
- };
-
-
-
- class stdin_lines : public tool
- {
- std::optional<std::shared_ptr<tool>> n;
- tool_env me;
- bool has_buffered = false;
- std::string next;
- public:
-
- stdin_lines(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(next);
- has_buffered = false;
- }
- }
-
- virtual void enqueue(std::string v)
- {}
-
- virtual bool has_queue()
- {
- if(!has_buffered)
- {
- std::getline(std::cin, next);
- has_buffered=true;
- }
- return true;
- }
- };
|