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"
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 std::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 std::string_view 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
);
50 void RemoveItem(const std::string
&name
);
54 /** Ini file that only supports loading. */
56 IniGroup
*group
; ///< the first group in the ini
57 IniGroup
**last_group
; ///< the last group in the ini
58 std::string comment
; ///< last comment in file
59 const char * const *list_group_names
; ///< nullptr terminated list with group names that are lists
60 const char * const *seq_group_names
; ///< nullptr terminated list with group names that are sequences.
62 IniLoadFile(const char * const *list_group_names
= nullptr, const char * const *seq_group_names
= nullptr);
63 virtual ~IniLoadFile();
65 IniGroup
*GetGroup(const std::string
&name
, bool create_new
= true);
66 void RemoveGroup(const char *name
);
68 void LoadFromDisk(const std::string
&filename
, Subdirectory subdir
);
72 * @param filename Name of the INI file.
73 * @param subdir The subdir to load the file from.
74 * @param[out] size Size of the opened file.
75 * @return File handle of the opened file, or \c nullptr.
77 virtual FILE *OpenFile(const std::string
&filename
, Subdirectory subdir
, size_t *size
) = 0;
80 * Report an error about the file contents.
81 * @param pre Prefix text of the \a buffer part.
82 * @param buffer Part of the file with the error.
83 * @param post Suffix text of the \a buffer part.
85 virtual void ReportFileError(const char * const pre
, const char * const buffer
, const char * const post
) = 0;
88 /** Ini file that supports both loading and saving. */
89 struct IniFile
: IniLoadFile
{
90 IniFile(const char * const *list_group_names
= nullptr);
92 bool SaveToDisk(const std::string
&filename
);
94 virtual FILE *OpenFile(const std::string
&filename
, Subdirectory subdir
, size_t *size
);
95 virtual void ReportFileError(const char * const pre
, const char * const buffer
, const char * const post
);
98 #endif /* INI_TYPE_H */