You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

116 lines
3.5 KiB

  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. #ifdef __GNUG__
  25. #pragma interface "apt-pkg/configuration.h"
  26. #endif
  27. #include <string>
  28. #include <iostream>
  29. using std::string;
  30. class Configuration
  31. {
  32. public:
  33. struct Item
  34. {
  35. string Value;
  36. string Tag;
  37. Item *Parent;
  38. Item *Child;
  39. Item *Next;
  40. string FullTag(const Item *Stop = 0) const;
  41. Item() : Parent(0), Child(0), Next(0) {};
  42. };
  43. private:
  44. Item *Root;
  45. bool ToFree;
  46. Item *Lookup(Item *Head,const char *S,unsigned long Len,bool Create);
  47. Item *Lookup(const char *Name,bool Create);
  48. inline const Item *Lookup(const char *Name) const
  49. {
  50. return ((Configuration *)this)->Lookup(Name,false);
  51. }
  52. public:
  53. string Find(const char *Name,const char *Default = 0) const;
  54. string Find(string Name,const char *Default = 0) const {return Find(Name.c_str(),Default);};
  55. string FindFile(const char *Name,const char *Default = 0) const;
  56. string FindDir(const char *Name,const char *Default = 0) const;
  57. int FindI(const char *Name,int Default = 0) const;
  58. int FindI(string Name,int Default = 0) const {return FindI(Name.c_str(),Default);};
  59. bool FindB(const char *Name,bool Default = false) const;
  60. bool FindB(string Name,bool Default = false) const {return FindB(Name.c_str(),Default);};
  61. string FindAny(const char *Name,const char *Default = 0) const;
  62. inline void Set(string Name,string Value) {Set(Name.c_str(),Value);};
  63. void CndSet(const char *Name,string Value);
  64. void Set(const char *Name,string Value);
  65. void Set(const char *Name,int Value);
  66. inline bool Exists(string Name) const {return Exists(Name.c_str());};
  67. bool Exists(const char *Name) const;
  68. bool ExistsAny(const char *Name) const;
  69. // clear a whole tree
  70. void Clear(string Name);
  71. // remove a certain value from a list (e.g. the list of "APT::Keep-Fds")
  72. void Clear(string List, string Value);
  73. void Clear(string List, int Value);
  74. inline const Item *Tree(const char *Name) const {return Lookup(Name);};
  75. inline void Dump() { Dump(std::clog); };
  76. void Dump(std::ostream& str);
  77. Configuration(const Item *Root);
  78. Configuration();
  79. ~Configuration();
  80. };
  81. extern Configuration *_config;
  82. bool ReadConfigFile(Configuration &Conf,string FName,bool AsSectional = false,
  83. unsigned Depth = 0);
  84. bool ReadConfigDir(Configuration &Conf,string Dir,bool AsSectional = false,
  85. unsigned Depth = 0);
  86. #endif