From 2ae1a81b6a4a5d5e8db8b9b819507ff991c6329f Mon Sep 17 00:00:00 2001 From: karl Date: Fri, 23 Oct 2020 11:22:47 +0200 Subject: [PATCH] Parse command line arguments --- main.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/main.cpp b/main.cpp index 0928ec8..832a210 100644 --- a/main.cpp +++ b/main.cpp @@ -1,10 +1,10 @@ #include +#include #include "Timing.h" #define LIVE_CELL 1 // 'x' in the input data #define DEAD_CELL 0 // '.' in the input data -#define NUM_GENERATIONS 250 // Using this struct seems to be more performant than just passing // a bool** around functions. However, also adding the neighbor_count @@ -120,17 +120,57 @@ void generation(World &world, int *neighbor_counts) { } } -int main() { +void print_usage() { + std::cerr << "Usage: gol --load infile.gol --save outfile.gol --generations number [--measure]" << std::endl; +} + +int main(int argc, char* argv[]) { Timing *timing = Timing::getInstance(); // Setup. timing->startSetup(); - // Read in the start state - std::string file_begin = "random10000"; + // Parse command line arguments + std::string infile; + std::string outfile; + int num_generations = 0; + bool measure = false; + if (argc < 6) { + print_usage(); + return 1; + } + + for (int i = 1; i < argc; i++) { + if (std::string(argv[i]) == "--load") { + if (i + 1 < argc) { + infile = argv[i+1]; + } else { + print_usage(); + return 1; + } + } else if (std::string(argv[i]) == "--save") { + if (i + 1 < argc) { + outfile = argv[i+1]; + } else { + print_usage(); + return 1; + } + } else if (std::string(argv[i]) == "--generations") { + if (i + 1 < argc) { + num_generations = std::stoi(argv[i+1]); + } else { + print_usage(); + return 1; + } + } else if (std::string(argv[i]) == "--measure") { + measure = true; + } + } + + // Read in the start state std::ifstream world_file; - world_file.open(file_begin + "_in.gol"); + world_file.open(infile); // Get x and y size std::string x_str, y_str; @@ -167,7 +207,7 @@ int main() { timing->startComputation(); // Do some generations - for (int i = 0; i < NUM_GENERATIONS; i++) { + for (int i = 0; i < num_generations; i++) { generation(world, neighbor_counts); } @@ -176,7 +216,7 @@ int main() { // Write the result std::ofstream result_file; - result_file.open(file_begin + "_out.gol"); + result_file.open(outfile); result_file << size_x << "," << size_y << '\n'; @@ -197,7 +237,7 @@ int main() { timing->stopFinalization(); - timing->print(); + if (measure) timing->print(); return 0; }