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