Skip to content

Commit

Permalink
Merge pull request #4 from russell-lundberg:options1
Browse files Browse the repository at this point in the history
finished removing boost and adding -x regex option
  • Loading branch information
russell-lundberg authored Jan 19, 2024
2 parents 0ff5848 + 27d9449 commit 2f8a8b8
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 20 deletions.
Binary file modified bin/main
Binary file not shown.
Binary file modified obj/main.o
Binary file not shown.
Binary file modified obj/options.o
Binary file not shown.
Binary file modified obj/util.o
Binary file not shown.
9 changes: 9 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ int main(int argc, char *argv[])

std::cout << "there are " << valid_words.size() << " valid words.\n";

// test for a regex filter
/*
if (Options["regex"]) {
// there is a regex option
std::cout << "main(): regex option value: " << Options["regex"] << ".\n";
}
*/

// display the inputted command line, for reference
std::cout << "You entered: ";
for ( int i = 0 ; i<= argc ; i++ ) {
std::cout << argv[i] << " ";
Expand Down
31 changes: 29 additions & 2 deletions src/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ void Option_Switch( int count, char** arg, int* j, std::unordered_map<std::strin

// test if the next argument would overflow argv
if ( *j+1 < count ) {
// there are more args, which could means letters submitted to -e.
// test if the next arg is an option
// there is at least one more argv element. Test if the next arg is an option
if ( ! isOption( arg[*j+1] ) ) {
// letters were submitted to -e, so ingest them
// std::cout << "Option_Switch():" << arg[*j] << " returned value " << arg[*j+1] << ".\n";
Expand All @@ -155,6 +154,34 @@ void Option_Switch( int count, char** arg, int* j, std::unordered_map<std::strin
}
}
}
else if ( strcmp(arg[*j],"-x") == 0 ){
// this is the regex option, that filters the valid words by matching this user-inputted regex.
std::cout << "Option_Switch(): Option \"-x\" detected.\n";

// test if the next argument would overflow argv
if ( *j+1 >= count ) {
// thia means now value was submitted with the -x option. That's an error/
std::cout << "Option_Switch(): \"-x\" option requires a regex, exiting.\n";
show_usage("");
exit(1);
}

// test if the next arg is an option
if ( isOption( arg[*j+1] ) ) {
// this is also an error, because if the next argv element is an option, it again
// means no regex was submitted with -x. That's an error.
std::cout << "Option_Switch(): \"-x\" option requires a regex, exiting.\n";
show_usage("");
exit(1);
}

std::cout << "Option_Switch: " << arg[*j] << " returned value " << arg[*j+1] << ".\n";

( *n ).emplace( "regex", arg[*j+1] );
// increment j becaue the j+1 arg has already been used. Don't want
// to use it in the for loop twice
*j += 1;
}
else if ( strcmp(arg[*j],"-b") == 0 ) {
// std::cout << "Option_Switch(): option b loop index value is " << *j << "\n";
( *n ).insert({"blanks","blank1"});
Expand Down
34 changes: 16 additions & 18 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ std::set<std::string> WWF::get_dictionary() {

}


struct comp {
template <typename T>

Expand Down Expand Up @@ -103,19 +102,13 @@ std::set<std::pair<std::string,int>> WWF::Words_Sorted(
// std::cout << "About to test regex.\n";

options.count("regex") == 1 ? isRegex = true : isRegex = false;

if ( options.count("regex") == 1 ) {
isRegex = 1;
// this casts the std::string input to a std::regex
regex_input = std::regex( options.at("regex") );
}

// std::cout << "Tested regex.\n";
// std::cout << "Util::Words_Sorted(): isRegex = " << isRegex << "\n";

for (auto& it : S) {
// this prints the valid, sorted words to the screen
std::cout << it.first << ' ' << it.second << "\n";
if ( isRegex ) {
regex_input = std::regex( options.at("regex") );
if ( std::regex_search(it.first, regex_input ) ) {
// long-ass process to cast an int to a string
std::stringstream stream;
Expand All @@ -127,17 +120,22 @@ std::set<std::pair<std::string,int>> WWF::Words_Sorted(
}
}
}
if ( regex_matches.size() ) {
std::cout << regex_matches.size()
<< " words match your regex \""
<< options.at("regex")
<< "\"\n";

for ( auto elem : regex_matches ) {
std::cout << elem << "\n";

if ( isRegex ) {

if ( regex_matches.size() ) {
std::cout << regex_matches.size()
<< " words match your regex \""
<< options.at("regex")
<< "\"\n";
for ( auto elem : regex_matches ) {
std::cout << elem << "\n";
}
}
else {
std::cout << "Words_Sorted(): no words match \"" << options.at("regex") << "\"\n";
}
}

return return_value;
} // end Words_Sorted()

Expand Down

0 comments on commit 2f8a8b8

Please sign in to comment.