Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

IFile.h

Go to the documentation of this file.
00001 // $Header: /nfs/slac/g/glast/ground/cvs/xml/xml/IFile.h,v 1.4 2001/07/14 23:21:04 igable Exp $
00002 // adapted from Bonn, originally written by Ruediger Gross-Hardt
00003 // hardy@servax.iskp.uni-bonn.de
00004 
00005 // converted to use STL map
00006 
00007 // User: Suson      Date: 6/29/98
00008 // Added getIntVector, getDoubleVector
00009 
00010 #ifndef XMLIFile_H
00011 #define XMLIFile_H
00012 
00013 #include <cstdio>
00014 #include <iostream>
00015 
00016 #include <string>
00017 #include <map>
00018 #include <vector>
00019 
00020 class DOM_Element;
00021 class DOM_Document;
00022 
00023 namespace xml {
00024   class IFile_Item 
00025     {
00026       // basic item, should be private to IFile
00027       friend class IFile_Section;
00028       friend class IFile;
00029     public:
00030       IFile_Item(){};
00031       //  IFile_Item(const char *line, int lineNumber);
00032       IFile_Item(std::string name, std::string value):itemname(name), 
00033         itemstring(value){}
00034       std::string& title()  { return itemname;    };
00035       std::string& comment(){ return itemcomment; };
00036       std::string& mystring() { return itemstring;  };
00037       
00038     private:
00039       std::string itemname;
00040       std::string itemstring;
00041       std::string itemcomment;
00042     };
00043   
00044   
00045   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00046   class IFile_Section : 
00047     public std::map<std::string, IFile_Item*, std::less<std::string> > {
00048     // section, a map of items
00049     friend class IFile;
00050     public:
00051     IFile_Section(){};
00052     IFile_Section( std::string name):sectionname(name){}
00053     ~IFile_Section();
00054     std::string& title() { return sectionname; };
00055     private:
00056     
00057     bool contains(std::string name){ return find(name)!= end(); }
00058     
00059     IFile_Item* lookUp(std::string name){
00060       iterator it = find(name);
00061       return (it==end())? 0 : (*it).second;
00062     }
00063     std::string sectionname;
00064   };
00065   
00066   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00067   
00068   class IFile : 
00069     protected std::map<std::string, IFile_Section*, std::less<std::string> > 
00070     {
00071       // IFile is a map of sections
00072       
00073     public:
00074       IFile (const char *filename);
00075       IFile (const DOM_Document& instrument); 
00076       // or do we want document element rather than DOM_Document as arg?
00077       IFile (const DOM_Element& instrument);
00078       
00079       virtual ~IFile ();
00080       
00081       typedef std::vector<int> intVector;
00082       typedef std::vector<double> doubleVector;
00083       
00084       virtual bool   contains   (const char *section, const char *item);
00085       // check, whether section and item is contained in IFile
00086       
00087       virtual int           getInt        (const char *section, const char *item);
00088       virtual double  getDouble  (const char *section, const char *item);
00089       virtual int           getBool       (const char *section, const char *item);
00090       virtual const char *getString  (const char *section, const char *item);
00091       virtual intVector getIntVector  (const char *section, const char *item);
00092       virtual doubleVector getDoubleVector (const char *section, 
00093                                             const char *item);
00094       // get data from [section]item
00095       
00096       virtual int        getInt  (const char *section, const char *item, 
00097                               int defValue);
00098       virtual double getDouble (const char *section, const char *item, 
00099                                 double defValue);
00100       virtual int    getBool (const char *section, const char *item, 
00101                               int defValue);
00102       virtual const char *getString  (const char *section, const char *item, 
00103                                       const char *defValue);
00104       virtual intVector getIntVector (const char *section, const char *item, 
00105                                       intVector defValues);
00106       virtual doubleVector getDoubleVector (const char *section, 
00107                                             const char *item,
00108                                             doubleVector defValues);
00109       // get data from [section]item, providing a default value, if not present
00110       
00111       void setString(const char *section, const char *item, 
00112                      const char* newString);
00113       
00114       void print(); // print contents to cout
00115       //   virtual void  printOn    (HOStream& out = herr);
00116       virtual void        printOn         (std::ostream& out = std::cout );
00117       // print IFile on stream
00118       // IFile &     reread       (); to be implemented
00119       // reread data from file
00120 
00121       static void       extractEnvVar(std::string *fileStr);
00122       
00123     protected:
00124       friend class IFile_Section;
00125       friend class IFile_Item;
00126       
00127       static void       stripBlanks  (char *str1, const char *str2, int flags);
00128       static int        stricmp      (const char *str1, const char *str2);
00130 
00131 
00132       // If only init is via xml file or DOM tree, don't need these helper 
00133       // functions which were used to deal with old-style ini file directly.
00134       // static FILE *fopen          (const char *buffer);
00135       //  static int    getLine      (char *buffer, int len, FILE *fp);
00136       // static void    stripBlanks2 (char *str1, const char *str2);
00137       
00138       //  static void   setDebugLevel(int);  <-- has no definition
00139       // helper functions (some should be private/protected)
00140       
00141       IFile() {};
00142     private:
00143       void       addSection(const DOM_Element& elt);
00144       void       domToIni(const DOM_Document& doc);
00145       void       domToIni(const DOM_Element& doc);
00146       virtual const char *_getstring  (const char *section, const char *item,
00147                                        int failIfNotFoundFlag=1);
00148       // internal function, that does the work
00149     };
00150 }  // end namespace xml 
00151 #endif
00152 

Generated at Wed Nov 21 12:22:26 2001 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000