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 /** Types of groups */
17 IGT_VARIABLES
= 0, ///< Values of the form "landscape = hilly".
18 IGT_LIST
= 1, ///< A list of values, separated by \n and terminated by the next group block.
19 IGT_SEQUENCE
= 2, ///< A list of uninterpreted lines, terminated by the next group block.
22 /** A single "line" in an ini file. */
24 std::string name
; ///< The name of this item
25 std::optional
<std::string
> value
; ///< The value of this item
26 std::string comment
; ///< The comment associated with this item
28 IniItem(std::string_view name
);
30 void SetValue(std::string_view value
);
33 /** A group within an ini file. */
35 std::list
<IniItem
> items
; ///< all items in the group
36 IniGroupType type
; ///< type of group
37 std::string name
; ///< name of group
38 std::string comment
; ///< comment for group
40 IniGroup(std::string_view name
, IniGroupType type
);
42 const IniItem
*GetItem(std::string_view name
) const;
43 IniItem
&GetOrCreateItem(std::string_view name
);
44 IniItem
&CreateItem(std::string_view name
);
45 void RemoveItem(std::string_view name
);
49 /** Ini file that only supports loading. */
51 using IniGroupNameList
= std::initializer_list
<std::string_view
>;
53 std::list
<IniGroup
> groups
; ///< all groups in the ini
54 std::string comment
; ///< last comment in file
55 const IniGroupNameList list_group_names
; ///< list of group names that are lists
56 const IniGroupNameList seq_group_names
; ///< list of group names that are sequences.
58 IniLoadFile(const IniGroupNameList
&list_group_names
= {}, const IniGroupNameList
&seq_group_names
= {});
59 virtual ~IniLoadFile() { }
61 const IniGroup
*GetGroup(std::string_view name
) const;
62 IniGroup
*GetGroup(std::string_view name
);
63 IniGroup
&GetOrCreateGroup(std::string_view name
);
64 IniGroup
&CreateGroup(std::string_view name
);
65 void RemoveGroup(std::string_view name
);
67 void LoadFromDisk(const std::string
&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 std::nullopt.
76 virtual std::optional
<FileHandle
> OpenFile(const std::string
&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 IniGroupNameList
&list_group_names
= {});
91 bool SaveToDisk(const std::string
&filename
);
93 std::optional
<FileHandle
> OpenFile(const std::string
&filename
, Subdirectory subdir
, size_t *size
) override
;
94 void ReportFileError(const char * const pre
, const char * const buffer
, const char * const post
) override
;
97 #endif /* INI_TYPE_H */