IniFileConfiguration.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // IniFileConfiguration.h
  3. //
  4. // Library: Util
  5. // Package: Configuration
  6. // Module: IniFileConfiguration
  7. //
  8. // Definition of the IniFileConfiguration class.
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Util_IniFileConfiguration_INCLUDED
  16. #define Util_IniFileConfiguration_INCLUDED
  17. #include "Poco/Util/Util.h"
  18. #ifndef POCO_UTIL_NO_INIFILECONFIGURATION
  19. #include "Poco/Util/AbstractConfiguration.h"
  20. #include <map>
  21. #include <istream>
  22. namespace Poco {
  23. namespace Util {
  24. class Util_API IniFileConfiguration: public AbstractConfiguration
  25. /// This implementation of a Configuration reads properties
  26. /// from a legacy Windows initialization (.ini) file.
  27. ///
  28. /// The file syntax is implemented as follows.
  29. /// - a line starting with a semicolon is treated as a comment and ignored
  30. /// - a line starting with a square bracket denotes a section key [<key>]
  31. /// - every other line denotes a property assignment in the form
  32. /// <value key> = <value>
  33. ///
  34. /// The name of a property is composed of the section key and the value key,
  35. /// separated by a period (<section key>.<value key>).
  36. ///
  37. /// Property names are not case sensitive. Leading and trailing whitespace is
  38. /// removed from both keys and values.
  39. {
  40. public:
  41. IniFileConfiguration();
  42. /// Creates an empty IniFileConfiguration.
  43. IniFileConfiguration(std::istream& istr);
  44. /// Creates an IniFileConfiguration and loads the configuration data
  45. /// from the given stream, which must be in initialization file format.
  46. IniFileConfiguration(const std::string& path);
  47. /// Creates an IniFileConfiguration and loads the configuration data
  48. /// from the given file, which must be in initialization file format.
  49. void load(std::istream& istr);
  50. /// Loads the configuration data from the given stream, which
  51. /// must be in initialization file format.
  52. void load(const std::string& path);
  53. /// Loads the configuration data from the given file, which
  54. /// must be in initialization file format.
  55. protected:
  56. bool getRaw(const std::string& key, std::string& value) const;
  57. void setRaw(const std::string& key, const std::string& value);
  58. void enumerate(const std::string& key, Keys& range) const;
  59. void removeRaw(const std::string& key);
  60. ~IniFileConfiguration();
  61. private:
  62. void parseLine(std::istream& istr);
  63. struct ICompare
  64. {
  65. bool operator () (const std::string& s1, const std::string& s2) const;
  66. };
  67. typedef std::map<std::string, std::string, ICompare> IStringMap;
  68. IStringMap _map;
  69. std::string _sectionKey;
  70. };
  71. } } // namespace Poco::Util
  72. #endif // POCO_UTIL_NO_INIFILECONFIGURATION
  73. #endif // Util_IniFileConfiguration_INCLUDED