From 194bf2ddc11fdb0061f76627d4dd519838429b90 Mon Sep 17 00:00:00 2001 From: mizipzor Date: Fri, 19 Feb 2010 21:29:23 +0100 Subject: [PATCH] added argstream to digger, showing how it can be used --- tools/digger.cpp | 67 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 16 deletions(-) diff --git a/tools/digger.cpp b/tools/digger.cpp index b9b503f4c..9e7ee48ec 100644 --- a/tools/digger.cpp +++ b/tools/digger.cpp @@ -22,7 +22,7 @@ using namespace std; #include #include #include -#include +#include // counts the occurances of a certain element in a vector int vec_count(vector& vec, uint16_t t) @@ -55,6 +55,20 @@ void string_split(vector& tokens, const std::string& src, const std::str } } +void parse_int_csv(vector& targets, const std::string& src) +{ + std::string::size_type start = 0; + std::string::size_type end; + while (true) + { + end = src.find(",", start); + targets.push_back(atoi(src.substr(start, end - start).c_str())); + if (end == std::string::npos) // last token handled + break; + start = end + 1; // skip next delim + } +} + // calculates the manhattan distance between two coords int manhattan_distance(int x, int y, int z, int xx, int yy, int zz) { @@ -261,27 +275,48 @@ void test() assert(tokens.size() == 1); assert(tokens[0] == "10"); } + { + vector targets; + parse_int_csv(targets, "9,10"); + assert(targets[0] == 9); + assert(targets[1] == 10); + } } -int main (int argc, const char* argv[]) +int main (int argc, char** argv) { - test(); - void *options= gopt_sort( & argc, argv, gopt_start( - gopt_option( 'h', 0, gopt_shorts( 'h', '?' ), gopt_longs( "help", "HELP" )), - gopt_option( 'z', 0, gopt_shorts( 0 ), gopt_longs( "version" )), - gopt_option( 'v', GOPT_REPEAT, gopt_shorts( 'v' ), gopt_longs( "verbose" )), - gopt_option( 'o', GOPT_ARG, gopt_shorts( 'o' ), gopt_longs( "output" )))); - - +test(); + + // Command line options + string s_targets; + string s_origin = "0,0,0"; + bool verbose; + int max; + argstream as(argc,argv); + + as >>option('v',"verbose",verbose,"Active verbose mode") // TODO handle verbose + >>parameter('o',"origin",s_origin,"Close to where we should designate targets, format: x,y,z") + >>parameter('t',"targets",s_targets,"What kinds of tile we should designate, format: type1,type2") + >>parameter('m',"max",max,"The maximum limit of designated targets") + >>help(); + + // handle some commands vector targets; - for (int i = 1; i < argc; ++i) + parse_int_csv(targets, s_targets); + + vector origin; + parse_int_csv(origin, s_origin); + + // sane check + if (!as.isOk()) { - targets.push_back(atoi(argv[i])); + cout << as.errorLog(); } - if (targets.size() == 0) + else if (targets.size() == 0 || origin.size() != 3) { - cout << "Usage: Call with a list of TileClass ids separated by a space,\n"; - cout << "every (visible) tile on the map with that id will be designated for digging.\n\n"; + //cout << "Usage: Call with a list of TileClass ids separated by a space,\n"; + //cout << "every (visible) tile on the map with that id will be designated for digging.\n\n"; + cout << as.usage(); } else { @@ -302,7 +337,7 @@ int main (int argc, const char* argv[]) // return 1; //} - int count = dig(DF, targets, 10, x_source, y_source, z_source); // <-- important part + int count = dig(DF, targets, 10, origin[0],origin[1],origin[2]); // <-- important part cout << count << " targets designated" << endl; DF.Detach();