Pol  Revision:3cfda13
cfgfile.h
Go to the documentation of this file.
1 
9 #ifndef CLIB_CFGFILE_H
10 #define CLIB_CFGFILE_H
11 
12 #define CFGFILE_USES_IOSTREAMS 0
13 
14 #if CFGFILE_USES_IOSTREAMS
15 #include <fstream>
16 #else
17 #include <stdio.h>
18 #endif
19 
20 #include <set>
21 #include <string>
22 #include <time.h>
23 #include <vector>
24 
25 #include "compilerspecifics.h"
26 #include "maputil.h"
27 
28 namespace Pol
29 {
30 namespace Clib
31 {
32 class ConfigElem;
33 class ConfigElemBase;
34 class VectorConfigElem;
35 
37 {
38 public:
39  virtual ~ConfigSource() {}
40  virtual void display_error( const std::string& msg, bool show_curline = true,
41  const ConfigElemBase* elem = NULL, bool error = true ) const = 0;
42 };
43 
44 class ConfigFile : public ConfigSource
45 {
46 public:
47  explicit ConfigFile( const char* filename = NULL, const char* allowed_types = NULL );
48  explicit ConfigFile( const std::string& filename, const char* allowed_types = NULL );
49  virtual ~ConfigFile();
50 
51  void open( const char* i_filename );
52 
53  bool read( ConfigElem& elem ); // true=got one, false=end of file
54  void readraw( ConfigElem& elem ); // reads 0 or more properties
55 
56  const std::string& filename() const;
57  time_t modified() const;
58  unsigned element_line_start() const;
59 
60 
61 protected:
62  void init( const char* i_filename, const char* allowed_types_str );
63  friend class ConfigElem;
64  friend class ConfigSection;
65  bool readline( std::string& strbuf );
66  bool read_properties( ConfigElem& elem );
67  bool read_properties( VectorConfigElem& elem );
68  bool _read( ConfigElem& elem );
69  bool _read( VectorConfigElem& elem );
70  virtual void display_error( const std::string& msg, bool show_curline = true,
71  const ConfigElemBase* elem = NULL,
72  bool error = true ) const POL_OVERRIDE;
73  POL_NORETURN void display_and_rethrow_exception();
74  void register_allowed_type( const char* allowed_type );
75 
76 private:
77  std::string _filename; // saved for exception reporting
78  time_t _modified; // used to detect modification
79 #if CFGFILE_USES_IOSTREAMS
80  std::ifstream ifs;
81 #else
82  FILE* fp;
83  static char buffer[1024];
84 #endif
85  int _element_line_start; // what line in the file did this elem start on?
86  int _cur_line;
87 
88  typedef std::set<std::string, ci_cmp_pred> AllowedTypesCont;
89  AllowedTypesCont allowed_types_;
90 };
91 
93 {
94 public:
95  virtual void display_error( const std::string& msg, bool show_curline, const ConfigElemBase* elem,
96  bool error ) const POL_OVERRIDE;
97 };
98 }
99 }
100 #endif
#define POL_OVERRIDE
virtual void display_error(const std::string &msg, bool show_curline=true, const ConfigElemBase *elem=NULL, bool error=true) const =0
STL namespace.
unsigned char buffer[10000]
Definition: UoToolMain.cpp:109
#define POL_NORETURN
virtual ~ConfigSource()
Definition: cfgfile.h:39
std::set< std::string, ci_cmp_pred > AllowedTypesCont
Definition: cfgfile.h:88
AllowedTypesCont allowed_types_
Definition: cfgfile.h:89
bool readline(Socket &sck, std::string &s, bool *timeout_exit, unsigned int timeout_secs, unsigned maxlen)
Definition: sckutil.cpp:17
Definition: berror.cpp:12