Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds config command line option #13

Merged
merged 5 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 2 additions & 0 deletions src/Expander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,4 +510,6 @@ void Expander::saveConfig(const std::string& filePath)
out << L"group.end " << getGroupEnd() << endl;
out << L"quote " << getQuote() << endl;

out.close();

}
106 changes: 106 additions & 0 deletions src/Helpers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Helpers.cpp
*
* Created on: Nov 17, 2023
* Author: ventsyv
*/

#include <iostream>
#include <string>
#include <codecvt>
#include <locale>
#include <algorithm>
#include "Expander.h"
#include "Helpers.h"

using namespace std;



void run_command(int argc, char *argv[])
{
wstring pattern;
PatternExpander::Expander exp;
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> 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<std::codecvt_utf8_utf16<wchar_t>> 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);
}
19 changes: 19 additions & 0 deletions src/Helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* helpers.h
*
* Created on: Nov 17, 2023
* Author: ventsyv
*/

#ifndef SRC_HELPERS_H_
#define SRC_HELPERS_H_

#include <string>

void run_command(int argc, char *argv[]);

void setConfig(int argc, char *argv[], const std::string& filePath);



#endif /* SRC_HELPERS_H_ */
83 changes: 25 additions & 58 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,31 @@
#include <codecvt>
#include <locale>
#include <algorithm>
#include <filesystem>
#include "Helpers.h"
#include "Expander.h"

using namespace std;

void run_command(int argc, char *argv[])
{
wstring pattern;
PatternExpander::Expander exp;
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> 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] <command> [<args>]
string usage = R"(usage: patexp [-h | --help] <command> [<args>]
commands:
run [<pat1> <pat2> .. ] | <stdin> : Generates strings from a list of patterns
validate [<pat1> <pat2> .. ] | <stdin> : Validates if the patterns provided are syntaxically correct
configure: Sets the various configuration options)";
configure: Sets the various configuration options
-b, --group_begin <val> : Sets the group begin symbol. Default: '['
-e, --esc <val> : Sets the escape symbol. Default: '/'
-q, --quote <val> : Sets the quote symbols. Default: '"'
-n, --group_end <val> : Sets the group end symbol. Default: ']'
-r, --range <val> : Sets the range symbol. Default: '-'
)";

if (argc > 1)
{
Expand All @@ -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;
Expand Down
40 changes: 40 additions & 0 deletions test/TestExpander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <gtest/gtest.h>
#include "Expander.h"
#include "Helpers.h"

using namespace ::testing;
using namespace std;
Expand Down Expand Up @@ -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<string> 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)
Expand Down