CSC-4730 / P6 / Driver.cpp
Driver.cpp
Raw
// CSC_4730 | P6 | Driver | Caleb Collar & Kai Kuebler | 10.29.21
#include "MutexSpit.cpp"
#include "MutexSpit.hpp"

static void PrintASCII(){
  ifstream ASCIIFile;
  string ASCIILine;
  ASCIIFile.open("ascii.txt");
  if(ASCIIFile.is_open()){
    while(getline(ASCIIFile, ASCIILine)){
      cout << setw(53) << ASCIILine << endl;
    }
    ASCIIFile.close();
  }
}

//Method to print usage from arg '-h'.
static void printUsage() {
  uint32_t width = 26;
  PrintASCII();
  cout << left << setw(width) << "|COMMANDS" << left << setw(width)
       << "|LIMITATIONS" << left << setw(width) << "|USAGE" << right << "|"
       << endl;
  cout << left << setw(width) << "|-h" << left << setw(width) << "|" << left
       << setw(width) << "|show usage dialog" << right << "|" << endl;
  cout << left << setw(width) << "|-z" << left << setw(width)
       << "|(1<<8) <= size >= (1<<20)" << left << setw(width)
       << "|specify deck size" << right << "|" << endl;
  cout << left << setw(width) << "|-s" << left << setw(width) << "|none" << left
       << setw(width) << "|specify rand num gen seed" << right << "|" << endl;
  cout << left << setw(width) << "|-t" << left << setw(width)
       << "|4 <= num threads >= 30" << left << setw(width)
       << "|specify number of threads" << right << "|" << endl;
}

//Method to handle arg inputs.
static void HandleOptions(int argc, char **argv, uint64_t &size, uint64_t &seed, uint32_t &nthreads) {
  int c;
  uint64_t min = 1 << 8, max = 1 << 20;
  size = max;
  seed = time(NULL);
  nthreads = 4;

  while ((c = getopt(argc, argv, "hz:s:t:")) != -1) {
    switch (c) {
    default:
    case 'h': //Print help usage.
      printUsage();
      exit(0);
      break;

    case 'z': //Deck size.
      if (strtoull(optarg, NULL, 0) >= min &&
          strtoull(optarg, NULL, 0) <= max) {
        size = strtoull(optarg, NULL, 0);
      } else {
        cout << "Error, invalid input. Exiting..." << endl;
        exit(1);
      }
      break;

    case 's': // Rand num seed.
      seed = strtoull(optarg, NULL, 0);
      break;

    case 't': // Num threads.
      if (strtoul(optarg, NULL, 0) >= 4 && strtoul(optarg, NULL, 0) <= 30) {
        nthreads = strtoul(optarg, NULL, 0);
      } else {
        cout << "Error, invalid input. Exiting..." << endl;
        exit(1);
      }
      break;
    }
  }
}

// Main driver method.
//  Compile using: g++ -Wall -std=c++11 -O3 Driver.cpp -lpthread
int main(int argc, char *argv[]) {
  uint64_t size, seed; // Size and seed for the random number generator.
  uint32_t nthreads;   // Number of threads to use.
  HandleOptions(argc, argv, size, seed, nthreads); // Handles the command line options.

  Spit<uint32_t> myGame(size, seed, nthreads); //templated to any type of int, or even a char (try it its kinda cool, size must be 128 or it will hang though)
  myGame.PlayGame(); //join is called in destructor, so no need for a join function
  return 0;
}