diff --git a/.gitignore b/.gitignore index e257658..eefc498 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,5 @@ *.out *.app +# Build files +build/* diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4860507 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +build/rug:src/*.cc + g++ -o build/rug src/*.cc -Iinclude +clean: + rm build/rug diff --git a/include/generate.h b/include/generate.h new file mode 100644 index 0000000..863fe12 --- /dev/null +++ b/include/generate.h @@ -0,0 +1,3 @@ +#pragma once + +void generate(short min_char_count, short max_char_count, short min_num_count, short max_num_count, int count); diff --git a/src/generate.cc b/src/generate.cc new file mode 100644 index 0000000..f641dfe --- /dev/null +++ b/src/generate.cc @@ -0,0 +1,34 @@ +#include +#include +#include + +void generate(short min_char_count, short max_char_count, short min_num_count, short max_num_count, int count) +{ + std::mt19937 engine(std::chrono::system_clock::now().time_since_epoch().count()); + + std::uniform_int_distribution char_count_distribution(min_char_count, max_char_count); + std::uniform_int_distribution char_distribution('a', 'z'); + std::uniform_int_distribution num_distribution(0, 9); + std::uniform_int_distribution num_count_distribution(min_num_count, max_num_count); + + for(int j = 0; j < count; ++j) + { + short char_count = char_count_distribution(engine); + short num_count = num_count_distribution(engine); + + char_count -= num_count; + + for(short i = 0; i < char_count; ++i) + { + std::cout << char_distribution(engine); + } + + for(short i = 0; i < num_count; ++i) + { + char c = num_distribution(engine) + 0x30; // convert to ascii + std::cout << c; + } + + std::cout << std::endl; + } +} diff --git a/src/main.cc b/src/main.cc new file mode 100644 index 0000000..3a81bd5 --- /dev/null +++ b/src/main.cc @@ -0,0 +1,58 @@ +#include +#include +#include + +int main(int argc, char** argv) +{ + cxxopts::Options options("rug", "Random username generator"); + + options.add_options() + ("min-char", "The minimum number of characters (including numbers)", cxxopts::value()->default_value("8")) + ("max-char", "The maximum number of characters (including numbers)", cxxopts::value()->default_value("10")) + ("min-num", "The minimum number of digits", cxxopts::value()->default_value("0")) + ("max-num", "The maximum number of digits", cxxopts::value()->default_value("4")) + ("count", "How many usernames should be generated", cxxopts::value()->default_value("1")) + ("h,help", "Print help") + ; + + auto result = options.parse(argc, argv); + + if(result.count("help")) + { + std::cout << options.help() << std::endl; + return 0; + } + + short min_char = result["min-char"].as(); + short max_char = result["max-char"].as(); + short min_num = result["min-num"].as(); + short max_num = result["max-num"].as(); + int count = result["count"].as(); + + if(min_char > max_char) + { + std::cerr << "ERROR: min-char cannot be greater than max-char" << std::endl << std::endl << options.help() << std::endl; + return -1; + } + + if(min_num > max_num) + { + std::cerr << "ERROR: min-num cannot be greater than max-num" << std::endl << std::endl << options.help() << std::endl; + return -1; + } + + if(max_num > min_char) + { + std::cerr << "ERROR: max-num cannot be greater than min-char" << std::endl << std::endl << options.help() << std::endl; + return -1; + } + + if(min_char < 0 || max_char < 0 || min_num < 0 || max_num < 0 || count < 0) + { + std::cerr << "ERROR: negative argument" << std::endl << std::endl << options.help() << std::endl; + return -1; + } + + generate(min_char, max_char, min_num, max_num, count); + return 0; +}