2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD 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, version 2.
|
|
|
|
* OpenTTD 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. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-08-24 13:50:31 +00:00
|
|
|
/** @file ini_type.h Types related to reading/writing '*.ini' files. */
|
|
|
|
|
|
|
|
#ifndef INI_TYPE_H
|
|
|
|
#define INI_TYPE_H
|
|
|
|
|
2011-08-24 13:38:26 +00:00
|
|
|
#include "fileio_type.h"
|
2020-05-17 21:32:03 +00:00
|
|
|
#include <string>
|
2020-12-14 20:46:35 +00:00
|
|
|
#include <optional>
|
2011-08-24 13:38:26 +00:00
|
|
|
|
2008-08-24 13:50:31 +00:00
|
|
|
/** Types of groups */
|
|
|
|
enum IniGroupType {
|
2011-03-03 20:56:33 +00:00
|
|
|
IGT_VARIABLES = 0, ///< Values of the form "landscape = hilly".
|
|
|
|
IGT_LIST = 1, ///< A list of values, separated by \n and terminated by the next group block.
|
|
|
|
IGT_SEQUENCE = 2, ///< A list of uninterpreted lines, terminated by the next group block.
|
2008-08-24 13:50:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** A single "line" in an ini file. */
|
|
|
|
struct IniItem {
|
2020-05-17 21:32:03 +00:00
|
|
|
IniItem *next; ///< The next item in this group
|
|
|
|
std::string name; ///< The name of this item
|
2020-12-14 20:46:35 +00:00
|
|
|
std::optional<std::string> value; ///< The value of this item
|
2020-05-17 21:32:03 +00:00
|
|
|
std::string comment; ///< The comment associated with this item
|
2008-08-24 13:50:31 +00:00
|
|
|
|
2020-05-17 21:32:03 +00:00
|
|
|
IniItem(struct IniGroup *parent, const std::string &name);
|
2008-08-24 13:50:31 +00:00
|
|
|
~IniItem();
|
2008-08-25 06:35:28 +00:00
|
|
|
|
|
|
|
void SetValue(const char *value);
|
2008-08-24 13:50:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** A group within an ini file. */
|
|
|
|
struct IniGroup {
|
|
|
|
IniGroup *next; ///< the next group within this file
|
|
|
|
IniGroupType type; ///< type of group
|
|
|
|
IniItem *item; ///< the first item in the group
|
|
|
|
IniItem **last_item; ///< the last item in the group
|
2020-05-17 21:32:03 +00:00
|
|
|
std::string name; ///< name of group
|
|
|
|
std::string comment; ///< comment for group
|
2008-08-24 13:50:31 +00:00
|
|
|
|
2020-05-17 21:32:03 +00:00
|
|
|
IniGroup(struct IniLoadFile *parent, const std::string &name);
|
2008-08-24 13:50:31 +00:00
|
|
|
~IniGroup();
|
|
|
|
|
2020-05-17 21:32:03 +00:00
|
|
|
IniItem *GetItem(const std::string &name, bool create);
|
2009-01-14 20:23:45 +00:00
|
|
|
void Clear();
|
2008-08-24 13:50:31 +00:00
|
|
|
};
|
|
|
|
|
2011-03-03 20:50:24 +00:00
|
|
|
/** Ini file that only supports loading. */
|
|
|
|
struct IniLoadFile {
|
2009-09-20 23:11:01 +00:00
|
|
|
IniGroup *group; ///< the first group in the ini
|
|
|
|
IniGroup **last_group; ///< the last group in the ini
|
2020-05-17 21:32:03 +00:00
|
|
|
std::string comment; ///< last comment in file
|
2019-04-10 21:07:06 +00:00
|
|
|
const char * const *list_group_names; ///< nullptr terminated list with group names that are lists
|
|
|
|
const char * const *seq_group_names; ///< nullptr terminated list with group names that are sequences.
|
2008-08-24 13:50:31 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
IniLoadFile(const char * const *list_group_names = nullptr, const char * const *seq_group_names = nullptr);
|
2011-03-03 20:50:24 +00:00
|
|
|
virtual ~IniLoadFile();
|
2008-08-24 13:50:31 +00:00
|
|
|
|
2020-05-17 21:32:03 +00:00
|
|
|
IniGroup *GetGroup(const std::string &name, bool create_new = true);
|
2008-08-24 13:50:31 +00:00
|
|
|
void RemoveGroup(const char *name);
|
|
|
|
|
2020-12-06 20:11:42 +00:00
|
|
|
void LoadFromDisk(const std::string &filename, Subdirectory subdir);
|
2011-03-03 20:53:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Open the INI file.
|
|
|
|
* @param filename Name of the INI file.
|
2011-08-24 13:38:26 +00:00
|
|
|
* @param subdir The subdir to load the file from.
|
2018-10-28 02:17:36 +00:00
|
|
|
* @param[out] size Size of the opened file.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @return File handle of the opened file, or \c nullptr.
|
2011-03-03 20:53:09 +00:00
|
|
|
*/
|
2020-12-06 20:11:49 +00:00
|
|
|
virtual FILE *OpenFile(const std::string &filename, Subdirectory subdir, size_t *size) = 0;
|
2011-03-03 20:53:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Report an error about the file contents.
|
|
|
|
* @param pre Prefix text of the \a buffer part.
|
|
|
|
* @param buffer Part of the file with the error.
|
|
|
|
* @param post Suffix text of the \a buffer part.
|
|
|
|
*/
|
|
|
|
virtual void ReportFileError(const char * const pre, const char * const buffer, const char * const post) = 0;
|
2011-03-03 20:50:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Ini file that supports both loading and saving. */
|
|
|
|
struct IniFile : IniLoadFile {
|
2019-04-10 21:07:06 +00:00
|
|
|
IniFile(const char * const *list_group_names = nullptr);
|
2011-03-03 20:50:24 +00:00
|
|
|
|
2020-12-06 20:11:42 +00:00
|
|
|
bool SaveToDisk(const std::string &filename);
|
2011-03-03 20:53:09 +00:00
|
|
|
|
2020-12-06 20:11:49 +00:00
|
|
|
virtual FILE *OpenFile(const std::string &filename, Subdirectory subdir, size_t *size);
|
2011-03-03 20:53:09 +00:00
|
|
|
virtual void ReportFileError(const char * const pre, const char * const buffer, const char * const post);
|
2008-08-24 13:50:31 +00:00
|
|
|
};
|
|
|
|
|
2008-08-24 17:29:57 +00:00
|
|
|
#endif /* INI_TYPE_H */
|