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 using std::string; 00038 00039 class Configuration 00040 { 00041 public: 00042 00043 struct Item 00044 { 00045 string Value; 00046 string Tag; 00047 Item *Parent; 00048 Item *Child; 00049 Item *Next; 00050 00051 string FullTag(const Item *Stop = 0) const; 00052 00053 Item() : Parent(0), Child(0), Next(0) {}; 00054 }; 00055 00056 private: 00057 00058 Item *Root; 00059 bool ToFree; 00060 00061 Item *Lookup(Item *Head,const char *S,unsigned long const &Len,bool const &Create); 00062 Item *Lookup(const char *Name,const bool &Create); 00063 inline const Item *Lookup(const char *Name) const 00064 { 00065 return ((Configuration *)this)->Lookup(Name,false); 00066 } 00067 00068 public: 00069 00070 string Find(const char *Name,const char *Default = 0) const; 00071 string Find(string const &Name,const char *Default = 0) const {return Find(Name.c_str(),Default);}; 00072 string Find(string const &Name, string const &Default) const {return Find(Name.c_str(),Default.c_str());}; 00073 string FindFile(const char *Name,const char *Default = 0) const; 00074 string FindDir(const char *Name,const char *Default = 0) const; 00075 std::vector<string> FindVector(const char *Name) const; 00076 std::vector<string> FindVector(string const &Name) const { return FindVector(Name.c_str()); }; 00077 int FindI(const char *Name,int const &Default = 0) const; 00078 int FindI(string const &Name,int const &Default = 0) const {return FindI(Name.c_str(),Default);}; 00079 bool FindB(const char *Name,bool const &Default = false) const; 00080 bool FindB(string const &Name,bool const &Default = false) const {return FindB(Name.c_str(),Default);}; 00081 string FindAny(const char *Name,const char *Default = 0) const; 00082 00083 inline void Set(const string &Name,const string &Value) {Set(Name.c_str(),Value);}; 00084 void CndSet(const char *Name,const string &Value); 00085 void Set(const char *Name,const string &Value); 00086 void Set(const char *Name,const int &Value); 00087 00088 inline bool Exists(const string &Name) const {return Exists(Name.c_str());}; 00089 bool Exists(const char *Name) const; 00090 bool ExistsAny(const char *Name) const; 00091 00092 // clear a whole tree 00093 void Clear(const string &Name); 00094 00095 // remove a certain value from a list (e.g. the list of "APT::Keep-Fds") 00096 void Clear(string const &List, string const &Value); 00097 void Clear(string const &List, int const &Value); 00098 00099 inline const Item *Tree(const char *Name) const {return Lookup(Name);}; 00100 00101 inline void Dump() { Dump(std::clog); }; 00102 void Dump(std::ostream& str); 00103 00104 Configuration(const Item *Root); 00105 Configuration(); 00106 ~Configuration(); 00107 00109 class MatchAgainstConfig 00110 { 00111 std::vector<regex_t *> patterns; 00112 void clearPatterns(); 00113 00114 public: 00115 MatchAgainstConfig(char const * Config); 00116 virtual ~MatchAgainstConfig(); 00117 00119 bool Match(char const * str) const; 00120 bool Match(std::string const &str) const { return Match(str.c_str()); }; 00121 00123 bool wasConstructedSuccessfully() const { return patterns.empty() == false; } 00124 }; 00125 }; 00126 00127 extern Configuration *_config; 00128 00129 bool ReadConfigFile(Configuration &Conf,const string &FName, 00130 bool const &AsSectional = false, 00131 unsigned const &Depth = 0); 00132 00133 bool ReadConfigDir(Configuration &Conf,const string &Dir, 00134 bool const &AsSectional = false, 00135 unsigned const &Depth = 0); 00136 00137 #endif