18 inline int GNUPlot(
const std::string &commands)
20 FILE *fp = popen(
"gnuplot -persist",
"w");
21 fprintf(fp,
"%s", commands.c_str());
29 template<
typename K,
typename V>
34 this->first = p.first;
35 this->second = p.second;
42 K
Key()
const{
return this->first; };
43 V Value()
const{
return this->second; };
44 K &
Key() {
return this->first; };
46 K &
Key(
const K &k) { this->first = k;
return(this->first); };
47 V &
Value(
const V &v) { this->second = v;
return(this->second); };
58 while(ret.empty() && std::getline(In,line)){
59 line = line.substr(0,line.find(
"#"));
60 std::istringstream Istr(line);
63 if(!token.empty() && token[0] !=
'#')
71 const std::string &etag)
73 std::ostringstream Ostr;
75 while(std::getline(In,line) && ret.empty()){
76 line = line.substr(0,line.find(
"#"));
78 std::istringstream Istr(line);
87 Ostr << line << std::endl;
99 inline const std::string
103 std::string::size_type
x = pname.find(
"/");
104 if(x == std::string::npos)
106 return(pname.substr(pname.find_last_of(
"/")+1));
116 template<
typename ContainerType>
117 void DumpContents(std::ostream &Ostr,
const ContainerType &c,std::string del =
"\n"){
119 typename ContainerType::const_iterator ci = c.begin();
122 Ostr << del << *ci++;
135 std::istringstream Istr(source);
138 tokens.push_back(token);
144 inline int OpenFile(std::ifstream &Inf,
const std::string &filename)
146 Inf.open(filename.c_str());
155 const ParamType ¶m);
160 friend std::ostream &
operator<<(std::ostream &oSt,
162 friend std::istream &
operator>>(std::istream &iSt,
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;
169 ParamType *ParamPtr(
const std::string &key);
174 std::string value(this->Param(key));
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." ||
191 std::string value(this->Param(key));
193 std::istringstream Istr(value);
202 std::vector<T> retval;
203 std::string value(this->Param(key));
205 std::istringstream Istr(value);
207 while(Istr >> tmpval)
208 retval.push_back(tmpval);
217 std::string value(this->Param(key));
219 std::istringstream Istr(value);
221 while(Istr >> tmpval)
222 retval.push_back(tmpval);
227 virtual int SetValue(
const std::string &key,
const std::string &value){
229 ParamPtr(key)->
Value(value);
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;
244 parameters::const_iterator pi = this->begin();
245 while(pi != this->end()){
246 unsigned int index = pi - this->begin();
248 return(&((*
this)[index]));
257 std::string value(this->Param(key));
259 std::istringstream Istr(value);
267 std::vector<std::string> retval;
268 std::string value(this->Param(key));
270 std::istringstream Istr(value);
272 while(Istr >> tmpval)
273 retval.push_back(tmpval);
280 std::list<std::string> retval;
281 std::string value(this->Param(key));
283 std::istringstream Istr(value);
285 while(Istr >> tmpval)
286 retval.push_back(tmpval);
296 std::getline(Is,line);
299 std::string::size_type
x = line.find(
'#');
300 line = line.substr(0,x);
303 if(x == std::string::npos)
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());
311 std::ostringstream Ostr;
312 std::vector<std::string>::iterator ti = tokens.begin();
313 if(ti != tokens.end())
315 while(ti != tokens.end())
316 Ostr <<
" " << *ti++;
317 param.
Value() = Ostr.str();
318 this->push_back(param);
327 parameters::const_iterator pi = this->begin();
328 while(pi != this->end()){
338 return(!Param(key).empty());
344 parameters::const_iterator pi = this->begin();
345 while(pi != this->end()){
347 if(pi != this->end())
366 std::ostream &
operator<<(std::ostream &Ostr,
const ParamType ¶m)
368 Ostr << param.
Key() <<
" = " << param.
Value();
442 std::vector<std::pair<std::string,int> >
_args;
478 return(ProcessOptions());
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);
535 _args.push_back(std::make_pair(a,reqd));
556 void SetArgName(
const std::string opstr,
const std::string argname)
558 _argname[GetShort(opstr)] = argname;
568 _argname[s] = argname;
576 void AddHelp(
char s,
const std::string &help){ _help[s] = help;};
583 void AddHelp(
const std::string &l,
const std::string &help)
585 _help[GetShort(l)] = help;
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();
663 std::vector<std::string>
GetArgs()
const {
return _nonops;};
671 void SetNotes(
const std::string ¬es){ _notes.assign(notes); };
675 void WriteRC(std::ostream &Ostr)
const;
679 void ReadRC(
const std::string &RCString);
683 template<
typename NumType>
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);
694 std::istringstream Istr(t2s);
702 std::istringstream Istr(t1s);
706 std::istringstream Istr(t2s);
715 template<
typename NumType>
718 if(stinter ==
".true." || stinter ==
".TRUE."){
722 if(stinter ==
".false." || stinter ==
".FALSE."){
726 if(!stinter.empty()){
727 std::istringstream Istr(stinter);
747 std::string::size_type
x;
749 x = line.find(
"<description>");
750 if(x != std::string::npos){
754 x = line.find(
"<options>");
755 if(x == std::string::npos){
756 cl.
_error_messages.push_back(
"Config file format error - options section misplaced?.");
759 std::string options_content;
761 std::istringstream Istr(options_content);
762 while(std::getline(Istr,line)){
763 std::istringstream OpIn(line);
770 cl.push_back(std::make_pair(opchar,token));
771 cl.
_type[opchar] = optype;
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.");
783 cl.
_help[opchar] =
"";
789 x = line.find(
"<arguments>");
790 if(x == std::string::npos){
791 cl.
_error_messages.push_back(
"Configuration intput format error in argument section.");
794 std::string argsection;
796 Istr.str(argsection);
797 while(std::getline(Istr,line)){
798 std::istringstream ArgIn(line);
801 ArgIn >> argname >> argtype;
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.");
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);
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)
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)
847 Out <<
"</arguments>\n";
860 _description.erase();
862 _program_name.erase();
864 _error_messages.resize(0);
875 _notes.assign(incom.
_notes);
877 _line.assign(incom.
_line);
878 std::vector<std::string>::iterator ici = incom.
_error_messages.begin();
880 _error_messages.push_back(*ici++);
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;
889 icmi = incom.
_help.begin();
890 while(icmi != incom.
_help.end()){
891 _help[icmi->first] = icmi->second;
895 while(icmi != incom.
_argname.end()){
896 _argname[icmi->first] = icmi->second;
899 std::map<char,int>::iterator icti = incom.
_type.begin();
900 while(icti != incom.
_type.end()){
901 _type[icti->first] = icti->second;
904 std::vector<std::pair<std::string,int> >::iterator icai = incom.
_args.begin();
905 while(icai != incom.
_args.end())
906 _args.push_back(*icai++);
908 std::map<std::string,std::string>::iterator icahi = incom.
_arghelp.begin();
909 while(icahi != incom.
_arghelp.end()){
910 _arghelp[icahi->first] = icahi->second;
913 comlinehandler::iterator cli = incom.begin();
914 while(cli != incom.end())
915 this->push_back(*cli++);
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];
933 if(ti != this->end())
934 Ostr << std::endl << std::endl;
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];
943 if(ai != _args.end())
944 Ostr << std::endl << std::endl;
947 Ostr << std::endl << _notes;
953 std::vector<std::pair<char,std::string> >::iterator ti = this->begin();
954 while(ti != this->end()){
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)
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();
985 while(oi != optionals.end()){
987 << (_type[*oi] == 1 ?
" [" :
" <")
989 << (_type[*oi] == 1 ?
"] " :
"> ");
994 std::string reqd = GetOpStringByType(3,3);
996 std::string::iterator oi = reqd.begin();
998 while(oi != reqd.end()){
999 Ostr <<
"-" << *oi <<
" <" << _argname[*oi]
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 ?
"> ":
"] ");
1016 std::vector<std::pair<char,std::string> >::iterator ti = this->begin();
1017 while(ti != this->end()){
1028 this->push_back(std::make_pair(s,l));
1029 _options[s] = std::string(
"");
1032 _argname[s] =
"arg";
1033 _help[s] = std::string(
"");
1039 this->push_back(std::make_pair(s,l));
1040 _options[s] = std::string(
"");
1042 _argname[s] = argname;
1043 _help[s] = std::string(
"");
1049 std::ostringstream Ostr;
1050 _program_name.assign(
stripdirs(args[0]));
1052 _toks.push_back(std::string(args[i]));
1057 _line.assign(Ostr.str());
1062 bool end_of_ops =
false;
1064 std::vector<std::string> &args(_toks);
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){
1075 if(si == this_argument.end())
1078 while(si != this_argument.end()){
1079 char flag_char = *si++;
1080 std::vector<std::pair<char,std::string> >::iterator oi = this->begin();
1082 while((oi != this->end()) && !found){
1083 if(flag_char == oi->first){
1085 if(si == this_argument.end()){
1086 if(_type[flag_char] != 0){
1087 if(!next_argument.empty() && next_argument[0] !=
'-'){
1088 _options[flag_char] = next_argument;
1091 else if(_type[flag_char] > 1){
1093 std::ostringstream Ostr;
1094 Ostr <<
"Option -" << flag_char <<
" requires an argument.";
1095 _error_messages.push_back(Ostr.str());
1098 _options[flag_char] =
".true.";
1101 _options[flag_char] =
".true.";
1104 if(_type[flag_char] > 1){
1106 std::ostringstream Ostr;
1107 Ostr <<
"Option -" << flag_char <<
" requires an argument.";
1108 _error_messages.push_back(Ostr.str());
1111 _options[flag_char] =
".true.";
1118 std::ostringstream Ostr;
1119 Ostr <<
"Option -" << flag_char <<
" is unrecognized.";
1120 _error_messages.push_back(Ostr.str());
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;
1133 else if(_type[flag_char] > 1){
1135 std::ostringstream Ostr;
1136 Ostr <<
"Option --" << GetLong(flag_char)
1137 <<
" requires an argument.";
1138 _error_messages.push_back(Ostr.str());
1141 _options[flag_char] =
".true.";
1144 _options[flag_char] =
".true.";
1148 std::ostringstream Ostr;
1149 Ostr <<
"Option --" << opstring <<
" is unrecognized.";
1150 _error_messages.push_back(Ostr.str());
1155 _nonops.push_back(this_argument);
1157 if(ai != args.end())
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){
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());
1176 unsigned int nreqd_args = 0;
1177 std::vector<std::pair<std::string,int> >::iterator aai = _args.begin();
1178 while(aai != _args.end()){
1183 if(nreqd_args > _nonops.size()){
1185 std::ostringstream Ostr;
1186 Ostr <<
"Missing required arguments.";
1187 _error_messages.push_back(Ostr.str());
1194 std::istringstream Istr(incl);
1195 Istr >> _program_name;
1198 while(Istr >> token)
1199 _line += (std::string(
" ")+token);
1205 std::ostringstream Ostr;
1206 Ostr << _program_name <<
" command line errors: " << std::endl;
void const size_t const size_t const size_t const double const double * x
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
void const size_t const size_t const size_t const int const double * V