1 /* $Id: ini_type.h 22824 2011-08-24 13:38:26Z rubidium $ */
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file ini_type.h Types related to reading/writing '*.ini' files. */
15 #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 char *name
; ///< The name of this item
28 char *value
; ///< The value of this item
29 char *comment
; ///< The comment associated with this item
31 IniItem(struct IniGroup
*parent
, const char *name
, const char *last
= NULL
);
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 char *name
; ///< name of group
44 char *comment
; ///< comment for group
46 IniGroup(struct IniLoadFile
*parent
, const char *name
, const char *last
= NULL
);
49 IniItem
*GetItem(const char *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 char *comment
; ///< last comment in file
58 const char * const *list_group_names
; ///< NULL terminated list with group names that are lists
59 const char * const *seq_group_names
; ///< NULL terminated list with group names that are sequences.
61 IniLoadFile(const char * const *list_group_names
= NULL
, const char * const *seq_group_names
= NULL
);
62 virtual ~IniLoadFile();
64 IniGroup
*GetGroup(const char *name
, size_t len
= 0, 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 size [out] Size of the opened file.
74 * @return File handle of the opened file, or \c NULL.
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
= NULL
);
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 */