configuration.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #include <apt-pkg/macros.h>
  29. #ifndef APT_8_CLEANER_HEADERS
  30. using std::string;
  31. #endif
  32. class Configuration
  33. {
  34. public:
  35. struct Item
  36. {
  37. std::string Value;
  38. std::string Tag;
  39. Item *Parent;
  40. Item *Child;
  41. Item *Next;
  42. std::string FullTag(const Item *Stop = 0) const;
  43. Item() : Parent(0), Child(0), Next(0) {};
  44. };
  45. private:
  46. Item *Root;
  47. bool ToFree;
  48. Item *Lookup(Item *Head,const char *S,unsigned long const &Len,bool const &Create);
  49. Item *Lookup(const char *Name,const bool &Create);
  50. inline const Item *Lookup(const char *Name) const
  51. {
  52. return ((Configuration *)this)->Lookup(Name,false);
  53. }
  54. public:
  55. std::string Find(const char *Name,const char *Default = 0) const;
  56. std::string Find(std::string const &Name,const char *Default = 0) const {return Find(Name.c_str(),Default);};
  57. std::string Find(std::string const &Name, std::string const &Default) const {return Find(Name.c_str(),Default.c_str());};
  58. std::string FindFile(const char *Name,const char *Default = 0) const;
  59. std::string FindDir(const char *Name,const char *Default = 0) const;
  60. /** return a list of child options
  61. *
  62. * Options like Acquire::Languages are handled as lists which
  63. * can be overridden and have a default. For the later two a comma
  64. * separated list of values is supported.
  65. *
  66. * \param Name of the parent node
  67. * \param Default list of values separated by commas */
  68. std::vector<std::string> FindVector(const char *Name, std::string const &Default = "", bool const Keys = false) const;
  69. 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); };
  70. int FindI(const char *Name,int const &Default = 0) const;
  71. int FindI(std::string const &Name,int const &Default = 0) const {return FindI(Name.c_str(),Default);};
  72. bool FindB(const char *Name,bool const &Default = false) const;
  73. bool FindB(std::string const &Name,bool const &Default = false) const {return FindB(Name.c_str(),Default);};
  74. std::string FindAny(const char *Name,const char *Default = 0) const;
  75. inline void Set(const std::string &Name,const std::string &Value) {Set(Name.c_str(),Value);};
  76. void CndSet(const char *Name,const std::string &Value);
  77. void CndSet(const char *Name,const int Value);
  78. void Set(const char *Name,const std::string &Value);
  79. void Set(const char *Name,const int &Value);
  80. inline bool Exists(const std::string &Name) const {return Exists(Name.c_str());};
  81. bool Exists(const char *Name) const;
  82. bool ExistsAny(const char *Name) const;
  83. void MoveSubTree(char const * const OldRoot, char const * const NewRoot);
  84. // clear a whole tree
  85. void Clear(const std::string &Name);
  86. void Clear();
  87. // remove a certain value from a list (e.g. the list of "APT::Keep-Fds")
  88. void Clear(std::string const &List, std::string const &Value);
  89. void Clear(std::string const &List, int const &Value);
  90. inline const Item *Tree(const char *Name) const {return Lookup(Name);};
  91. inline void Dump() { Dump(std::clog); };
  92. void Dump(std::ostream& str);
  93. void Dump(std::ostream& str, char const * const root,
  94. char const * const format, bool const emptyValue);
  95. Configuration(const Item *Root);
  96. Configuration();
  97. ~Configuration();
  98. /** \brief match a string against a configurable list of patterns */
  99. class MatchAgainstConfig
  100. {
  101. std::vector<regex_t *> patterns;
  102. APT_HIDDEN void clearPatterns();
  103. public:
  104. MatchAgainstConfig(char const * Config);
  105. virtual ~MatchAgainstConfig();
  106. /** \brief Returns \b true for a string matching one of the patterns */
  107. bool Match(char const * str) const;
  108. bool Match(std::string const &str) const { return Match(str.c_str()); };
  109. /** \brief returns if the matcher setup was successful */
  110. bool wasConstructedSuccessfully() const { return patterns.empty() == false; }
  111. };
  112. };
  113. extern Configuration *_config;
  114. bool ReadConfigFile(Configuration &Conf,const std::string &FName,
  115. bool const &AsSectional = false,
  116. unsigned const &Depth = 0);
  117. bool ReadConfigDir(Configuration &Conf,const std::string &Dir,
  118. bool const &AsSectional = false,
  119. unsigned const &Depth = 0);
  120. #endif