Initial code
This commit is contained in:
		
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -32,3 +32,5 @@
 | 
			
		||||
*.out
 | 
			
		||||
*.app
 | 
			
		||||
 | 
			
		||||
# Build files
 | 
			
		||||
build/*
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										4
									
								
								Makefile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								Makefile
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,4 @@
 | 
			
		||||
build/rug:src/*.cc
 | 
			
		||||
	g++ -o build/rug src/*.cc -Iinclude
 | 
			
		||||
clean:
 | 
			
		||||
	rm build/rug
 | 
			
		||||
							
								
								
									
										3
									
								
								include/generate.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								include/generate.h
									
									
									
									
									
										Normal file
									
								
							@@ -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);
 | 
			
		||||
							
								
								
									
										34
									
								
								src/generate.cc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								src/generate.cc
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,34 @@
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <random>
 | 
			
		||||
#include <chrono>
 | 
			
		||||
 | 
			
		||||
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<short> char_count_distribution(min_char_count, max_char_count);
 | 
			
		||||
	std::uniform_int_distribution<char> char_distribution('a', 'z');
 | 
			
		||||
	std::uniform_int_distribution<char> num_distribution(0, 9);
 | 
			
		||||
	std::uniform_int_distribution<short> 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;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										58
									
								
								src/main.cc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								src/main.cc
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,58 @@
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <cxxopts.hpp>
 | 
			
		||||
#include <generate.h>
 | 
			
		||||
 | 
			
		||||
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<short>()->default_value("8"))
 | 
			
		||||
		("max-char", "The maximum number of characters (including numbers)", cxxopts::value<short>()->default_value("10"))
 | 
			
		||||
		("min-num", "The minimum number of digits", cxxopts::value<short>()->default_value("0"))
 | 
			
		||||
		("max-num", "The maximum number of digits", cxxopts::value<short>()->default_value("4"))
 | 
			
		||||
		("count", "How many usernames should be generated", cxxopts::value<int>()->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>();
 | 
			
		||||
	short max_char = result["max-char"].as<short>();
 | 
			
		||||
	short min_num = result["min-num"].as<short>();
 | 
			
		||||
	short max_num = result["max-num"].as<short>();
 | 
			
		||||
	int count = result["count"].as<int>();
 | 
			
		||||
 | 
			
		||||
	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;
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user