apt  2.2.4
About: Apt (Advanced Package Tool) is a management system for software packages (Debian/Ubuntu). Release series 2.2.
  Fossies Dox: apt-2.2.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

configuration.h
Go to the documentation of this file.
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* ######################################################################
4 
5  Configuration Class
6 
7  This class provides a configuration file and command line parser
8  for a tree-oriented configuration environment. All runtime configuration
9  is stored in here.
10 
11  Each configuration name is given as a fully scoped string such as
12  Foo::Bar
13  And has associated with it a text string. The Configuration class only
14  provides storage and lookup for this tree, other classes provide
15  configuration file formats (and parsers/emitters if needed).
16 
17  Most things can get by quite happily with,
18  cout << _config->Find("Foo::Bar") << endl;
19 
20  A special extension, support for ordered lists is provided by using the
21  special syntax, "block::list::" the trailing :: designates the
22  item as a list. To access the list you must use the tree function on
23  "block::list".
24 
25  ##################################################################### */
26  /*}}}*/
27 #ifndef PKGLIB_CONFIGURATION_H
28 #define PKGLIB_CONFIGURATION_H
29 
30 #include <regex.h>
31 
32 #include <iostream>
33 #include <string>
34 #include <vector>
35 
36 #include <apt-pkg/macros.h>
37 
38 
40 {
41  public:
42 
43  struct Item
44  {
45  std::string Value;
46  std::string Tag;
50 
51  std::string FullTag(const Item *Stop = 0) const;
52 
53  Item() : Parent(0), Child(0), Next(0) {};
54  };
55 
56  private:
57 
59  bool ToFree;
60 
61  Item *Lookup(Item *Head,const char *S,unsigned long const &Len,bool const &Create);
62  Item *Lookup(const char *Name,const bool &Create);
63  inline const Item *Lookup(const char *Name) const
64  {
65  return const_cast<Configuration *>(this)->Lookup(Name,false);
66  }
67 
68  public:
69 
70  std::string Find(const char *Name,const char *Default = 0) const;
71  std::string Find(std::string const &Name,const char *Default = 0) const {return Find(Name.c_str(),Default);};
72  std::string Find(std::string const &Name, std::string const &Default) const {return Find(Name.c_str(),Default.c_str());};
73  std::string FindFile(const char *Name,const char *Default = 0) const;
74  std::string FindDir(const char *Name,const char *Default = 0) const;
75  /** return a list of child options
76  *
77  * Options like Acquire::Languages are handled as lists which
78  * can be overridden and have a default. For the later two a comma
79  * separated list of values is supported.
80  *
81  * \param Name of the parent node
82  * \param Default list of values separated by commas */
83  std::vector<std::string> FindVector(const char *Name, std::string const &Default = "", bool const Keys = false) const;
84  std::vector<std::string> FindVector(std::string const &Name, std::string const &Default = "", bool const Keys = false) const { return FindVector(Name.c_str(), Default, Keys); };
85 
86  int FindI(const char *Name,int const &Default = 0) const;
87  int FindI(std::string const &Name,int const &Default = 0) const {return FindI(Name.c_str(),Default);};
88  bool FindB(const char *Name,bool const &Default = false) const;
89  bool FindB(std::string const &Name,bool const &Default = false) const {return FindB(Name.c_str(),Default);};
90  std::string FindAny(const char *Name,const char *Default = 0) const;
91 
92  inline void Set(const std::string &Name,const std::string &Value) {Set(Name.c_str(),Value);};
93  void CndSet(const char *Name,const std::string &Value);
94  void CndSet(const char *Name,const int Value);
95  void Set(const char *Name,const std::string &Value);
96  void Set(const char *Name,const int &Value);
97 
98  inline bool Exists(const std::string &Name) const {return Exists(Name.c_str());};
99  bool Exists(const char *Name) const;
100  bool ExistsAny(const char *Name) const;
101 
102  void MoveSubTree(char const * const OldRoot, char const * const NewRoot);
103 
104  // clear a whole tree
105  void Clear(const std::string &Name);
106  void Clear();
107 
108  // remove a certain value from a list (e.g. the list of "APT::Keep-Fds")
109  void Clear(std::string const &List, std::string const &Value);
110  void Clear(std::string const &List, int const &Value);
111 
112  inline const Item *Tree(const char *Name) const {return Lookup(Name);};
113 
114  inline void Dump() { Dump(std::clog); };
115  void Dump(std::ostream& str);
116  void Dump(std::ostream& str, char const * const root,
117  char const * const format, bool const emptyValue);
118 
119  explicit Configuration(const Item *Root);
120  Configuration();
121  ~~Configuration();
122 
123  /** \brief match a string against a configurable list of patterns */
125  {
126  std::vector<regex_t *> patterns;
127  APT_HIDDEN void clearPatterns();
128 
129  public:
130  explicit MatchAgainstConfig(char const * Config);
131  virtual ~~MatchAgainstConfig();
132 
133  /** \brief Returns \b true for a string matching one of the patterns */
134  bool Match(char const * str) const;
135  bool Match(std::string const &str) const { return Match(str.c_str()); };
136 
137  /** \brief returns if the matcher setup was successful */
138  bool wasConstructedSuccessfully() const { return patterns.empty() == false; }
139  };
140 };
141 
143 
144 APT_PUBLIC bool ReadConfigFile(Configuration &Conf,const std::string &FName,
145  bool const &AsSectional = false,
146  unsigned const &Depth = 0);
147 
148 APT_PUBLIC bool ReadConfigDir(Configuration &Conf,const std::string &Dir,
149  bool const &AsSectional = false,
150  unsigned const &Depth = 0);
151 
152 #endif
match a string against a configurable list of patterns
bool Match(std::string const &str) const
bool wasConstructedSuccessfully() const
returns if the matcher setup was successful
std::vector< regex_t * > patterns
void Dump(std::ostream &str, char const *const root, char const *const format, bool const emptyValue)
void Clear(std::string const &List, std::string const &Value)
bool Exists(const std::string &Name) const
Definition: configuration.h:98
void Clear(std::string const &List, int const &Value)
void CndSet(const char *Name, const int Value)
int FindI(const char *Name, int const &Default=0) const
void Set(const char *Name, const std::string &Value)
void Set(const std::string &Name, const std::string &Value)
Definition: configuration.h:92
const Item * Tree(const char *Name) const
std::vector< std::string > FindVector(std::string const &Name, std::string const &Default="", bool const Keys=false) const
Definition: configuration.h:84
bool FindB(std::string const &Name, bool const &Default=false) const
Definition: configuration.h:89
void Set(const char *Name, const int &Value)
void Dump(std::ostream &str)
const Item * Lookup(const char *Name) const
Definition: configuration.h:63
std::string Find(std::string const &Name, const char *Default=0) const
Definition: configuration.h:71
std::vector< std::string > FindVector(const char *Name, std::string const &Default="", bool const Keys=false) const
std::string Find(const char *Name, const char *Default=0) const
Item * Lookup(const char *Name, const bool &Create)
std::string Find(std::string const &Name, std::string const &Default) const
Definition: configuration.h:72
void Clear(const std::string &Name)
bool FindB(const char *Name, bool const &Default=false) const
int FindI(std::string const &Name, int const &Default=0) const
Definition: configuration.h:87
void CndSet(const char *Name, const std::string &Value)
APT_PUBLIC bool ReadConfigDir(Configuration &Conf, const std::string &Dir, bool const &AsSectional=false, unsigned const &Depth=0)
APT_PUBLIC bool ReadConfigFile(Configuration &Conf, const std::string &FName, bool const &AsSectional=false, unsigned const &Depth=0)
APT_PUBLIC Configuration * _config
#define APT_PUBLIC
Definition: macros.h:77
#define APT_HIDDEN
Definition: macros.h:78