Added Include folder
*facepalm*
This commit is contained in:
74
include/cmd/cmd_tr.hpp
Normal file
74
include/cmd/cmd_tr.hpp
Normal file
@ -0,0 +1,74 @@
|
||||
#pragma once
|
||||
|
||||
#include "cmd/args/arg.hpp"
|
||||
#include "cmd/args/arg_generic.hpp"
|
||||
#include "cmd/args/arg_input.hpp"
|
||||
#include "cmd/args/arg_manager.hpp"
|
||||
#include "cmd/cmd_base.hpp"
|
||||
#include "util/input.hpp"
|
||||
#include "util/stringliteral.hpp"
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace fsh {
|
||||
|
||||
class CmdTr : public Command {
|
||||
public:
|
||||
virtual void execute(FlagNode flag, ArgNodes& vec, std::istream& in, std::ostream& out) override {
|
||||
ArgFactory af;
|
||||
switch (vec.size()) {
|
||||
case 2:
|
||||
af.add_rule<StringLiteral>(1);
|
||||
case 1:
|
||||
af.add_rule<StringLiteral>(1);
|
||||
break;
|
||||
|
||||
default:
|
||||
af.add_input_rule();
|
||||
af.add_rule<StringLiteral>(1);
|
||||
af.add_rule<StringLiteral>(1);
|
||||
break;
|
||||
}
|
||||
Command::execute(flag, vec, in,out, af);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
std::string replace_all(std::string str,const std::string &what,const std::string &with) {
|
||||
if(what == "") throw std::runtime_error("\"\" cannot be used as what");
|
||||
|
||||
unsigned long long pos = str.find(what);
|
||||
|
||||
while(pos != std::string::npos) {
|
||||
str.replace(pos,what.size(),with) ;
|
||||
pos = str.find(what,pos+with.size());
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override {
|
||||
std::string s;
|
||||
std::string o;
|
||||
std::string what = "";
|
||||
std::string with = "";
|
||||
while (std::getline(in, s)) { o += s + "\n"; }
|
||||
|
||||
switch (args.size()) {
|
||||
case 2:
|
||||
with = args.get<StringLiteral>(1).value();
|
||||
case 1:
|
||||
what = args.get<StringLiteral>(0).value();
|
||||
break;
|
||||
case 3:
|
||||
what = args.get<StringLiteral>(1).value();
|
||||
with = args.get<StringLiteral>(2).value();
|
||||
}
|
||||
|
||||
out << replace_all(o,what,with);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user