2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
8 /** @file ini_type.h Types related to reading/writing '*.ini' files. */
13 #include "fileio_type.h"
15 #include "3rdparty/optional/ottd_optional.h"
17 /** Types of groups */
19 IGT_VARIABLES
= 0, ///< Values of the form "landscape = hilly".
20 IGT_LIST
= 1, ///< A list of values, separated by \n and terminated by the next group block.
21 IGT_SEQUENCE
= 2, ///< A list of uninterpreted lines, terminated by the next group block.
24 /** A single "line" in an ini file. */
26 IniItem
*next
; ///< The next item in this group
27 std::string name
; ///< The name of this item
28 opt::optional
<std::string
> value
; ///< The value of this item
29 std::string comment
; ///< The comment associated with this item
31 IniItem(struct IniGroup
*parent
, const std::string
&name
);
34 void SetValue(const char *value
);
37 /** A group within an ini file. */
39 IniGroup
*next
; ///< the next group within this file
40 IniGroupType type
; ///< type of group
41 IniItem
*item
; ///< the first item in the group
42 IniItem
**last_item
; ///< the last item in the group
43 std::string name
; ///< name of group
44 std::string comment
; ///< comment for group
46 IniGroup(struct IniLoadFile
*parent
, const std::string
&name
);
49 IniItem
*GetItem(const std::string
&name
, bool create
);
53 /** Ini file that only supports loading. */
55 IniGroup
*group
; ///< the first group in the ini
56 IniGroup
**last_group
; ///< the last group in the ini
57 std::string comment
; ///< last comment in file
58 const char * const *list_group_names
; ///< nullptr terminated list with group names that are lists
59 const char * const *seq_group_names
; ///< nullptr terminated list with group names that are sequences.
61 IniLoadFile(const char * const *list_group_names
= nullptr, const char * const *seq_group_names
= nullptr);
62 virtual ~IniLoadFile();
64 IniGroup
*GetGroup(const std::string
&name
, bool create_new
= true);
65 void RemoveGroup(const char *name
);
67 void LoadFromDisk(const char *filename
, Subdirectory subdir
);
71 * @param filename Name of the INI file.
72 * @param subdir The subdir to load the file from.
73 * @param[out] size Size of the opened file.
74 * @return File handle of the opened file, or \c nullptr.
76 virtual FILE *OpenFile(const char *filename
, Subdirectory subdir
, size_t *size
) = 0;
79 * Report an error about the file contents.
80 * @param pre Prefix text of the \a buffer part.
81 * @param buffer Part of the file with the error.
82 * @param post Suffix text of the \a buffer part.
84 virtual void ReportFileError(const char * const pre
, const char * const buffer
, const char * const post
) = 0;
87 /** Ini file that supports both loading and saving. */
88 struct IniFile
: IniLoadFile
{
89 IniFile(const char * const *list_group_names
= nullptr);
91 bool SaveToDisk(const char *filename
);
93 virtual FILE *OpenFile(const char *filename
, Subdirectory subdir
, size_t *size
);
94 virtual void ReportFileError(const char * const pre
, const char * const buffer
, const char * const post
);
97 #endif /* INI_TYPE_H */