8#include <xercesc/dom/DOMDocument.hpp>
16#define FATAL_MACRO(output) std::cerr << output;throw(IFileException(output))
22XERCES_CPP_NAMESPACE_USE
31 for (iterator section_map=begin(); section_map!=end(); ++section_map) {
33 out <<
"\n[" << (*section_map).first <<
"]\n";
35 for( IFile_Section::iterator item_map = section.begin();
36 item_map != section.end(); ++item_map){
38 out << (*item_map).first <<
" = " << (item.
mystring()) <<
"\n";
48 while ( *str1 && *str2 && toupper(*str1)==toupper(*str2) )
54 return (toupper(*str1) - toupper(*str2));
64 if (*str2==
' ' || *str2==
'\t')
74 while ( *str2 && (*str2==
' ' || *str2==
'\t'))
81 str1 += strlen (str1);
83 do str1--;
while (*str1==
' ' || *str1==
'\t');
92 iterator it = begin();
94 delete (*it++).second;
100 iterator it = begin();
102 delete (*it++).second;
111 std::cerr <<
"Attempt to construct IFile from null DOMDocument"
125 std::cerr <<
"Attempt to construct IFile from null DOMDocument"
144 std::string filenameStr = filename;
145 Util::expandEnvVar(&filenameStr);
149 DOMDocument* doc = parser.
parse(filenameStr.c_str());
153 std::cerr <<
"Attempt to construct IFile from null DOMDocument"
165 void IFile::domToIni(
const DOMDocument* doc) {
166 DOMElement*
root = doc->getDocumentElement();
172 void IFile::domToIni(
const DOMElement*
root) {
176 std::vector<DOMElement*> sections;
178 unsigned int nChild = sections.size();
180 for (
unsigned int iChild = 0; iChild < nChild; iChild++) {
181 addSection(sections[iChild]);
186 void IFile::addSection(
const DOMElement* section) {
189 if (tagName.compare(
"section") ) {
190 std::string errorString =
191 "Expecting tagName==section, found " + tagName;
198 (*this)[curSection->title()]=curSection;
200 std::vector<DOMElement*> children;
204 unsigned int nChild = children.size();
205 for (
unsigned int iChild = 0; iChild < nChild; iChild++) {
206 DOMElement* child = children[iChild];
208 if (!(tagName.compare(
"section")) ) {
211 else if (!(tagName.compare(
"item")) ) {
219 (*curSection)[newItem->title()]= newItem;
222 std::string errorString =
"unexpected tag in initialization:"
233 return (IFile::_getstring (section, item, 0) != 0);
238 const char *IFile::_getstring(
const char *sectionname,
const char *itemname,
241 char hitem[1000], hsection[1000];
248 const_iterator entry = find(std::string(hsection));
250 if (entry != end() ) {
251 section = (*entry).second;
253 IFile_Section::const_iterator it = section->find(std::string(hitem));
254 item = (it != section->end() ) ?item = (*it).second : 0;
260 INFO (
"getstring: [" << hsection <<
"]" << hitem <<
": ->" <<
261 (item->string()) <<
"<-");
268 std::string errorString =
269 std::string(
"cannot find section [") + sectionname +
"]";
273 std::string errorString =
274 std::string(
"cannot find item '") + itemname +
"' in section ["
288 return _getstring(section, item);
294 const char* newString)
296 char hitem[1000], hsection[1000];
303 iterator it = find(std::string(hsection));
306 section = (*it).second;
308 if (section->contains(hitem) )
309 item = section->lookUp(hitem);
312 if (item) item->
mystring()=newString;
319 std::string hilf (IFile::_getstring (section, item));
325 std::cerr << (
"from xmlBase::IFile::getDouble ") << std::endl;
327 std::cerr << ex.
getMsg() << std::endl;
337 std::string hilf (IFile::_getstring (section, item));
343 std::cerr << (
"from xmlBase::IFile::getInt ") << std::endl;
344 std::cerr << ex.
getMsg() << std::endl;
353 std::string hilf (IFile::_getstring (section, item));
357 else if (hilf ==
"true")
359 else if (hilf ==
"1")
361 else if (hilf ==
"no")
363 else if (hilf ==
"false")
365 else if (hilf ==
"0")
368 std::string errorString(
"[");
369 errorString += section + std::string(
"]") + item +
" = \'" + hilf
370 +
"\' is not boolean";
383 strncpy(buffer, IFile::_getstring(section,item),
sizeof(buffer) -1);
384 if ( strlen(buffer) >=
sizeof(buffer) ) {
385 FATAL_MACRO(
"string returned from _getstring is too long");
389 char *vString = strtok(buffer,
"}");
390 vString = strtok(buffer,
"{");
392 char *
test = strtok(vString,
",");
393 while (
test != NULL) {
394 iv.push_back(atoi(
test));
395 test = strtok((
char*)NULL,
",");
397 if (iv.size() <= 0) {
398 std::string hilf (buffer);
399 std::string errorString(
"[");
400 errorString += section + std::string(
"]") + item +
" = \'"
401 + hilf +
"\' is not an integer vector";
415 strncpy(buffer, IFile::_getstring(section,item),
sizeof(buffer) );
416 if (strlen(buffer) >=
sizeof(buffer) ) {
420 char *vString = strtok(buffer,
"}");
421 vString = strtok(buffer,
"{");
423 char *
test = strtok(vString,
",");
424 while (
test != NULL) {
425 dv.push_back(atof(
test));
426 test = strtok((
char*)NULL,
",");
428 if (dv.size() <= 0) {
429 std::string hilf (buffer);
430 std::string errorString(
"[");
431 errorString += section + std::string(
"]") + item +
" = \'"
432 + hilf +
"\' is not an double vector";
441 return (
contains(section, item) )?
getInt( section, item ):defValue;
455 const char *defValue) {
#define FATAL_MACRO(output)
static double stringToDouble(const std::string &InStr)
static int stringToInt(const std::string &InStr)
Exception class used when converting from string to numeric type.
static std::string getTagName(const DOMElement *node)
static void getChildrenByTagName(const DOMElement *parent, const std::string &tagName, std::vector< DOMElement * > &children, bool clear=true)
static std::string getAttribute(const DOMElement *elt, const char *attName)
virtual int getInt(const char *section, const char *item)
std::vector< double > doubleVector
void setString(const char *section, const char *item, const char *newString)
virtual intVector getIntVector(const char *section, const char *item)
std::vector< int > intVector
virtual bool contains(const char *section, const char *item)
static void stripBlanks(char *str1, const char *str2, int flags)
virtual doubleVector getDoubleVector(const char *section, const char *item)
friend class IFile_Section
virtual const char * getString(const char *section, const char *item)
virtual double getDouble(const char *section, const char *item)
virtual void printOn(std::ostream &out=std::cout)
static int stricmp(const char *str1, const char *str2)
virtual int getBool(const char *section, const char *item)
DOMDocument * parse(const char *const filename, const std::string &docType=std::string(""))
Parse an xml file, returning document node if successful.
void doSchema(bool doit)
Call this method to turn on schema processing (else it's off)