diff --git a/Makefile b/Makefile index 8dc5c97..c6fb70f 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ EXE_FILE:=patexp -SRC_FILES:= Expander.o +SRC_FILES:= Expander.o Helpers.o MAIN_FILE:= main.o TEST_FILES:= TestExpander.o diff --git a/src/Expander.cpp b/src/Expander.cpp index 5bae237..22abc07 100755 --- a/src/Expander.cpp +++ b/src/Expander.cpp @@ -510,4 +510,6 @@ void Expander::saveConfig(const std::string& filePath) out << L"group.end " << getGroupEnd() << endl; out << L"quote " << getQuote() << endl; + out.close(); + } diff --git a/src/Helpers.cpp b/src/Helpers.cpp new file mode 100644 index 0000000..bb8eba5 --- /dev/null +++ b/src/Helpers.cpp @@ -0,0 +1,106 @@ +/* + * Helpers.cpp + * + * Created on: Nov 17, 2023 + * Author: ventsyv + */ + +#include +#include +#include +#include +#include +#include "Expander.h" +#include "Helpers.h" + +using namespace std; + + + +void run_command(int argc, char *argv[]) +{ + wstring pattern; + PatternExpander::Expander exp; + std::wstring_convert> wcu8; + + string command(argv[0]); + std::transform(command.begin(), command.end(), command.begin(), ::tolower); + + if (argc > 1) + { + for (int currentParam = 1; currentParam < argc; currentParam++) + { + pattern = wcu8.from_bytes(argv[currentParam]); + if (command == "validate") + { + exp.validate(pattern); + wcerr << exp.output.str(); + } + else if (command == "run") + { + exp.generate(pattern); + } + + } + // Validate will not produce any data output + wcout << exp; + } + else + { + string temp; + while (cin >> temp) + { + pattern = wcu8.from_bytes(temp); + if (command == "validate") + { + exp.validate(pattern); + wcerr << exp.output.str(); + } + else if (command == "run") + { + exp.generate(pattern); + } + } + // Validate will not produce any data output + wcout << exp; + } +} + +void setConfig(int argc, char *argv[], const string& filePath) +{ + PatternExpander::Expander exp; + std::wstring_convert> wcu8; + int i = 0; + while (i < argc) + { + string option(argv[i++]); + if (i >= argc) + { + cerr << "Skipping option '" << option << "' - no value provided" << endl; + return; + } + string temp(argv[i++]); + wstring val = wcu8.from_bytes(temp); + if (val.length() != 1) + { + cerr << "Skipping option '" << option << "' due to invalid value '" << temp << "'. Values are single characters only." << endl; + continue; + } + + std::transform(option.begin(), option.end(), option.begin(), + ::tolower); + if (option == "-q" || option == "--quote") + exp.setQuote(val[0]); + else if (option == "-e" || option == "--esc") + exp.setEscChar(val[0]); + else if (option == "-b" || option == "--group_begin" ) + exp.setGroupBegin(val[0]); + else if (option == "-n" || option == "--group_end") + exp.setGroupEnd(val[0]); + else if (option == "-r" || option == "range") + exp.setRangeChar(val[0]); + else + cout << "Skipping unknown option: "<< option << endl; + } + exp.saveConfig(filePath); +} diff --git a/src/Helpers.h b/src/Helpers.h new file mode 100644 index 0000000..ac77bc0 --- /dev/null +++ b/src/Helpers.h @@ -0,0 +1,19 @@ +/* + * helpers.h + * + * Created on: Nov 17, 2023 + * Author: ventsyv + */ + +#ifndef SRC_HELPERS_H_ +#define SRC_HELPERS_H_ + +#include + +void run_command(int argc, char *argv[]); + +void setConfig(int argc, char *argv[], const std::string& filePath); + + + +#endif /* SRC_HELPERS_H_ */ diff --git a/src/main.cpp b/src/main.cpp index ebbe525..5cbdf77 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,69 +3,31 @@ #include #include #include +#include +#include "Helpers.h" #include "Expander.h" using namespace std; -void run_command(int argc, char *argv[]) -{ - wstring pattern; - PatternExpander::Expander exp; - std::wstring_convert> wcu8; - - string command(argv[1]); - std::transform(command.begin(), command.end(), command.begin(), ::tolower); +const string DEFAULT_CONFIG_FILE_NAME = ".pathexpconfig"; - if (argc > 2) - { - for (int currentParam = 2; currentParam < argc; currentParam++) - { - pattern = wcu8.from_bytes(argv[currentParam]); - if (command == "validate") - { - exp.validate(pattern); - wcerr << exp.output.str(); - } - else if (command == "run") - { - exp.generate(pattern); - } - - } - // Validate will not produce any data output - wcout << exp; - } - else - { - string temp; - while (cin >> temp) - { - pattern = wcu8.from_bytes(temp); - if (command == "validate") - { - exp.validate(pattern); - wcerr << exp.output.str(); - } - else if (command == "run") - { - exp.generate(pattern); - } - } - // Validate will not produce any data output - wcout << exp; - } -} int main(int argc, char *argv[]) { std::setlocale(LC_CTYPE, "C.UTF8"); - wstring usage = LR"(usage: patexp [-h | --help] [] + string usage = R"(usage: patexp [-h | --help] [] commands: run [ .. ] | : Generates strings from a list of patterns validate [ .. ] | : Validates if the patterns provided are syntaxically correct - configure: Sets the various configuration options)"; + configure: Sets the various configuration options + -b, --group_begin : Sets the group begin symbol. Default: '[' + -e, --esc : Sets the escape symbol. Default: '/' + -q, --quote : Sets the quote symbols. Default: '"' + -n, --group_end : Sets the group end symbol. Default: ']' + -r, --range : Sets the range symbol. Default: '-' +)"; if (argc > 1) { @@ -74,31 +36,36 @@ int main(int argc, char *argv[]) ::tolower); if (command == "-h" || command == "--help") { - wcout << usage << endl; + cout << usage << endl; exit(0); } else if (command == "run" || command == "validate") { - run_command(argc, argv); - + //The first arg is the name of the executable so start at the one after + run_command(argc - 1, argv + 1); } else if (command == "config" || command == "configure") { - wcout << "Configure is currently not implemented" << endl; - exit(1); + filesystem::path path(getenv("HOME")); + path.append(DEFAULT_CONFIG_FILE_NAME); + + //The first arg is the name of the executable + //The second argument is the name of the command (in this case "config") + //We need to skip both of those + setConfig(argc - 2, argv + 2, path); } else { - wcerr << "Error: unknown command" << endl; - wcout << usage << endl; + cerr << "Error: unknown command" << endl; + cout << usage << endl; } } else { // command not provided - wcerr << "Error: no command was provided" << endl; - wcout << usage << endl; + cerr << "Error: no command was provided" << endl; + cout << usage << endl; } return 0; diff --git a/test/TestExpander.cpp b/test/TestExpander.cpp index cca7bcc..b60c7b5 100644 --- a/test/TestExpander.cpp +++ b/test/TestExpander.cpp @@ -4,6 +4,7 @@ #include #include "Expander.h" +#include "Helpers.h" using namespace ::testing; using namespace std; @@ -902,6 +903,45 @@ TEST_F(TestExpander, testSaveConfig) } +TEST_F(TestExpander, testSetConfig) +{ + string configFileName = std::tmpnam(nullptr); + + string args = R"(-e # -r > -b { -n } -q ^)"; + stringstream ss(args); + vector temp; + string item; + while (ss >> item) + temp.push_back(item); + + char** argv = new char*[temp.size()]; + for (size_t i = 0; i < temp.size(); i++) + { + argv[i] = new char[temp[i].length()]; + strcpy(argv[i], temp[i].c_str()); + } + + setConfig(temp.size(), argv, configFileName); + + //Load the alt config file. All settings will change + underTest.loadConfig(configFileName); + + ASSERT_EQ(underTest.getEscChar(), L'#'); + ASSERT_EQ(underTest.getGroupBegin(), L'{'); + ASSERT_EQ(underTest.getGroupEnd(), L'}'); + ASSERT_EQ(underTest.getRangeChar(), L'>'); + ASSERT_EQ(underTest.getQuote(), L'^'); + + + for (size_t i = 0; i < temp.size(); i++) + { + delete argv[i]; + } + delete [] argv; + + +} + int main(int argc, char **argv)