configuration.h 4.6 KB

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