25 #include <Base/Common/W32Compat.hh>
31 # include <Base/Common/getopt_W32.h>
45 #include "ParamTypes.hh"
48 #include <sys/types.h>
57 typedef map<const std::string,BIAS::ParamTypeBase*>::const_iterator CI;
76 DestructorWarning_ = !DisableDestructorWarning;
79 AddParamString(
"writeconfig",
"Write file with current configuration");
80 AddParamString(
"readconfig",
"Read configuration from file");
82 AddParamString(
"writeconfigXML",
"Write XML file with current configuration");
83 AddParamString(
"readconfigXML",
"Read configuration from XML file");
85 AddParamBool(
"expert",
"Expert Mode: also show and write parameters marked "
87 SetParameterWriteToFile(
"writeconfig",
false);
88 SetParameterWriteToFile(
"readconfig",
false);
90 SetParameterWriteToFile(
"writeconfigXML",
false);
91 SetParameterWriteToFile(
"readconfigXML",
false);
93 SetParameterWriteToFile(
"expert",
false);
95 setlocale(LC_ALL,
"C");
100 WriteComments_ =
true;
101 WriteIndent_ =
false;
102 WriteEnhanced_ =
false;
103 WriteFlagSet_ =
false;
110 for ( CI it=ParamMap_.begin(); it!=ParamMap_.end(); it++){
123 int namew=GetSizeOfLongestParamName()+2, typew=7;
124 int valw=GetSizeOfLongestParamValueString()+2;
126 namew=(namew<20)?(20):namew;
127 valw=(valw<7)?(7):valw;
133 os <<
"idx"<<setw(namew)<<
"name"<<setw(typew)
134 <<
"type"<<setw(valw)<<
"value"<<
" options"<<endl;
137 vector<int> grps=UniqueSortedGroups();
138 vector<int>::iterator git;
139 for (git=grps.begin(); git!=grps.end(); git++){
140 os <<
"\n############### Group ID "<<*git<<
" : "<<GetGroupName(*git)
141 <<
" #######################\n";
142 for(it=ParamMap_.begin(); it!=ParamMap_.end(); it++){
143 if ((*it).second->Group_==*git){
144 if ((showenhanced || (!(*it).second->Enhanced_)) &&
145 (!(*it).second->Hidden_)) {
146 os << pos <<
": " << setw(namew) << it->first;
147 os << setw(typew) << it->second->GetTypeName()<<
" "
148 << setw(valw) << it->second->GetValueString() <<
" "
149 << it->second->ShortCmd_ ;
150 if ((*it).second->Enhanced_) os <<
" (enhanced)";
158 for(it=ParamMap_.begin(); it!=ParamMap_.end(); it++){
159 if ((*it).second->Group_==grp){
160 if ((showenhanced || (!(*it).second->Enhanced_)) &&
161 (!(*it).second->Hidden_)) {
162 os << pos <<
": " << setw(namew) << it->first;
163 os << setw(typew) << it->second->GetTypeName()<<
" "
164 << setw(valw) << it->second->GetValueString() <<
" "
165 << it->second->ShortCmd_;
166 if ((*it).second->Enhanced_) os <<
" (enhanced)";
181 bool expert = *GetParamBool(
"expert");
186 for (CI it = ParamMap_.begin(); it != ParamMap_.end(); it++) {
187 if ((!it->second->Enhanced_ || expert) && !it->second->Hidden_) {
188 int len = it->first.length();
189 if (len > namew) namew = len;
195 os <<
"Available Parameters : ";
198 vector<int> grps = UniqueSortedGroups();
199 for (vector<int>::iterator git = grps.begin(); git!=grps.end(); git++)
202 os << endl <<
" ---- " << GetGroupName(*git) <<
" ------------";
204 for (CI it = ParamMap_.begin(); it != ParamMap_.end(); it++)
206 if (it->second->Group_ == *git && !it->second->Hidden_ &&
207 (expert || !it->second->Enhanced_))
209 sprintf(tmpstr,
"--%s", it->first.c_str());
211 os << setfill(
' ') << setw(namew+2);
212 os << left << tmpstr;
213 if (it->second->ShortCmd_ == 0)
215 else if (WriteIndent_)
216 os <<
" -" << it->second->ShortCmd_ <<
" ";
218 os <<
", -" << it->second->ShortCmd_ <<
" ";
220 os << setfill(
' ') << setw(typew);
221 if (dynamic_cast<ParamTypeInt*>(it->second))
226 else if (dynamic_cast<ParamTypeBool*>(it->second))
229 os << left <<
"BOOL";
231 else if (dynamic_cast<ParamTypeDouble*>(it->second))
234 os << left <<
"DOUBLE";
236 else if (dynamic_cast<ParamTypeString*>(it->second))
239 os << left <<
"STRING";
241 else if (dynamic_cast<ParamTypeVecDbl*>(it->second))
244 os << left <<
"VECDBL";
246 else if (dynamic_cast<ParamTypeVecInt*>(it->second))
249 os << left <<
"VECINT";
251 else if (dynamic_cast<ParamTypeEnum*>(it->second))
254 os << left <<
"ENUM";
256 os <<
"\n\t[ " <<
dynamic_cast<ParamTypeEnum*
>(it->second)->GetHint() <<
" ]";
260 os << left <<
"unknown";
263 if (it->second->Help_ !=
"")
264 os << it->second->Help_;
266 os <<
"No help available";
277 const std::string &help,
int deflt,
278 int min,
int max ,
char cmdshort,
int Group)
280 if ( ParamMap_.find(name)!=ParamMap_.end() )
282 BIASERR(
"Cannot add parameter "<<name<<
". Already in list!");
287 BIASASSERT(NewItem!=NULL);
289 NewItem->
Name_ = name;
290 NewItem->
Help_ = help;
298 pair<string, ParamTypeBase*> NewPair(name, NewItem);
299 ParamMap_.insert(ParamMap_.end(), NewPair );
301 return &(NewItem->
value_);;
306 bool deflt,
char cmdshort,
int Group )
308 if ( ParamMap_.find(name)!=ParamMap_.end() )
310 BIASERR(
"Cannot add parameter "<<name<<
". Already in list!");
314 NewItem->
Name_ = name;
315 NewItem->
Help_ = help;
321 pair<string, ParamTypeBase*> NewPair(name,NewItem);
322 ParamMap_.insert(ParamMap_.end(), NewPair );
323 return &(NewItem->
value_);
328 const std::string &help,
329 std::string deflt,
char cmdshort,
332 if ( ParamMap_.find(name)!=ParamMap_.end() )
334 BIASERR(
"Cannot add parameter "<<name<<
". Already in list!");
338 NewItem->
Name_ = name;
339 NewItem->
Help_ = help;
345 pair<string, ParamTypeBase*> NewPair(name,NewItem);
346 ParamMap_.insert(ParamMap_.end(), NewPair );
347 return &(NewItem->
value_);;
352 const std::string &help,
354 double min,
double max,
355 char cmdshort,
int Group)
357 if ( ParamMap_.find(name)!=ParamMap_.end() )
359 BIASERR(
"Cannot add parameter "<<name<<
". Already in list!");
363 NewItem->
Name_ = name;
364 NewItem->
Help_ = help;
372 pair<string, ParamTypeBase*> NewPair(name,NewItem);
373 ParamMap_.insert(ParamMap_.end(), NewPair );
374 return &(NewItem->
value_);;
379 const std::string &help,
381 char cmdshort,
int Group )
383 if ( ParamMap_.find(name)!=ParamMap_.end() )
385 BIASERR(
"Cannot add parameter "<<name<<
". Already in list!");
389 NewItem->
Name_ = name;
390 NewItem->
Help_ = help;
396 pair<string, ParamTypeBase*> NewPair(name,NewItem);
397 ParamMap_.insert(ParamMap_.end(), NewPair );
398 return &(NewItem->
value_);
402 const std::string &help,
403 const std::string &deflt,
407 std::vector<double> parsedValues;
410 std::stringstream defStream(deflt);
412 while (defStream.good()) {
414 parsedValues.push_back(temp);
416 values.
newsize(parsedValues.size());
417 for (
unsigned int i=0;i<parsedValues.size();i++)
418 values[i]=parsedValues[i];
419 return AddParamVecDbl(name, help, values, cmdshort,Group);
424 const std::string &help,
426 char cmdshort,
int Group )
428 if ( ParamMap_.find(name)!=ParamMap_.end() )
430 BIASERR(
"Cannot add parameter "<<name<<
". Already in list!");
434 NewItem->
Name_ = name;
435 NewItem->
Help_ = help;
441 pair<string, ParamTypeBase*> NewPair(name,NewItem);
442 ParamMap_.insert(ParamMap_.end(), NewPair );
443 return &(NewItem->
value_);;
447 const std::string &help,
448 const std::string &deflt,
452 std::vector<int> parsedValues;
455 std::stringstream defStream(deflt);
457 while (defStream.good()) {
459 parsedValues.push_back(temp);
461 values.
newsize(parsedValues.size());
462 for (
unsigned int i=0;i<parsedValues.size();i++)
463 values[i]=parsedValues[i];
464 return AddParamVecInt(name, help, values, cmdshort,Group);
469 const std::string &help,
470 const std::vector<std::string> &enums,
472 const std::vector<int> * IDs,
476 if ( ParamMap_.find(name)!=ParamMap_.end() )
478 BIASERR(
"Cannot add parameter "<<name<<
". Already in list!");
483 BIASERR(
"At least one item is needed for enum type");
486 if ( IDs != NULL && IDs->size() != enums.size() )
488 BIASERR(
"You gave "<<enums.size()<<
"enums but "<<IDs->size()
489 <<
" ID values! Both numbers must be equal!");
493 NewItem->
Name_ = name;
494 NewItem->
Help_ = help;
500 pair<string,int> NewEnumPair;
502 for(
unsigned int i=0; i<enums.size(); i++)
504 newID = IDs?(*IDs)[i]:i;
505 if ( NewItem->
Map_.find(enums[i]) != NewItem->
Map_.end() ) {
506 BIASERR(
"Given enum IDs must be unique!");
510 NewEnumPair = make_pair(enums[i],newID );
511 NewItem->
Map_.insert(NewItem->
Map_.end(), NewEnumPair);
519 pair<string, ParamTypeBase*> NewPair(name,NewItem);
520 ParamMap_.insert(ParamMap_.end(), NewPair );
521 return &(NewItem->
value_);
529 if ( ParamMap_.find(name) == ParamMap_.end() )
return false;
539 for(it=ParamMap_.begin(); (it!=ParamMap_.end() || (pos == i)); it++){
543 if (
ParamTypeInt *item = dynamic_cast<ParamTypeInt*>(it->second) )
544 return &(item->value_);
546 BIASERR(
"Type of parameter is invalid!");
551 BIASERR(
"Index is invalid!");
560 for(it=ParamMap_.begin(); (it!=ParamMap_.end() || (pos == i)); it++){
564 if (
ParamTypeDouble *item = dynamic_cast<ParamTypeDouble*>(it->second) )
565 return &(item->value_);
567 BIASERR(
"Parameter is invalid!");
572 BIASERR(
"Index is invalid!");
581 for(it=ParamMap_.begin(); (it!=ParamMap_.end() || (pos == i)); it++){
585 if (
ParamTypeBool *item = dynamic_cast<ParamTypeBool*>(it->second) )
586 return &(item->value_);
588 BIASERR(
"Parameter is invalid!");
593 BIASERR(
"Index is invalid!");
602 for(it=ParamMap_.begin(); (it!=ParamMap_.end() || (pos == i)); it++){
606 if (
ParamTypeString *item = dynamic_cast<ParamTypeString*>(it->second) )
607 return &(item->value_);
609 BIASERR(
"Parameter is invalid!");
614 BIASERR(
"Index is invalid!");
620 CI it = ParamMap_.find(name);
621 if ( it==ParamMap_.end() ){
622 BIASERR(
"int parameter "<<name<<
" not found!");
625 else if (
ParamTypeInt *item = dynamic_cast<ParamTypeInt*>(it->second) )
626 return &(item->value_);
628 BIASERR(
"Parameter "<<name<<
" is not of type INTEGER!");
635 CI it = ParamMap_.find(name);
636 if ( it==ParamMap_.end() ){
637 BIASERR(
"bool parameter "<<name<<
" not found!");
640 else if (
ParamTypeBool *item = dynamic_cast<ParamTypeBool*>(it->second) )
641 return &(item->value_);
643 BIASERR(
"Parameter "<<name<<
" is not of type BOOLEAN!");
651 CI it = ParamMap_.find(name);
652 if ( it==ParamMap_.end() ){
653 BIASERR(
"string parameter "<<name<<
" not found!");
656 else if (
ParamTypeString *item=dynamic_cast<ParamTypeString*>(it->second))
657 return &(item->value_);
659 BIASERR(
"Parameter "<<name<<
" is not of type STRING!");
667 CI it = ParamMap_.find(name);
668 if ( it==ParamMap_.end() ){
669 BIASERR(
"double parameter "<<name<<
" not found!");
672 else if (
ParamTypeDouble *item=dynamic_cast<ParamTypeDouble*>(it->second))
673 return &(item->value_);
675 BIASERR(
"Parameter "<<name<<
" is not of type DOUBLE!");
682 CI it = ParamMap_.find(name);
683 if ( it==ParamMap_.end() ){
684 BIASERR(
"vecdbl parameter "<<name<<
" not found!");
687 else if (
ParamTypeVecDbl *item=dynamic_cast<ParamTypeVecDbl*>(it->second))
688 return &(item->value_);
690 BIASERR(
"Parameter "<<name<<
" is not of type VECTOR OF DOUBLE!");
698 CI it = ParamMap_.find(name);
699 if ( it==ParamMap_.end() ){
700 BIASERR(
"vecint parameter "<<name<<
" not found!");
703 else if (
ParamTypeVecInt *item=dynamic_cast<ParamTypeVecInt*>(it->second))
704 return &(item->value_);
706 BIASERR(
"Parameter "<<name<<
" is not of type VECTOR OF INTEGER!");
714 CI it = ParamMap_.find(name);
715 if ( it==ParamMap_.end() ){
716 BIASERR(
"enum parameter \""<<name<<
"\" not found!");
719 else if (
ParamTypeEnum *item = dynamic_cast<ParamTypeEnum*>(it->second) )
720 return &(item->value_);
722 BIASERR(
"Parameter "<<name<<
" is not of type ENUM!");
731 CI it = ParamMap_.find(name);
732 if ( it==ParamMap_.end() )
734 BIASERR(
"Parameter \""<<name<<
"\" not found, cannot set parameter,"
735 <<
" returning NULL!");
738 if (
ParamTypeInt *item = dynamic_cast<ParamTypeInt*>(it->second) )
740 if ( value < item->min_ || value > item->max_ )
742 BIASERR(
"Valid values for \""<<name<<
"\" are between "
743 <<item->min_<<
" and "<<item->max_<<
"!");
746 item->value_ = value;
747 return &(item->value_);
751 BIASERR(
"Parameter "<<name<<
" is not of type INTEGER!");
759 CI it = ParamMap_.find(name);
760 if ( it==ParamMap_.end() )
762 BIASERR(
"Parameter \""<<name<<
"\" not found, cannot set parameter,"
763 <<
" returning NULL!");
766 if (
ParamTypeBool *item = dynamic_cast<ParamTypeBool*>(it->second) )
768 item->value_ = value;
769 return &(item->value_);
773 BIASERR(
"Parameter "<<name<<
" is not of type BOOLEAN!");
780 const std::string &value )
782 CI it = ParamMap_.find(name);
783 if ( it==ParamMap_.end() )
785 BIASERR(
"Parameter \""<<name<<
"\" not found, cannot set parameter,"
786 <<
" returning NULL!");
789 else if(
ParamTypeString *item = dynamic_cast<ParamTypeString*>(it->second) )
791 item->value_ = value;
792 return &(item->value_);
796 BIASERR(
"Parameter "<<name<<
" is not of type STRING!");
804 CI it = ParamMap_.find(name);
805 if ( it==ParamMap_.end() )
807 BIASERR(
"Parameter \""<<name<<
"\" not found, cannot set parameter,"
808 <<
" returning NULL!");
811 if (
ParamTypeDouble *item = dynamic_cast<ParamTypeDouble*>(it->second) )
813 if ( value < item->min_ || value > item->max_ )
815 BIASERR(
"Valid values for "<<name<<
" are between "
816 <<item->min_<<
" and "<<item->max_<<
"!");
819 item->value_ = value;
820 return &(item->value_);
824 BIASERR(
"Parameter "<<name<<
" is not of type DOUBLE!");
833 CI it = ParamMap_.find(name);
834 if ( it==ParamMap_.end() )
836 BIASERR(
"Parameter \""<<name<<
"\" not found, cannot set parameter,"
837 <<
" returning NULL!");
840 if (
ParamTypeVecDbl *item = dynamic_cast<ParamTypeVecDbl*>(it->second) )
849 item->value_ = value;
850 return &(item->value_);
854 BIASERR(
"Parameter "<<name<<
" is not of type VECTOR OF DOUBLE!");
863 CI it = ParamMap_.find(name);
864 if ( it==ParamMap_.end() )
866 BIASERR(
"Parameter \""<<name<<
"\" not found, cannot set parameter,"
867 <<
" returning NULL!");
870 if (
ParamTypeVecInt *item = dynamic_cast<ParamTypeVecInt*>(it->second) )
879 item->value_ = value;
880 return &(item->value_);
884 BIASERR(
"Parameter "<<name<<
" is not of type VECTOR OF INTEGER!");
892 CI it = ParamMap_.find(name);
893 if ( it==ParamMap_.end() )
895 BIASERR(
"Parameter \""<<name<<
"\" not found, cannot set parameter,"
896 <<
" returning NULL!");
899 if (
ParamTypeEnum *item = dynamic_cast<ParamTypeEnum*>(it->second) )
901 map<string,int>::iterator it;
902 if ( (it=item->Map_.find(value)) == item->Map_.end() ) {
903 BIASERR(value <<
" is not a valid ID for this enum!");
906 item->value_ = it->second;
907 return &(item->value_);
911 BIASERR(
"Parameter "<<name<<
" is not of type ENUM!");
919 CI it = ParamMap_.find(name);
920 if ( it==ParamMap_.end() )
922 BIASERR(
"Parameter \""<<name<<
"\" not found, cannot set parameter,"
923 <<
" returning NULL!");
926 if (
ParamTypeEnum *item = dynamic_cast<ParamTypeEnum*>(it->second) ) {
928 map<string, int>::iterator it;
929 for(it=item->Map_.begin(); it!=item->Map_.end(); it++)
930 if ( it->second == value ){
931 item->value_ = value;
932 return &(item->value_);
934 BIASERR(value <<
" is not in this enum!");
938 BIASERR(
"Parameter "<<name<<
" is not of type ENUM!");
946 CI it = ParamMap_.find(name);
947 if ( it==ParamMap_.end() )
949 BIASERR(
"Parameter \""<<name<<
"\" not found, cannot set parameter,"
953 it->second->WriteToFile_ = writeToFile;
959 CI it = ParamMap_.find(name);
960 if ( it==ParamMap_.end() )
962 BIASERR(
"Parameter \""<<name<<
"\" not found, cannot set to enhanced, "
966 it->second->Enhanced_ = enhanced;
972 CI it = ParamMap_.find(name);
973 if ( it==ParamMap_.end() )
975 BIASERR(
"Parameter \""<<name<<
"\" not found, cannot set to hidden, "
979 it->second->Hidden_ = hidden;
986 const std::string &default_filename)
990 *GetParamString(
"readconfig")=default_filename;
991 res = ParseCommandLine(argc, argv);
992 string *name=GetParamString(
"writeconfig");
993 bool *expert=GetParamBool(
"expert");
998 SetWriteOptions(WriteIndent_,
true,WriteEnhanced_ || *expert);
999 WriteParameter(*name);
1000 BIASDOUT(D_PARAM_CONF_FILE,
"wrote default param file "
1001 <<default_filename<<endl);
1005 if (*GetParamString(
"readconfig")==default_filename) {
1006 BIASDOUT(D_PARAM_CONF_FILE,
"using default param file "
1007 <<default_filename<<endl);
1008 ifstream is(default_filename.c_str());
1011 SetWriteOptions(WriteIndent_,
false,WriteEnhanced_|| *expert);
1012 WriteParameter(default_filename);
1013 BIASDOUT(D_PARAM_CONF_FILE,
"wrote default param file "
1014 <<default_filename<<endl);
1017 BIASDOUT(D_PARAM_CONF_FILE,
"found default param file "
1018 <<default_filename<<endl);
1021 BIASDOUT(D_PARAM_CONF_FILE,
"using user given param file "
1022 <<*GetParamString(
"readconfig")<<endl);
1034 if ( (readPrj = GetParamString(
"readconfig"))!=NULL ){
1035 if (*readPrj !=
""){
1037 BIASDOUT(D_PARAM_CONF_FILE,
"Reading configuration from file: "
1039 if (ReadParameter(*readPrj)<0)
return -1;
1044 BIASDOUT(D_PARAM_PARSECMDL,
"Param::ParseCommandLine()");
1047 if (ArgV_.size() ==0)
1048 for (
int i=0;i<argc; i++)
1049 ArgV_.push_back(argv[i]);
1055 char *shortoptstring=
new char[ParamMap_.size()*3+1];
1056 bool bReadConfig =
true, bParse=
true;
1059 shortoptstring[0] = 0;
1066 longopts =
new struct option[ParamMap_.size()+1];
1067 memset(&longopts[ParamMap_.size()], 0,
sizeof(
struct option));
1071 for (CI it=ParamMap_.begin(); it!=ParamMap_.end(); ++it)
1073 longopts[itemcount].
name = (it->first).c_str();
1075 if ( dynamic_cast<ParamTypeBool*>(it->second) != NULL )
1076 longopts[itemcount].
has_arg = optional_argument;
1077 else longopts[itemcount].has_arg = required_argument;
1080 longopts[itemcount].flag = NULL;
1083 longopts[itemcount].val = itemcount+1000;
1086 if ( it->second->ShortCmd_!=0 ) {
1087 tmpstr[0] = it->second->ShortCmd_;
1088 strcat(shortoptstring, tmpstr);
1090 strcat(shortoptstring,
":");
1092 if ( dynamic_cast<ParamTypeBool*>(it->second) != NULL)
1093 strcat(shortoptstring,
":");
1101 int short_or_index=0;
1109 short_or_index = ::getopt_long(argc, argv, shortoptstring,
1114 if (short_or_index ==
'?' || short_or_index ==
':') {
1120 if (short_or_index == -1)
1126 if (short_or_index >=1000){
1128 int index = short_or_index -1000;
1129 it = ParamMap_.find(longopts[index].
name);
1130 BIASDOUT(D_PARAM_PARSECMDL,
"longopt at "<<index
1131 <<
" : "<<it->first);
1134 for(it=ParamMap_.begin();it!=ParamMap_.end(); it++)
1135 if ( it->second->ShortCmd_==short_or_index ){
1136 BIASDOUT(D_PARAM_PARSECMDL,
"shortopt with "
1137 <<(
char)short_or_index<<
" : "<<it->first);
1143 const char* optparam;
1144 if (optarg != NULL) {
1153 it->second->SetFromString(optparam);
1161 #ifdef BIAS_HAVE_XML2
1162 if ( (*(readPrj = GetParamString(
"readconfigXML")))!=
"" ){
1164 BIASDOUT(D_PARAM_CONF_FILE,
"Reading configuration from XML-file: "
1166 if (ReadParameterXML(*readPrj)<0)
return -1;
1171 if ( (*(readPrj = GetParamString(
"readconfig")))!=
"" ){
1173 BIASDOUT(D_PARAM_CONF_FILE,
"Reading configuration from file: "
1175 if (ReadParameter(*readPrj)<0)
return -1;
1180 BIASDOUT(D_PARAM_CONF_FILE,
"no configuration file given\n");
1191 if (*(writePrj=GetParamString(
"writeconfig")) !=
"") {
1192 string s = *writePrj;
1193 SetParamString(
"writeconfig",(
string)
"");
1194 bool *expert=GetParamBool(
"expert");
1195 SetWriteOptions(WriteIndent_,WriteComments_,WriteEnhanced_ || *expert);
1197 WriteFlagSet_ =
true;
1200 #ifdef BIAS_HAVE_XML2
1202 if (*(writePrj=GetParamString(
"writeconfigXML")) !=
"") {
1203 string s = *writePrj;
1204 SetParamString(
"writeconfigXML",(
string)
"");
1205 bool *expert=GetParamBool(
"expert");
1206 SetWriteOptions(WriteIndent_,WriteComments_,WriteEnhanced_ || *expert);
1207 WriteParameterXML(s);
1208 WriteFlagSet_ =
true;
1213 delete[] shortoptstring;
1218 #define READPARAM_CHECKDUPES
1221 setlocale(LC_ALL,
"C");
1228 char commentstr[1000];
1232 #ifdef READPARAM_CHECKDUPES
1233 map<string,bool> *ExistingNames =
new map<string,bool>;
1238 ifs.open(filename.c_str());
1240 BIASERR(
"error reading parameter file: "<<filename);
1250 if (namestr[0] ==
'#') {
1251 ifs.getline(commentstr,
sizeof(commentstr),
'\n');
1252 BIASDOUT(D_PARAM_IO,
"Comment read: "<<namestr<<commentstr);
1256 ifs >> assignstr >>valstr ;
1258 if (valstr[0] ==
'"' && valstr[valstr.length()-1] !=
'"') {
1260 while ( c !=
'\"') {
1270 #ifdef READPARAM_CHECKDUPES
1271 current = (string)namestr;
1273 if ( ExistingNames->find(current) != ExistingNames->end() ){
1274 BIASERR(
"Oops! Found duplicate parameter '" << current
1275 <<
"' in line " << line <<
" of file '" << filename
1281 pair<string, bool> NewPair(current,
true);
1282 ExistingNames->insert(ExistingNames->end(), NewPair );
1287 if (valstr[0] ==
'"' && valstr[valstr.length()-1] ==
'"') {
1288 valstr = valstr.substr(1,valstr.length()-2);
1293 if ( (it=ParamMap_.find(namestr))==ParamMap_.end() ){
1294 BIASERR(
"Unknown parameter \""<<namestr
1295 <<
"\" in file "<<filename<<
" line: "<<line);
1300 it->second->SetFromString(valstr);
1301 BIASDOUT(D_PARAM_IO, filename<<
" parameter: "<<namestr
1302 <<
" type: "<<typestr <<
" valstr: "<<valstr);
1303 BIASDOUT(D_PARAM_IO, filename<<
" parameter: "<<namestr
1304 <<
" value after SetFromString(): "
1305 <<it->second->GetValueString());
1312 #ifdef READPARAM_CHECKDUPES
1313 delete ExistingNames;
1318 #ifdef BIAS_HAVE_XML2
1325 vector<xmlNodePtr> subgroups;
1331 xmlNodePtr root = tree.
read(filename);
1336 BIASERR(
"Error reading XML-file: " << filename << endl);
1340 xmlNodePtr parent = root;
1351 subgroups.push_back(child);
1372 if (attribType ==
"STRING") {
1374 }
else if (attribType ==
"INT") {
1376 }
else if (attribType ==
"DOUBLE"){
1378 }
else if (attribType ==
"BOOL"){
1380 }
else if (attribType ==
"VECDBL"){
1381 SetParamVecDbl(paramname,
1383 }
else if (attribType ==
"VECINT"){
1384 SetParamVecInt(paramname,
1386 }
else if (attribType ==
"ENUM"){
1389 BIASERR(
"Unknown parametertype: " << attribType << endl);
1402 if ((child == NULL)&&(subgroups.size()>0)) {
1404 parent = subgroups[subgroups.size()-1];
1405 subgroups.pop_back();
1409 if ((child == NULL)&&(subgroups.size()==0)) {
1423 vector<int> gv=UniqueSortedGroups();
1427 return (*(gv.rbegin()))+1;
1432 CI it = ParamMap_.find(name);
1433 if (it != ParamMap_.end()){
1434 return it->second->Group_;
1436 BIASERR(
"no parameter with name "<<name);
1442 for (
unsigned int i=0;i<GroupNames_.size();i++)
1443 if (GroupNames_[i] == name)
return i;
1451 vector<int> ids=UniqueSortedGroups();
1452 if (find(ids.begin(), ids.end(), id)==ids.end()){
1456 if (((*ids.rbegin())+1)>(int)GroupNames_.size()){
1457 GroupNames_.resize((*ids.rbegin())+1,
"");
1459 if(
id<(
int)GroupNames_.size())
1460 GroupNames_[
id]=name;
1467 if (group_id==GRP_ALL){
1470 if (group_id==GRP_NOSHOW){
1473 if (group_id>(
int)(GroupNames_.size())-1 || group_id<0){
1474 BIASERR(
"invalid group id or no name given. "
1475 "group_id="<<group_id<<
"GroupNames_.size()="<<GroupNames_.size())
1479 return GroupNames_[group_id];
1487 for (it=ParamMap_.begin(); it!=ParamMap_.end(); it++){
1488 if (it->second->WriteToFile_) res.push_back(it->second->Group_);
1490 sort(res.begin(), res.end());
1491 vector<int>::iterator end = unique(res.begin(), res.end());
1492 res.erase(end, res.end());
1498 vector<int> ids=UniqueSortedGroups();
1499 return (find(ids.begin(), ids.end(), group_id)!=ids.end());
1507 for (CI it=ParamMap_.begin(); it!=ParamMap_.end(); it++){
1508 if (it->second->Name_.size()>length &&
1509 !it->second->Enhanced_ && !it->second->Hidden_)
1510 length=it->second->Name_.size();
1520 for (CI it=ParamMap_.begin(); it!=ParamMap_.end(); it++){
1521 if (it->second->GetValueString().size()>length &&
1522 !it->second->Enhanced_ && !it->second->Hidden_)
1523 length=it->second->GetValueString().size();
1535 int namew=GetSizeOfLongestParamName()+2, typew=12;
1536 int valuew=GetSizeOfLongestParamValueString()+2;
1538 namew=(namew<20)?(20):namew;
1539 valuew=(valuew<7)?(7):valuew;
1545 ofs.open(filename.c_str());
1546 if ( !ofs.is_open() )
1547 BIASERR(
"File '" << filename <<
"' not opened!" << endl);
1550 vector<int> grps=UniqueSortedGroups();
1551 vector<int>::iterator git;
1552 for (git=grps.begin(); git!=grps.end(); git++){
1554 bool ThisGroupInUse =
false;
1555 for ( CI it=ParamMap_.begin(); it!=ParamMap_.end(); it++){
1556 if (it->second->WriteToFile_ && it->second->Group_ == *git
1557 && (WriteEnhanced_ || (!(*it).second->Enhanced_)) &&
1558 (!(*it).second->Hidden_)) {
1559 ThisGroupInUse =
true;
1563 if (!ThisGroupInUse)
continue;
1566 ofs <<
"\n############### Group ID "<<*git<<
" : "<<GetGroupName(*git)
1567 <<
" #######################\n";
1570 for ( CI it=ParamMap_.begin(); it!=ParamMap_.end(); it++){
1571 if (it->second->WriteToFile_ && it->second->Group_ == *git
1572 && (WriteEnhanced_ || (!(*it).second->Enhanced_)) &&
1573 (!(*it).second->Hidden_))
1578 ofs <<endl<<
"# "<<it->second->Name_<<
": "<<it->second->GetHint()
1579 <<endl<<
"# "<<it->second->Help_<<endl;
1581 oss <<setw(namew)<<it->first<<
" = "
1582 <<setw(valuew)<<left<<it->second->GetValueString()<<right
1584 while (oss.str().size() <40) oss<<
" ";
1588 oss <<
"PARAM_"<<it->second->GetTypeName();
1589 ofs << setw(typew) << oss.str()<< endl;
1597 #ifdef BIAS_HAVE_XML2
1604 xmlNodePtr root = tree.
create(
"XMLParamfile");
1607 stringstream tempString;
1609 vector<int> grps=UniqueSortedGroups();
1610 vector<int>::iterator git;
1611 for (git=grps.begin(); git!=grps.end(); git++){
1613 bool ThisGroupInUse =
false;
1614 for ( CI it=ParamMap_.begin(); it!=ParamMap_.end(); it++){
1615 if (it->second->WriteToFile_ && it->second->Group_ == *git
1616 && (WriteEnhanced_ || (!(*it).second->Enhanced_)) &&
1617 (!(*it).second->Hidden_)) {
1618 ThisGroupInUse =
true;
1622 if (!ThisGroupInUse)
continue;
1627 const char *groupName = (GetGroupName(*git)).c_str();
1629 tempString <<
"Group_" << *git <<
"_";
1630 while (groupName[i] != 0) {
1631 if (groupName[i] ==
' ') {
1634 tempString << groupName[i];
1640 xmlNodePtr groupNode = tree.
addChildNode(root, tempString.str());
1641 if (groupNode == NULL) {
1642 BIASERR(
"Error creating group: " + tempString.str() +
1643 " / " + GetGroupName(*git));
1650 for ( CI it=ParamMap_.begin(); it!=ParamMap_.end(); it++){
1651 if (it->second->WriteToFile_ && it->second->Group_ == *git
1652 && (WriteEnhanced_ || (!(*it).second->Enhanced_)) &&
1653 (!(*it).second->Hidden_))
1656 xmlNodePtr lastNode = groupNode;
1657 std::string
name = it->first;
1658 std::string::size_type lastFound = std::string::npos;
1659 std::string::size_type found = name.rfind(
'-');
1661 while (found != string::npos){
1662 tempString << name.substr(found+1, lastFound-found);
1663 lastFound = found-1;
1665 xmlNodePtr temp = tree.
getChild(lastNode, tempString.str());
1667 lastNode = tree.
addChildNode(lastNode, tempString.str());
1668 if (lastNode == NULL){
1669 BIASERR(
"Error creating SubGroup: " + tempString.str() +
1679 found = name.rfind(
'-',lastFound);
1682 if (lastFound == std::string::npos) {
1685 tempString << name.substr(0,lastFound+1);
1688 lastNode = tree.
addChildNode(lastNode, tempString.str());
1689 if (lastNode == NULL){
1690 BIASERR(
"Error creating param: " + tempString.str() +
1697 if (WriteComments_) {
1698 tree.
addComment(lastNode, it->second->Help_);
1702 if (it->second->GetTypeName() ==
"BOOL") {
1707 tree.
addAttribute(lastNode,
"Name", it->second->Name_);
1708 }
else if (it->second->GetTypeName() ==
"INT") {
1711 tree.
addAttribute(lastNode,
"Range", it->second->GetHint());
1714 tree.
addAttribute(lastNode,
"Name", it->second->Name_);
1715 }
else if (it->second->GetTypeName() ==
"DOUBLE") {
1718 tree.
addAttribute(lastNode,
"Range", it->second->GetHint());
1721 tree.
addAttribute(lastNode,
"Name", it->second->Name_);
1722 }
else if (it->second->GetTypeName() ==
"STRING") {
1727 tree.
addAttribute(lastNode,
"Name", it->second->Name_);
1728 }
else if (it->second->GetTypeName() ==
"VECDBL") {
1733 tree.
addAttribute(lastNode,
"Name", it->second->Name_);
1734 }
else if (it->second->GetTypeName() ==
"VECINT") {
1739 tree.
addAttribute(lastNode,
"Name", it->second->Name_);
1740 }
else if (it->second->GetTypeName() ==
"ENUM") {
1743 tree.
addAttribute(lastNode,
"Range", it->second->GetHint());
1744 tree.
addAttribute(lastNode,
"Name", it->second->Name_);
1746 BIASERR(
"!!! No matching ParamType for attribute:"
1747 + it->second->GetTypeName() +
" !!!");
1752 tree.
addAttribute(lastNode,
"Type", it->second->GetTypeName());
1754 tree.
addAttribute(lastNode,
"Enhanced", it->second->Enhanced_);
1760 if (tree.
write(filename) == 0){
1767 BIASERR(
"\nCould not write parameterfile:\n " + filename +
"\n");
1782 now = localtime(&now_s);
1783 sprintf(datestr,
"%4d-%02d-%02d-%02d:%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday,
1784 now->tm_hour,now->tm_min);
1786 fname = filename + datestr;
1787 ofs.open(fname.c_str());
1790 perror(fname.c_str());
1793 ofs <<
"#!/bin/sh"<<endl<<endl;
1794 for (
unsigned int i=0;i<ArgV_.size(); i++)
1795 ofs <<ArgV_[i]<<
" ";
1799 chmod(filename.c_str(),0755);
1808 for (CI it=ParamMap_.begin(); it!=ParamMap_.end(); it++)
1809 it->second->SetDefault();
1817 if ( (it=ParamMap_.find(name)) != ParamMap_.end()) {
1818 it->second->CallbackObject_ = obj;
1821 BIASERR(
"Unknown parameter \""<<name<<
"\"in RegisterCallbackObject()");
1828 if ( (it=ParamMap_.find(name)) != ParamMap_.end()) {
1836 BIASERR(
"Unknown parameter \""<<name<<
"\"in RegisterCallbackObject()");
1843 CI it = ParamMap_.find(name);
1844 if ( it==ParamMap_.end() ){
1845 BIASERR(
"parameter "<<name<<
" not found!");
1849 it->second->ShortCmd_ = cmd;
1854 std::vector<std::string>& LinesInFile)
1856 ifstream ifs(ListFileName.c_str());
1858 BIASERR(
"stream to list file "<<ListFileName<<
" could not be established");
1867 ifs.get(*(sstr.rdbuf()));
1868 if(sstr.str().size()>0) {
1869 LinesInFile.push_back(sstr.str());
1877 }
while(ifs.get()!=EOF);
1884 std::map<const std::string,BIAS::ParamTypeBase*> &
1890 const std::map<const std::string,BIAS::ParamTypeBase*> &
int * SetParamInt(const std::string &name, const int &value)
void addAttribute(const xmlNodePtr Node, const std::string &AttributeName, bool AttributeValue)
Add an attribute to a node.
std::string getAttributeName(const xmlAttrPtr Attribute) const
Get the name of a given Attribute.
double * GetParamDouble(const std::string &name) const
BIAS::Vector< int > * SetParamVecInt(const std::string &name, const BIAS::Vector< int > &value)
virtual int SetRangeInt(const std::string &name, const int min, const int max)
Set [Min,Max] as new range, especially usefull in derived class PramGUI
xmlNodePtr getNextChild()
Get the next child of the parent specified in the last getFirstChild() call, the class remembers the ...
bool WriteToFile_
is only written to parameter file if true
int write(const std::string &Filename, bool AutoAddCompressionSuffix=true) const
Write the whole tree that was constructed in memory to disk.
int RegisterCallbackObject(const std::string &name, ParamCallbackInterface *obj)
int SetHiddenFlag(const std::string &name, bool hidden)
if a parametr is marked as hidden, it is obsolete and hidden for all users, use this for parameters o...
int UpdateParameter(int &argc, char *argv[], const std::string &default_filename)
update all arguments from command line and parameter file
xmlNodePtr read(const std::string &Filename)
Read and parse an XML file from disk, DtD validation is not yet implemented.
std::string * GetParamStringByIndex(const int i) const
Get a Param-value by Index.
int SetParameterWriteToFile(const std::string &name, bool writeToFile)
void addComment(const xmlNodePtr Node, const std::string &Comment)
Add comment to a node.
double * GetParamDoubleByIndex(const int i) const
Get a Param-value by Index.
int GetSizeOfLongestParamName()
returns the size of the longest param name not marked as enhanced or hidden
std::vector< int > UniqueSortedGroups()
Returns a vector of all group IDs (unique) in ascending order.
BIAS::Vector< int > value_
xmlNodePtr getChild(const xmlNodePtr ParentNode, const std::string &ChildName)
Get a child of a Parent node by specifying the childs name, NULL is returned if the ParentNode has no...
Param(bool DisableDestructorWarning=false)
void ClearAllParams()
empties internal datastructures
xmlNodePtr create(const std::string &RootNodeName)
Create the base of a new XML-Tree in memory, already with a one and only root node.
virtual void ParameterChanged(const std::string ¶mname, const void *data)
bool * AddParamBool(const std::string &name, const std::string &help, bool deflt=false, char cmdshort=0, int Group=GRP_NOSHOW)
double * AddParamDouble(const std::string &name, const std::string &help, double deflt=0.0, double min=-DBL_MAX, double max=DBL_MAX, char cmdshort=0, int Group=GRP_NOSHOW)
int ReadParameter(const std::string &filename)
read values for parameters from file
int * AddParamEnum(const std::string &name, const std::string &help, const std::vector< std::string > &enums, const int deflt=0, const std::vector< int > *IDs=NULL, const char cmdshort=0, const int Group=GRP_NOSHOW)
std::map< std::string, int > Map_
bool getAttributeValueBool(const xmlAttrPtr Attribute) const
Get the value of a given Attribute, with type-cast overloads for different attribute types...
int * GetParamEnum(const std::string &name) const
xmlAttrPtr getFirstAttribute(const xmlNodePtr Node)
Get the first attribute of a given parent, or NULL for no attributes.
int ParseCommandLine(int &argc, char *argv[])
scan command line arguments for valid parameters
double * SetParamDouble(const std::string &name, const double &value)
bool * GetParamBool(const std::string &name) const
std::vector< double > getAttributeValueVecDbl(const xmlAttrPtr Attribute) const
BIAS::Vector< double > * SetParamVecDbl(const std::string &name, const BIAS::Vector< double > &value)
std::string getAttributeValueString(const xmlAttrPtr Attribute) const
int getAttributeValueInt(const xmlAttrPtr Attribute) const
Vector< T > & newsize(Subscript N)
std::string * SetParamString(const std::string &name, const std::string &value)
int * SetParamEnum(const std::string &name, const int &value)
set the enum to the ID 'value'
bool IsUsedGroupID(const int group_id)
returns if the group id is used
This class provides an interface to be called if parameter changes occured.
void Usage(std::ostream &os=std::cout)
print Help-Information to stdout
Wrapper class for reading and writing XML files based on the XML library libxml2. ...
int GetGroupIDForGroupName(const std::string &name)
void SetDefaults()
sets all entries to their initially (AddParam) given default values
BIAS::Vector< int > * GetParamVecInt(const std::string &name) const
bool CheckParam(const std::string &name)
Check if parameter has already been added.
xmlNodePtr addChildNode(const xmlNodePtr ParentNode, const std::string &NewNodeName)
Add a child node to an incoming node with the given name.
std::string * GetParamString(const std::string &name) const
BIAS::Vector< int > * AddParamVecInt(const std::string &name, const std::string &help, const BIAS::Vector< int > &deflt, char cmdshort=0, int Group=GRP_NOSHOW)
Add a parameter that expects a string on command line like "<value0> <value1> <value2> ...
BIAS::Vector< double > * GetParamVecDbl(const std::string &name) const
int SetShortCommand(const std::string &name, const char cmd)
Add a short commandline switch to an existing parameter.
int WriteParameter(const std::string &filename)
store complete set of parameter in file if write_comments is set, alo the help strings are written to...
BIAS::Vector< int > default_
int * GetParamInt(const std::string &name) const
int GetFreeGroupID()
returns unused group id
xmlNodePtr getFirstChild(const xmlNodePtr ParentNode)
Get the first child of a given parent, or NULL for no childs.
static int ParseListFile(const std::string &ListFileName, std::vector< std::string > &LinesInFile)
Extracts lines from passed file.
bool * SetParamBool(const std::string &name, const bool &value)
BIAS::Vector< double > * AddParamVecDbl(const std::string &name, const std::string &help, const BIAS::Vector< double > &deflt, char cmdshort=0, int Group=GRP_NOSHOW)
Add a parameter that expects a string on command line like "<value0> <value1> <value2> ...
BIAS::Vector< double > default_
xmlAttrPtr getNextAttribute()
Get the next attribute of the parent specified in the last getFirstAttribute() call, the class remembers the last returned attribute.
bool * GetParamBoolByIndex(const int i) const
Get a Param-value by Index.
int SetGroupName(const int group_id, const std::string &name)
sets the name for a group
double getAttributeValueDouble(const xmlAttrPtr Attribute) const
std::map< const std::string, BIAS::ParamTypeBase * > & GetParamMap()
BIAS::Vector< double > value_
virtual ~ParamCallbackInterface()
int * AddParamInt(const std::string &name, const std::string &help, int deflt=0, int min=std::numeric_limits< int >::min(), int max=std::numeric_limits< int >::max(), char cmdshort=0, int Group=GRP_NOSHOW)
For all adding routines:
void ShowData(std::ostream &os=std::cout, int grp=GRP_ALL, bool showenhanced=true)
print all data in group grp including current values to os if grp = GRP_ALL, print all values if show...
int * GetParamIntByIndex(const int i) const
Get a Param-value by Index.
int ReadParameterXML(const std::string &filename)
read values for parameters from xml-file
int SetEnhancedFlag(const std::string &name, bool enhanced)
if a parametr is marked as enhanced, it is hidden from the naive user
std::string GetGroupName(const int group_id)
gets the name of a group
int WriteParameterXML(const std::string &filename)
std::vector< int > getAttributeValueVecInt(const xmlAttrPtr Attribute) const
std::string * AddParamString(const std::string &name, const std::string &help, std::string deflt="", char cmdshort=0, int Group=GRP_NOSHOW)
int GetGroupID(const std::string &name)
returns group id of parameter with name
int GetSizeOfLongestParamValueString()
returns the size of the longest param value string not marked as enhanced or hidden ...
int WriteRunMe(const std::string &filename)
writes complete command line including program name and unknown command line options, no defaults, no params read from file