From 283c6bba39b146cfb63a73ba4463f25c1d5650dc Mon Sep 17 00:00:00 2001 From: Zeno Rogue Date: Mon, 26 Feb 2018 13:14:20 +0100 Subject: [PATCH] cleanup: arguments --- commandline.cpp | 92 +++++++++++++++++++++++++++++++------------------ hyper.cpp | 9 ++--- hyper.h | 27 ++++++--------- hyperweb.cpp | 26 ++++++++------ mapeditor.cpp | 4 +-- 5 files changed, 91 insertions(+), 67 deletions(-) diff --git a/commandline.cpp b/commandline.cpp index 7328e6f8..5bbd4609 100644 --- a/commandline.cpp +++ b/commandline.cpp @@ -9,31 +9,32 @@ string picfile = "hyperrogue.pic"; const char *musicfile = ""; const char *loadlevel = NULL; -eLand readland(const char *s) { - string ss = s; +bool appears(const string& haystack, const string& needle) { + return haystack.find(needle) != string::npos; + } + +eLand readland(const string& ss) { if(ss == "II") return laCrossroads2; if(ss == "III") return laCrossroads3; if(ss == "IV") return laCrossroads4; if(ss == "V") return laCrossroads5; - for(int l=0; l argument; + int pos; + + void lshift() { pos++; } + + void shift() { + lshift(); if(pos >= size(argument)) { printf("Missing parameter\n"); exit(1); } + } + + const string& args() { return argument[pos]; } + const char* argcs() { return args().c_str(); } + int argi() { return atoi(argcs()); } + int arghex() { return strtol(argcs(), NULL, 16); } + ld argf() { return atof(argcs()); } + bool argis(const string& s) { return args() == s; } + + void init(int argc, char **argv) { for(int i=0; i *hooks_args; namespace arg { - int argc; const char **argv; int curphase; auto ah = addHook(hooks_args, 0, readCommon); @@ -540,11 +566,11 @@ namespace arg { void read(int phase) { curphase = phase; callhooks(hooks_config); - while(argc) { + while(pos < size(argument)) { for(auto& h: *hooks_args) { int r = h.second(); if(r == 2) return; if(r == 0) { lshift(); goto cont; } } - printf("Unknown option: %s\n", args()); + printf("Unknown option: %s\n", argcs()); exit(3); cont: ; } diff --git a/hyper.cpp b/hyper.cpp index 91e81209..0fb436a5 100644 --- a/hyper.cpp +++ b/hyper.cpp @@ -40,12 +40,14 @@ void moreStack() { } #endif -hookset *hooks_main; +hookset *hooks_main; #ifndef NOMAIN -int main(int argc, const char **argv) { +int main(int argc, char **argv) { #if ISWEB - emscripten_get_commandline(argc, argv); + emscripten_get_commandline(); +#else + arg::init(argc, argv); #endif if(callhandlers(false, hooks_main, argc, argv)) return 0; #if !ISWEB @@ -54,7 +56,6 @@ int main(int argc, const char **argv) { #endif #endif #if CAP_COMMANDLINE - arg::init(argc, argv); initializeCLI(); #endif initAll(); diff --git a/hyper.h b/hyper.h index 785ef1cf..62b414c4 100644 --- a/hyper.h +++ b/hyper.h @@ -1503,29 +1503,22 @@ extern int cshpos; namespace arg { #if CAP_COMMANDLINE - extern int argc; extern const char **argv; - inline void lshift() { - argc--; argv++; - } + void lshift(); - inline void shift() { - lshift(); if(!argc) { printf("Missing parameter\n"); exit(1); } - } + void shift(); - inline const char* args() { return *argv; } - inline int argi() { return atoi(*argv); } - inline ld argf() { return atof(*argv); } - inline bool argis(const char *s) { return strcmp(*argv, s) == 0; } + const string& args(); + int argi(); + ld argf(); + bool argis(const string& s); + int arghex(); - inline void init(int _argc, const char **_argv) { argc=_argc-1; argv=_argv+1; } + void init(int _argc, char **_argv); extern int curphase; - inline void phaseerror(int x) { - printf("Command line error: cannot read command '%s' from phase %d in phase %d\n", args(), x, curphase); - exit(1); - } + void phaseerror(int x); // returned values: 0 = ok, 1 = not recognized, 2 = shift phase int readCommon(); @@ -1758,7 +1751,7 @@ template V callhandlers(V zero, hookset *h, U&. extern hookset *hooks_handleKey; extern hookset *hooks_drawcell; -extern hookset *hooks_main; +extern hookset *hooks_main; extern hookset *hooks_args; extern hookset *hooks_nextland; diff --git a/hyperweb.cpp b/hyperweb.cpp index af6d9385..31b5dc48 100644 --- a/hyperweb.cpp +++ b/hyperweb.cpp @@ -24,7 +24,7 @@ template void emscripten_set_main_loop(A a, B b, C c) #endif void initweb(); -void emscripten_get_commandline(int& argc, const char **& argv); +void emscripten_get_commandline(); void loadCompressedChar(int &otwidth, int &otheight, int *tpix); @@ -151,10 +151,10 @@ transmatrix getOrientation() { rotmatrix(0, 2, gamma * M_PI / 180); } -vector emscripten_args; -vector emscripten_args_c; - -void emscripten_get_commandline(int& argc, const char **& argv) { +void emscripten_get_commandline() { +#ifdef EMSCRIPTEN_FIXED_ARG + string s = EMSCRIPTEN_FIXED_ARG; +#else char *str = (char*)EM_ASM_INT({ var jsString = document.location.href; var lengthBytes = lengthBytesUTF8(jsString)+1; @@ -163,14 +163,17 @@ void emscripten_get_commandline(int& argc, const char **& argv) { return stringOnWasmHeap; }); string s = str; +#endif s += "+xxxxxx"; for(int i=0; i