| 1 | // |
| 2 | // PropertyFileConfiguration.h |
| 3 | // |
| 4 | // Library: Util |
| 5 | // Package: Configuration |
| 6 | // Module: PropertyFileConfiguration |
| 7 | // |
| 8 | // Definition of the PropertyFileConfiguration 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 | |
| 16 | |
| 17 | #ifndef Util_PropertyFileConfiguration_INCLUDED |
| 18 | #define Util_PropertyFileConfiguration_INCLUDED |
| 19 | |
| 20 | |
| 21 | #include "Poco/Util/Util.h" |
| 22 | #include "Poco/Util/MapConfiguration.h" |
| 23 | #include <istream> |
| 24 | #include <ostream> |
| 25 | #include <list> |
| 26 | |
| 27 | |
| 28 | namespace Poco { |
| 29 | namespace Util { |
| 30 | |
| 31 | |
| 32 | class Util_API PropertyFileConfiguration: public MapConfiguration |
| 33 | /// This implementation of a Configuration reads properties |
| 34 | /// from a Java-style properties file. |
| 35 | /// |
| 36 | /// The file syntax is implemented as follows. |
| 37 | /// - a line starting with a hash '#' or exclamation mark '!' is treated as a comment and ignored |
| 38 | /// - every other line denotes a property assignment in the form |
| 39 | /// <key> = <value> or |
| 40 | /// <key> : <value> |
| 41 | /// |
| 42 | /// Keys and values may contain special characters represented by the following escape sequences: |
| 43 | /// - \t: tab (0x09) |
| 44 | /// - \n: line feed (0x0a) |
| 45 | /// - \r: carriage return (0x0d) |
| 46 | /// - \f: form feed (0x0c) |
| 47 | /// |
| 48 | /// For every other sequence that starts with a backslash, the backslash is removed. |
| 49 | /// Therefore, the sequence \a would just yield an 'a'. |
| 50 | /// |
| 51 | /// A value can spread across multiple lines if the last character in a line (the character |
| 52 | /// immediately before the carriage return or line feed character) is a single backslash. |
| 53 | /// |
| 54 | /// Property names are case sensitive. Leading and trailing whitespace is |
| 55 | /// removed from both keys and values. A property name can neither contain |
| 56 | /// a colon ':' nor an equal sign '=' character. |
| 57 | { |
| 58 | public: |
| 59 | PropertyFileConfiguration(); |
| 60 | /// Creates an empty PropertyFileConfiguration. |
| 61 | |
| 62 | PropertyFileConfiguration(std::istream& istr, bool = false); |
| 63 | /// Creates an PropertyFileConfiguration and loads the configuration data |
| 64 | /// from the given stream, which must be in properties file format. |
| 65 | /// Set the preserveComment to preserve the comments in the given stream. |
| 66 | |
| 67 | PropertyFileConfiguration(const std::string& path, bool = false); |
| 68 | /// Creates an PropertyFileConfiguration and loads the configuration data |
| 69 | /// from the given file, which must be in properties file format. |
| 70 | /// Set the preserveComment to preserve the comments in the given stream. |
| 71 | |
| 72 | void load(std::istream& istr, bool = false); |
| 73 | /// Loads the configuration data from the given stream, which |
| 74 | /// must be in properties file format. |
| 75 | /// Set the preserveComment to preserve the comments in the given stream. |
| 76 | |
| 77 | void load(const std::string& path, bool = false); |
| 78 | /// Loads the configuration data from the given file, which |
| 79 | /// must be in properties file format. |
| 80 | /// Set the preserveComment to preserve the comments in the given stream. |
| 81 | |
| 82 | void save(std::ostream& ostr) const; |
| 83 | /// Writes the configuration data to the given stream. |
| 84 | /// |
| 85 | /// The data is written as a sequence of statements in the form |
| 86 | /// <key>: <value> |
| 87 | /// separated by a newline character. |
| 88 | |
| 89 | void save(const std::string& path) const; |
| 90 | /// Writes the configuration data to the given file. |
| 91 | |
| 92 | protected: |
| 93 | void setRaw(const std::string& key, const std::string& value); |
| 94 | void removeRaw(const std::string& key); |
| 95 | ~PropertyFileConfiguration(); |
| 96 | |
| 97 | private: |
| 98 | typedef std::list<std::string> FileContent; |
| 99 | typedef std::map<std::string, FileContent::iterator> KeyFileContentItMap; |
| 100 | |
| 101 | void parseLine(std::istream& istr); |
| 102 | void skipSpace(std::istream& istr) const; |
| 103 | bool (int c) const ; |
| 104 | void (std::istream& istr); |
| 105 | void skipLine(std::istream& istr) const; |
| 106 | void saveKeyValue(std::istream& istr); |
| 107 | bool isNewLine(int c) const; |
| 108 | bool isKeyValueSeparator(int c) const; |
| 109 | std::string composeOneLine(const std::string& key, const std::string& value) const; |
| 110 | |
| 111 | bool ; |
| 112 | FileContent _fileContent; |
| 113 | KeyFileContentItMap _keyFileContentItMap; |
| 114 | |
| 115 | static int readChar(std::istream& istr); |
| 116 | }; |
| 117 | |
| 118 | |
| 119 | } } // namespace Poco::Util |
| 120 | |
| 121 | |
| 122 | #endif // Util_PropertyFileConfiguration_INCLUDED |
| 123 | |