Initial commit

This commit is contained in:
2024-12-02 06:30:40 +00:00
commit fd62fb822d
33 changed files with 1972 additions and 0 deletions

46
include/ast/ast.hpp Normal file
View File

@ -0,0 +1,46 @@
/*
* Fsh Grammar
*
* Command_Line ::= Command EOF
* | Command Pipeline_Command
*
* Pipeline_Command ::= "|" Command EOF
* | "|" Command "|" Pipeline_Command
*
* Command ::= Command_Name [Flag_Opt] {Command_Argument} [Redirect]
*
* Redirects ::= [LRedirect Word] RRedirect Word | [RRedirect Word] LRedirect Word
*
* Command_Argument ::= Word | String_Literal
*
* Command_Name ::= Word
*/
#pragma once
#include "lexer.hpp"
#include "util/text.hpp"
#include "ast/ast_base.hpp"
#include "ast/ast_token.hpp"
#include "ast/ast_component.hpp"
#include "ast/ast_executable.hpp"
namespace fsh {
class AstFactory {
public:
// Generates an abstract syntax tree
static std::shared_ptr<ExecutableNode> generate_ast(std::list<Token>& list) {
auto it = list.begin();
return CommandLineNode::build(it);
}
private:
static AstFactory& get_factory();
AstFactory() {}
AstFactory(const AstFactory&) = default;
};
}

67
include/ast/ast_base.hpp Normal file
View File

@ -0,0 +1,67 @@
#pragma once
#include <string>
#include <memory>
#include <stdexcept>
#include <sstream>
#include "lexer.hpp"
#include "util/text.hpp"
namespace fsh {
enum NodeType {
COMMAND_LINE,
COMMAND,
PIPELINE_COMMAND,
LREDIRECTS,
RREDIRECTS,
REDIRECTS,
STRING_LITERAL,
COMMAND_ARGUMENT,
TOKEN
};
class AstNode {
friend class std::shared_ptr<AstNode>;
public:
NodeType gtype() {return type;}
const std::string& gname() {return name;};
protected:
using TokenType = Lexer::TokenType;
AstNode(NodeType type, std::string name) : type(type), name(name) {}
template <class T>
static std::shared_ptr<T> Optional(std::list<Token>::iterator& it, std::shared_ptr<T>& node_ptr);
template <class T>
static std::shared_ptr<T> Optional(std::list<Token>::iterator& it) {
std::shared_ptr<T> node_ptr;
return Optional<T>(it, node_ptr);
}
template <class T>
static std::shared_ptr<T> Mandatory(std::list<Token>::iterator& it) {return T::build(it);}
private:
AstNode();
AstNode(const AstNode&);
NodeType type;
const std::string name;
};
class ExecutableNode : public AstNode {
public:
virtual void print(int indent) {};
virtual void execute(std::istream &in, std::ostream &out) {}
protected:
ExecutableNode(NodeType type, std::string Name) : AstNode(type, Name) {}
private:
};
class AstBuildError : public std::runtime_error {
public:
AstBuildError(std::string err) : std::runtime_error(err) {}
};
}

View File

@ -0,0 +1,90 @@
#pragma once
#include <memory>
#include <optional>
#include "ast/ast_base.hpp"
#include "ast_token.hpp"
#include "lexer.hpp"
#include "util/text.hpp"
namespace fsh {
class CommandArgumentNode : public AstNode {
public:
static std::shared_ptr<CommandArgumentNode> build(std::list<Token>::iterator& it);
Lexer::TokenType gtoken_type() {return type;}
std::string& gvalue() {return value;}
protected:
CommandArgumentNode(TokenNode<TokenType::WORD>::shared word)
: AstNode(NodeType::COMMAND_ARGUMENT, "StringArgument"),
type(word->gtoken_type()),
value(word->gvalue()) {}
CommandArgumentNode(TokenNode<TokenType::STRING_LITERAL>::shared word)
: AstNode(NodeType::COMMAND_ARGUMENT, "StringArgument"),
type(word->gtoken_type()),
value(word->gvalue()) {}
private:
Lexer::TokenType type;
std::string value;
};
class LRedirectNode : public AstNode {
public:
static std::shared_ptr<LRedirectNode> build(std::list<Token>::iterator& it);
std::string gvalue() {return input;}
protected:
LRedirectNode(TokenNode<TokenType::LREDIRECTION>::shared, TokenNode<TokenType::WORD>::shared in)
: AstNode(NodeType::LREDIRECTS, "LRedirectNode"),
input(in->gvalue()) {}
private:
std::string input;
};
class RRedirectNode : public AstNode {
public:
static std::shared_ptr<RRedirectNode> build(std::list<Token>::iterator& it);
std::string gvalue() {return output;}
bool gappend() {return append;}
protected:
RRedirectNode(TokenNode<TokenType::RREDIRECTION>::shared rr, TokenNode<TokenType::WORD>::shared out)
: AstNode(NodeType::RREDIRECTS, "LRedirectNode"),
output(out->gvalue()),
append(rr->gvalue().size()>1) {}
private:
std::string output;
bool append;
};
class RedirectsNode : public AstNode {
public:
static std::shared_ptr<RedirectsNode> build(std::list<Token>::iterator& it);
std::optional<std::string> ginput() {return input;}
std::optional<std::string> goutput() {return output;}
bool gappend() {return append;}
protected:
RedirectsNode(std::shared_ptr<LRedirectNode> in, std::shared_ptr<RRedirectNode> out)
: AstNode(NodeType::REDIRECTS, "RedirectNode"),
input(util::mk_optional(in)),
output(util::mk_optional(out)) {if(out) append = out->gappend();}
private:
std::optional<std::string> input;
std::optional<std::string> output;
bool append = false;
};
}

View File

@ -0,0 +1,78 @@
#pragma once
#include <memory>
#include <stdexcept>
#include <optional>
#include "ast/ast_base.hpp"
#include "ast/ast_token.hpp"
#include "ast/ast_component.hpp"
#include "lexer.hpp"
#include "util/text.hpp"
namespace fsh {
class CommandNode : public ExecutableNode {
using ArgumentVec = std::vector<std::shared_ptr<CommandArgumentNode> >;
public:
static std::shared_ptr<CommandNode> build(std::list<Token>::iterator& it);
virtual void print(int indent) override;
virtual void execute(std::istream &in, std::ostream &out) override;
protected:
CommandNode(
TokenNode<TokenType::WORD>::shared cmd_name,
TokenNode<TokenType::FLAG>::shared flag,
ArgumentVec args,
std::shared_ptr<RedirectsNode> redirects
) : ExecutableNode(NodeType::COMMAND, "CommandNode"),
cmd_name(cmd_name->gvalue()),
flag(util::mk_optional(flag)),
redirects(redirects),
args(args) {}
private:
std::string cmd_name;
std::optional<std::string> flag;
std::shared_ptr<RedirectsNode> redirects;
ArgumentVec args;
};
class PipeLineNode : public ExecutableNode {
public:
static std::shared_ptr<PipeLineNode> build(std::list<Token>::iterator& it);
virtual void print(int indent) override;
virtual void execute(std::istream &in, std::ostream &out) override;
protected:
PipeLineNode(std::shared_ptr<CommandNode> l, std::shared_ptr<ExecutableNode> r)
: ExecutableNode(NodeType::PIPELINE_COMMAND, "PipeLine"),
l_command(l),
r_command(r) {}
private:
std::shared_ptr<ExecutableNode> l_command;
std::shared_ptr<ExecutableNode> r_command;
};
class CommandLineNode : public ExecutableNode {
public:
static std::shared_ptr<CommandLineNode> build(std::list<Token>::iterator& it);
virtual void print(int indent) override;
virtual void execute(std::istream &in, std::ostream &out) override;
protected:
CommandLineNode(std::shared_ptr<ExecutableNode> command, std::shared_ptr<ExecutableNode> pipe = nullptr)
: ExecutableNode(NodeType::COMMAND_LINE, "CommandLine"),
command(command),
pipe(pipe) {}
private:
std::shared_ptr<ExecutableNode> command;
std::shared_ptr<ExecutableNode> pipe;
};
}

26
include/ast/ast_token.hpp Normal file
View File

@ -0,0 +1,26 @@
#pragma once
#include <memory>
#include "ast/ast_base.hpp"
#include "util/text.hpp"
namespace fsh {
template<Lexer::TokenType T>
class TokenNode : public AstNode {
public:
using shared = std::shared_ptr<TokenNode>;
Lexer::TokenType gtoken_type() {return T;}
std::string& gvalue() {return value;};
static std::shared_ptr<TokenNode> build(std::list<Token>::iterator& it);
protected:
TokenNode(std::string value)
: AstNode(NodeType::TOKEN, util::tokens[T]), value(value) {}
private:
std::string value;
};
}

224
include/cmd/arg.hpp Normal file
View File

@ -0,0 +1,224 @@
#pragma once
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <unordered_map>
#include <iostream>
#include "ast/ast_component.hpp"
#include "util/input.hpp"
namespace fsh{
class _Argument {
public:
virtual void svalue(std::shared_ptr<CommandArgumentNode> can) {
svalue(can->gvalue(), can->gtoken_type());
}
virtual void svalue(const std::string& can, const Lexer::TokenType& type = Lexer::TokenType::FLAG) {}
template<typename T>
static std::shared_ptr<_Argument> create() {return std::make_shared<T>();}
protected:
_Argument(){}
};
template<typename T>
class Argument : public _Argument {
public:
Argument() {}
virtual void svalue(const std::string& val, const Lexer::TokenType& type) override{
if constexpr (std::is_same_v<T, std::string>) {
value = val;
} else {
std::stringstream ss_val(val);
if(!(ss_val >> value)) {
throw std::invalid_argument("Incorrect type");
}
}
is_string_literal = type == Lexer::TokenType::STRING_LITERAL;
}
virtual T& gvalue() {return value;}
static T& get(std::shared_ptr<_Argument> a) {
return std::dynamic_pointer_cast<Argument<T> >(a)->gvalue();
}
private:
bool is_string_literal;
T value;
};
class ArgInput : public _Argument {
public:
ArgInput() {}
virtual void svalue(const std::string& val, const Lexer::TokenType& type) override{
const std::string& txt = val;
if(type == Lexer::TokenType::STRING_LITERAL) {
str = std::stringstream(txt);
} else {
file = std::ifstream(txt, std::ios::in);
if(!*file) {
throw std::runtime_error("Failed to open file");
}
}
}
virtual std::istream& gvalue(){
if(str)
return *str;
return *file;
}
static std::istream& get(std::shared_ptr<_Argument> a) {
return std::dynamic_pointer_cast<ArgInput>(a)->gvalue();
}
private:
std::optional<std::stringstream> str;
std::optional<std::ifstream> file;
};
class ArgManager {
friend class ArgFactory;
using PosArgs = std::vector<std::shared_ptr<_Argument> >;
using FlagOpts = std::unordered_map< std::string ,std::shared_ptr<_Argument> >;
public:
ArgManager() {}
template<typename T>
std::optional<T> get(const int id) {
if(id < pos_argument.size()) return Argument<T>::get(pos_argument[id]);
return std::make_optional<T>();
}
std::istream& get_input(const int id) {
if(id < pos_argument.size()) return ArgInput::get(pos_argument[id]);
return util::cin;
}
template<typename T>
std::optional<T> get(const std::string& id) {
if(flags.find(id) != flags.end()) return Argument<T>::get(flags[id]);
return std::make_optional<T>();
}
bool get(const std::string& id) {
return flags.find(id) != flags.end();
}
void push_arg(std::shared_ptr<_Argument> arg) {
pos_argument.push_back(arg);
}
void push_flag(const std::string &id,std::shared_ptr<_Argument> arg = nullptr) {
flags[id] = arg;
}
private:
PosArgs pos_argument;
FlagOpts flags;
};
class ArgFactory {
using FlagNode = std::optional<std::string>&;
using ArgNodes = std::vector<std::shared_ptr<CommandArgumentNode > >;
struct ArgRule
{
std::shared_ptr<_Argument> (*build) (void);
bool mandatory;
bool extends;
};
struct FlagRule
{
std::shared_ptr<_Argument> (*build) (void);
bool specialinput; //Not implemented
bool capturing;
bool extends; //Not implemented
};
public:
template<typename T>
void add_rule(bool mandatory, bool extends = false) {
pos_arg_rules.push_back({&_Argument::create<Argument<T> >, mandatory, extends});
}
void add_input_rule() {
has_input = true;
pos_arg_rules.push_back({&_Argument::create<ArgInput>, false, false});
}
template<typename T = bool >
void add_rule(const std::string name, bool capturing = false) {
flag_rules[name] = {_Argument::create<Argument<T> >, false, capturing, false};
}
void parse(ArgManager& manager, ArgNodes& vec, FlagNode flag) {
if(flag) {
parse_flag(manager, flag);
}
int i = 0;
for(const auto& arg : vec) {
std::shared_ptr<_Argument> a;
if(i >= pos_arg_rules.size()) throw std::invalid_argument("More arguments then excpected");
manager.push_arg(build_arg(pos_arg_rules[i].build, arg));
if(!pos_arg_rules[i].extends) i++;
}
}
bool ghas_input() {return has_input;}
private:
std::vector<ArgRule> pos_arg_rules;
std::unordered_map<std::string, FlagRule> flag_rules;
bool has_input = false;
void parse_flag(ArgManager& manager, FlagNode flag) {
const std::string f = *flag;
int f_sz = f.size();
for(const auto& [key, rule] : flag_rules) {
const std::string_view k(key);
if(k == f) {
manager.push_flag(f);
return;
}
if(rule.capturing && f == k.substr(0, f_sz)) {
auto arg = build_arg(rule.build, (std::string) k.substr(f_sz));
manager.push_flag(f, arg);
return;
}
}
throw std::invalid_argument("Invalid flag");
}
std::shared_ptr<_Argument> build_arg(std::shared_ptr<_Argument> (*build_func) (void),const std::string& str) {
auto arg = build_func();
arg->svalue(str);
return arg;
}
std::shared_ptr<_Argument> build_arg(std::shared_ptr<_Argument> (*build_func) (void),
std::shared_ptr<CommandArgumentNode> cmd_arg) {
auto arg = build_func();
arg->svalue(cmd_arg);
return arg;
}
};
}

53
include/cmd/cmd.hpp Normal file
View File

@ -0,0 +1,53 @@
#pragma once
#include <unordered_map>
#include "cmd/cmd_base.hpp"
#include "cmd/cmd_wc.hpp"
#include "cmd/cmd_time.hpp"
#include "cmd/cmd_date.hpp"
#include "cmd/cmd_echo.hpp"
#include "cmd/cmd_touch.hpp"
#include "cmd/cmd_misc.hpp"
namespace fsh
{
class CommandRegistry {
public:
static CommandRegistry& instance() {
static CommandRegistry cmd_registry;
return cmd_registry;
}
Command& get(const std::string n) {
if(cmds.find(n) == cmds.end()) {
throw std::runtime_error("Command not found");
}
return *(cmds[n]);
}
Command& operator[](const std::string n) {
return get(n);
}
private:
CommandRegistry() {
cmds["wc" ] = Command::register_cmd<CmdWc>();
cmds["date" ] = Command::register_cmd<CmdDate>();
cmds["time" ] = Command::register_cmd<CmdTime>();
cmds["echo" ] = Command::register_cmd<CmdEcho>();
cmds["exit" ] = Command::register_cmd<CmdExit>();
cmds["touch"] = Command::register_cmd<CmdTouch>();
cmds["debug"] = Command::register_cmd<CmdPrintTree>();
}
std::unordered_map<std::string, std::unique_ptr<Command> > cmds;
};
}

41
include/cmd/cmd_base.hpp Normal file
View File

@ -0,0 +1,41 @@
#pragma once
#include <array>
#include <string>
#include <sstream>
#include <unordered_map>
#include <memory>
#include "ast/ast.hpp"
#include "cmd/arg.hpp"
namespace fsh{
class Command {
using FlagNode = std::optional<std::string>&;
using ArgNodes = std::vector<std::shared_ptr<CommandArgumentNode > >;
public:
void execute(FlagNode flag, ArgNodes& vec, std::istream& in, std::ostream& out);
template <typename T>
static std::unique_ptr<Command> register_cmd() {
std::unique_ptr<Command> cmd = std::make_unique<T>();
cmd->register_flags();
return cmd;
}
protected:
virtual void register_flags() {};
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) {};
ArgFactory& get_factory() {return arg_factory;}
private:
ArgFactory arg_factory;
};
}

21
include/cmd/cmd_date.hpp Normal file
View File

@ -0,0 +1,21 @@
#pragma once
#include <ctime>
#include <iomanip>
#include "cmd/cmd_base.hpp"
namespace fsh {
class CmdDate : public Command {
protected:
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override {
std::time_t time = std::time(nullptr);
out << std::asctime(std::localtime(&time));
}
};
}

26
include/cmd/cmd_echo.hpp Normal file
View File

@ -0,0 +1,26 @@
#pragma once
#include "cmd/cmd_base.hpp"
namespace fsh {
class CmdEcho : public Command {
protected:
virtual void register_flags() override {
ArgFactory& factory = get_factory();
factory.add_input_rule();
}
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override {
std::string s;
std::string o;
while(getline(in, s)) {
o += s + "\n";
}
out << o;
}
};
}

43
include/cmd/cmd_misc.hpp Normal file
View File

@ -0,0 +1,43 @@
#pragma once
#include <sstream>
#include "cmd/cmd_base.hpp"
#include "fsh.hpp"
#include "ast/ast.hpp"
namespace fsh {
class CmdExit : public Command {
protected:
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override {
fsh::instance().environment["EXITING"] = "1";
}
};
class CmdPrintTree : public Command {
protected:
virtual void register_flags() override {
auto& factory = get_factory();
factory.add_input_rule();
}
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override {
std::string line;
std::getline(in, line);
auto tokens = Lexer::process(line);
auto ast = AstFactory::generate_ast(tokens);
ast->print(0);
}
};
}

20
include/cmd/cmd_time.hpp Normal file
View File

@ -0,0 +1,20 @@
#pragma once
#include <ctime>
#include <iomanip>
#include "cmd/cmd_base.hpp"
namespace fsh {
class CmdTime : public Command {
protected:
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override {
std::time_t time = std::time(nullptr);
out << std::put_time(std::localtime(&time), "%T%n");
}
};
}

24
include/cmd/cmd_touch.hpp Normal file
View File

@ -0,0 +1,24 @@
#pragma once
#include "cmd/cmd_base.hpp"
namespace fsh {
class CmdTouch : public Command {
protected:
virtual void register_flags() override {
ArgFactory& factory = get_factory();
factory.add_rule<std::string>(true);
}
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override {
auto name = *(args.get<std::string>(0));
if(std::ifstream(name, std::ios::in).good() || !std::ofstream(name, std::ios::out).is_open()) {
throw std::runtime_error("File exists");
}
}
};
}

37
include/cmd/cmd_wc.hpp Normal file
View File

@ -0,0 +1,37 @@
#pragma once
#include "cmd/cmd_base.hpp"
namespace fsh {
class CmdWc : public Command {
protected:
virtual void register_flags() override {
ArgFactory& factory = get_factory();
factory.add_input_rule();
factory.add_rule("-c");
factory.add_rule("-w");
}
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override {
int i = 0;
char c;
if(args.get("-c")) {
while(in.get(c)) i++;
out << i << "\n";
} else if (args.get("-w")) {
bool prev_space = true;
while(in.get(c)) {
if(std::isspace(c)) { prev_space = true; }
else if(prev_space) { prev_space = false; i++;}
}
out << i << "\n";
} else {
throw std::invalid_argument("Didn't provide flag");
}
}
};
}

37
include/fsh.hpp Normal file
View File

@ -0,0 +1,37 @@
#pragma once
#include <unordered_map>
#include <iostream>
#include "ast/ast.hpp"
#include "util/input.hpp"
namespace fsh
{
class fsh
{
public:
std::unordered_map<std::string, std::string> environment;
static fsh& instance() {
static fsh f;
return f;
}
void run_line(std::string &line, std::istream &in = util::cin, std::ostream &out = std::cout);
void run();
private:
fsh(){
environment["PROMPT"] = "$";
environment["QUIT"] = "";
}
};
}

92
include/lexer.hpp Normal file
View File

@ -0,0 +1,92 @@
#pragma once
#include <list>
#include <functional>
#include <string>
namespace fsh {
class Lexer {
public:
enum TokenType {
WHITESPACE,
WORD,
STRING_LITERAL,
OPT,
FLAG,
LREDIRECTION,
RREDIRECTION,
PIPE,
END_OF_STREAM
};
using Token = std::pair<Lexer::TokenType, std::string>;
int peek();
int consume();
Token next_token();
static std::list<Token> process(std::string line);
private:
std::string workspace;
unsigned int idx;
Lexer(std::string str) : workspace(str), idx(0) {}
};
using Token = Lexer::Token;
class LexerStateMachine {
friend class Lexer;
private:
enum State {
START,
REDIRECT,
FLAG_OPT,
POTENTIAL_WORD,
STRING_LITERAL,
STATE_COUNT,
END
};
using StateHandler = std::function<State(void)>;
Lexer& parser;
std::string token;
Lexer::TokenType type;
std::vector<StateHandler> state_handlers;
LexerStateMachine(Lexer& parser) : parser(parser), token(""), type(Lexer::TokenType::WHITESPACE), state_handlers(STATE_COUNT) {
state_handlers[State::START] = std::bind(&LexerStateMachine::handle_start , this);
state_handlers[State::REDIRECT ] = std::bind(&LexerStateMachine::handle_redirect , this);
state_handlers[State::FLAG_OPT ] = std::bind(&LexerStateMachine::handle_flag_opt , this);
state_handlers[State::POTENTIAL_WORD] = std::bind(&LexerStateMachine::handle_potential_word, this);
state_handlers[State::STRING_LITERAL] = std::bind(&LexerStateMachine::handle_string_literal, this);
}
State set_type(Lexer::TokenType type, State state);
Token run();
State handle_start();
State handle_redirect();
State handle_flag_opt();
State handle_potential_word();
State handle_string_literal();
State word_like_handler(Lexer::TokenType type, State state);
};
inline LexerStateMachine::State LexerStateMachine::set_type(
Lexer::TokenType type = Lexer::TokenType::WHITESPACE,
State state = State::END
){
this->type = type;
return state;
}
}

62
include/util/input.hpp Normal file
View File

@ -0,0 +1,62 @@
#pragma once
#include <istream>
#include <streambuf>
namespace fsh::util
{
/**
* Applies some settings to terminals to fix some inconsistencies between
* windows and linux terminals
*
* * **Windows** : it enables using ansii characters
* * **Linux** : it enables [Noncanonical Mode](https://www.gnu.org/software/libc/manual/html_node/Noncanonical-Input.html)
*/
void prepTerminal();
#if defined(__unix__) || defined(__APPLE__)
class CursorIStreamBuffer : public std::streambuf {
public:
enum EscapeSequence {
FAILED,
POSSIBLE,
LEFT_ARROW,
RIGHT_ARROW
};
CursorIStreamBuffer() : valid(0), read_chars(0), cursor_pos(0), buffer() {
setg(buffer,buffer,buffer);
}
protected:
int underflow() override;
// int uflow() override;
// std::streamsize xsgetn(char* s, std::streamsize n) override;
private:
int valid;
EscapeSequence testForEscapeCodes(char chr);
int readInputToNewLine();
std::string input;
int read_chars;
int cursor_pos;
char buffer[32];
};
class CursorIStream : public std::istream {
public:
CursorIStream() : std::istream(&buffer) {}
private:
CursorIStreamBuffer buffer;
};
extern CursorIStream cin;
#endif
}

55
include/util/text.hpp Normal file
View File

@ -0,0 +1,55 @@
#pragma once
#include <string>
#include <optional>
#include <fstream>
#include <stdexcept>
namespace fsh::util {
extern const char* tokens[];
template <class T>
static inline std::optional<std::string> mk_optional(T t){
if(t){
return t->gvalue();
}
return std::optional<std::string>();
}
static inline bool contains(char x, std::string text) {
for (const auto& chr : text){
if(chr == x) return 1;
}
return 0;
};
static inline std::ifstream input(const std::string& x) {
std::ifstream in(x, std::ios::in);
if(!in) {
throw std::runtime_error("Can't open file");
}
return in;
}
static inline std::ofstream output(const std::string& x) {
std::ofstream out(x, std::ios::out);
if(!out.is_open()) {
throw std::runtime_error("Can't open file");
}
return out;
}
static inline std::ofstream output_append(const std::string& x) {
std::ofstream out(x, std::ios::app | std::ios::out);
if(!out.is_open()) {
throw std::runtime_error("Can't open file");
}
return out;
}
}