apt: apt-pkg/contrib/configuration.h Source File

apt 2.7.6 commandline package manager

configuration.h
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
39 class APT_PUBLIC Configuration
40{
41 public:
42
43 struct Item
44 {
45 std::string Value;
46 std::string Tag;
47 Item *Parent;
48 Item *Child;
49 Item *Next;
50
51 std::string FullTag(const Item *Stop = 0) const;
52
53 Item() : Parent(0), Child(0), Next(0) {};
54 };
55
56 private:
57
58 Item *Root;
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;
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
124 class MatchAgainstConfig
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
134 bool Match(char const * str) const;
135 bool Match(std::string const &str) const { return Match(str.c_str()); };
136
138 bool wasConstructedSuccessfully() const { return patterns.empty() == false; }
139 };
140};
141
142APT_PUBLIC extern Configuration *_config;
143
144APT_PUBLIC bool ReadConfigFile(Configuration &Conf,const std::string &FName,
145 bool const &AsSectional = false,
146 unsigned const &Depth = 0);
147
148APT_PUBLIC bool ReadConfigDir(Configuration &Conf,const std::string &Dir,
149 bool const &AsSectional = false,
150 unsigned const &Depth = 0);
151
152#endif
Configuration::MatchAgainstConfig
match a string against a configurable list of patterns
Definition configuration.h:125
Configuration::MatchAgainstConfig::wasConstructedSuccessfully
bool wasConstructedSuccessfully() const
returns if the matcher setup was successful
Definition configuration.h:138
Configuration
Definition configuration.h:40
Configuration::Item
Definition configuration.h:44