apt 3.0.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
40class 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
105 // clear a whole tree
106 void Clear(const std::string &Name);
107 void Clear();
108
109 // remove a certain value from a list (e.g. the list of "APT::Keep-Fds")
110 void Clear(std::string const &List, std::string const &Value);
111 void Clear(std::string const &List, int const &Value);
112
113 inline const Item *Tree(const char *Name) const {return Lookup(Name);};
114
115 inline void Dump() { Dump(std::clog); };
116 void Dump(std::ostream& str);
117 void Dump(std::ostream& str, char const * const root,
118 char const * const format, bool const emptyValue);
119
120#ifdef APT_COMPILING_APT
121 bool SectionInSubTree(char const *const SubTree, std::string_view Needle);
122#endif
123 explicit Configuration(const Item *Root);
126
129 {
130 std::vector<regex_t *> patterns;
131 APT_HIDDEN void clearPatterns();
132
133 public:
134 explicit MatchAgainstConfig(char const * Config);
135 virtual ~MatchAgainstConfig();
136
138 bool Match(char const * str) const;
139 bool Match(std::string const &str) const { return Match(str.c_str()); };
140
142 bool wasConstructedSuccessfully() const { return patterns.empty() == false; }
143 };
144};
145
146APT_PUBLIC extern Configuration *_config;
147
148APT_PUBLIC bool ReadConfigFile(Configuration &Conf,const std::string &FName,
149 bool const &AsSectional = false,
150 unsigned const &Depth = 0);
151
152APT_PUBLIC bool ReadConfigDir(Configuration &Conf,const std::string &Dir,
153 bool const &AsSectional = false,
154 unsigned const &Depth = 0);
155
156#endif
match a string against a configurable list of patterns
Definition configuration.h:129
bool wasConstructedSuccessfully() const
returns if the matcher setup was successful
Definition configuration.h:142
Definition configuration.h:41
Definition configuration.h:45