2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
2005-04-23 14:30:53 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2014-02-18 01:34:18 +00:00
|
|
|
*
|
2005-04-23 14:30:53 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2014-02-18 01:34:18 +00:00
|
|
|
*
|
2005-04-23 14:30:53 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2005-04-23 14:30:53 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-08-08 14:22:08 +00:00
|
|
|
#ifndef COMMON_INI_FILE_H
|
|
|
|
#define COMMON_INI_FILE_H
|
2005-04-23 14:30:53 +00:00
|
|
|
|
2011-04-24 08:34:27 +00:00
|
|
|
#include "common/hash-str.h"
|
2005-04-23 14:30:53 +00:00
|
|
|
#include "common/list.h"
|
|
|
|
#include "common/str.h"
|
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
2020-07-08 21:30:36 +00:00
|
|
|
/**
|
|
|
|
* @defgroup common_ini_file INI files
|
|
|
|
* @ingroup common
|
|
|
|
*
|
|
|
|
* @brief API for operations on INI configuration files.
|
|
|
|
*
|
2021-05-04 08:45:03 +00:00
|
|
|
*
|
2020-07-08 21:30:36 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2011-04-24 08:34:27 +00:00
|
|
|
class SeekableReadStream;
|
|
|
|
class WriteStream;
|
|
|
|
|
2005-04-23 14:30:53 +00:00
|
|
|
/**
|
2020-10-07 22:04:21 +00:00
|
|
|
* This class allows for reading and writing INI-style config files.
|
2005-04-23 14:30:53 +00:00
|
|
|
*
|
|
|
|
* Lines starting with a '#' are ignored (i.e. treated as comments).
|
|
|
|
* Some effort is made to preserve comments, though.
|
|
|
|
*
|
|
|
|
* This class makes no attempts to provide fast access to key/value pairs.
|
2020-10-07 22:04:21 +00:00
|
|
|
* In particular, it stores all sections and key/value pairs in lists, not
|
2005-04-23 14:30:53 +00:00
|
|
|
* in dictionaries/maps. This makes it very easy to read/write the data
|
|
|
|
* from/to files, but of course is not appropriate for fast access.
|
|
|
|
* The main reason is that this class is indeed geared toward doing precisely
|
2020-10-07 22:04:21 +00:00
|
|
|
* that.
|
2005-04-23 14:30:53 +00:00
|
|
|
*/
|
2013-08-08 14:22:08 +00:00
|
|
|
class INIFile {
|
2005-04-23 14:30:53 +00:00
|
|
|
public:
|
|
|
|
struct KeyValue {
|
2020-10-07 22:04:21 +00:00
|
|
|
String key; /*!< Key of the configuration entry. */
|
|
|
|
String value; /*!< Value of the configuration entry. */
|
|
|
|
String comment; /*!< Comment within an INI file. */
|
2005-04-23 14:30:53 +00:00
|
|
|
};
|
|
|
|
|
2020-10-07 22:04:21 +00:00
|
|
|
typedef List<KeyValue> SectionKeyList; /*!< A list of all key/value pairs in this section. */
|
2006-03-07 02:23:37 +00:00
|
|
|
|
2020-10-07 22:04:21 +00:00
|
|
|
/** A section in an INI file.
|
2005-04-23 14:30:53 +00:00
|
|
|
*
|
2020-10-07 22:04:21 +00:00
|
|
|
* Corresponds to the following:
|
|
|
|
* @code
|
|
|
|
* [mySection]
|
|
|
|
* key=value
|
|
|
|
* @endcode
|
|
|
|
* Comments are also stored, for convenience of users who prefer to edit
|
|
|
|
* INI files manually.
|
2005-04-23 14:30:53 +00:00
|
|
|
*/
|
|
|
|
struct Section {
|
2020-10-07 22:04:21 +00:00
|
|
|
String name; /*!< Name of the section. */
|
|
|
|
List<KeyValue> keys; /*!< List of all keys in this section. */
|
|
|
|
String comment; /*!< Comment within the section. */
|
|
|
|
|
|
|
|
bool hasKey(const String &key) const; /*!< Check whether the section has a @p key. */
|
|
|
|
const KeyValue* getKey(const String &key) const; /*!< Get the value assigned to a @p key. */
|
|
|
|
void setKey(const String &key, const String &value); /*!< Assign a @p value to a @p key. */
|
|
|
|
void removeKey(const String &key); /*!< Remove a @p key from this section. */
|
|
|
|
const SectionKeyList getKeys() const { return keys; } /*!< Get a list of all keys in the section. */
|
2005-04-23 14:30:53 +00:00
|
|
|
};
|
|
|
|
|
2020-10-07 22:04:21 +00:00
|
|
|
typedef List<Section> SectionList; /*!< A list of all sections in this INI file. */
|
2006-03-07 02:23:37 +00:00
|
|
|
|
2005-04-23 14:30:53 +00:00
|
|
|
public:
|
2019-08-20 10:12:12 +00:00
|
|
|
INIFile();
|
2019-08-20 08:38:27 +00:00
|
|
|
~INIFile() {}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-04-23 14:30:53 +00:00
|
|
|
// TODO: Maybe add a copy constructor etc.?
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-04-23 14:30:53 +00:00
|
|
|
/**
|
|
|
|
* Check whether the given string is a valid section or key name.
|
2020-10-07 22:04:21 +00:00
|
|
|
* For that, it must only consist of letters, numbers, dashes, and
|
|
|
|
* underscores. In particular, whitespace and "#", "=", "[", "]"
|
|
|
|
* are not valid.
|
2005-04-23 14:30:53 +00:00
|
|
|
*/
|
2019-08-20 08:38:27 +00:00
|
|
|
bool isValidName(const String &name) const;
|
2005-04-23 14:30:53 +00:00
|
|
|
|
2020-10-07 22:04:21 +00:00
|
|
|
/** Reset everything stored in this INI file. */
|
2005-04-23 14:30:53 +00:00
|
|
|
void clear();
|
|
|
|
|
2020-10-07 22:04:21 +00:00
|
|
|
bool loadFromFile(const String &filename); /*!< Load configuration from a file. */
|
|
|
|
bool loadFromSaveFile(const String &filename); /*!< Load configuration from a save file. */
|
|
|
|
bool loadFromStream(SeekableReadStream &stream); /*!< Load configuration from a @ref SeekableReadStream. */
|
|
|
|
bool saveToFile(const String &filename); /*!< Save the current configuration to a file. */
|
|
|
|
bool saveToSaveFile(const String &filename); /*!< Save the current configuration to a save file. */
|
|
|
|
bool saveToStream(WriteStream &stream); /*!< Save the current configuration to a @ref WriteStream. */
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2020-10-07 22:04:21 +00:00
|
|
|
bool hasSection(const String §ion) const; /*!< Check whether the INI file has a section with the specified name. */
|
|
|
|
void addSection(const String §ion); /*!< Add a section with the specified name to the INI file. */
|
|
|
|
void removeSection(const String §ion); /*!< Remove the @p section from the INI file. */
|
|
|
|
void renameSection(const String &oldName, const String &newName); /*!< Rename the INI file from @p oldName to @p newName. */
|
2005-04-23 14:30:53 +00:00
|
|
|
|
2020-10-07 22:04:21 +00:00
|
|
|
void setDefaultSectionName(const String &name); /*!< Set initial section name for section-less INI files. */
|
2017-11-28 22:04:46 +00:00
|
|
|
|
2020-10-07 22:04:21 +00:00
|
|
|
bool hasKey(const String &key, const String §ion) const; /*!< Check whether the @p section has a @p key. */
|
|
|
|
bool getKey(const String &key, const String §ion, String &value) const; /*!< Get the @p value of a @p key in a @p section. */
|
|
|
|
void setKey(const String &key, const String §ion, const String &value); /*!< Assign a @p value to a @p key in a @p section. */
|
|
|
|
void removeKey(const String &key, const String §ion); /*!< Remove a @p key from this @p section. */
|
2005-04-23 14:30:53 +00:00
|
|
|
|
2020-10-07 22:04:21 +00:00
|
|
|
const SectionList getSections() const { return _sections; } /*!< Get a list of sections in this INI file. */
|
|
|
|
const SectionKeyList getKeys(const String §ion) const; /*!< Get a list of keys in a @p section. */
|
2005-04-23 14:30:53 +00:00
|
|
|
|
2020-10-07 22:04:21 +00:00
|
|
|
void listKeyValues(StringMap &kv); /*!< Get a list of all key/value pairs in this INI file. */
|
2005-04-23 14:30:53 +00:00
|
|
|
|
2020-10-07 22:04:21 +00:00
|
|
|
void allowNonEnglishCharacters(); /*!< Allow non-English characters in this INI file. */
|
2019-08-20 10:12:12 +00:00
|
|
|
|
2005-04-23 14:30:53 +00:00
|
|
|
private:
|
2017-11-28 22:04:46 +00:00
|
|
|
String _defaultSectionName;
|
2006-03-07 02:23:37 +00:00
|
|
|
SectionList _sections;
|
2019-08-20 10:12:12 +00:00
|
|
|
bool _allowNonEnglishCharacters;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-04-23 14:30:53 +00:00
|
|
|
Section *getSection(const String §ion);
|
|
|
|
const Section *getSection(const String §ion) const;
|
|
|
|
};
|
|
|
|
|
2020-07-08 21:30:36 +00:00
|
|
|
/** @} */
|
|
|
|
|
2013-01-26 18:33:27 +00:00
|
|
|
} // End of namespace Common
|
2005-04-23 14:30:53 +00:00
|
|
|
|
|
|
|
#endif
|