/* © Copyright 2008 Randal A. Koene With design assistance from J. van Pelt & A. van Ooyen, and support from the Netherlands Organization for Scientific Research (NWO) Program Computational Life Sciences grant CLS2003 (635.100.005) and from the EC Marie Curie Research and Training Network (RTN) NEURoVERS-it 019247. This file is part of NETMORPH. NETMORPH is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. NETMORPH is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with NETMORPH. If not, see . */ // Command_Line_Parameters.cc // Randal A. Koene, 20041215 #include "Command_Line_Parameters.hh" #include "file.hh" #include "global.hh" Command_Line_Parameters * main_clp; String CLP_recentinclude; #ifdef TRACK_RECOGNIZED_COMMANDS //#define FIXED_TRACKING_ARRAY #ifdef FIXED_TRACKING_ARRAY int fixedrecognizedarray[10240]; #endif //STLstd::vector nullrecognized; bool initbatchaddparams = false; // This flag is used to prevent inefficient array expansion. Command_Line_Parameters::Command_Line_Parameters(int _clpargc, char * _clpargv[], const char helpstr[], const char rcfile[]): calledbyforminput(false), clpargc(_clpargc), clpargv(_clpargv), numparameters(0), includefileerrors(0), recognized(NULL), recognizedsize(0), numsubstitutions(0) { //STLrecognized(&nullrecognized) { initbatchaddparams = true; #ifdef FIXED_TRACKING_ARRAY recognized = fixedrecognizedarray; #endif #else Command_Line_Parameters::Command_Line_Parameters(int _clpargc, char * _clpargv[], const char helpstr[], const char rcfile[]): calledbyforminput(false), clpargc(_clpargc), clpargv(_clpargv), numparameters(0), includefileerrors(0), numsubstitutions(0) { #endif Parse_File(rcfile); if ((!Parse_Command_Line()) || (!Detect_Form_Input())) { cout << helpstr; cout << '\n'; report_compiler_directives(); exit(0); } #ifdef TRACK_RECOGNIZED_COMMANDS if (numparameters>0) { #ifndef FIXED_TRACKING_ARRAY recognized = new int[numparameters]; recognizedsize = numparameters; //STLcout << "Vector size: " << recognized->size() << '\n'; cout.flush(); //STLrecognized = new std::vector(numparameters,(int) 0); //STLcout << "Vector size: " << recognized->size() << '\n'; cout.flush(); #else if (numparameters<10240) recognizedsize=numparameters; else recognizedsize=10240; #endif for (int i=0; i='A') && (s[i]<='Z')) || ((s[i]>='a') && (s[i]<='z'))) break; if ((i0)) s = s.from(i); } #ifdef TRACK_RECOGNIZED_COMMANDS void Command_Line_Parameters::Expand_Recognized(unsigned int oldlen, unsigned int newlen) { if (newlen<=oldlen) return; int * r = new int[newlen]; if (recognized) { for (unsigned int i=0; (i=0) { parvalue[n] = pvalue; // new value takes precedence over old value #ifdef TRACK_RECOGNIZED_COMMANDS origin[n] = co; // new origin of new value #endif } else { parname[numparameters] = pname; parvalue[numparameters] = pvalue; origin[numparameters] = co; numparameters++; #ifdef TRACK_RECOGNIZED_COMMANDS #ifdef FIXED_TRACKING_ARRAY if (!initbatchaddparams) { // [***INCOMPLETE] This breaks if numparameters>10240. recognized[numparameters-1]=0; if (numparameters<10240) recognizedsize=numparameters; else recognizedsize=10240; } #else //STLif (!initbatchaddparams) recognized->push_back(0); if (!initbatchaddparams) Expand_Recognized(numparameters-1,numparameters); #endif #endif } } void Command_Line_Parameters::Parse_File(const char filename[]) { String fname(filename); if (fname.empty()) return; String parstr; if (!read_file_into_String(fname,parstr)) { includefileerrors++; return; } CLP_recentinclude = fname; // remove comment lines, which may not terminate with ';' BigRegex inithashcomments("^[ \t]*#[^\n]*\n"); // (init)hashcomment must be separated, since gsub treats the whole string as one line BigRegex hashcomments("\n[ \t]*#[^\n]*\n"); while (parstr.gsub(hashcomments,"\n")>0) ; parstr.gsub(inithashcomments,""); BigRegex initCPPcomments("^[ \t]*[/][/][^\n]*\n"); // (init)CPPcomment must be separated, since gsub treats the whole string as one line BigRegex CPPcomments("\n[ \t]*[/][/][^\n]*\n"); while (parstr.gsub(CPPcomments,"\n")>0) ; parstr.gsub(initCPPcomments,""); BigRegex Ccomments("[/][*].*[*][/]"); // [***UNTESTED] This may not work as expected, as it may find the last instance of a comment end. parstr.gsub(Ccomments,""); // make a list of parameter=value pairs StringList plist(parstr,';'); int pnum = plist.length()-1; // *** empty element at end of StringList String pname; for (int i = 0; i0) && (clen<(LLEN-1))) { cin.get(lbuf,clen+1); if (!querystr.empty()) querystr += '&'; querystr += lbuf; } } if (querystr.length()==0) return true; StringList qlist(querystr,'&'); int qnum = qlist.length()-1; // *** empty element at end of StringList #ifdef TRACK_RECOGNIZED_COMMANDS for (int i=0; i1) { String pname(clpargv[1]); if ((pname == "-h") || (pname == "-?")) return false; #ifdef TRACK_RECOGNIZED_COMMANDS Add_Parameter(pname.before('='),pname.after('='),"command line"); #else Add_Parameter(pname.before('='),pname.after('=')); #endif for (int i = 2; i=numparameters) return String(""); String escapedURI(parvalue[n]); String s; char c; int d1,d2; for (unsigned int i = 0; i='0') && (c<='9')) d1 = (int) (c - '0'); else if ((c>='A') && (c<='F')) d1 = ((int) (c - 'A')) + 10; else d1 = ((int) (c - 'a')) + 10; i++; c = escapedURI[i]; if ((c>='0') && (c<='9')) d2 = (int) (c - '0'); else if ((c>='A') && (c<='F')) d2 = ((int) (c - 'A')) + 10; else d2 = ((int) (c - 'a')) + 10; d1 *=16; d1 += d2; c = (char) d1; } s += c; } return s; } String Command_Line_Parameters::str(String sep) { String s; for (int i=0; imax) { res = max; if (warn) warning(String(warn)+", setting to maximum value "+String((long) res)+".\n"); } return res; } double Command_Line_Parameters::get_double(int n, double min, double max, const char * warn) { char * endptr = NULL; const char * dblstr = ParValue(n).chars(); double res = 0.0; if (!ParValue(n).empty()) res = strtod(dblstr,&endptr); if ((resmax) { res = max; if (warn) warning(String(warn)+", setting to maximum value "+String(res,"%f")+".\n"); } return res; } int Command_Line_Parameters::init_int(int & parnum, String parlbl, int defaultval, int min, int max, const char * warn) { int res = defaultval; if ((parnum=Specifies_Parameter(parlbl))>=0) { res = get_int(parnum,min,max,warn); } return res; } double Command_Line_Parameters::init_double(int & parnum, String parlbl, double defaultval, double min, double max, const char * warn) { double res = defaultval; if ((parnum=Specifies_Parameter(parlbl))>=0) { res = get_double(parnum,min,max,warn); } return res; } #ifdef TRACK_RECOGNIZED_COMMANDS int Command_Line_Parameters::NumUnrecognized() { int res = 0; //STLfor (unsigned int i=0; isize(); i++) if ((*recognized)[i]<1) res++; if (recognized) for (int i=0; isize() << " and numparameters=" << numparameters << '\n'; cout.flush(); //STLfor (unsigned int i=0; ((isize()) && (i<(unsigned int) numparameters)); i++) { for (int i=0; (i