/ src / libnml / inifile / inifile.hh
inifile.hh
  1  /********************************************************************
  2  * Description: inifile.hh
  3  *   Declarations for INI file format functions
  4  *
  5  *   Derived from a work by Fred Proctor & Will Shackleford
  6  *
  7  * Author:
  8  * License: GPL Version 2
  9  * System: Linux
 10  *    
 11  * Copyright (c) 2004 All rights reserved.
 12  *
 13  * Last change: 
 14  ********************************************************************/
 15  
 16  #ifndef INIFILE_HH
 17  #define INIFILE_HH
 18  
 19  #include <inifile.h>
 20  #include <string>
 21  #include <boost/lexical_cast.hpp>
 22  
 23  #ifndef __cplusplus
 24  #warning Inclusion of <inifile.hh> from C programs is deprecated.  Include <inifile.h> instead.
 25  #endif
 26  
 27  #ifdef __cplusplus
 28  #include <fcntl.h>
 29  class IniFile {
 30  public:
 31      typedef enum {
 32          ERR_NONE                = 0x00,
 33          ERR_NOT_OPEN            = 0x01,
 34          ERR_SECTION_NOT_FOUND   = 0x02,
 35          ERR_TAG_NOT_FOUND       = 0x04,
 36          ERR_CONVERSION          = 0x08,
 37          ERR_LIMITS              = 0x10,
 38          ERR_OVER_EXTENDED       = 0x20,
 39      } ErrorCode;
 40  
 41      class Exception {
 42      public:
 43          ErrorCode               errCode;
 44          const char *            tag;
 45          const char *            section;
 46          int                     num;
 47          unsigned int            lineNo;
 48  
 49          void                    Print(FILE *fp=stderr);
 50      };
 51  
 52  
 53                                  IniFile(int errMask=0, FILE *fp=NULL);
 54                                  ~IniFile(void){ Close(); }
 55  
 56      bool                        Open(const char *file);
 57      bool                        Close(void);
 58      bool                        IsOpen(void){ return(fp != NULL); }
 59  
 60      const char *                Find(const char *tag, const char *section=NULL,
 61                                       int num = 1, int *lineno = NULL);
 62  
 63      template<class T>
 64      ErrorCode                   Find(T *result, T min, T max,
 65                                       const char *tag,const char *section,
 66                                       int num=1) {
 67          ErrorCode errCode;
 68          T tmp;
 69          if((errCode = Find(&tmp, tag, section, num)) != ERR_NONE)
 70              return(errCode);
 71  
 72          if((tmp > max) || (tmp < min)) {
 73              ThrowException(ERR_LIMITS);
 74              return(ERR_LIMITS);
 75          }
 76  
 77          *result = tmp;
 78  
 79          return(ERR_NONE);
 80      }
 81  
 82      template<class T>
 83      ErrorCode                   Find(T *result,
 84                                       const char *tag,const char *section,
 85                                       int num=1) {
 86          ErrorCode errCode;
 87          std::string tmp;
 88          if((errCode = Find(&tmp, tag, section, num)) != ERR_NONE)
 89              return(errCode);
 90  
 91          try {
 92              *result = boost::lexical_cast<T>(tmp);
 93          } catch (boost::bad_lexical_cast &) {
 94              ThrowException(ERR_CONVERSION);
 95              return(ERR_CONVERSION);
 96          }
 97  
 98          return(ERR_NONE);
 99      }
100  
101      ErrorCode                   Find(std::string *s,
102                                       const char *tag,const char *section,
103                                       int num=1) {
104          const char *tmp = Find(tag, section, num);
105          if(!tmp)
106              return ERR_TAG_NOT_FOUND; // can't distinguish errors, ugh
107  
108          *s = tmp;
109  
110          return(ERR_NONE);
111      }
112  
113      const char *                FindString(char *dest, size_t n,
114  				     const char *tag, const char *section=NULL,
115  				     int num = 1, int *lineno = NULL);
116      const char *                FindPath(char *dest, size_t n,
117  				     const char *tag, const char *section=NULL,
118  				     int num = 1, int *lineno = NULL);
119      void                        EnableExceptions(int _errMask){
120                                      errMask = _errMask;
121                                  }
122  
123      ErrorCode                   TildeExpansion(const char *file, char *path,
124  					       size_t n);
125  
126  protected:
127      struct StrIntPair {
128          const char             *pStr;
129          int                     value;
130      };
131  
132      struct StrDoublePair {
133          const char              *pStr;
134          double                   value;
135      };
136  
137  
138      ErrorCode                   Find(double *result, StrDoublePair *,
139                                       const char *tag, const char *section=NULL,
140                                       int num = 1, int *lineno = NULL);
141      ErrorCode                   Find(int *result, StrIntPair *,
142                                       const char *tag, const char *section=NULL,
143                                       int num = 1, int *lineno = NULL);
144  
145  
146  private:
147      FILE                        *fp;
148      struct flock                lock;
149      bool                        owned;
150  
151      Exception                   exception;
152      int                         errMask;
153  
154      unsigned int                lineNo;
155      const char *                tag;
156      const char *                section;
157      int                         num;
158  
159      bool                        CheckIfOpen(void);
160      bool                        LockFile(void);
161      void                        ThrowException(ErrorCode);
162      char                        *AfterEqual(const char *string);
163      char                        *SkipWhite(const char *string);
164  };
165  #endif
166  
167  
168  #endif