NAMD
Classes | Public Member Functions | List of all members
ParseOptions Class Reference

#include <ParseOptions.h>

Classes

class  DataElement
 

Public Member Functions

 ParseOptions (void)
 
 ~ParseOptions (void)
 
int require (const char *newname, const char *parent, const char *msg, BigReal *ptr, BigReal defalt)
 
int require (const char *newname, const char *parent, const char *msg, BigReal *ptr)
 
int require (const char *newname, const char *parent, const char *msg, Vector *ptr, Vector defalt)
 
int require (const char *newname, const char *parent, const char *msg, Vector *ptr)
 
int require (const char *newname, const char *parent, const char *msg, int *ptr, int defalt)
 
int require (const char *newname, const char *parent, const char *msg, int *ptr)
 
int require (const char *newname, const char *parent, const char *msg, unsigned int *ptr, unsigned int defalt)
 
int require (const char *newname, const char *parent, const char *msg, unsigned int *ptr)
 
int requireB (const char *newname, const char *parent, const char *msg, int *ptr, int defalt)
 
int requireB (const char *newname, const char *parent, const char *msg, int *ptr)
 
int require (const char *newname, const char *parent, const char *msg, StringList **ptr=NULL, int many_allowed=FALSE)
 
int require (const char *newname, const char *parent, const char *msg, char *ptr)
 
int optional (const char *newname, const char *parent, const char *msg, BigReal *ptr, BigReal defalt)
 
int optional (const char *newname, const char *parent, const char *msg, BigReal *ptr)
 
int optional (const char *newname, const char *parent, const char *msg, Vector *ptr, Vector defalt)
 
int optional (const char *newname, const char *parent, const char *msg, Vector *ptr)
 
int optional (const char *newname, const char *parent, const char *msg, int *ptr, int defalt)
 
int optional (const char *newname, const char *parent, const char *msg, int *ptr)
 
int optional (const char *newname, const char *parent, const char *msg, unsigned int *ptr, unsigned int defalt)
 
int optional (const char *newname, const char *parent, const char *msg, unsigned int *ptr)
 
int optionalB (const char *newname, const char *parent, const char *msg, int *ptr, int defalt)
 
int optionalB (const char *newname, const char *parent, const char *msg, int *ptr)
 
int optional (const char *newname, const char *parent, const char *msg, StringList **ptr=NULL, int many_allowed=FALSE)
 
int optional (const char *newname, const char *parent, const char *msg, char *ptr)
 
Range range (const char *name)
 
void range (const char *name, Range newrange)
 
Bool check_consistency (void)
 
Bool set (const ConfigList &configlist)
 
char * getfromptr (const char *name, char *outbuf)
 
int istruefromptr (const char *name)
 
int issetfromptr (const char *name)
 
Bool get (const char *name, int *val)
 
Bool get (const char *name, BigReal *val)
 
Bool get (const char *name, Vector *val)
 
Bool get (const char *name, StringList **val)
 
Bool get (const char *name, char *val, int n=0)
 
int num (const char *name)
 
Bool defined (const char *name)
 
Bool exists (const char *name)
 
Bool units (const char *name, Units units)
 
Bool units (const char *name, Units *units)
 

Detailed Description

Definition at line 42 of file ParseOptions.h.

Constructor & Destructor Documentation

ParseOptions::ParseOptions ( void  )

Definition at line 199 of file ParseOptions.C.

References TRUE, ParseOptions::DataElement::type, and ParseOptions::DataElement::UNDEF.

199  {
200  configList = NULL;
201  array_size = 0;
202  array_max_size = 20;
203  data_array = new DataElement*[array_max_size];
204  DataElement *tmp = new DataElement("main", "main", TRUE,
205  "Error in ParseOptions",
206  (int *) NULL, 0);
207  tmp->type = DataElement::UNDEF;
208  add_element(tmp);
209 }
#define TRUE
Definition: common.h:119
ParseOptions::~ParseOptions ( void  )

Definition at line 212 of file ParseOptions.C.

212  {
213  for (int i=0; i<array_size; i++) {
214  delete data_array[i];
215  }
216  delete [] data_array;
217 }

Member Function Documentation

Bool ParseOptions::check_consistency ( void  )

Definition at line 391 of file ParseOptions.C.

References endi(), FALSE, iERROR(), iout, ParseOptions::DataElement::name, ParseOptions::DataElement::parent, and TRUE.

Referenced by SimParameters::initialize_config_data().

391  {
392  int i;
393  // check for lack of parent
394  {
395  int has_error = FALSE;
396  for(i=1; i<array_size; i++) {
397  if (!data_array[i]->parent_ptr) {
398  // missing a parent
399  iout << iERROR << "Configuration element '" << data_array[i]->name
400  << "' defined, but the parent element" << "\n" << endi;
401  iout << iERROR << " '" << data_array[i]->parent << "' is nowhere "
402  << "to be found" << "\n" << endi;
403  has_error = TRUE;
404  }
405  }
406  if (has_error) return 0;
407  }
408 
409  // check for loop constructs in the "main" heirarchy
410  int *arr = new int[array_size];
411  for (i=0; i<array_size; i++) { // initialize it
412  arr[i] = 0;
413  }
414  if (!check_children(0, arr)) {
415  // a loop was found
416  iout << iERROR << "Loop found in ParseOptions data" << "\n" << endi;
417  delete [] arr;
418  return 0;
419  }
420 
421  // check for elements inaccessible to "main"
422  {
423  int has_error = FALSE;
424  for (i=1; i<array_size; i++) {
425  if (arr[i] == 0) {
426  // found an inaccesible element
427  if (has_error == FALSE) { // first time, so print message
428  iout << iERROR
429  << "Found data in ParseOptions which are inaccessible "
430  << "to" << "\n" << endi;
431  iout << iERROR
432  << "the main data hierarchy. Errors in:" << "\n" << endi;
433  has_error = TRUE;
434  }
435  iout << iERROR << " '" << data_array[i]->name << "' depends on '"
436  << data_array[i]->parent << "'" << "\n" << endi;
437  }
438  }
439  if (has_error) {
440  delete [] arr;
441  return 0;
442  }
443  }
444  // looks like everything went well
445  delete [] arr;
446  return 1;
447 }
#define FALSE
Definition: common.h:118
#define iout
Definition: InfoStream.h:87
std::ostream & iERROR(std::ostream &s)
Definition: InfoStream.C:109
infostream & endi(infostream &s)
Definition: InfoStream.C:38
#define TRUE
Definition: common.h:119
Bool ParseOptions::defined ( const char *  name)

Definition at line 913 of file ParseOptions.C.

References FALSE, ParseOptions::DataElement::is_defined, and TRUE.

914 {
915  if (!name) return FALSE;
916  DataElement *tmp = internal_find(name);
917  if (!tmp) return FALSE;
918  if (tmp->is_defined) {
919  return TRUE;
920  }
921  return FALSE;
922 }
#define FALSE
Definition: common.h:118
#define TRUE
Definition: common.h:119
Bool ParseOptions::exists ( const char *  name)

Definition at line 904 of file ParseOptions.C.

Referenced by set().

905 {
906  if (!name) return 0;
907  if (internal_find(name)) {
908  return 1;
909  }
910  return 0;
911 }
Bool ParseOptions::get ( const char *  name,
int *  val 
)

Definition at line 998 of file ParseOptions.C.

References ParseOptions::DataElement::BOOL, StringList::data, endi(), FALSE, ParseOptions::DataElement::fdata, ParseOptions::DataElement::FLOAT, ParseOptions::DataElement::idata, iERROR(), ParseOptions::DataElement::INT, iout, ParseOptions::DataElement::is_defined, iWARN(), ParseOptions::DataElement::sldata, ParseOptions::DataElement::STRING, ParseOptions::DataElement::STRINGLIST, TRUE, ParseOptions::DataElement::type, and ParseOptions::DataElement::VECTOR.

998  {
999  if (!val) return FALSE;
1000  DataElement *el = internal_find(name);
1001  if (el == NULL || !el->is_defined) {
1002  return FALSE;
1003  }
1004  switch (el->type) {
1005  case DataElement::FLOAT :
1006  iout << iWARN
1007  << "ParseOptions doing a conversion from float to int for '"
1008  << name << "'" << "\n" << endi;
1009  *val = (int) el->fdata;
1010  return TRUE;
1011  case DataElement::INT:
1012  case DataElement::BOOL:
1013  *val = el->idata;
1014  return TRUE;
1016  case DataElement::STRING :
1017  iout << iWARN
1018  << "ParseOptions doing a conversion from StringList[0] to int "
1019  << "for '" << name << "'" << "\n" << endi;
1020  *val = atoi(el->sldata->data);
1021  return TRUE;
1022  case DataElement::VECTOR :
1023  iout << iERROR
1024  << "ParseOptions cannot convert from Vector to int for '"
1025  << name << "'" << "\n" << endi;
1026  return FALSE;
1027  default:
1028  iout << iERROR
1029  << "Unknown data type " << (int)(el->type) << " for '" << name << "'"
1030  << "\n" << endi;
1031  }
1032  return FALSE;
1033 }
#define FALSE
Definition: common.h:118
std::ostream & iWARN(std::ostream &s)
Definition: InfoStream.C:108
#define iout
Definition: InfoStream.h:87
std::ostream & iERROR(std::ostream &s)
Definition: InfoStream.C:109
infostream & endi(infostream &s)
Definition: InfoStream.C:38
#define TRUE
Definition: common.h:119
Bool ParseOptions::get ( const char *  name,
BigReal val 
)

Definition at line 1035 of file ParseOptions.C.

References ParseOptions::DataElement::BOOL, StringList::data, endi(), FALSE, ParseOptions::DataElement::fdata, ParseOptions::DataElement::FLOAT, ParseOptions::DataElement::idata, iERROR(), ParseOptions::DataElement::INT, iout, ParseOptions::DataElement::is_defined, iWARN(), ParseOptions::DataElement::sldata, ParseOptions::DataElement::STRING, ParseOptions::DataElement::STRINGLIST, TRUE, ParseOptions::DataElement::type, and ParseOptions::DataElement::VECTOR.

1035  {
1036  if (!val) return FALSE;
1037  DataElement *el = internal_find(name);
1038  if (el == NULL || !el->is_defined) {
1039  return FALSE;
1040  }
1041  switch (el -> type) {
1042  case DataElement::FLOAT:
1043  *val = el->fdata;
1044  return TRUE;
1045  case DataElement::INT:
1046  iout << iWARN
1047  << "ParseOptions doing a conversion from int to float '"
1048  << name << "'" << "\n" << endi;
1049  *val = (BigReal) el->idata;
1050  return TRUE;
1051  case DataElement::BOOL:
1052  iout << iWARN
1053  << "ParseOptions doing a conversion from boolean to float for '"
1054  << name << "'" << "\n" << endi;
1055  *val = (BigReal) el->idata;
1056  return TRUE;
1057  case DataElement::STRING:
1059  iout << iWARN
1060  << "ParseOptions doing a conversion from StringList[0] to float "
1061  << "for '" << name << "'" << "\n" << endi;
1062  *val = atof(el->sldata->data);
1063  return TRUE;
1064  case DataElement::VECTOR :
1065  iout << iERROR
1066  << "ParseOptions cannot convert from Vector to float for '"
1067  << name << "'" << "\n" << endi;
1068  return FALSE;
1069  default:
1070  iout << iERROR
1071  << "Unknown data type " << (int)(el->type) << " for '" << name << "'"
1072  << "\n" << endi;
1073  }
1074  return FALSE;
1075 }
#define FALSE
Definition: common.h:118
std::ostream & iWARN(std::ostream &s)
Definition: InfoStream.C:108
#define iout
Definition: InfoStream.h:87
std::ostream & iERROR(std::ostream &s)
Definition: InfoStream.C:109
infostream & endi(infostream &s)
Definition: InfoStream.C:38
#define TRUE
Definition: common.h:119
double BigReal
Definition: common.h:114
Bool ParseOptions::get ( const char *  name,
Vector val 
)

Definition at line 1076 of file ParseOptions.C.

References StringList::data, endi(), FALSE, ParseOptions::DataElement::FLOAT, iERROR(), ParseOptions::DataElement::INT, iout, ParseOptions::DataElement::is_defined, iWARN(), Vector::set(), ParseOptions::DataElement::sldata, ParseOptions::DataElement::STRING, ParseOptions::DataElement::STRINGLIST, TRUE, ParseOptions::DataElement::type, ParseOptions::DataElement::vdata, and ParseOptions::DataElement::VECTOR.

1076  {
1077  if (!val) return FALSE;
1078  DataElement *el = internal_find(name);
1079  if (el == NULL || !el->is_defined) {
1080  return FALSE;
1081  }
1082  switch (el -> type) {
1083  case DataElement::FLOAT:
1084  iout << iERROR
1085  << "ParseOptions cannot convert from float to Vector for '"
1086  << name << "'" << "\n" << endi;
1087  return FALSE;
1088  case DataElement::INT:
1089  iout << iERROR
1090  << "ParseOptions cannot convert from int to Vector for '"
1091  << name << "'" << "\n" << endi;
1092  return FALSE;
1093  case DataElement::STRING:
1095  iout << iWARN
1096  << "ParseOptions doing a conversion from StringList[0] to "
1097  << "Vector for '" << name << "'" << "\n" << endi;
1098  Vector v;
1099  if (!v.set(el->sldata->data)) {
1100  iout << iERROR
1101  << "Could not convert '" << el->sldata->data
1102  << "' to a Vector";
1103  return FALSE;
1104  }
1105  *val = v;
1106  return TRUE;
1107  }
1108  case DataElement::VECTOR :
1109  *val = el->vdata;
1110  return TRUE;
1111  default:
1112  iout << iERROR
1113  << "Unknown data type " << (int)(el->type) << " for '" << name << "'"
1114  << "\n" << endi;
1115 
1116  }
1117  return FALSE;
1118 }
Definition: Vector.h:64
#define FALSE
Definition: common.h:118
std::ostream & iWARN(std::ostream &s)
Definition: InfoStream.C:108
#define iout
Definition: InfoStream.h:87
Bool set(const char *s)
Definition: Vector.h:228
std::ostream & iERROR(std::ostream &s)
Definition: InfoStream.C:109
infostream & endi(infostream &s)
Definition: InfoStream.C:38
#define TRUE
Definition: common.h:119
Bool ParseOptions::get ( const char *  name,
StringList **  val 
)

Definition at line 1119 of file ParseOptions.C.

References FALSE, ConfigList::find(), ParseOptions::DataElement::is_defined, and TRUE.

1119  {
1120  if (!val) return FALSE;
1121  DataElement *el = internal_find(name); // first check it is internally valid
1122  if (el == NULL || !el->is_defined) {
1123  return FALSE;
1124  }
1125  // then simply ask the configList itself for the answer
1126  // (while I do keep the information internally, in sldata, why bother?
1127  if (!configList) { return FALSE; }
1128  *val = configList->find(name);
1129  if (!*val) { return FALSE; } // paranoia, I know...
1130  return TRUE;
1131 }
#define FALSE
Definition: common.h:118
StringList * find(const char *name) const
Definition: ConfigList.C:341
#define TRUE
Definition: common.h:119
Bool ParseOptions::get ( const char *  name,
char *  val,
int  n = 0 
)

Definition at line 1134 of file ParseOptions.C.

References StringList::data, FALSE, StringList::next, STRINGNULL, and TRUE.

1135 {
1136  if (!val || n<0) {return FALSE;}
1137  StringList *tmp;
1138  if (!get(name, &tmp)) {val[0]=STRINGNULL; return FALSE; }
1139  int i=n;
1140  while (i>0 && tmp) { // search for the nth element
1141  tmp=tmp->next;
1142  i--;
1143  }
1144  if (tmp) { // if it was long enough, return it
1145  strcpy(val, tmp->data);
1146  return TRUE;
1147  }
1148  val[0] = STRINGNULL;
1149  return FALSE;
1150 }
#define FALSE
Definition: common.h:118
#define STRINGNULL
Definition: common.h:128
StringList * next
Definition: ConfigList.h:49
char * data
Definition: ConfigList.h:48
#define TRUE
Definition: common.h:119
char * ParseOptions::getfromptr ( const char *  name,
char *  outbuf 
)

Definition at line 938 of file ParseOptions.C.

References ParseOptions::DataElement::BOOL, StringList::data, endi(), ParseOptions::DataElement::fdata, ParseOptions::DataElement::FLOAT, ParseOptions::DataElement::fptr, ParseOptions::DataElement::idata, iERROR(), ParseOptions::DataElement::INT, iout, ParseOptions::DataElement::iptr, NAMD_bug(), PRINT_DOUBLE, PRINT_VECTOR(), ParseOptions::DataElement::sldata, ParseOptions::DataElement::slptr, ParseOptions::DataElement::sptr, ParseOptions::DataElement::STRING, ParseOptions::DataElement::STRINGLIST, ParseOptions::DataElement::type, ParseOptions::DataElement::vdata, ParseOptions::DataElement::VECTOR, and ParseOptions::DataElement::vptr.

938  {
939 #ifdef NAMD_TCL
940  if ( ! name ) NAMD_bug("ParseOptions::getfromptr called with null name");
941  if ( ! outbuf ) NAMD_bug("ParseOptions::getfromptr called with null outbuf");
942  DataElement *el = internal_find(name);
943  if ( el == NULL ) return 0;
944  switch (el->type) {
945  case DataElement::FLOAT :
946  if ( el->fptr ) PRINT_DOUBLE(outbuf, *(el->fptr));
947  else PRINT_DOUBLE(outbuf, el->fdata);
948  return outbuf;
949  case DataElement::INT:
950  case DataElement::BOOL:
951  if ( el->iptr ) sprintf(outbuf,"%d", *(el->iptr));
952  else sprintf(outbuf,"%d", el->idata);
953  return outbuf;
955  if ( el->slptr ) return (*(el->slptr))->data;
956  else if ( el->sldata ) return el->sldata->data;
957  else return 0;
958  case DataElement::STRING :
959  if ( el->sptr ) return el->sptr;
960  else if ( el->sldata ) return el->sldata->data;
961  else return 0;
962  case DataElement::VECTOR :
963  if ( el->vptr ) PRINT_VECTOR(outbuf, *(el->vptr));
964  else PRINT_VECTOR(outbuf, el->vdata);
965  return outbuf;
966  default:
967  iout << iERROR
968  << "Unknown data type " << (int)(el->type) << " for '" << name << "'"
969  << "\n" << endi;
970  }
971 #endif
972  return 0;
973 }
static void PRINT_VECTOR(char *buf, Vector val)
Definition: ParseOptions.C:927
#define iout
Definition: InfoStream.h:87
void NAMD_bug(const char *err_msg)
Definition: common.C:123
#define PRINT_DOUBLE(BUF, VAL)
Definition: ParseOptions.C:925
std::ostream & iERROR(std::ostream &s)
Definition: InfoStream.C:109
infostream & endi(infostream &s)
Definition: InfoStream.C:38
int ParseOptions::issetfromptr ( const char *  name)

Definition at line 987 of file ParseOptions.C.

References ParseOptions::DataElement::is_defined, and NAMD_bug().

987  {
988  if ( ! name ) NAMD_bug("ParseOptions::getfromptr called with null name");
989  DataElement *el = internal_find(name);
990  if ( el == NULL ) return -1;
991  return (el->is_defined ? 1 : 0);
992 }
void NAMD_bug(const char *err_msg)
Definition: common.C:123
int ParseOptions::istruefromptr ( const char *  name)

Definition at line 976 of file ParseOptions.C.

References ParseOptions::DataElement::BOOL, ParseOptions::DataElement::idata, ParseOptions::DataElement::iptr, ParseOptions::DataElement::is_defined, NAMD_bug(), and ParseOptions::DataElement::type.

976  {
977  if ( ! name ) NAMD_bug("ParseOptions::getfromptr called with null name");
978  DataElement *el = internal_find(name);
979  if ( el == NULL ) return -1;
980  if ( el->type != DataElement::BOOL ) return -2;
981  if ( el->iptr ) return ((*(el->iptr)) ? 1 : 0);
982  if ( ! el->is_defined ) return -3; // ignores defaults
983  return (el->idata ? 1 : 0);
984 }
void NAMD_bug(const char *err_msg)
Definition: common.C:123
int ParseOptions::num ( const char *  name)

Definition at line 1153 of file ParseOptions.C.

References ParseOptions::DataElement::many_allowed, and StringList::next.

1154 {
1155  DataElement *el = internal_find(name);
1156  if (!el || !el ->is_defined) {
1157  return 0;
1158  }
1159  if (!el->many_allowed) {
1160  return 1;
1161  }
1162  StringList *tmp;
1163  if (!get(name, &tmp)) { return 0; }
1164  int i=0;
1165  while (tmp) {
1166  i++;
1167  tmp=tmp->next;
1168  }
1169  return i;
1170 }
StringList * next
Definition: ConfigList.h:49
int ParseOptions::optional ( const char *  newname,
const char *  parent,
const char *  msg,
BigReal ptr,
BigReal  defalt 
)
int ParseOptions::optional ( const char *  newname,
const char *  parent,
const char *  msg,
BigReal ptr 
)
int ParseOptions::optional ( const char *  newname,
const char *  parent,
const char *  msg,
Vector ptr,
Vector  defalt 
)
int ParseOptions::optional ( const char *  newname,
const char *  parent,
const char *  msg,
Vector ptr 
)
int ParseOptions::optional ( const char *  newname,
const char *  parent,
const char *  msg,
int *  ptr,
int  defalt 
)
int ParseOptions::optional ( const char *  newname,
const char *  parent,
const char *  msg,
int *  ptr 
)
int ParseOptions::optional ( const char *  newname,
const char *  parent,
const char *  msg,
unsigned int *  ptr,
unsigned int  defalt 
)
int ParseOptions::optional ( const char *  newname,
const char *  parent,
const char *  msg,
unsigned int *  ptr 
)
int ParseOptions::optional ( const char *  newname,
const char *  parent,
const char *  msg,
StringList **  ptr = NULL,
int  many_allowed = FALSE 
)
int ParseOptions::optional ( const char *  newname,
const char *  parent,
const char *  msg,
char *  ptr 
)
int ParseOptions::optionalB ( const char *  newname,
const char *  parent,
const char *  msg,
int *  ptr,
int  defalt 
)
int ParseOptions::optionalB ( const char *  newname,
const char *  parent,
const char *  msg,
int *  ptr 
)
Range ParseOptions::range ( const char *  name)

Definition at line 1186 of file ParseOptions.C.

References endi(), FREE_RANGE, iERROR(), iout, and ParseOptions::DataElement::range.

1187 {
1188  DataElement *el = internal_find(name);
1189  if (!el) {
1190  iout << iERROR
1191  << "Trying to get the range of undefined variable '"
1192  << name << "'" << "\n" << endi;
1193  return FREE_RANGE;
1194  }
1195  return el->range;
1196 }
#define iout
Definition: InfoStream.h:87
std::ostream & iERROR(std::ostream &s)
Definition: InfoStream.C:109
infostream & endi(infostream &s)
Definition: InfoStream.C:38
void ParseOptions::range ( const char *  name,
Range  newrange 
)

Definition at line 1174 of file ParseOptions.C.

References endi(), iERROR(), iout, and ParseOptions::DataElement::range.

1175 {
1176  DataElement *el = internal_find(name);
1177  if (!el) {
1178  iout << iERROR
1179  << "Trying to set the range of undefined variable '"
1180  << name << "'" << "\n" << endi;
1181  return;
1182  }
1183  el->range = newrange;
1184 
1185 }
#define iout
Definition: InfoStream.h:87
std::ostream & iERROR(std::ostream &s)
Definition: InfoStream.C:109
infostream & endi(infostream &s)
Definition: InfoStream.C:38
int ParseOptions::require ( const char *  newname,
const char *  parent,
const char *  msg,
BigReal ptr,
BigReal  defalt 
)
int ParseOptions::require ( const char *  newname,
const char *  parent,
const char *  msg,
BigReal ptr 
)
int ParseOptions::require ( const char *  newname,
const char *  parent,
const char *  msg,
Vector ptr,
Vector  defalt 
)
int ParseOptions::require ( const char *  newname,
const char *  parent,
const char *  msg,
Vector ptr 
)
int ParseOptions::require ( const char *  newname,
const char *  parent,
const char *  msg,
int *  ptr,
int  defalt 
)
int ParseOptions::require ( const char *  newname,
const char *  parent,
const char *  msg,
int *  ptr 
)
int ParseOptions::require ( const char *  newname,
const char *  parent,
const char *  msg,
unsigned int *  ptr,
unsigned int  defalt 
)
int ParseOptions::require ( const char *  newname,
const char *  parent,
const char *  msg,
unsigned int *  ptr 
)
int ParseOptions::require ( const char *  newname,
const char *  parent,
const char *  msg,
StringList **  ptr = NULL,
int  many_allowed = FALSE 
)
int ParseOptions::require ( const char *  newname,
const char *  parent,
const char *  msg,
char *  ptr 
)
int ParseOptions::requireB ( const char *  newname,
const char *  parent,
const char *  msg,
int *  ptr,
int  defalt 
)
int ParseOptions::requireB ( const char *  newname,
const char *  parent,
const char *  msg,
int *  ptr 
)
Bool ParseOptions::set ( const ConfigList configlist)

Definition at line 642 of file ParseOptions.C.

References ParseOptions::DataElement::BOOL, StringList::data, endi(), ParseOptions::DataElement::error_message, exists(), FALSE, ParseOptions::DataElement::fdata, ParseOptions::DataElement::fdef, ConfigList::find(), ParseOptions::DataElement::FLOAT, ParseOptions::DataElement::has_default, ConfigList::head(), ParseOptions::DataElement::idata, ParseOptions::DataElement::idef, iERROR(), ParseOptions::DataElement::index, ParseOptions::DataElement::INT, iout, ParseOptions::DataElement::is_defined, ParseOptions::DataElement::is_optional, iWARN(), ParseOptions::DataElement::many_allowed, ParseOptions::DataElement::name, next(), StringList::next, ParseOptions::DataElement::parent_ptr, ParseOptions::DataElement::sldata, ParseOptions::DataElement::STRING, ParseOptions::DataElement::STRINGLIST, TRUE, ParseOptions::DataElement::type, ParseOptions::DataElement::uidata, ParseOptions::DataElement::uidef, ParseOptions::DataElement::UINT, ParseOptions::DataElement::vdata, ParseOptions::DataElement::vdef, and ParseOptions::DataElement::VECTOR.

Referenced by SimParameters::initialize_config_data().

643 {
644  // the algorithm is easy, though it looks scary
645  int cont = TRUE, i; // do some initialization
646  StringList *slptr;
647  DataElement *data;
648  int has_error = FALSE;
649  int *checked = new int[array_size];
650  configList = &clist;
651 
652  // I make here a list of element I have already checked, starting
653  // at the head. I check only children of those that have already
654  // been defined and add it to the checked list
655  for (i=0; i<array_size; i++)
656  {
657  checked[i] = FALSE;
658  }
659 
660  // make "main" 'defined' (at this point, nothing else should be defined)
661  data_array[0]->is_defined = TRUE;
662  checked[0] = TRUE; // and make "main" checked
663 
664  // while there is still data which hasn't been defined
665  while (cont)
666  {
667  cont = FALSE;
668  for (i=1; i<array_size; i++)
669  { // check each element
670  data = data_array[i];
671 
672  // find unchecked data which has a parent which was checked
673  // and defined
674  if (!checked[data->index] &&
675  checked[data-> parent_ptr -> index] &&
676  data -> parent_ptr -> is_defined)
677  {
678  cont = TRUE;
679  checked[data->index] = TRUE; // so I don't check again
680 
681  // check to see if data is available in the StringList
682  slptr = clist.find(data->name);
683 
684  if (slptr != NULL)
685  { // it is
686 
687  // most data types allow only 1 item, so check if that is
688  // a problem. (some StringLists, like 'parameters', allow
689  // multiple strings)
690  if (!data->many_allowed && slptr->next != NULL)
691  {
692  iout << iERROR << "Multiple definitions of '" << data->name << "'" << "\n" << endi;
693  iout << iERROR << " in the configuration file are not allowed" << "\n" << endi;
694  has_error = TRUE;
695  }
696 
697  data->is_defined = TRUE;
698 
699  // set the appropriate data field
700  if (data->type == DataElement::FLOAT)
701  {
702  if (!scan_float(data, slptr->data))
703  has_error = TRUE;
704  }
705  else if (data->type == DataElement::VECTOR)
706  {
707  if (!scan_vector(data, slptr->data))
708  has_error = TRUE;
709  }
710  else if (data->type == DataElement::INT)
711  {
712  if (!scan_int(data, slptr->data))
713  has_error = TRUE;
714  }
715  else if (data->type == DataElement::UINT)
716  {
717  if (!scan_uint(data, slptr->data))
718  has_error = TRUE;
719  }
720  else if (data->type == DataElement::BOOL)
721  {
722  if (!scan_bool(data, slptr->data))
723  has_error = TRUE;
724  }
725  else if (data->type == DataElement::STRINGLIST ||
726  data->type == DataElement::STRING )
727  {
728  data->sldata = slptr;
729  }
730  else
731  {
732  iout << iERROR << "Unknown ParseOption data type " << (int)(data->type) << " for "
733  << "variable " << data->name << "\n" << endi;
734  has_error = TRUE;
735  }
736  }
737  else
738  { // no definition; is there a default?
739  if (data->has_default)
740  {
741  data->is_defined = TRUE;
742 
743  if (data->type == DataElement::FLOAT)
744  {
745  data->fdata = data->fdef;
746  }
747  else if (data->type == DataElement::VECTOR)
748  {
749  data->vdata = data->vdef;
750  }
751  else if (data->type == DataElement::UINT)
752  {
753  data->uidata = data->uidef;
754  }
755  else if (data->type == DataElement::INT ||
756  data->type == DataElement::BOOL)
757  {
758  data->idata = data->idef;
759  }
760  else
761  {
762  iout << iERROR << "Unknown ParseOption data type " << (int)(data->type) << " for "
763  << "variable " << data->name << "\n" << endi;
764  has_error = TRUE;
765  }
766  }
767  }
768 
769  // at this point we should have gotten data from the file or the defaults,
770  // or it hasn't yet been defined. If it still isn't defined, check
771  // to see if it is optional
772  if (!data->is_defined)
773  { // if still not defined,
774  if (!data->is_optional)
775  { // it is it required
776  has_error = TRUE;
777  iout << iERROR << "'" << data->name << "' is a required configuration option" << "\n" << endi;
778 
779  // print some helpful information if this isn't a "main" option
780  if (data->parent_ptr != data_array[0])
781  {
782  iout << iERROR << " when '" << data->parent_ptr -> name << "' is set" << "\n" << endi;
783  } // printed parent info
784 
785  iout << iERROR << data->name << " defines: " << data->error_message << "\n" << endi;
786 
787  } // printed error message
788  }
789  else
790  { // there was a definition, so assign to the variable
791  if (data->type == DataElement::FLOAT)
792  {
793  if (!set_float(data))
794  has_error = TRUE;
795  }
796  else if ( data -> type == DataElement::VECTOR)
797  {
798  set_vector(data);
799  }
800  else if ( data -> type == DataElement::INT)
801  {
802  if (!set_int(data))
803  has_error = TRUE;
804  }
805  else if ( data -> type == DataElement::UINT)
806  {
807  if (!set_uint(data))
808  has_error = TRUE;
809  }
810  else if ( data -> type == DataElement::BOOL)
811  {
812  set_bool(data);
813 
814  if (is_parent_node(data))
815  {
816  // this makes the boolean variable undefined if it is 'off'
817  // _and_ it is a parent; this makes it agree with namd's
818  // configuration option semantics
819  data->is_defined = data->idata;
820  }
821  }
822  else if ( data -> type == DataElement::STRINGLIST)
823  {
824  set_stringlist(data);
825  }
826  else if ( data -> type == DataElement::STRING)
827  {
828  set_string(data);
829  }
830  else
831  {
832  // already printed the error message
833  }
834  }
835  } // end of checking the available variables
836  } // end of pass through the list
837  } // end of finding data in the ConfigList
838 
839 
840  // and now print the warning messages
841 
842  // first, find elements which are in the configuration file and are
843  // valid, but which were not needed (ie, the checked array wasn''t set)
844  {
845  int flg = 0;
846 
847  for (int i=1; i<array_size; i++)
848  {
849  if (!checked[i]) { // wasn't needed
850  data = data_array[i];
851  if (clist.find(data->name)) {
852  if (flg == 0) {
853  flg = 1;
854  iout << iWARN
855  << "The following variables were set in the\n";
856  iout << iWARN
857  << "configuration file but will be ignored:\n" << endi;
858  }
859  iout << iWARN << " " << data->name;
860  if (data->parent_ptr != data_array[0]) {
861  iout << " (" << data->parent_ptr->name << ")";
862  }
863  iout << "\n" << endi;
864  }
865  }
866  }
867  }
868  // and now look for names which are in the config list but which
869  // are not in the parseoptions list
870  {
871  int flg = 0;
872  ConfigList::ConfigListNode const *ptr;
873  for (ptr = clist.head(); ptr != NULL; ptr = ptr -> next) {
874  if (!exists(ptr -> name)) {
875  if (flg == 0) {
876  flg = 1;
877  has_error = TRUE;
878  iout << iERROR
879  << "The following variables were set in the\n";
880  iout << iERROR
881  << "configuration file but are NOT VALID\n" << endi;
882  }
883  iout << iERROR << " " << ptr -> name << "\n" << endi;
884  }
885  }
886  }
887 
888  delete [] checked;
889  return !has_error;
890 }
#define FALSE
Definition: common.h:118
std::ostream & iWARN(std::ostream &s)
Definition: InfoStream.C:108
#define iout
Definition: InfoStream.h:87
static Units next(Units u)
Definition: ParseOptions.C:48
StringList * next
Definition: ConfigList.h:49
Index index
Definition: Parameters.C:148
char * data
Definition: ConfigList.h:48
std::ostream & iERROR(std::ostream &s)
Definition: InfoStream.C:109
infostream & endi(infostream &s)
Definition: InfoStream.C:38
#define TRUE
Definition: common.h:119
Bool exists(const char *name)
Definition: ParseOptions.C:904
Bool ParseOptions::units ( const char *  name,
Units  units 
)

Definition at line 1199 of file ParseOptions.C.

References endi(), FALSE, ParseOptions::DataElement::FLOAT, iERROR(), ParseOptions::DataElement::INT, iout, N_UNIT, TRUE, and ustring().

1200 {
1201  DataElement *tmp = internal_find(name);
1202  if (!tmp) {
1203  iout << iERROR
1204  << name << " not found so units not set" << "\n" << endi;
1205  return FALSE;
1206  }
1207  if ((tmp -> type == DataElement::INT && units != N_UNIT) ||
1208  (tmp -> type != DataElement::INT &&
1209  tmp -> type != DataElement::FLOAT)) {
1210  iout << iERROR
1211  << "Cannot set units '" << ustring(units) << "' for option '"
1212  << name << "'; wrong data type" << "\n" << endi;
1213  return FALSE;
1214  }
1215  tmp -> units = units;
1216  return TRUE;
1217 }
const char * ustring(Units u)
Definition: ParseOptions.C:42
Bool units(const char *name, Units units)
#define FALSE
Definition: common.h:118
#define iout
Definition: InfoStream.h:87
std::ostream & iERROR(std::ostream &s)
Definition: InfoStream.C:109
infostream & endi(infostream &s)
Definition: InfoStream.C:38
#define TRUE
Definition: common.h:119
Bool ParseOptions::units ( const char *  name,
Units units 
)

Definition at line 1219 of file ParseOptions.C.

References endi(), FALSE, ParseOptions::DataElement::FLOAT, iERROR(), ParseOptions::DataElement::INT, iout, N_UNIT, TRUE, and ParseOptions::DataElement::units.

1220 {
1221  DataElement *tmp = internal_find(name);
1222  *units = N_UNIT;
1223  if (!tmp) {
1224  iout << iERROR
1225  << "'" << name << "' doesn't exist so cannot get its units"
1226  << "\n" << endi;
1227  return FALSE;
1228  }
1229  if (tmp -> type != DataElement::INT &&
1230  tmp -> type != DataElement::FLOAT) {
1231  iout << iERROR
1232  << "Can only get units for FLOAT and INT variables, and '"
1233  << name << "' isn't one of those" << "\n" << endi;
1234  return FALSE;
1235  }
1236  *units = tmp->units;
1237  return TRUE;
1238 }
Bool units(const char *name, Units units)
#define FALSE
Definition: common.h:118
#define iout
Definition: InfoStream.h:87
std::ostream & iERROR(std::ostream &s)
Definition: InfoStream.C:109
infostream & endi(infostream &s)
Definition: InfoStream.C:38
#define TRUE
Definition: common.h:119

The documentation for this class was generated from the following files: