fork/slippc

git clone https://git.julia.blue/fork/slippc

Patrick CrainBumper parser and analyzer versions to synchronize with slip.py81596bb

0.5.0
3.1 KiB110 linesraw
1#include <algorithm>
2
3#include "util.h"
4#include "parser.h"
5#include "analyzer.h"
6
7// https://stackoverflow.com/questions/865668/how-to-parse-command-line-arguments-in-c
8char* getCmdOption(char ** begin, char ** end, const std::string & option) {
9  char ** itr = std::find(begin, end, option);
10  if (itr != end && ++itr != end) {
11    return *itr;
12  }
13  return 0;
14}
15
16bool cmdOptionExists(char** begin, char** end, const std::string& option) {
17  return std::find(begin, end, option) != end;
18}
19
20void printUsage() {
21  std::cout
22    << "Usage: slippc -i <infile> [-j <jsonfile>] [-a <analysisfile>] [-f] [-d <debuglevel>] [-h]:" << std::endl
23    << "  -i     Parse and analyze <infile> (not very useful on its own)" << std::endl
24    << "  -j     Output <infile> in .json format to <jsonfile> (use \"-\" for stdout)" << std::endl
25    << "  -a     Output an analysis of <infile> in .json format to <analysisfile> (use \"-\" for stdout)" << std::endl
26    << "  -f     When used with -j <jsonfile>, write full frame info (instead of just frame deltas)" << std::endl
27    << "  -d     Run at debug level <debuglevel> (show debug output)" << std::endl
28    << "  -h     Show this help message" << std::endl
29    ;
30}
31
32int main(int argc, char** argv) {
33  if (cmdOptionExists(argv, argv+argc, "-h")) {
34    printUsage();
35    return 0;
36  }
37
38  int debug = 0;
39  char* dlevel = getCmdOption(argv, argv + argc, "-d");
40  if (dlevel) {
41    if (dlevel[0] >= '0' && dlevel[0] <= '2') {
42      debug = dlevel[0]-'0';
43    } else {
44      std::cerr << "Warning: invalid debug level" << std::endl;
45    }
46  }
47  if (debug) {
48    std::cout << "Running at debug level " << +debug << std::endl;
49  }
50
51  char * infile = getCmdOption(argv, argv + argc, "-i");
52  if (not infile) {
53    printUsage();
54    return -1;
55  }
56  bool delta = not cmdOptionExists(argv, argv+argc, "-f");
57
58  slip::Parser *p = new slip::Parser(debug);
59  if (not p->load(infile)) {
60    if (debug) {
61      std::cout << "Could not load input; exiting" << std::endl;
62    }
63    delete p;
64    return 2;
65  }
66
67  char * outfile = getCmdOption(argv, argv + argc, "-j");
68  if (outfile) {
69    if (outfile[0] == '-' && outfile[1] == '\0') {
70      if (debug) {
71        std::cout << "Writing Slippi JSON data to stdout" << std::endl;
72      }
73      std::cout << p->asJson(delta) << std::endl;
74    } else {
75      if (debug) {
76        std::cout << "Saving Slippi JSON data to file" << std::endl;
77      }
78      p->save(outfile,delta);
79    }
80  }
81
82  char * analysisfile = getCmdOption(argv, argv + argc, "-a");
83  if (analysisfile) {
84    slip::Analysis *a  = p->analyze();
85    if (a->success) {
86      if (analysisfile[0] == '-' && analysisfile[1] == '\0') {
87        if (debug) {
88          std::cout << "Writing analysis to stdout" << std::endl;
89        }
90        std::cout << a->asJson() << std::endl;
91      } else {
92        if (debug) {
93          std::cout << "Saving analysis to file" << std::endl;
94        }
95        a->save(analysisfile);
96      }
97    }
98    if (debug) {
99      std::cout << "Deleting analysis" << std::endl;
100    }
101
102    delete a;
103  }
104
105  if (debug) {
106    std::cout << "Cleaning up" << std::endl;
107  }
108  delete p;
109  return 0;
110}