configuration.h 3.6 KB

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