* apt-pkg/init.cc:
[ntk/apt.git] / apt-pkg / contrib / configuration.h
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: configuration.h,v 1.16 2002/11/11 06:55:50 doogie Exp $
4 /* ######################################################################
5
6 Configuration Class
7
8 This class provides a configuration file and command line parser
9 for a tree-oriented configuration environment. All runtime configuration
10 is stored in here.
11
12 Each configuration name is given as a fully scoped string such as
13 Foo::Bar
14 And has associated with it a text string. The Configuration class only
15 provides storage and lookup for this tree, other classes provide
16 configuration file formats (and parsers/emitters if needed).
17
18 Most things can get by quite happily with,
19 cout << _config->Find("Foo::Bar") << endl;
20
21 A special extension, support for ordered lists is provided by using the
22 special syntax, "block::list::" the trailing :: designates the
23 item as a list. To access the list you must use the tree function on
24 "block::list".
25
26 ##################################################################### */
27 /*}}}*/
28 #ifndef PKGLIB_CONFIGURATION_H
29 #define PKGLIB_CONFIGURATION_H
30
31 #include <regex.h>
32
33 #include <string>
34 #include <vector>
35 #include <iostream>
36
37 using std::string;
38
39 class Configuration
40 {
41 public:
42
43 struct Item
44 {
45 string Value;
46 string Tag;
47 Item *Parent;
48 Item *Child;
49 Item *Next;
50
51 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 ((Configuration *)this)->Lookup(Name,false);
66 }
67
68 public:
69
70 string Find(const char *Name,const char *Default = 0) const;
71 string Find(string const &Name,const char *Default = 0) const {return Find(Name.c_str(),Default);};
72 string Find(string const &Name, string const &Default) const {return Find(Name.c_str(),Default.c_str());};
73 string FindFile(const char *Name,const char *Default = 0) const;
74 string FindDir(const char *Name,const char *Default = 0) const;
75 std::vector<string> FindVector(const char *Name) const;
76 std::vector<string> FindVector(string const &Name) const { return FindVector(Name.c_str()); };
77 int FindI(const char *Name,int const &Default = 0) const;
78 int FindI(string const &Name,int const &Default = 0) const {return FindI(Name.c_str(),Default);};
79 bool FindB(const char *Name,bool const &Default = false) const;
80 bool FindB(string const &Name,bool const &Default = false) const {return FindB(Name.c_str(),Default);};
81 string FindAny(const char *Name,const char *Default = 0) const;
82
83 inline void Set(const string &Name,const string &Value) {Set(Name.c_str(),Value);};
84 void CndSet(const char *Name,const string &Value);
85 void CndSet(const char *Name,const int Value);
86 void Set(const char *Name,const string &Value);
87 void Set(const char *Name,const int Value);
88
89 inline bool Exists(const string &Name) const {return Exists(Name.c_str());};
90 bool Exists(const char *Name) const;
91 bool ExistsAny(const char *Name) const;
92
93 // clear a whole tree
94 void Clear(const string &Name);
95
96 // remove a certain value from a list (e.g. the list of "APT::Keep-Fds")
97 void Clear(string const &List, string const &Value);
98 void Clear(string const &List, int const &Value);
99
100 inline const Item *Tree(const char *Name) const {return Lookup(Name);};
101
102 inline void Dump() { Dump(std::clog); };
103 void Dump(std::ostream& str);
104
105 Configuration(const Item *Root);
106 Configuration();
107 ~Configuration();
108
109 /** \brief match a string against a configurable list of patterns */
110 class MatchAgainstConfig
111 {
112 std::vector<regex_t *> patterns;
113 void clearPatterns();
114
115 public:
116 MatchAgainstConfig(char const * Config);
117 virtual ~MatchAgainstConfig();
118
119 /** \brief Returns \b true for a string matching one of the patterns */
120 bool Match(char const * str) const;
121 bool Match(std::string const &str) const { return Match(str.c_str()); };
122
123 /** \brief returns if the matcher setup was successful */
124 bool wasConstructedSuccessfully() const { return patterns.empty() == false; }
125 };
126 };
127
128 extern Configuration *_config;
129
130 bool ReadConfigFile(Configuration &Conf,const string &FName,
131 bool const &AsSectional = false,
132 unsigned const &Depth = 0);
133
134 bool ReadConfigDir(Configuration &Conf,const string &Dir,
135 bool const &AsSectional = false,
136 unsigned const &Depth = 0);
137
138 #endif