|
apt @VERSION@
|
00001 // -*- mode: cpp; mode: fold -*- 00002 // Description /*{{{*/ 00003 // $Id: configuration.h,v 1.16 2002/11/11 06:55:50 doogie Exp $ 00004 /* ###################################################################### 00005 00006 Configuration Class 00007 00008 This class provides a configuration file and command line parser 00009 for a tree-oriented configuration environment. All runtime configuration 00010 is stored in here. 00011 00012 Each configuration name is given as a fully scoped string such as 00013 Foo::Bar 00014 And has associated with it a text string. The Configuration class only 00015 provides storage and lookup for this tree, other classes provide 00016 configuration file formats (and parsers/emitters if needed). 00017 00018 Most things can get by quite happily with, 00019 cout << _config->Find("Foo::Bar") << endl; 00020 00021 A special extension, support for ordered lists is provided by using the 00022 special syntax, "block::list::" the trailing :: designates the 00023 item as a list. To access the list you must use the tree function on 00024 "block::list". 00025 00026 ##################################################################### */ 00027 /*}}}*/ 00028 #ifndef PKGLIB_CONFIGURATION_H 00029 #define PKGLIB_CONFIGURATION_H 00030 00031 #include <regex.h> 00032 00033 #include <string> 00034 #include <vector> 00035 #include <iostream> 00036 00037 class Configuration 00038 { 00039 public: 00040 00041 struct Item 00042 { 00043 std::string Value; 00044 std::string Tag; 00045 Item *Parent; 00046 Item *Child; 00047 Item *Next; 00048 00049 std::string FullTag(const Item *Stop = 0) const; 00050 00051 Item() : Parent(0), Child(0), Next(0) {}; 00052 }; 00053 00054 private: 00055 00056 Item *Root; 00057 bool ToFree; 00058 00059 Item *Lookup(Item *Head,const char *S,unsigned long const &Len,bool const &Create); 00060 Item *Lookup(const char *Name,const bool &Create); 00061 inline const Item *Lookup(const char *Name) const 00062 { 00063 return ((Configuration *)this)->Lookup(Name,false); 00064 } 00065 00066 public: 00067 00068 std::string Find(const char *Name,const char *Default = 0) const; 00069 std::string Find(std::string const &Name,const char *Default = 0) const {return Find(Name.c_str(),Default);}; 00070 std::string Find(std::string const &Name, std::string const &Default) const {return Find(Name.c_str(),Default.c_str());}; 00071 std::string FindFile(const char *Name,const char *Default = 0) const; 00072 std::string FindDir(const char *Name,const char *Default = 0) const; 00073 std::vector<std::string> FindVector(const char *Name) const; 00074 std::vector<std::string> FindVector(std::string const &Name) const { return FindVector(Name.c_str()); }; 00075 int FindI(const char *Name,int const &Default = 0) const; 00076 int FindI(std::string const &Name,int const &Default = 0) const {return FindI(Name.c_str(),Default);}; 00077 bool FindB(const char *Name,bool const &Default = false) const; 00078 bool FindB(std::string const &Name,bool const &Default = false) const {return FindB(Name.c_str(),Default);}; 00079 std::string FindAny(const char *Name,const char *Default = 0) const; 00080 00081 inline void Set(const std::string &Name,const std::string &Value) {Set(Name.c_str(),Value);}; 00082 void CndSet(const char *Name,const std::string &Value); 00083 void CndSet(const char *Name,const int Value); 00084 void Set(const char *Name,const std::string &Value); 00085 void Set(const char *Name,const int &Value); 00086 00087 inline bool Exists(const std::string &Name) const {return Exists(Name.c_str());}; 00088 bool Exists(const char *Name) const; 00089 bool ExistsAny(const char *Name) const; 00090 00091 // clear a whole tree 00092 void Clear(const std::string &Name); 00093 00094 // remove a certain value from a list (e.g. the list of "APT::Keep-Fds") 00095 void Clear(std::string const &List, std::string const &Value); 00096 void Clear(std::string const &List, int const &Value); 00097 00098 inline const Item *Tree(const char *Name) const {return Lookup(Name);}; 00099 00100 inline void Dump() { Dump(std::clog); }; 00101 void Dump(std::ostream& str); 00102 00103 Configuration(const Item *Root); 00104 Configuration(); 00105 ~Configuration(); 00106 00108 class MatchAgainstConfig 00109 { 00110 std::vector<regex_t *> patterns; 00111 void clearPatterns(); 00112 00113 public: 00114 MatchAgainstConfig(char const * Config); 00115 virtual ~MatchAgainstConfig(); 00116 00118 bool Match(char const * str) const; 00119 bool Match(std::string const &str) const { return Match(str.c_str()); }; 00120 00122 bool wasConstructedSuccessfully() const { return patterns.empty() == false; } 00123 }; 00124 }; 00125 00126 extern Configuration *_config; 00127 00128 bool ReadConfigFile(Configuration &Conf,const std::string &FName, 00129 bool const &AsSectional = false, 00130 unsigned const &Depth = 0); 00131 00132 bool ReadConfigDir(Configuration &Conf,const std::string &Dir, 00133 bool const &AsSectional = false, 00134 unsigned const &Depth = 0); 00135 00136 #endif
1.7.4