parser.h

Go to the documentation of this file.
00001 /* Configuration parser.
00002  *
00003  * PegSoft configuration parser library (c) 2004 PegSoft
00004  * Contact us at pegsoft@pegsoft.net
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License version 2 as
00008  * published by the Free Software Foundation.
00009  *
00010  * This program is distributed in the hope that it will be useful, but
00011  * WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this software (the COPYING file); if not, write to the
00017  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
00018  * USA.
00019  *
00020  */
00021 
00027 #ifndef CONF_PARSER_H
00028 #define CONF_PARSER_H
00029 
00030 /*************************************************************************/
00031 
00032 #include <iosfwd>
00033 #include <map>
00034 #include <string>
00035 
00036 #include <misc/string.h>
00037 
00038 namespace conf
00039 {
00040 
00041 /*************************************************************************/
00042 
00043 class ConfDir;
00044 class ConfGlobal;
00045 class ConfLogger;
00046 
00047 /*************************************************************************/
00048 
00050 
00057 class Conf
00058 {
00059     public:
00060 #if DEBUG
00062         enum
00063         {
00064             dbgParse = 1,
00065             dbgSubstitution = 2,
00066             dbgProcessLow = 3,
00067             dbgProcess = 4,
00068             dbgUserDefined = 256
00069         };
00070 #endif
00071         
00073         typedef std::map<std::string, ConfDir *, misc::insensitive_less> DirList;
00074     
00076 
00082         Conf(ConfLogger &logger) : m_directives(), m_global(0),
00083                 m_logger(&logger), m_context(), m_line(0), m_ready(false),
00084                 m_reloading(false) { }
00086         Conf(Conf const &conf) : m_directives(conf.m_directives),
00087                 m_global(conf.m_global), m_logger(conf.m_logger),
00088                 m_context(conf.m_context), m_line(conf.m_line),
00089                 m_ready(conf.m_ready), m_reloading(conf.m_reloading) { }
00091         ~Conf() { };
00093         Conf &operator=(Conf const &right);
00094         
00096 
00109         bool add(ConfDir &dir);
00111 
00117         bool remove(ConfDir &dir);
00119 
00125         ConfDir *find(std::string const &name);
00126         
00128 
00151         bool begin(ConfGlobal &global, bool reload);
00153 
00161         bool parse_file(std::string const &filename);
00163 
00171         bool parse_string(std::string const &str, std::string const &context);
00173 
00182         bool parse(std::istream &is, std::string const &context);
00184 
00195         bool end();
00196         
00198 
00203         void warning(std::string const &msg) const;
00205         void warningf(char const *format, ...) const FORMAT(printf,2,3);
00206         
00208 
00213         void error(std::string const &err) const;
00215         void errorf(char const *format, ...) const FORMAT(printf,2,3);
00216 #if DEBUG
00218 
00235         void debug(int type, std::string const &msg) const;
00237         void debugf(int type, char const *format, ...) const 
00238                 FORMAT(printf,3,4);
00239 #endif
00240 
00242         DirList const &directives() const { return m_directives; }
00244 
00249         ConfGlobal &global() const { return *m_global; }
00251 
00254         ConfLogger &logger() const { return *m_logger; }
00256 
00261         std::string const &context() const { return m_context; }
00263         char const *c_context() const { return m_context.c_str(); }
00265 
00270         unsigned int line() const { return m_line; }
00272         bool ready() const { return m_ready; }
00274         bool reloading() const { return m_reloading; }
00275     private:
00276         DirList m_directives;
00277         ConfGlobal *m_global;
00278         ConfLogger *m_logger;
00279     
00280         std::string m_context;
00281         unsigned int m_line;
00282         
00283         bool m_ready;
00284         bool m_reloading;
00285         
00286         bool parse_real(std::istream &is, std::string const &context);
00287 #if DEBUG
00288         void print_directive(unsigned int count,
00289                 std::string const params[]) const;
00290 #endif
00291         bool process_directive(unsigned int &count, std::string params[]);
00292 };
00293 
00294 /*************************************************************************/
00295 
00297 
00305 class ConfLogger
00306 {
00307     public:
00309         virtual ~ConfLogger() { }
00310         
00312         virtual void warning(std::string const &context, unsigned int line,
00313                 std::string const &msg) = 0;
00315         virtual void error(std::string const &context, unsigned int line,
00316                 std::string const &error) = 0;
00317 #if DEBUG
00319         virtual void debug(std::string const &context, unsigned int line,
00320                 int type, std::string const &msg) = 0;
00321 #endif
00322 };
00323 
00324 /*************************************************************************/
00325 
00327 
00328 class ConfStdLogger : public ConfLogger
00329 {
00330     public:
00332         virtual void warning(std::string const &context, unsigned int line,
00333                 std::string const &msg);
00335         virtual void error(std::string const &context, unsigned int line,
00336                 std::string const &error);
00337 #if DEBUG
00339         virtual void debug(std::string const &context, unsigned int line,
00340                 int type, std::string const &msg);
00341 #endif
00342 };
00343 
00344 /*************************************************************************/
00345 
00346 inline Conf &Conf::operator=(Conf const &right)
00347 {
00348     if (&right != this) 
00349     {
00350         m_directives = right.m_directives;
00351         m_global = right.m_global;
00352         m_logger = right.m_logger;
00353         m_context = right.m_context;
00354         m_line = right.m_line;
00355         m_ready = right.m_ready;
00356         m_reloading = right.m_reloading;
00357     }
00358     
00359     return *this;
00360 }
00361 
00362 /*************************************************************************/
00363 
00364 } /* namespace conf */
00365 
00366 /*************************************************************************/
00367 
00368 #endif /* CONF_PARSER_H */

Generated on Fri Apr 18 22:03:27 2008 for Epona API by  doxygen 1.5.3