4#include "xmlBase/IFile.h"
5#include "facilities/Util.h"
6#include "xmlBase/Dom.h"
7#include "xmlBase/XmlParser.h"
8#include <xercesc/dom/DOMDocument.hpp>
16#define FATAL_MACRO( output ) \
17 std::cerr << output; \
18 throw( IFileException( output ) )
24 XERCES_CPP_NAMESPACE_USE
32 for ( iterator section_map = begin(); section_map != end(); ++section_map )
35 out <<
"\n[" << ( *section_map ).first <<
"]\n";
37 for ( IFile_Section::iterator item_map = section.begin(); item_map != section.end();
41 out << ( *item_map ).first <<
" = " << ( item.
mystring() ) <<
"\n";
53 while ( *str1 && *str2 && toupper( *str1 ) == toupper( *str2 ) )
59 return ( toupper( *str1 ) - toupper( *str2 ) );
68 if ( *str2 ==
' ' || *str2 ==
'\t' ) str2++;
69 else *str1++ = *str2++;
76 while ( *str2 && ( *str2 ==
' ' || *str2 ==
'\t' ) ) str2++;
82 str1 += strlen( str1 );
85 while ( *str1 ==
' ' || *str1 ==
'\t' );
93 iterator it = begin();
94 while ( it != end() )
delete ( *it++ ).second;
98 iterator it = begin();
99 while ( it != end() )
delete ( *it++ ).second;
108 std::cerr <<
"Attempt to construct IFile from null DOMDocument" << std::endl;
121 std::cerr <<
"Attempt to construct IFile from null DOMDocument" << std::endl;
137 std::string filenameStr = filename;
138 Util::expandEnvVar( &filenameStr );
142 DOMDocument* doc = parser.
parse( filenameStr.c_str() );
147 std::cerr <<
"Attempt to construct IFile from null DOMDocument" << std::endl;
158 void IFile::domToIni(
const DOMDocument* doc ) {
159 DOMElement*
root = doc->getDocumentElement();
165 void IFile::domToIni(
const DOMElement*
root ) {
169 std::vector<DOMElement*> sections;
171 unsigned int nChild = sections.size();
173 for (
unsigned int iChild = 0; iChild < nChild; iChild++ )
174 { addSection( sections[iChild] ); }
178 void IFile::addSection(
const DOMElement* section ) {
181 if ( tagName.compare(
"section" ) )
183 std::string errorString =
"Expecting tagName==section, found " + tagName;
190 ( *this )[curSection->title()] = curSection;
192 std::vector<DOMElement*> children;
196 unsigned int nChild = children.size();
197 for (
unsigned int iChild = 0; iChild < nChild; iChild++ )
199 DOMElement* child = children[iChild];
201 if ( !( tagName.compare(
"section" ) ) ) { addSection( child ); }
202 else if ( !( tagName.compare(
"item" ) ) )
210 ( *curSection )[newItem->title()] = newItem;
214 std::string errorString =
"unexpected tag in initialization:" + tagName;
222 return ( IFile::_getstring( section, item, 0 ) != 0 );
226 const char* IFile::_getstring(
const char* sectionname,
const char* itemname,
228 char hitem[1000], hsection[1000];
235 const_iterator entry = find( std::string( hsection ) );
237 if ( entry != end() )
239 section = ( *entry ).second;
241 IFile_Section::const_iterator it = section->find( std::string( hitem ) );
242 item = ( it != section->end() ) ? item = ( *it ).second : 0;
248 INFO(
"getstring: [" << hsection <<
"]" << hitem <<
": ->" << ( item->string() )
257 std::string errorString = std::string(
"cannot find section [" ) + sectionname +
"]";
262 std::string errorString = std::string(
"cannot find item '" ) + itemname +
263 "' in section [" + sectionname +
"]";
275 return _getstring( section, item );
281 const char* newString ) {
282 char hitem[1000], hsection[1000];
289 iterator it = find( std::string( hsection ) );
293 section = ( *it ).second;
295 if ( section->contains( hitem ) ) item = section->lookUp( hitem );
298 if ( item ) item->
mystring() = newString;
304 std::string hilf( IFile::_getstring( section, item ) );
309 std::cerr << (
"from xmlBase::IFile::getDouble " ) << std::endl;
311 std::cerr << ex.
getMsg() << std::endl;
319 std::string hilf( IFile::_getstring( section, item ) );
324 std::cerr << (
"from xmlBase::IFile::getInt " ) << std::endl;
325 std::cerr << ex.
getMsg() << std::endl;
332 std::string hilf( IFile::_getstring( section, item ) );
334 if ( hilf ==
"yes" )
return ( 1 );
335 else if ( hilf ==
"true" )
return ( 1 );
336 else if ( hilf ==
"1" )
return ( 1 );
337 else if ( hilf ==
"no" )
return ( 0 );
338 else if ( hilf ==
"false" )
return ( 0 );
339 else if ( hilf ==
"0" )
return ( 0 );
342 std::string errorString(
"[" );
344 section + std::string(
"]" ) + item +
" = \'" + hilf +
"\' is not boolean";
355 strncpy( buffer, IFile::_getstring( section, item ),
sizeof( buffer ) - 1 );
356 if ( strlen( buffer ) >=
sizeof( buffer ) )
358 FATAL_MACRO(
"string returned from _getstring is too long" );
362 char* vString = strtok( buffer,
"}" );
363 vString = strtok( buffer,
"{" );
365 char*
test = strtok( vString,
"," );
366 while (
test != NULL )
368 iv.push_back( atoi(
test ) );
369 test = strtok( (
char*)NULL,
"," );
371 if ( iv.size() <= 0 )
373 std::string hilf( buffer );
374 std::string errorString(
"[" );
376 section + std::string(
"]" ) + item +
" = \'" + hilf +
"\' is not an integer vector";
387 strncpy( buffer, IFile::_getstring( section, item ),
sizeof( buffer ) );
388 if ( strlen( buffer ) >=
sizeof( buffer ) )
390 FATAL_MACRO(
"string from _getstring() too long" );
393 char* vString = strtok( buffer,
"}" );
394 vString = strtok( buffer,
"{" );
396 char*
test = strtok( vString,
"," );
397 while (
test != NULL )
399 dv.push_back( atof(
test ) );
400 test = strtok( (
char*)NULL,
"," );
402 if ( dv.size() <= 0 )
404 std::string hilf( buffer );
405 std::string errorString(
"[" );
407 section + std::string(
"]" ) + item +
" = \'" + hilf +
"\' is not an double vector";
416 return (
contains( section, item ) ) ?
getInt( section, item ) : defValue;
420 return (
contains( section, item ) ) ?
getBool( section, item ) : defValue;
427 const char*
IFile::getString(
const char* section,
const char* item,
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)
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)
friend class IFile_Section
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).