Massive reformating and cleanup
This commit is contained in:
@ -13,55 +13,53 @@
|
||||
#include "cmd/args/arg_input.hpp"
|
||||
#include "cmd/args/arg_manager.hpp"
|
||||
|
||||
namespace fsh{
|
||||
namespace fsh {
|
||||
|
||||
class ArgFactory {
|
||||
public:
|
||||
using FlagNode = std::optional<std::string>&;
|
||||
using ArgNodes = std::vector<std::shared_ptr<CommandArgumentNode > >;
|
||||
using BuildFunc = std::shared_ptr<_Argument> (*) (void);
|
||||
class ArgFactory {
|
||||
public:
|
||||
using FlagNode = std::optional<std::string>&;
|
||||
using ArgNodes = std::vector<std::shared_ptr<CommandArgumentNode> >;
|
||||
using BuildFunc = std::shared_ptr<_Argument> (*)(void);
|
||||
|
||||
struct ArgRule
|
||||
{
|
||||
BuildFunc build;
|
||||
bool mandatory;
|
||||
bool extends;
|
||||
struct ArgRule {
|
||||
BuildFunc build;
|
||||
bool mandatory;
|
||||
bool extends;
|
||||
};
|
||||
|
||||
struct FlagRule {
|
||||
BuildFunc build;
|
||||
bool specialinput; //Not implemented
|
||||
bool capturing;
|
||||
bool extends; //Not implemented
|
||||
};
|
||||
|
||||
void add_input_rule() {
|
||||
has_input = true;
|
||||
pos_arg_rules.push_back({ &_Argument::create<ArgInput>, false, false });
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void add_rule(bool mandatory, bool extends = false) {
|
||||
pos_arg_rules.push_back({ &_Argument::create<Argument<T> >, mandatory, extends });
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
std::shared_ptr<_Argument> build_arg(BuildFunc build_func, const std::string& str);
|
||||
std::shared_ptr<_Argument> build_arg(BuildFunc build_func, std::shared_ptr<CommandArgumentNode> cmd_arg);
|
||||
};
|
||||
|
||||
struct FlagRule
|
||||
{
|
||||
BuildFunc build;
|
||||
bool specialinput; //Not implemented
|
||||
bool capturing;
|
||||
bool extends; //Not implemented
|
||||
};
|
||||
|
||||
|
||||
void add_input_rule() {
|
||||
has_input = true;
|
||||
pos_arg_rules.push_back({&_Argument::create<ArgInput>, false, false});
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_rule(bool mandatory, bool extends = false) {
|
||||
pos_arg_rules.push_back({&_Argument::create<Argument<T> >, mandatory, extends});
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
std::shared_ptr<_Argument> build_arg(BuildFunc build_func, const std::string& str);
|
||||
std::shared_ptr<_Argument> build_arg(BuildFunc build_func, std::shared_ptr<CommandArgumentNode> cmd_arg);
|
||||
};
|
||||
}
|
||||
@ -4,19 +4,20 @@
|
||||
|
||||
#include "ast/ast_component.hpp"
|
||||
|
||||
namespace fsh{
|
||||
namespace fsh {
|
||||
|
||||
class _Argument {
|
||||
public:
|
||||
virtual void svalue(const std::string& can, const Lexer::TokenType& type = Lexer::TokenType::FLAG) {}
|
||||
virtual void svalue(std::shared_ptr<CommandArgumentNode> can) {
|
||||
svalue(can->gvalue(), can->gtoken_type());
|
||||
}
|
||||
class _Argument {
|
||||
public:
|
||||
virtual void svalue(const std::string& can, const Lexer::TokenType& type = Lexer::TokenType::FLAG) {}
|
||||
virtual void svalue(std::shared_ptr<CommandArgumentNode> can) { svalue(can->gvalue(), can->gtoken_type()); }
|
||||
|
||||
template<typename T>
|
||||
static std::shared_ptr<_Argument> create() {return std::make_shared<T>();}
|
||||
protected:
|
||||
_Argument(){}
|
||||
};
|
||||
template <typename T>
|
||||
static std::shared_ptr<_Argument> create() {
|
||||
return std::make_shared<T>();
|
||||
}
|
||||
|
||||
protected:
|
||||
_Argument() {}
|
||||
};
|
||||
|
||||
}
|
||||
@ -5,35 +5,32 @@
|
||||
#include "cmd/args/arg_base.hpp"
|
||||
#include "ast/ast_component.hpp"
|
||||
|
||||
namespace fsh{
|
||||
namespace fsh {
|
||||
|
||||
template<typename T>
|
||||
class Argument : public _Argument {
|
||||
public:
|
||||
static T& get(std::shared_ptr<_Argument> a) {
|
||||
return std::dynamic_pointer_cast<Argument<T> >(a)->gvalue();
|
||||
}
|
||||
template <typename T>
|
||||
class Argument : public _Argument {
|
||||
public:
|
||||
static T& get(std::shared_ptr<_Argument> a) { return std::dynamic_pointer_cast<Argument<T> >(a)->gvalue(); }
|
||||
|
||||
Argument() {}
|
||||
Argument() {}
|
||||
|
||||
virtual void svalue(const std::string& val, const Lexer::TokenType& type) override;
|
||||
virtual T& gvalue() {return value;}
|
||||
private:
|
||||
bool is_string_literal; // Currently no getter
|
||||
T value;
|
||||
};
|
||||
virtual void svalue(const std::string& val, const Lexer::TokenType& type) override;
|
||||
virtual T& gvalue() { return value; }
|
||||
|
||||
template<class T>
|
||||
void Argument<T>::svalue(const std::string& val, const Lexer::TokenType& type) {
|
||||
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");
|
||||
private:
|
||||
bool is_string_literal; // Currently no getter
|
||||
T value;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
void Argument<T>::svalue(const std::string& val, const Lexer::TokenType& type) {
|
||||
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;
|
||||
}
|
||||
is_string_literal = type == Lexer::TokenType::STRING_LITERAL;
|
||||
}
|
||||
|
||||
}
|
||||
@ -5,22 +5,22 @@
|
||||
|
||||
#include "cmd/args/arg_base.hpp"
|
||||
|
||||
namespace fsh{
|
||||
namespace fsh {
|
||||
|
||||
class ArgInput : public _Argument {
|
||||
public:
|
||||
static std::istream& get(std::shared_ptr<_Argument> a) {
|
||||
return std::dynamic_pointer_cast<ArgInput>(a)->gvalue();
|
||||
}
|
||||
class ArgInput : public _Argument {
|
||||
public:
|
||||
static std::istream& get(std::shared_ptr<_Argument> a) {
|
||||
return std::dynamic_pointer_cast<ArgInput>(a)->gvalue();
|
||||
}
|
||||
|
||||
ArgInput() {}
|
||||
ArgInput() {}
|
||||
|
||||
virtual void svalue(const std::string& val, const Lexer::TokenType& type) override;
|
||||
virtual std::istream& gvalue();
|
||||
virtual void svalue(const std::string& val, const Lexer::TokenType& type) override;
|
||||
virtual std::istream& gvalue();
|
||||
|
||||
private:
|
||||
std::optional<std::stringstream> str;
|
||||
std::optional<std::ifstream> file;
|
||||
};
|
||||
private:
|
||||
std::optional<std::stringstream> str;
|
||||
std::optional<std::ifstream> file;
|
||||
};
|
||||
|
||||
}
|
||||
@ -3,46 +3,40 @@
|
||||
#include "cmd/args/arg_input.hpp"
|
||||
#include "util/input.hpp"
|
||||
|
||||
namespace fsh{
|
||||
namespace fsh {
|
||||
|
||||
class ArgManager {
|
||||
friend class ArgFactory;
|
||||
class ArgManager {
|
||||
friend class ArgFactory;
|
||||
|
||||
using PosArgs = std::vector<std::shared_ptr<_Argument> >;
|
||||
using FlagOpts = std::unordered_map< std::string ,std::shared_ptr<_Argument> >;
|
||||
using PosArgs = std::vector<std::shared_ptr<_Argument> >;
|
||||
using FlagOpts = std::unordered_map<std::string, std::shared_ptr<_Argument> >;
|
||||
|
||||
public:
|
||||
ArgManager() {}
|
||||
public:
|
||||
ArgManager() {}
|
||||
|
||||
std::istream& get_input(const unsigned int id) {
|
||||
if (id < pos_argument.size()) return ArgInput::get(pos_argument[id]);
|
||||
return util::cin;
|
||||
}
|
||||
|
||||
std::istream& get_input(const unsigned int id) {
|
||||
if(id < pos_argument.size()) return ArgInput::get(pos_argument[id]);
|
||||
return util::cin;
|
||||
}
|
||||
template <typename T>
|
||||
std::optional<T> get(const unsigned int id) {
|
||||
if ((unsigned int)id < pos_argument.size()) return Argument<T>::get(pos_argument[id]);
|
||||
return std::make_optional<T>();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::optional<T> get(const unsigned int id) {
|
||||
if((unsigned int) id < pos_argument.size()) return Argument<T>::get(pos_argument[id]);
|
||||
return std::make_optional<T>();
|
||||
}
|
||||
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();
|
||||
}
|
||||
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;
|
||||
};
|
||||
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;
|
||||
};
|
||||
}
|
||||
@ -3,51 +3,41 @@
|
||||
#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"
|
||||
#include "cmd/cmd_time.hpp"
|
||||
#include "cmd/cmd_touch.hpp"
|
||||
#include "cmd/cmd_wc.hpp"
|
||||
|
||||
namespace fsh
|
||||
{
|
||||
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");
|
||||
class CommandRegistry {
|
||||
public:
|
||||
static CommandRegistry& instance() {
|
||||
static CommandRegistry cmd_registry;
|
||||
return cmd_registry;
|
||||
}
|
||||
return *(cmds[n]);
|
||||
}
|
||||
|
||||
Command& operator[](const std::string n) {
|
||||
return get(n);
|
||||
}
|
||||
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>();
|
||||
}
|
||||
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;
|
||||
std::unordered_map<std::string, std::unique_ptr<Command> > cmds;
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
} // namespace fsh
|
||||
@ -9,33 +9,30 @@
|
||||
#include "ast/ast.hpp"
|
||||
#include "cmd/args/arg.hpp"
|
||||
|
||||
namespace fsh{
|
||||
namespace fsh {
|
||||
|
||||
class Command {
|
||||
class Command {
|
||||
|
||||
using FlagNode = std::optional<std::string>&;
|
||||
using ArgNodes = std::vector<std::shared_ptr<CommandArgumentNode > >;
|
||||
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);
|
||||
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;
|
||||
}
|
||||
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;
|
||||
};
|
||||
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;
|
||||
};
|
||||
|
||||
}
|
||||
@ -5,17 +5,15 @@
|
||||
|
||||
#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));
|
||||
}
|
||||
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));
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@ -4,23 +4,20 @@
|
||||
|
||||
namespace fsh {
|
||||
|
||||
class CmdEcho : public Command {
|
||||
|
||||
protected:
|
||||
virtual void register_flags() override {
|
||||
ArgFactory& factory = get_factory();
|
||||
factory.add_input_rule();
|
||||
}
|
||||
class CmdEcho : public Command {
|
||||
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@ -6,38 +6,33 @@
|
||||
#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 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:
|
||||
class CmdPrintTree : public Command {
|
||||
|
||||
virtual void register_flags() override {
|
||||
auto& factory = get_factory();
|
||||
factory.add_input_rule();
|
||||
}
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
auto tokens = Lexer::process(line);
|
||||
auto ast = AstFactory::generate_ast(tokens);
|
||||
|
||||
ast->print(0);
|
||||
}
|
||||
|
||||
};
|
||||
ast->print(0);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@ -7,14 +7,13 @@
|
||||
|
||||
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");
|
||||
}
|
||||
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");
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@ -4,21 +4,20 @@
|
||||
|
||||
namespace fsh {
|
||||
|
||||
class CmdTouch : public Command {
|
||||
|
||||
protected:
|
||||
virtual void register_flags() override {
|
||||
ArgFactory& factory = get_factory();
|
||||
factory.add_rule<std::string>(true);
|
||||
}
|
||||
class CmdTouch : public Command {
|
||||
|
||||
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");
|
||||
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");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@ -4,45 +4,43 @@
|
||||
|
||||
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");
|
||||
}
|
||||
class CmdWc : public Command {
|
||||
|
||||
int count_chars(std::istream& in) {
|
||||
int i = 0;
|
||||
char c;
|
||||
while(in.get(c)) i++;
|
||||
return i;
|
||||
}
|
||||
|
||||
int count_words(std::istream& in) {
|
||||
int i = 0;
|
||||
char c;
|
||||
bool prev_space = true;
|
||||
while(in.get(c)) {
|
||||
i+= prev_space & !std::isspace(c);
|
||||
prev_space = std::isspace(c);
|
||||
protected:
|
||||
virtual void register_flags() override {
|
||||
ArgFactory& factory = get_factory();
|
||||
factory.add_input_rule();
|
||||
factory.add_rule("-c");
|
||||
factory.add_rule("-w");
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override {
|
||||
bool prev_space = true;
|
||||
if(args.get("-c")) {
|
||||
out << count_chars(in) << "\n";
|
||||
} else if (args.get("-w")) {
|
||||
out << count_words(in) << "\n";
|
||||
} else {
|
||||
throw std::invalid_argument("Didn't provide flag");
|
||||
int count_chars(std::istream& in) {
|
||||
int i = 0;
|
||||
char c;
|
||||
while (in.get(c)) i++;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
int count_words(std::istream& in) {
|
||||
int i = 0;
|
||||
char c;
|
||||
bool prev_space = true;
|
||||
while (in.get(c)) {
|
||||
i += prev_space & !std::isspace(c);
|
||||
prev_space = std::isspace(c);
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override {
|
||||
if (args.get("-c")) {
|
||||
out << count_chars(in) << "\n";
|
||||
} else if (args.get("-w")) {
|
||||
out << count_words(in) << "\n";
|
||||
} else {
|
||||
throw std::invalid_argument("Didn't provide flag");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user