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

apt 3.3.3 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 <string_view>
35#include <vector>
36
37#include <apt-pkg/macros.h>
38
39
40 class APT_PUBLIC Configuration
41{
42 public:
43
44 struct Item
45 {
46 std::string Value;
47 std::string Tag;
48 Item *Parent;
49 Item *Child;
50 Item *Next;
51
52 std::string FullTag(const Item *Stop = 0) const;
53
54 Item() : Parent(0), Child(0), Next(0) {};
55 };
56
57 private:
58
59 Item *Root;
60 bool ToFree;
61
62 Item *Lookup(Item *Head,const char *S,unsigned long const &Len,bool const &Create);
63 Item *Lookup(const char *Name,const bool &Create);
64 inline const Item *Lookup(const char *Name) const
65 {
66 return const_cast<Configuration *>(this)->Lookup(Name,false);
67 }
68
69 public:
70
71 std::string Find(const char *Name,const char *Default = 0) const;
72 std::string Find(std::string const &Name,const char *Default = 0) const {return Find(Name.c_str(),Default);};
73 std::string Find(std::string const &Name, std::string const &Default) const {return Find(Name.c_str(),Default.c_str());};
74 std::string FindFile(const char *Name,const char *Default = 0) const;
75 std::string FindDir(const char *Name,const char *Default = 0) const;
84 std::vector<std::string> FindVector(const char *Name, std::string const &Default = "", bool const Keys = false) const;
85 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); };
86
87 int FindI(const char *Name,int const &Default = 0) const;
88 int FindI(std::string const &Name,int const &Default = 0) const {return FindI(Name.c_str(),Default);};
89 bool FindB(const char *Name,bool const &Default = false) const;
90 bool FindB(std::string const &Name,bool const &Default = false) const {return FindB(Name.c_str(),Default);};
91 std::string FindAny(const char *Name,const char *Default = 0) const;
92
93 inline void Set(const std::string &Name,const std::string_view &Value) {Set(Name.c_str(),Value);};
94 void CndSet(const char *Name,const std::string_view &Value);
95 void CndSet(const char *Name,const int Value);
96 void Set(const char *Name,const std::string_view &Value);
97 void Set(const char *Name,const int &Value);
98
99 inline bool Exists(const std::string &Name) const {return Exists(Name.c_str());};
100 bool Exists(const char *Name) const;
101 bool ExistsAny(const char *Name) const;
102
103 void MoveSubTree(char const * const OldRoot, char const * const NewRoot);
104 void MoveSubTree(char const *const OldRoot, char const *const NewRoot, bool Overwrite);
105
106 // clear a whole tree
107 void Clear(const std::string &Name);
108 void Clear();
109
110 // remove a certain value from a list (e.g. the list of "APT::Keep-Fds")
111 void Clear(std::string const &List, std::string const &Value);
112 void Clear(std::string const &List, int const &Value);
113
114 inline const Item *Tree(const char *Name) const {return Lookup(Name);};
115
116 inline void Dump() { Dump(std::clog); };
117 void Dump(std::ostream& str);
118 void Dump(std::ostream& str, char const * const root,
119 char const * const format, bool const emptyValue);
120
121#ifdef APT_COMPILING_APT
122 bool SectionInSubTree(char const *const SubTree, std::string_view Needle);
123#endif
124 explicit Configuration(const Item *Root);
125 Configuration();
126 ~Configuration();
127
129 class MatchAgainstConfig
130 {
131 std::vector<regex_t *> patterns;
132 APT_HIDDEN void clearPatterns();
133
134 public:
135 explicit MatchAgainstConfig(char const * Config);
136 virtual ~MatchAgainstConfig();
137
139 bool Match(char const * str) const;
140 bool Match(std::string const &str) const { return Match(str.c_str()); };
141
143 bool wasConstructedSuccessfully() const { return patterns.empty() == false; }
144 };
145};
146
147APT_PUBLIC extern Configuration *_config;
148
149APT_PUBLIC bool ReadConfigFile(Configuration &Conf,const std::string &FName,
150 bool const &AsSectional = false,
151 unsigned const &Depth = 0);
152
153APT_PUBLIC bool ReadConfigDir(Configuration &Conf,const std::string &Dir,
154 bool const &AsSectional = false,
155 unsigned const &Depth = 0);
156
157#endif
Configuration::MatchAgainstConfig::wasConstructedSuccessfully
bool wasConstructedSuccessfully() const
returns if the matcher setup was successful
Definition configuration.h:143
Configuration::MatchAgainstConfig::Match
bool Match(char const *str) const
Returns true for a string matching one of the patterns.
Definition configuration.cc:1228
Configuration
Definition configuration.h:41
Configuration::Item
Definition configuration.h:45