fork/slippc
git clone https://git.julia.blue/fork/slippc
81596bb
0.5.0
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 ) { 9char ** itr = std::find (begin ,end ,option ); 10if (itr != end && ++ itr != end ) { 11return * itr ; 12 } 13return 0 ; 14} 15 16bool cmdOptionExists (char ** begin ,char ** end ,const std::string & option ) { 17return 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 ) { 33if (cmdOptionExists (argv ,argv + argc ,"-h" )) { 34printUsage (); 35return 0 ; 36 } 37 38int debug = 0 ; 39char * dlevel = getCmdOption (argv ,argv + argc ,"-d" ); 40if (dlevel ) { 41if (dlevel [0 ] >='0' && dlevel [0 ] <='2' ) { 42debug = dlevel [0 ]- '0' ; 43 }else { 44 std::cerr <<"Warning: invalid debug level" << std::endl ; 45 } 46 } 47if (debug ) { 48 std::cout <<"Running at debug level " <<+ debug << std::endl ; 49 } 50 51char * infile = getCmdOption (argv ,argv + argc ,"-i" ); 52if (notinfile ) { 53printUsage (); 54return -1 ; 55 } 56bool delta = notcmdOptionExists (argv ,argv + argc ,"-f" ); 57 58 slip::Parser * p = new slip::Parser (debug ); 59if (notp -> load (infile )) { 60if (debug ) { 61 std::cout <<"Could not load input; exiting" << std::endl ; 62 } 63delete p ; 64return 2 ; 65 } 66 67char * outfile = getCmdOption (argv ,argv + argc ,"-j" ); 68if (outfile ) { 69if (outfile [0 ]== '-' && outfile [1 ]== '\0' ) { 70if (debug ) { 71 std::cout <<"Writing Slippi JSON data to stdout" << std::endl ; 72 } 73 std::cout <<p -> asJson (delta ) << std::endl ; 74 }else { 75if (debug ) { 76 std::cout <<"Saving Slippi JSON data to file" << std::endl ; 77 } 78p -> save (outfile ,delta ); 79 } 80 } 81 82char * analysisfile = getCmdOption (argv ,argv + argc ,"-a" ); 83if (analysisfile ) { 84 slip::Analysis * a = p -> analyze (); 85if (a -> success ) { 86if (analysisfile [0 ]== '-' && analysisfile [1 ]== '\0' ) { 87if (debug ) { 88 std::cout <<"Writing analysis to stdout" << std::endl ; 89 } 90 std::cout <<a -> asJson () << std::endl ; 91 }else { 92if (debug ) { 93 std::cout <<"Saving analysis to file" << std::endl ; 94 } 95a -> save (analysisfile ); 96 } 97 } 98if (debug ) { 99 std::cout <<"Deleting analysis" << std::endl ; 100 } 101 102delete a ; 103 } 104 105if (debug ) { 106 std::cout <<"Cleaning up" << std::endl ; 107 } 108delete p ; 109return 0 ; 110}