configuration.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: configuration.h,v 1.12 2001/02/20 07:03:17 jgg 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. class Configuration
  29. {
  30. public:
  31. struct Item
  32. {
  33. string Value;
  34. string Tag;
  35. Item *Parent;
  36. Item *Child;
  37. Item *Next;
  38. string FullTag(const Item *Stop = 0) const;
  39. Item() : Parent(0), Child(0), Next(0) {};
  40. };
  41. private:
  42. Item *Root;
  43. bool ToFree;
  44. Item *Lookup(Item *Head,const char *S,unsigned long Len,bool Create);
  45. Item *Lookup(const char *Name,bool Create);
  46. inline const Item *Lookup(const char *Name) const
  47. {
  48. return ((Configuration *)this)->Lookup(Name,false);
  49. }
  50. public:
  51. string Find(const char *Name,const char *Default = 0) const;
  52. string Find(string Name,const char *Default = 0) const {return Find(Name.c_str(),Default);};
  53. string FindFile(const char *Name,const char *Default = 0) const;
  54. string FindDir(const char *Name,const char *Default = 0) const;
  55. int FindI(const char *Name,int Default = 0) const;
  56. int FindI(string Name,bool Default = 0) const {return FindI(Name.c_str(),Default);};
  57. bool FindB(const char *Name,bool Default = false) const;
  58. bool FindB(string Name,bool Default = false) const {return FindB(Name.c_str(),Default);};
  59. string FindAny(const char *Name,const char *Default = 0) const;
  60. inline void Set(string Name,string Value) {Set(Name.c_str(),Value);};
  61. void CndSet(const char *Name,string Value);
  62. void Set(const char *Name,string Value);
  63. void Set(const char *Name,int Value);
  64. inline bool Exists(string Name) const {return Exists(Name.c_str());};
  65. bool Exists(const char *Name) const;
  66. bool ExistsAny(const char *Name) const;
  67. void Clear(string Name);
  68. inline const Item *Tree(const char *Name) const {return Lookup(Name);};
  69. void Dump();
  70. Configuration(const Item *Root);
  71. Configuration();
  72. ~Configuration();
  73. };
  74. extern Configuration *_config;
  75. bool ReadConfigFile(Configuration &Conf,string FName,bool AsSectional = false,
  76. unsigned Depth = 0);
  77. bool ReadConfigDir(Configuration &Conf,string Dir,bool AsSectional = false,
  78. unsigned Depth = 0);
  79. #endif