PlasCom2  1.0
XPACC Multi-physics simluation application
AppTools.H
Go to the documentation of this file.
1 #include <string>
2 #include <vector>
3 #include <map>
4 #include <sstream>
5 #include <istream>
6 #include <ostream>
7 #include <fstream>
8 #include <list>
9 #include <cstdlib>
10 #include <cstdio>
11 #include <cassert>
12 
13 namespace apptools {
14 
18  inline int GNUPlot(const std::string &commands)
19  {
20  FILE *fp = popen("gnuplot -persist","w");
21  fprintf(fp, "%s", commands.c_str());
22  pclose(fp);
23  return(0);
24  }
25 
29  template<typename K,typename V>
30  class keyvaluepair : public std::pair<K,V>
31  {
32  public:
33  keyvaluepair(const std::pair<K,V> &p){
34  this->first = p.first;
35  this->second = p.second;
36  };
37  keyvaluepair(const K &k,const V &v){
38  this->first = k;
39  this->second = v;
40  };
42  K Key() const{ return this->first; };
43  V Value() const{ return this->second; };
44  K &Key() { return this->first; };
45  V &Value() { return this->second; };
46  K &Key(const K &k) { this->first = k; return(this->first); };
47  V &Value(const V &v) { this->second = v; return(this->second); };
48  // virtual std::ostream &Dump(std::ostream &Ostr){
49  // Ostr << Key() << ":" << Value();
50  // return(Ostr);
51  // };
52  };
53 
54  inline std::string GetNextContent(std::istream &In)
55  {
56  std::string line;
57  std::string ret;
58  while(ret.empty() && std::getline(In,line)){
59  line = line.substr(0,line.find("#"));
60  std::istringstream Istr(line);
61  std::string token;
62  Istr >> token;
63  if(!token.empty() && token[0] != '#')
64  ret.assign(line);
65  }
66  return(ret);
67  }
68 
69  inline void GetContentUntil(std::istream &In,
70  std::string ret,
71  const std::string &etag)
72  {
73  std::ostringstream Ostr;
74  std::string line;
75  while(std::getline(In,line) && ret.empty()){
76  line = line.substr(0,line.find("#"));
77  if(!line.empty()){
78  std::istringstream Istr(line);
79  std::string token;
80  Istr >> token;
81  if(!token.empty()){
82  if(token == etag){
83  // Ostr << line << std::endl;
84  ret = Ostr.str();
85  }
86  else
87  Ostr << line << std::endl;
88  }
89  }
90  }
91  }
92 
99  inline const std::string
100  stripdirs(const std::string &pname)
101  {
102  std::string retval;
103  std::string::size_type x = pname.find("/");
104  if(x == std::string::npos)
105  return(pname);
106  return(pname.substr(pname.find_last_of("/")+1));
107  }
108 
109 
116  template<typename ContainerType>
117  void DumpContents(std::ostream &Ostr,const ContainerType &c,std::string del = "\n"){
118  if(!c.empty()){
119  typename ContainerType::const_iterator ci = c.begin();
120  Ostr << *ci++;
121  while(ci != c.end())
122  Ostr << del << *ci++;
123  }
124  }
125 
131  inline void
132  TokenizeString(std::vector<std::string> &tokens,const std::string &source)
133  {
134  tokens.resize(0);
135  std::istringstream Istr(source);
136  std::string token;
137  while(Istr >> token)
138  tokens.push_back(token);
139  }
140 
144  inline int OpenFile(std::ifstream &Inf,const std::string &filename)
145  {
146  Inf.open(filename.c_str());
147  if(!Inf)
148  return(-1);
149  return 0;
150  }
151 
153 
154  std::ostream &operator<<(std::ostream &Ostr,
155  const ParamType &param);
156 
157  class parameters : public std::vector<ParamType>
158 
159  {
160  friend std::ostream &operator<<(std::ostream &oSt,
161  const parameters &pv);
162  friend std::istream &operator>>(std::istream &iSt,
163  parameters &pv);
164  public:
165  std::string GetValue(const std::string &key) const;
166  std::vector<std::string> GetValueVector(const std::string &key) const;
167  std::list<std::string> GetValueList(const std::string &key) const;
168 
169  ParamType *ParamPtr(const std::string &key);
170 
171  bool GetFlagValue(const std::string &key) const
172  {
173  bool retVal = false;
174  std::string value(this->Param(key));
175  if(!value.empty()){
176  if(value=="on" || value=="yes" ||
177  value=="ON" || value=="YES" ||
178  value=="Yes" || value=="True" ||
179  value=="1" || value=="true" ||
180  value=="TRUE" || value==".true." ||
181  value==".TRUE.")
182  retVal = true;
183  }
184  return(retVal);
185  };
186 
187  template<typename T>
188  T GetValue(const std::string &key) const
189  {
190  T retval = T();
191  std::string value(this->Param(key));
192  if(!value.empty()){
193  std::istringstream Istr(value);
194  Istr >> retval;
195  }
196  return(retval);
197  };
198 
199  template<typename T>
200  std::vector<T> GetValueVector(const std::string &key) const
201  {
202  std::vector<T> retval;
203  std::string value(this->Param(key));
204  if(!value.empty()){
205  std::istringstream Istr(value);
206  T tmpval;
207  while(Istr >> tmpval)
208  retval.push_back(tmpval);
209  }
210  return(retval);
211  };
212 
213  template<typename T>
214  std::list<T> GetValueList(const std::string &key) const
215  {
216  std::list<T> retval;
217  std::string value(this->Param(key));
218  if(!value.empty()){
219  std::istringstream Istr(value);
220  T tmpval;
221  while(Istr >> tmpval)
222  retval.push_back(tmpval);
223  }
224  return(retval);
225  };
226 
227  virtual int SetValue(const std::string &key,const std::string &value){
228  if(ParamPtr(key)){
229  ParamPtr(key)->Value(value);
230  return(0);
231  }
232  return(1);
233  };
234  virtual std::istream &ReadFromStream(std::istream &Is);
235  virtual std::ostream &WriteToStream(std::ostream &Os) const;
236  std::string Param(const std::string &Key) const;
237  bool IsSet(const std::string &Key) const;
238  virtual ~parameters(){};
239  };
240  std::ostream &operator<<(std::ostream &oSt,const parameters &pv);
241  std::istream &operator>>(std::istream &iSt,parameters &pv);
242  ParamType *parameters::ParamPtr(const std::string &key)
243  {
244  parameters::const_iterator pi = this->begin();
245  while(pi != this->end()){
246  unsigned int index = pi - this->begin();
247  if(pi->first == key)
248  return(&((*this)[index]));
249  pi++;
250  }
251  return(NULL);
252  }
253 
254  std::string parameters::GetValue(const std::string &key) const
255  {
256  std::string retval;
257  std::string value(this->Param(key));
258  if(!value.empty()){
259  std::istringstream Istr(value);
260  Istr >> retval;
261  }
262  return(retval);
263  }
264 
265  std::vector<std::string> parameters::GetValueVector(const std::string &key) const
266  {
267  std::vector<std::string> retval;
268  std::string value(this->Param(key));
269  if(!value.empty()){
270  std::istringstream Istr(value);
271  std::string tmpval;
272  while(Istr >> tmpval)
273  retval.push_back(tmpval);
274  }
275  return(retval);
276  }
277 
278  std::list<std::string> parameters::GetValueList(const std::string &key) const
279  {
280  std::list<std::string> retval;
281  std::string value(this->Param(key));
282  if(!value.empty()){
283  std::istringstream Istr(value);
284  std::string tmpval;
285  while(Istr >> tmpval)
286  retval.push_back(tmpval);
287  }
288  return(retval);
289  }
290 
291  std::istream &parameters::ReadFromStream(std::istream &Is)
292  {
293  std::string line;
294  int n = 0;
295  while(Is){
296  std::getline(Is,line);
297  n++;
298  // Removes any leading whitespace
299  std::string::size_type x = line.find('#');
300  line = line.substr(0,x);
301  if(!line.empty()){
302  x = line.find('=');
303  if(x == std::string::npos)
304  return(Is);
305  ParamType param;
306  std::istringstream Instr(line.substr(0,x));
307  Instr >> param.Key();
308  std::vector<std::string> tokens;
309  line = line.substr(x+1,line.size());
310  TokenizeString(tokens,line);
311  std::ostringstream Ostr;
312  std::vector<std::string>::iterator ti = tokens.begin();
313  if(ti != tokens.end())
314  Ostr << *ti++;
315  while(ti != tokens.end())
316  Ostr << " " << *ti++;
317  param.Value() = Ostr.str();
318  this->push_back(param);
319  }
320  }
321  return(Is);
322  }
323 
324  std::string parameters::Param(const std::string &key) const
325  {
326  std::string empty;
327  parameters::const_iterator pi = this->begin();
328  while(pi != this->end()){
329  if(pi->first == key)
330  return(pi->second);
331  pi++;
332  }
333  return(empty);
334  }
335 
336  bool parameters::IsSet(const std::string &key) const
337  {
338  return(!Param(key).empty());
339  };
340 
341 
342  std::ostream &parameters::WriteToStream(std::ostream &oSt) const
343  {
344  parameters::const_iterator pi = this->begin();
345  while(pi != this->end()){
346  oSt << *pi++;
347  if(pi != this->end())
348  oSt << std::endl;
349  }
350  return(oSt);
351  };
352 
353  std::ostream &operator<<(std::ostream &oSt,
354  const parameters &pv)
355  {
356  return(pv.WriteToStream(oSt));
357  }
358 
359  std::istream &operator>>(std::istream &iSt,
360  parameters &pv)
361  {
362  return(pv.ReadFromStream(iSt));
363  }
364 
365 
366  std::ostream &operator<<(std::ostream &Ostr,const ParamType &param)
367  {
368  Ostr << param.Key() << " = " << param.Value();
369  return(Ostr);
370  }
371 
372 
411  class comlinehandler : public std::vector< std::pair<char,std::string> >
412  {
413  friend std::ostream &operator<<(std::ostream &Out,const comlinehandler &cl);
414  friend std::istream &operator>>(std::istream &In,comlinehandler &cl);
415  protected:
422  std::string _description;
424  std::string _notes;
426  std::string _program_name;
428  std::string _line;
430  std::vector<std::string> _error_messages;
432  std::vector<std::string> _nonops;
434  std::map<char,std::string> _options;
436  std::map<char,std::string> _help;
438  std::map<char,std::string> _argname;
440  std::map<char,int> _type;
442  std::vector<std::pair<std::string,int> > _args;
444  std::map<std::string,std::string> _arghelp;
446  std::vector<std::string> _toks;
447  public:
460  comlinehandler(const char *args[])
461  {
462  Record(args);
463  };
471  void Copy(comlinehandler &incom);
475  int ProcessCommandLine(const char *args[])
476  {
477  Record(args);
478  return(ProcessOptions());
479  };
483  std::string GetRawComLine() const { return (_program_name+std::string(" ")+_line);};
487  void SetRawComLine(const std::string &incl);
495  int ProcessOptions();
502  void Record(const char *args[]);
512  void AddOption(char s,const std::string &l,int = 0);
522  void AddOption(char s,const std::string &l,int,const std::string argname);
532  void AddArgument(const std::string &a,int reqd = 0)
533  {
534  //_args.push_back(std::make_pair<std::string,int>(a,reqd));
535  _args.push_back(std::make_pair(a,reqd));
536  };
546  void AddArgHelp(const std::string &a,const std::string &help)
547  {
548  _arghelp[a] = help;
549  };
556  void SetArgName(const std::string opstr,const std::string argname)
557  {
558  _argname[GetShort(opstr)] = argname;
559  };
566  void SetArgName(char s,const std::string &argname)
567  {
568  _argname[s] = argname;
569  };
576  void AddHelp(char s,const std::string &help){ _help[s] = help;};
583  void AddHelp(const std::string &l,const std::string &help)
584  {
585  _help[GetShort(l)] = help;
586  };
593  std::string GetOpStringByType(int mintype,int maxtype);
597  std::string GetHelp(char s){return(_help[s]);};
601  std::string GetHelp(const std::string &l){return(_help[GetShort(l)]);};
605  char GetShort(const std::string &l);
609  std::string GetLong(const char &s);
619  std::string ShortUsage();
632  std::string LongUsage();
640  std::string GetOption(const char &s){return(_options[s]);};
648  std::string GetOption(const std::string &l){return(_options[GetShort(l)]);};
655  std::string ErrorReport();
659  std::string ProgramName() const { return _program_name;};
663  std::vector<std::string> GetArgs() const { return _nonops;};
667  void SetDescription(const std::string &desc){ _description.assign(desc); };
671  void SetNotes(const std::string &notes){ _notes.assign(notes); };
675  void WriteRC(std::ostream &Ostr) const;
679  void ReadRC(const std::string &RCString);
683  template<typename NumType>
684  void ProcessRange(NumType &t1,NumType &t2,const std::string stinter)
685  {
686  if(!stinter.empty()){
687  std::string::size_type x = stinter.find(":");
688  std::string t1s = stinter;
689  std::string t2s = stinter;
690  if(x != std::string::npos){
691  t1s = stinter.substr(0,x);
692  t2s = stinter.substr(x+1);
693  if(!t2s.empty()){
694  std::istringstream Istr(t2s);
695  Istr >> t2;
696  }
697  }
698  else {
699  t1s.erase();
700  }
701  if(!t1s.empty()){
702  std::istringstream Istr(t1s);
703  Istr >> t1;
704  }
705  if(!t2s.empty()){
706  std::istringstream Istr(t2s);
707  Istr >> t2;
708  }
709 
710  }
711  };
715  template<typename NumType>
716  void ResolveOption(NumType &t2,const std::string stinter)
717  {
718  if(stinter == ".true." || stinter == ".TRUE."){
719  t2 = 1;
720  return;
721  }
722  if(stinter == ".false." || stinter == ".FALSE."){
723  t2 = 0;
724  return;
725  }
726  if(!stinter.empty()){
727  std::istringstream Istr(stinter);
728  Istr >> t2;
729  }
730  };
734  virtual void Initialize(void){};
738  virtual void UserInitialize(void){};
739 
740  virtual ~comlinehandler(){};
741  };
742 
743 
744  std::istream &operator>>(std::istream &In,comlinehandler &cl)
745  {
746  std::string line;
747  std::string::size_type x;
748  line = GetNextContent(In);
749  x = line.find("<description>");
750  if(x != std::string::npos){
751  GetContentUntil(In,cl._description,"</description>");
752  line = GetNextContent(In);
753  }
754  x = line.find("<options>");
755  if(x == std::string::npos){
756  cl._error_messages.push_back("Config file format error - options section misplaced?.");
757  return(In);
758  }
759  std::string options_content;
760  GetContentUntil(In,options_content,"</options>");
761  std::istringstream Istr(options_content);
762  while(std::getline(Istr,line)){
763  std::istringstream OpIn(line);
764  std::string token;
765  char opchar;
766  int optype;
767  OpIn >> opchar;
768  OpIn >> token;
769  OpIn >> optype;
770  cl.push_back(std::make_pair(opchar,token));
771  cl._type[opchar] = optype;
772  if(optype > 0){
773  std::string token;
774  OpIn >> token;
775  cl._argname[opchar] = token;
776  }
777  std::getline(Istr,line);
778  x = line.find("<help>");
779  if(x == std::string::npos){
780  cl._error_messages.push_back("Configuration input format error in option help section.");
781  return(In);
782  }
783  cl._help[opchar] = "";
784  GetContentUntil(Istr,cl._help[opchar],"</help>");
785  }
786  line = GetNextContent(In);
787  if(line.empty())
788  return(In);
789  x = line.find("<arguments>");
790  if(x == std::string::npos){
791  cl._error_messages.push_back("Configuration intput format error in argument section.");
792  return(In);
793  }
794  std::string argsection;
795  GetContentUntil(In,argsection,"</arguments>");
796  Istr.str(argsection);
797  while(std::getline(Istr,line)){
798  std::istringstream ArgIn(line);
799  std::string argname;
800  int argtype;
801  ArgIn >> argname >> argtype;
802  cl._arghelp[argname] = argtype;
803  std::string tag;
804  ArgIn >> tag;
805  std::string::size_type x = tag.find("<help>");
806  if(x == std::string::npos){
807  cl._error_messages.push_back("Configuration input format error in arghelp section.");
808  return(In);
809  }
810  cl._arghelp[argname] = "";
811  GetContentUntil(In,cl._arghelp[argname],"</help>");
812  }
813  return(In);
814  }
815 
816  std::ostream &operator<<(std::ostream &Out,const comlinehandler &cl)
817  {
818  if(!cl._description.empty())
819  Out << "<description>\n" << cl._description << "</description>\n";
820  Out << "<options>\n";
821  comlinehandler::const_iterator cli = cl.begin();
822  while(cli != cl.end()){
823  char opchar = cli->first;
824  std::map<char,int>::const_iterator ti = cl._type.find(opchar);
825  Out << cli->first << " " << cli->second << " "
826  << (ti == cl._type.end() ? 0 : ti->second);
827  if(ti != cl._type.end()){
828  std::map<char,std::string>::const_iterator ai = cl._argname.find(opchar);
829  Out << " " << (ai == cl._argname.end() ? "(unspecified)" : ai->second);
830  }
831  std::map<char,std::string>::const_iterator hi = cl._help.find(opchar);
832  Out << "\n<help>\n" << (hi == cl._help.end() ? "(unspecified)" : hi->second)
833  << "\n</help>\n";
834  cli++;
835  }
836  Out << "</options>\n";
837  if(!cl._args.empty()){
838  Out << "<arguments>\n";
839  std::vector<std::pair<std::string,int> >::const_iterator ai = cl._args.begin();
840  while(ai != cl._args.end()){
841  std::map<std::string,std::string>::const_iterator ahi = cl._arghelp.find(ai->first);
842  Out << ai->first << " " << ai->second
843  << "\n<help>\n" << (ahi == cl._arghelp.end() ? "(unspecified)" : ahi->second)
844  << "\n</help>\n";
845  ai++;
846  }
847  Out << "</arguments>\n";
848  }
849  return(Out);
850  }
851 
852 
854  {
855  Copy(incom);
856  };
857 
859  {
860  _description.erase();
861  _notes.erase();
862  _program_name.erase();
863  _line.erase();
864  _error_messages.resize(0);
865  _nonops.resize(0);
866  _options.clear();
867  _help.clear();
868  _argname.clear();
869  _type.clear();
870  _args.resize(0);
871  _arghelp.clear();
872  this->resize(0);
873 
874  _description.assign(incom._description);
875  _notes.assign(incom._notes);
876  _program_name.assign(incom._program_name);
877  _line.assign(incom._line);
878  std::vector<std::string>::iterator ici = incom._error_messages.begin();
879  while(ici != incom._error_messages.end())
880  _error_messages.push_back(*ici++);
881  ici = incom._nonops.begin();
882  while(ici != incom._nonops.end())
883  _nonops.push_back(*ici++);
884  std::map<char,std::string>::iterator icmi = incom._options.begin();
885  while(icmi != incom._options.end()){
886  _options[icmi->first] = icmi->second;
887  icmi++;
888  }
889  icmi = incom._help.begin();
890  while(icmi != incom._help.end()){
891  _help[icmi->first] = icmi->second;
892  icmi++;
893  }
894  icmi = incom._argname.begin();
895  while(icmi != incom._argname.end()){
896  _argname[icmi->first] = icmi->second;
897  icmi++;
898  }
899  std::map<char,int>::iterator icti = incom._type.begin();
900  while(icti != incom._type.end()){
901  _type[icti->first] = icti->second;
902  icti++;
903  }
904  std::vector<std::pair<std::string,int> >::iterator icai = incom._args.begin();
905  while(icai != incom._args.end())
906  _args.push_back(*icai++);
907 
908  std::map<std::string,std::string>::iterator icahi = incom._arghelp.begin();
909  while(icahi != incom._arghelp.end()){
910  _arghelp[icahi->first] = icahi->second;
911  icahi++;
912  }
913  comlinehandler::iterator cli = incom.begin();
914  while(cli != incom.end())
915  this->push_back(*cli++);
916  };
917 
919  {
920  std::ostringstream Ostr;
921  if(!_description.empty())
922  Ostr << _description << std::endl << std::endl;
923  Ostr << "Usage: " << std::endl
924  << std::endl << ShortUsage() << std::endl << std::endl;
925  std::vector<std::pair<char,std::string> >::const_iterator ti = this->begin();
926  while(ti != this->end()){
927  Ostr << "\t" << "-" << ti->first << ",--" << ti->second
928  << (_type[ti->first] > 0 ? (_type[ti->first]==1 ? " [" : " <") : "")
929  << _argname[ti->first]
930  << (_type[ti->first] > 0 ? (_type[ti->first]==1 ? "]" : ">") : "")
931  << std::endl << "\t\t" << _help[ti->first];
932  ti++;
933  if(ti != this->end())
934  Ostr << std::endl << std::endl;
935  }
936  Ostr << std::endl << std::endl;
937  std::vector<std::pair<std::string,int> >::const_iterator ai = _args.begin();
938  while(ai != _args.end()){
939  Ostr << "\t" << (ai->second ? "<" : "[") << ai->first
940  << (ai->second ? ">" : "]") << std::endl << "\t\t"
941  << _arghelp[ai->first];
942  ai++;
943  if(ai != _args.end())
944  Ostr << std::endl << std::endl;
945  }
946  if(!_notes.empty())
947  Ostr << std::endl << _notes;
948  return(Ostr.str());
949  }
950 
951  std::string comlinehandler::GetLong(const char &s)
952  {
953  std::vector<std::pair<char,std::string> >::iterator ti = this->begin();
954  while(ti != this->end()){
955  if(ti->first == s)
956  return(ti->second);
957  ti++;
958  }
959  return("");
960  }
961 
962  std::string comlinehandler::GetOpStringByType(int mintype,int maxtype)
963  {
964  std::ostringstream Ostr;
965  std::vector<std::pair<char,std::string> >::iterator oi = this->begin();
966  while(oi != this->end()){
967  if(_type[oi->first] >= mintype && _type[oi->first] <= maxtype)
968  Ostr << oi->first;
969  oi++;
970  }
971  return(Ostr.str());
972  }
973 
975  {
976  std::ostringstream Ostr;
977  std::string flagstring = GetOpStringByType(0,0);
978  Ostr << _program_name << " ";
979  if(!flagstring.empty())
980  Ostr << "[-" << flagstring << "] ";
981  std::string optionals = GetOpStringByType(1,2);
982  if(!optionals.empty()){
983  std::string::iterator oi = optionals.begin();
984  Ostr << "[";
985  while(oi != optionals.end()){
986  Ostr << "-" << *oi
987  << (_type[*oi] == 1 ? " [" : " <")
988  << _argname[*oi]
989  << (_type[*oi] == 1 ? "] " : "> ");
990  oi++;
991  }
992  Ostr << "] ";
993  }
994  std::string reqd = GetOpStringByType(3,3);
995  if(!reqd.empty()){
996  std::string::iterator oi = reqd.begin();
997  Ostr << "<";
998  while(oi != reqd.end()){
999  Ostr << "-" << *oi << " <" << _argname[*oi]
1000  << "> ";
1001  oi++;
1002  }
1003  Ostr << "> ";
1004  }
1005  std::vector<std::pair<std::string,int> >::iterator ai = _args.begin();
1006  while(ai != _args.end()){
1007  Ostr << (ai->second > 0 ? "<" : "[") << ai->first
1008  << (ai->second > 0 ? "> ": "] ");
1009  ai++;
1010  }
1011  return(Ostr.str());
1012  };
1013 
1014  char comlinehandler::GetShort(const std::string &l)
1015  {
1016  std::vector<std::pair<char,std::string> >::iterator ti = this->begin();
1017  while(ti != this->end()){
1018  if(ti->second == l)
1019  return(ti->first);
1020  ti++;
1021  }
1022  return('\0');
1023  };
1024 
1025  void comlinehandler::AddOption(char s,const std::string &l,int atype)
1026  {
1027  //this->push_back(std::make_pair<char,std::string>(s,l));
1028  this->push_back(std::make_pair(s,l));
1029  _options[s] = std::string("");
1030  _type[s] = atype;
1031  if(atype > 0)
1032  _argname[s] = "arg";
1033  _help[s] = std::string("");
1034  }
1035 
1036  void comlinehandler::AddOption(char s,const std::string &l,int type,const std::string argname)
1037  {
1038  //this->push_back(std::make_pair<char,std::string>(s,l));
1039  this->push_back(std::make_pair(s,l));
1040  _options[s] = std::string("");
1041  _type[s] = type;
1042  _argname[s] = argname;
1043  _help[s] = std::string("");
1044  }
1045 
1046  void comlinehandler::Record(const char *args[])
1047  {
1048  int i = 1;
1049  std::ostringstream Ostr;
1050  _program_name.assign(stripdirs(args[0]));
1051  while(args[i]){
1052  _toks.push_back(std::string(args[i]));
1053  Ostr << args[i++];
1054  if(args[i])
1055  Ostr << " ";
1056  }
1057  _line.assign(Ostr.str());
1058  }
1059 
1061  {
1062  bool end_of_ops = false;
1063  int errs = 0;
1064  std::vector<std::string> &args(_toks);
1065  // TokenizeString(args,_line);
1066  std::vector<std::string>::iterator ai = args.begin();
1067  while(ai != args.end()){
1068  std::string this_argument = *ai++;
1069  std::string next_argument;
1070  if(ai != args.end())
1071  next_argument = *ai--;
1072  std::string::iterator si = this_argument.begin();
1073  if(*si == '-' && !end_of_ops){
1074  si++;
1075  if(si == this_argument.end())
1076  end_of_ops = true;
1077  if(*si != '-') { // then we are processing a short option
1078  while(si != this_argument.end()){
1079  char flag_char = *si++;
1080  std::vector<std::pair<char,std::string> >::iterator oi = this->begin();
1081  bool found = false;
1082  while((oi != this->end()) && !found){
1083  if(flag_char == oi->first){
1084  found = true;
1085  if(si == this_argument.end()){ // means it's not a string of flags
1086  if(_type[flag_char] != 0){
1087  if(!next_argument.empty() && next_argument[0] != '-'){
1088  _options[flag_char] = next_argument;
1089  ai++;
1090  }
1091  else if(_type[flag_char] > 1){ // next arg wasn't valid, err if reqd
1092  errs++;
1093  std::ostringstream Ostr;
1094  Ostr << "Option -" << flag_char << " requires an argument.";
1095  _error_messages.push_back(Ostr.str());
1096  }
1097  else
1098  _options[flag_char] = ".true.";
1099  }
1100  else
1101  _options[flag_char] = ".true.";
1102  }
1103  else{ // it's a string of flags
1104  if(_type[flag_char] > 1){ // it requires an argument, can't be in a flag string
1105  errs++;
1106  std::ostringstream Ostr;
1107  Ostr << "Option -" << flag_char << " requires an argument.";
1108  _error_messages.push_back(Ostr.str());
1109  }
1110  else
1111  _options[flag_char] = ".true.";
1112  }
1113  }
1114  oi++;
1115  }
1116  if(!found){
1117  errs++;
1118  std::ostringstream Ostr;
1119  Ostr << "Option -" << flag_char << " is unrecognized.";
1120  _error_messages.push_back(Ostr.str());
1121  }
1122  }
1123  }
1124  else { // we are processing a long option
1125  std::string opstring = this_argument.substr(2);
1126  char flag_char = GetShort(opstring);
1127  if(flag_char != '\0'){
1128  if(_type[flag_char] != 0){
1129  if(!next_argument.empty() && next_argument[0] != '-'){
1130  _options[flag_char] = next_argument;
1131  ai++;
1132  }
1133  else if(_type[flag_char] > 1){
1134  errs++;
1135  std::ostringstream Ostr;
1136  Ostr << "Option --" << GetLong(flag_char)
1137  << " requires an argument.";
1138  _error_messages.push_back(Ostr.str());
1139  }
1140  else
1141  _options[flag_char] = ".true.";
1142  }
1143  else
1144  _options[flag_char] = ".true.";
1145  }
1146  else{
1147  errs++;
1148  std::ostringstream Ostr;
1149  Ostr << "Option --" << opstring << " is unrecognized.";
1150  _error_messages.push_back(Ostr.str());
1151  }
1152  }
1153  }
1154  else { // non option arguments
1155  _nonops.push_back(this_argument);
1156  }
1157  if(ai != args.end())
1158  ai++;
1159  }
1160  // After finished parsing all arguments, go back and determine
1161  // whether any required input was missing.
1162  //
1163  // First, process the options
1164  std::vector<std::pair<char,std::string> >::iterator oi = this->begin();
1165  while(oi != this->end()){
1166  if(GetOption(oi->first).empty() && _type[oi->first]==3){
1167  errs++;
1168  std::ostringstream Ostr;
1169  Ostr << "Required option: -" << oi->first << ",--" << oi->second
1170  << " <" << _argname[oi->first] << "> was not specified.";
1171  _error_messages.push_back(Ostr.str());
1172  }
1173  oi++;
1174  }
1175  // Next, make sure there were enough nonop args
1176  unsigned int nreqd_args = 0;
1177  std::vector<std::pair<std::string,int> >::iterator aai = _args.begin();
1178  while(aai != _args.end()){
1179  if(aai->second > 0)
1180  nreqd_args++;
1181  aai++;
1182  }
1183  if(nreqd_args > _nonops.size()){
1184  errs++;
1185  std::ostringstream Ostr;
1186  Ostr << "Missing required arguments.";
1187  _error_messages.push_back(Ostr.str());
1188  }
1189  return(errs);
1190  }
1191 
1192  void comlinehandler::SetRawComLine(const std::string &incl)
1193  {
1194  std::istringstream Istr(incl);
1195  Istr >> _program_name;
1196  std::string token;
1197  Istr >> _line;
1198  while(Istr >> token)
1199  _line += (std::string(" ")+token);
1200  }
1201 
1202 
1204  {
1205  std::ostringstream Ostr;
1206  Ostr << _program_name << " command line errors: " << std::endl;
1207  DumpContents(Ostr,_error_messages);
1208  return(Ostr.str());
1209  }
1210 
1211  void comlinehandler::WriteRC(std::ostream &Ostr) const {return;}
1212  void comlinehandler::ReadRC(const std::string &RCString) {return;}
1213 }
1214 
void SetRawComLine(const std::string &incl)
Raw Command Line Access.
Definition: AppTools.H:1192
virtual void UserInitialize(void)
virtual function for program specific Initialization.
Definition: AppTools.H:738
char GetShort(const std::string &l)
Obtain the short one char option from the long word version.
Definition: AppTools.H:1014
keyvaluepair(const K &k, const V &v)
Definition: AppTools.H:37
int ProcessCommandLine(const char *args[])
One fell swoop processing of command line.
Definition: AppTools.H:475
keyvaluepair(const std::pair< K, V > &p)
Definition: AppTools.H:33
virtual std::istream & ReadFromStream(std::istream &Is)
Definition: AppTools.H:291
void AddOption(char s, const std::string &l, int=0)
User interface to describe simple option.
Definition: AppTools.H:1025
std::list< std::string > GetValueList(const std::string &key) const
Definition: AppTools.H:278
std::string _line
unformatted command line
Definition: AppTools.H:428
std::map< char, int > _type
stores the type of option
Definition: AppTools.H:440
std::string ShortUsage()
Generate short usage string.
Definition: AppTools.H:974
std::string GetNextContent(std::istream &In)
Definition: AppTools.H:54
std::string _program_name
the name of the program
Definition: AppTools.H:426
V & Value(const V &v)
Definition: AppTools.H:47
void ProcessRange(NumType &t1, NumType &t2, const std::string stinter)
Process a range in the format "t1:t2".
Definition: AppTools.H:684
std::string GetHelp(const std::string &l)
Obtain the usage string for an option.
Definition: AppTools.H:601
void AddHelp(char s, const std::string &help)
Specify usage for an option.
Definition: AppTools.H:576
std::map< char, std::string > _argname
stores a name for arguments
Definition: AppTools.H:438
std::ostream & operator<<(std::ostream &Ostr, const ParamType &param)
Definition: AppTools.H:366
std::vector< std::string > _nonops
stores non-option arguments
Definition: AppTools.H:432
std::string GetRawComLine() const
Raw Command Line Access.
Definition: AppTools.H:483
T GetValue(const std::string &key) const
Definition: AppTools.H:188
std::vector< T > GetValueVector(const std::string &key) const
Definition: AppTools.H:200
K & Key(const K &k)
Definition: AppTools.H:46
void SetNotes(const std::string &notes)
Set notes string.
Definition: AppTools.H:671
std::vector< std::string > GetArgs() const
Argument access.
Definition: AppTools.H:663
std::string GetOption(const char &s)
Get the value of an option.
Definition: AppTools.H:640
std::string LongUsage()
Generate long usage string.
Definition: AppTools.H:918
keyvaluepair< std::string, std::string > ParamType
Definition: AppTools.H:152
std::vector< std::pair< std::string, int > > _args
application arguments;
Definition: AppTools.H:442
int ProcessOptions()
Processes all command line tokens.
Definition: AppTools.H:1060
virtual std::ostream & WriteToStream(std::ostream &Os) const
Definition: AppTools.H:342
void WriteRC(std::ostream &Ostr) const
Write an RC string that can be used for config.
Definition: AppTools.H:1211
void AddHelp(const std::string &l, const std::string &help)
Specify usage for an option.
Definition: AppTools.H:583
std::map< std::string, std::string > _arghelp
help string for args
Definition: AppTools.H:444
void const size_t const size_t const size_t const double const double * x
std::istream & operator>>(std::istream &iSt, parameters &pv)
Definition: AppTools.H:359
virtual ~comlinehandler()
Definition: AppTools.H:740
void SetArgName(char s, const std::string &argname)
Specify name of an option argument.
Definition: AppTools.H:566
std::vector< std::string > _error_messages
stores error messages
Definition: AppTools.H:430
void SetDescription(const std::string &desc)
Set description string.
Definition: AppTools.H:667
int OpenFile(std::ifstream &Inf, const std::string &filename)
File opener.
Definition: AppTools.H:144
Simple key-value pair.
Definition: AppTools.H:30
void Record(const char *args[])
Minimal recording of command line.
Definition: AppTools.H:1046
bool GetFlagValue(const std::string &key) const
Definition: AppTools.H:171
std::string GetOption(const std::string &l)
Get the value of an option.
Definition: AppTools.H:648
std::map< char, std::string > _help
stores the help string for each op
Definition: AppTools.H:436
std::vector< std::string > GetValueVector(const std::string &key) const
Definition: AppTools.H:265
virtual void Initialize(void)
virtual function for program specific Initialization.
Definition: AppTools.H:734
comlinehandler(const char *args[])
Constructor.
Definition: AppTools.H:460
std::string GetValue(const std::string &key) const
Definition: AppTools.H:254
void size_t int size_t int size_t int int int int double int int double double *void size_t int size_t int int int int int double int size_t size_t size_t double double *void size_t int size_t int size_t size_t int double int double double *void size_t size_t size_t double * a
virtual int SetValue(const std::string &key, const std::string &value)
Definition: AppTools.H:227
void TokenizeString(std::vector< std::string > &tokens, const std::string &source)
Tokenize string.
Definition: AppTools.H:132
std::string ErrorReport()
Error reporting.
Definition: AppTools.H:1203
void DumpContents(std::ostream &Ostr, const ContainerType &c, std::string del="\)
Dump container contents.
Definition: AppTools.H:117
std::string GetOpStringByType(int mintype, int maxtype)
Obtains option strings by type.
Definition: AppTools.H:962
void Copy(comlinehandler &incom)
Copy method.
Definition: AppTools.H:858
void const size_t const size_t const size_t const int const double * V
std::map< char, std::string > _options
stores the value of each option
Definition: AppTools.H:434
std::vector< std::string > _toks
commandline tokens
Definition: AppTools.H:446
std::list< T > GetValueList(const std::string &key) const
Definition: AppTools.H:214
std::string Param(const std::string &Key) const
Definition: AppTools.H:324
virtual ~parameters()
Definition: AppTools.H:238
std::string _description
application description.
Definition: AppTools.H:422
void AddArgHelp(const std::string &a, const std::string &help)
Specify the usage info for application argument.
Definition: AppTools.H:546
std::string GetHelp(char s)
Obtain the usage string for an option.
Definition: AppTools.H:597
std::string GetLong(const char &s)
Obtain the long word option from the short char version.
Definition: AppTools.H:951
void GetContentUntil(std::istream &In, std::string ret, const std::string &etag)
Definition: AppTools.H:69
void AddArgument(const std::string &a, int reqd=0)
User interface to describe an application argument.
Definition: AppTools.H:532
const std::string stripdirs(const std::string &pname)
Strip absolute path.
Definition: AppTools.H:100
Command line processing.
Definition: AppTools.H:411
std::string _notes
Notes to be displayed at the end of LongUsage().
Definition: AppTools.H:424
std::string ProgramName() const
Program name access.
Definition: AppTools.H:659
void SetArgName(const std::string opstr, const std::string argname)
Specify name of an option argument.
Definition: AppTools.H:556
void ResolveOption(NumType &t2, const std::string stinter)
Resolve an option (i.e.
Definition: AppTools.H:716
void ReadRC(const std::string &RCString)
Read a config from RC string.
Definition: AppTools.H:1212
bool IsSet(const std::string &Key) const
Definition: AppTools.H:336
comlinehandler()
Default constructor.
Definition: AppTools.H:451
ParamType * ParamPtr(const std::string &key)
Definition: AppTools.H:242
int GNUPlot(const std::string &commands)
Simple GnuPlot plotter util.
Definition: AppTools.H:18