xmlBase::IFile Class Reference

#include <IFile.h>

List of all members.

Public Types

typedef std::vector< int > intVector
typedef std::vector< double > doubleVector

Public Member Functions

 IFile (const char *filename)
 IFile (const DOMDocument *instrument)
 IFile (const DOMElement *instrument)
virtual ~IFile ()
virtual bool contains (const char *section, const char *item)
virtual int getInt (const char *section, const char *item)
virtual double getDouble (const char *section, const char *item)
virtual int getBool (const char *section, const char *item)
virtual const char * getString (const char *section, const char *item)
virtual intVector getIntVector (const char *section, const char *item)
virtual doubleVector getDoubleVector (const char *section, const char *item)
virtual int getInt (const char *section, const char *item, int defValue)
virtual double getDouble (const char *section, const char *item, double defValue)
virtual int getBool (const char *section, const char *item, int defValue)
virtual const char * getString (const char *section, const char *item, const char *defValue)
virtual intVector getIntVector (const char *section, const char *item, intVector defValues)
virtual doubleVector getDoubleVector (const char *section, const char *item, doubleVector defValues)
void setString (const char *section, const char *item, const char *newString)
void print ()
virtual void printOn (std::ostream &out=std::cout)

Protected Member Functions

 IFile ()

Static Protected Member Functions

static void stripBlanks (char *str1, const char *str2, int flags)
static int stricmp (const char *str1, const char *str2)

Private Member Functions

void addSection (const DOMElement *elt)
void domToIni (const DOMDocument *doc)
void domToIni (const DOMElement *doc)
virtual const char * _getstring (const char *section, const char *item, int failIfNotFoundFlag=1)

Friends

class IFile_Section
class IFile_Item


Detailed Description

Definition at line 81 of file IFile.h.


Member Typedef Documentation

typedef std::vector<double> xmlBase::IFile::doubleVector

Definition at line 95 of file IFile.h.

typedef std::vector<int> xmlBase::IFile::intVector

Definition at line 94 of file IFile.h.


Constructor & Destructor Documentation

xmlBase::IFile::IFile ( const char *  filename  ) 

Definition at line 136 of file IFile.cxx.

References domToIni(), xmlBase::XmlParser::doSchema(), xmlBase::XmlParser::parse(), and deljobs::string.

00137     {   
00138       using facilities::Util;
00139 
00140       XmlParser parser;
00141       
00142       parser.doSchema(true);
00143 
00144       std::string filenameStr = filename;
00145       Util::expandEnvVar(&filenameStr);
00146 
00147       // What if this fails (e.g., file doesn't exist or is not 
00148       // well-formed)?? How to report it?
00149       DOMDocument* doc = parser.parse(filenameStr.c_str());
00150       
00151       // Check it's a good doc.  
00152       if (doc == 0) {
00153         std::cerr << "Attempt to construct IFile from null DOMDocument" 
00154                   << std::endl;
00155         std::cerr.flush();
00156         exit(1);
00157         //   FATAL_MACRO("Attempt to construct IFile from null DomDocument");
00158       }
00159       
00160       // If so, initialize IFile from it
00161       domToIni(doc);
00162     }

xmlBase::IFile::IFile ( const DOMDocument *  instrument  ) 

Definition at line 106 of file IFile.cxx.

References domToIni().

00107     {
00108       // check that argument is non-null
00109       if (doc == 0) {
00110         //  FATAL_MACRO("Attempt to construct IFile from null DOMDocument");
00111         std::cerr << "Attempt to construct IFile from null DOMDocument" 
00112                   << std::endl;
00113         std::cerr.flush();
00114         exit(1);
00115       }
00116       // If so, call service to do the actual work
00117       domToIni(doc);
00118     }

xmlBase::IFile::IFile ( const DOMElement *  instrument  ) 

Definition at line 120 of file IFile.cxx.

References domToIni().

00121     {
00122       // check that argument is non-null
00123       if (doc == 0) {
00124         //        FATAL_MACRO("Attempt to construct IFile from null DOMElement");
00125         std::cerr << "Attempt to construct IFile from null DOMDocument" 
00126                   << std::endl;
00127         std::cerr.flush();
00128         exit(1);
00129       }
00130       // If so, call service to do the actual work
00131       domToIni(doc);
00132     }

xmlBase::IFile::~IFile (  )  [virtual]

Definition at line 90 of file IFile.cxx.

References deljobs::end, and EvtCyclic3::second().

00091     {
00092       iterator it = begin();
00093       while (it != end())
00094         delete (*it++).second;
00095       
00096     }

xmlBase::IFile::IFile (  )  [inline, protected]

Definition at line 146 of file IFile.h.

00146 {};


Member Function Documentation

const char * xmlBase::IFile::_getstring ( const char *  section,
const char *  item,
int  failIfNotFoundFlag = 1 
) [private, virtual]

Definition at line 238 of file IFile.cxx.

References ALL, deljobs::end, FATAL_MACRO, Bes_Common::INFO, xmlBase::IFile_Item::mystring(), deljobs::string, and stripBlanks().

Referenced by contains(), getBool(), getDouble(), getDoubleVector(), getInt(), getIntVector(), and getString().

00240     {
00241       char     hitem[1000], hsection[1000];
00242       IFile_Item    *item = 0;
00243       IFile_Section *section =0;
00244       
00245       stripBlanks (hitem,    itemname,    ALL);
00246       stripBlanks (hsection, sectionname, ALL);
00247       
00248       const_iterator entry = find(std::string(hsection));
00249       
00250       if (entry  != end() )      {
00251         section = (*entry).second;
00252         
00253         IFile_Section::const_iterator it = section->find(std::string(hitem));
00254         item = (it != section->end() ) ?item = (*it).second :  0;
00255         
00256       }
00257       
00258       if (item != 0)   {
00259 #ifdef DEBUG
00260         INFO ("getstring: [" << hsection << "]" << hitem << ": ->" << 
00261               (item->string()) << "<-");
00262 #endif
00263         return item->mystring().c_str();
00264       }
00265       else if (failFlag)
00266       {
00267         if (section == 0) {
00268           std::string errorString =
00269             std::string("cannot find section [") + sectionname + "]";
00270           FATAL_MACRO (errorString);
00271         }
00272         else {
00273           std::string errorString =
00274             std::string("cannot find item '") + itemname +  "' in section [" 
00275             + sectionname + "]";
00276           FATAL_MACRO (errorString);
00277         }
00278         return 0;
00279       }
00280       
00281       else   return 0;
00282     }

void xmlBase::IFile::addSection ( const DOMElement *  elt  )  [private]

Definition at line 186 of file IFile.cxx.

References FATAL_MACRO, xmlBase::Dom::getAttribute(), xmlBase::Dom::getChildrenByTagName(), xmlBase::Dom::getTagName(), IFile_Item, IFile_Section, deljobs::string, xmlBase::IFile_Item::title(), and xmlBase::IFile_Section::title().

Referenced by domToIni().

00186                                                    {
00187     std::string tagName = Dom::getTagName(section);
00188 
00189     if (tagName.compare("section") ) {
00190       std::string errorString = 
00191         "Expecting tagName==section, found " +  tagName;
00192       FATAL_MACRO(errorString);
00193     }
00194     
00195     // start a section
00196     std::string sectName = Dom::getAttribute(section, "name");
00197     IFile_Section* curSection = new IFile_Section(sectName);
00198     (*this)[curSection->title()]=curSection;
00199     
00200     std::vector<DOMElement*> children;
00201 
00202     Dom::getChildrenByTagName(section, "*", children);
00203 
00204     unsigned int nChild = children.size();
00205     for (unsigned int iChild = 0; iChild < nChild; iChild++) {
00206       DOMElement* child = children[iChild];
00207       std::string tagName = Dom::getTagName(child);
00208       if (!(tagName.compare("section")) ) {
00209         addSection(child);
00210       }
00211       else if (!(tagName.compare("item")) ) {
00212         std::string  itemName = Dom::getAttribute(child, "name");
00213         std::string itemValue = Dom::getAttribute(child, "value");
00214         
00215         // Make the new item
00216         IFile_Item* newItem = 
00217           new IFile_Item(itemName, itemValue);
00218         // Add it to the section map
00219         (*curSection)[newItem->title()]= newItem;
00220       }
00221       else {
00222         std::string errorString = "unexpected tag in initialization:" 
00223           + tagName;
00224         FATAL_MACRO(errorString);
00225       }      // end if..else
00226     }     // end for
00227     
00228   }

bool xmlBase::IFile::contains ( const char *  section,
const char *  item 
) [virtual]

Definition at line 231 of file IFile.cxx.

References _getstring().

Referenced by getBool(), getDouble(), getDoubleVector(), getInt(), getIntVector(), getString(), and lookFor().

00232     {
00233       return (IFile::_getstring (section, item, 0) != 0);
00234     }

void xmlBase::IFile::domToIni ( const DOMElement *  doc  )  [private]

Definition at line 172 of file IFile.cxx.

References addSection(), and xmlBase::Dom::getChildrenByTagName().

00172                                              {
00173     // Done this way, any child elements which are *not* sections
00174     // will simply be ignored.  Another strategy would be to look 
00175     // at all children and complain if any are not sections
00176     std::vector<DOMElement*> sections;
00177     Dom::getChildrenByTagName(root, "section", sections);
00178     unsigned int nChild = sections.size();
00179 
00180     for (unsigned int iChild = 0; iChild < nChild; iChild++) {
00181       addSection(sections[iChild]);
00182     }
00183   }

void xmlBase::IFile::domToIni ( const DOMDocument *  doc  )  [private]

Definition at line 165 of file IFile.cxx.

References root.

Referenced by IFile().

00165                                              {
00166     DOMElement*  root = doc->getDocumentElement();
00167     
00168     // Now invoke element version to do the work
00169     domToIni(root);
00170   }        

int xmlBase::IFile::getBool ( const char *  section,
const char *  item,
int  defValue 
) [virtual]

Definition at line 444 of file IFile.cxx.

References contains(), and getBool().

00445     {
00446       return ( contains(section, item) )? getBool( section, item ):defValue;
00447     }

int xmlBase::IFile::getBool ( const char *  section,
const char *  item 
) [virtual]

Definition at line 351 of file IFile.cxx.

References _getstring(), FATAL_MACRO, and deljobs::string.

Referenced by getBool().

00352     {
00353       std::string hilf (IFile::_getstring (section, item));
00354       
00355       if      (hilf == "yes")
00356         return (1);
00357       else if (hilf == "true")
00358         return (1);
00359       else if (hilf == "1")
00360         return (1);
00361       else if (hilf == "no")
00362         return (0);
00363       else if (hilf == "false")
00364         return (0);
00365       else if (hilf == "0")
00366         return (0);
00367       else {
00368         std::string errorString("[");
00369         errorString += section + std::string("]") + item + " = \'" + hilf 
00370           + "\' is not boolean";
00371         FATAL_MACRO (errorString);
00372         return (0);
00373       }
00374     }

double xmlBase::IFile::getDouble ( const char *  section,
const char *  item,
double  defValue 
) [virtual]

Definition at line 449 of file IFile.cxx.

References contains(), and getDouble().

00450                                              {
00451     return ( contains(section, item) )? getDouble( section, item ):defValue;
00452   }

double xmlBase::IFile::getDouble ( const char *  section,
const char *  item 
) [virtual]

Definition at line 316 of file IFile.cxx.

References _getstring(), and deljobs::string.

Referenced by getDouble(), and lookFor().

00317     {
00318       //      double    hf;
00319       std::string hilf (IFile::_getstring (section, item));
00320 
00321       try  {
00322         return facilities::Util::stringToDouble(hilf);
00323       }
00324       catch (facilities::WrongType ex) {
00325         std::cerr << ("from xmlBase::IFile::getDouble  ") << std::endl;
00326           //        FATAL_MACRO
00327         std::cerr << ex.getMsg() << std::endl;
00328         throw(IFileException(" "));
00329       }
00330     }

IFile::doubleVector xmlBase::IFile::getDoubleVector ( const char *  section,
const char *  item,
doubleVector  defValues 
) [virtual]

Definition at line 465 of file IFile.cxx.

References contains(), and getDoubleVector().

00467                                                                        {
00468     return (contains(section, item))? getDoubleVector(section, item):defValues;
00469   }

IFile::doubleVector xmlBase::IFile::getDoubleVector ( const char *  section,
const char *  item 
) [virtual]

Definition at line 409 of file IFile.cxx.

References _getstring(), FATAL_MACRO, deljobs::string, and test.

Referenced by getDoubleVector().

00411     {
00412       doubleVector dv;
00413       char buffer[1024];
00414       
00415       strncpy(buffer, IFile::_getstring(section,item), sizeof(buffer) );
00416       if (strlen(buffer) >= sizeof(buffer) ) {
00417         FATAL_MACRO("string from _getstring() too long");
00418         return dv;
00419       }
00420       char *vString = strtok(buffer,"}");
00421       vString = strtok(buffer,"{");
00422       
00423       char *test = strtok(vString,",");
00424       while (test != NULL) {
00425         dv.push_back(atof(test));
00426         test = strtok((char*)NULL,",");
00427       }
00428       if (dv.size() <= 0) {
00429         std::string hilf (buffer);
00430         std::string errorString("[");
00431         errorString += section + std::string("]")  + item + " = \'"
00432                                + hilf + "\' is not an double vector";
00433         FATAL_MACRO (errorString);
00434       }
00435       return (dv);
00436     }

int xmlBase::IFile::getInt ( const char *  section,
const char *  item,
int  defValue 
) [virtual]

Definition at line 440 of file IFile.cxx.

References contains(), and getInt().

00440                                                                         {
00441     return ( contains(section, item) )? getInt( section, item ):defValue;
00442   }

int xmlBase::IFile::getInt ( const char *  section,
const char *  item 
) [virtual]

Definition at line 334 of file IFile.cxx.

References _getstring(), and deljobs::string.

Referenced by getInt(), and lookFor().

00335     {
00336       //      int       hf;
00337       std::string hilf (IFile::_getstring (section, item));
00338 
00339       try  {
00340         return facilities::Util::stringToInt(hilf);
00341       }
00342       catch (facilities::WrongType ex) {
00343         std::cerr << ("from xmlBase::IFile::getInt  ") << std::endl;
00344         std::cerr << ex.getMsg() << std::endl;
00345         throw(IFileException(" "));
00346       }
00347     }

IFile::intVector xmlBase::IFile::getIntVector ( const char *  section,
const char *  item,
intVector  defValues 
) [virtual]

Definition at line 459 of file IFile.cxx.

References contains(), and getIntVector().

00460                                                                 {
00461     return (contains(section, item))? getIntVector(section, item) 
00462       : defValues;
00463   }

IFile::intVector xmlBase::IFile::getIntVector ( const char *  section,
const char *  item 
) [virtual]

Definition at line 378 of file IFile.cxx.

References _getstring(), FATAL_MACRO, deljobs::string, and test.

Referenced by getIntVector().

00379     {
00380       intVector iv;
00381       char buffer[1024];
00382       
00383       strncpy(buffer, IFile::_getstring(section,item), sizeof(buffer) -1);
00384       if ( strlen(buffer) >= sizeof(buffer) ) {
00385         FATAL_MACRO("string returned from _getstring is too long");
00386         return iv;
00387       }
00388       
00389       char *vString = strtok(buffer,"}");
00390       vString = strtok(buffer,"{");
00391       
00392       char *test = strtok(vString,",");
00393       while (test != NULL) {
00394         iv.push_back(atoi(test));
00395         test = strtok((char*)NULL,",");
00396       }
00397       if (iv.size() <= 0) {
00398         std::string hilf (buffer);
00399         std::string errorString("[");
00400         errorString += section + std::string("]")  + item + " = \'"
00401           + hilf + "\' is not an integer vector";
00402         FATAL_MACRO (errorString);
00403 
00404       }
00405       return (iv);
00406     }

const char * xmlBase::IFile::getString ( const char *  section,
const char *  item,
const char *  defValue 
) [virtual]

Definition at line 454 of file IFile.cxx.

References contains(), and getString().

00455                                                       {
00456     return ( contains(section, item) )? getString( section, item ):defValue;
00457   }

const char * xmlBase::IFile::getString ( const char *  section,
const char *  item 
) [virtual]

Definition at line 286 of file IFile.cxx.

References _getstring().

Referenced by getString(), and lookFor().

00287     {
00288       return _getstring(section, item);
00289     }

void xmlBase::IFile::print (  ) 

Definition at line 43 of file IFile.cxx.

References printOn().

00043 {printOn(std::cout); std::cout.flush();}

void xmlBase::IFile::printOn ( std::ostream out = std::cout  )  [virtual]

Definition at line 30 of file IFile.cxx.

References deljobs::end.

Referenced by print().

00030                                       {
00031     for (iterator section_map=begin(); section_map!=end(); ++section_map) {
00032       IFile_Section& section = *(*section_map).second;
00033       out << "\n[" <<  (*section_map).first << "]\n";
00034       
00035       for( IFile_Section::iterator item_map = section.begin();
00036            item_map != section.end(); ++item_map){
00037         IFile_Item& item = *(*item_map).second;
00038         out << (*item_map).first << " = "  << (item.mystring()) << "\n";
00039       }
00040     }
00041   }

void xmlBase::IFile::setString ( const char *  section,
const char *  item,
const char *  newString 
)

Definition at line 293 of file IFile.cxx.

References ALL, xmlBase::IFile_Section::contains(), deljobs::end, xmlBase::IFile_Section::lookUp(), xmlBase::IFile_Item::mystring(), deljobs::string, and stripBlanks().

00295     {
00296       char     hitem[1000], hsection[1000];
00297       IFile_Item    *item =0;
00298       IFile_Section *section=0;
00299       
00300       stripBlanks (hitem,    itemname,    ALL);
00301       stripBlanks (hsection, sectionname, ALL);
00302       
00303       iterator it = find(std::string(hsection));
00304       
00305       if ( it != end() )      {
00306         section = (*it).second;
00307         
00308         if (section->contains(hitem) )
00309           item = section->lookUp(hitem);
00310       }
00311       
00312       if (item) item->mystring()=newString;
00313     }

int xmlBase::IFile::stricmp ( const char *  str1,
const char *  str2 
) [static, protected]

Definition at line 46 of file IFile.cxx.

00047     {
00048       while ( *str1  &&  *str2  &&  toupper(*str1)==toupper(*str2) )
00049       {
00050         str1++;
00051         str2++;
00052       }
00053       
00054       return (toupper(*str1) - toupper(*str2));
00055     }

void xmlBase::IFile::stripBlanks ( char *  str1,
const char *  str2,
int  flags 
) [static, protected]

Definition at line 58 of file IFile.cxx.

References ALL, LEADING, and TRAILING.

Referenced by _getstring(), and setString().

00059     {
00060       if (flags & ALL)
00061       {
00062         while ( *str2 )
00063         {
00064           if (*str2==' '  ||  *str2=='\t')
00065             str2++;
00066           else
00067             *str1++ = *str2++;
00068         }
00069         *str1=0;
00070       }
00071       else
00072       {
00073         if (flags & LEADING)
00074           while ( *str2  &&  (*str2==' '  ||  *str2=='\t'))
00075             str2++;
00076         
00077         strcpy (str1, str2);
00078         
00079         if (flags & TRAILING)
00080         {
00081           str1 += strlen (str1);
00082           
00083           do str1--; while (*str1==' '  ||  *str1=='\t');
00084           *++str1 = 0;
00085         }
00086       }
00087     }


Friends And Related Function Documentation

friend class IFile_Item [friend]

Definition at line 138 of file IFile.h.

Referenced by addSection().

friend class IFile_Section [friend]

check for and replace an environment variable within a string Soon to become obsolete -- maybe is by now

Definition at line 137 of file IFile.h.

Referenced by addSection().


Generated on Tue Nov 29 23:36:39 2016 for BOSS_7.0.2 by  doxygen 1.4.7