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 IniItem
*next
; ///< The next item in this group
25 char *name
; ///< The name of this item
26 char *value
; ///< The value of this item
27 char *comment
; ///< The comment associated with this item
29 IniItem(struct IniGroup
*parent
, const char *name
, const char *last
= nullptr);
32 void SetValue(const char *value
);
35 /** A group within an ini file. */
37 IniGroup
*next
; ///< the next group within this file
38 IniGroupType type
; ///< type of group
39 IniItem
*item
; ///< the first item in the group
40 IniItem
**last_item
; ///< the last item in the group
41 char *name
; ///< name of group
42 char *comment
; ///< comment for group
44 IniGroup(struct IniLoadFile
*parent
, const char *name
, const char *last
= nullptr);
47 IniItem
*GetItem(const char *name
, bool create
);
51 /** Ini file that only supports loading. */
53 IniGroup
*group
; ///< the first group in the ini
54 IniGroup
**last_group
; ///< the last group in the ini
55 char *comment
; ///< last comment in file
56 const char * const *list_group_names
; ///< nullptr terminated list with group names that are lists
57 const char * const *seq_group_names
; ///< nullptr terminated list with group names that are sequences.
59 IniLoadFile(const char * const *list_group_names
= nullptr, const char * const *seq_group_names
= nullptr);
60 virtual ~IniLoadFile();
62 IniGroup
*GetGroup(const char *name
, size_t len
= 0, bool create_new
= true);
63 void RemoveGroup(const char *name
);
65 void LoadFromDisk(const char *filename
, Subdirectory subdir
);
69 * @param filename Name of the INI file.
70 * @param subdir The subdir to load the file from.
71 * @param[out] size Size of the opened file.
72 * @return File handle of the opened file, or \c nullptr.
74 virtual FILE *OpenFile(const char *filename
, Subdirectory subdir
, size_t *size
) = 0;
77 * Report an error about the file contents.
78 * @param pre Prefix text of the \a buffer part.
79 * @param buffer Part of the file with the error.
80 * @param post Suffix text of the \a buffer part.
82 virtual void ReportFileError(const char * const pre
, const char * const buffer
, const char * const post
) = 0;
85 /** Ini file that supports both loading and saving. */
86 struct IniFile
: IniLoadFile
{
87 IniFile(const char * const *list_group_names
= nullptr);
89 bool SaveToDisk(const char *filename
);
91 virtual FILE *OpenFile(const char *filename
, Subdirectory subdir
, size_t *size
);
92 virtual void ReportFileError(const char * const pre
, const char * const buffer
, const char * const post
);
95 #endif /* INI_TYPE_H */