OpenTTD Source: src/ini_type.h Source File

OpenTTD Source 14.1

ini_type.h
Go to the documentation of this file.
1 /*
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/>.
6 */
7
10 #ifndef INI_TYPE_H
11 #define INI_TYPE_H
12
13 #include "fileio_type.h"
14
16 enum IniGroupType {
17 IGT_VARIABLES = 0,
18 IGT_LIST = 1,
19 IGT_SEQUENCE = 2,
20 };
21
23 struct IniItem {
24 std::string name;
25 std::optional<std::string> value;
26 std::string comment;
27
28 IniItem(const std::string &name);
29
30 void SetValue(const std::string_view value);
31 };
32
34 struct IniGroup {
35 std::list<IniItem> items;
36 IniGroupType type;
37 std::string name;
38 std::string comment;
39
40 IniGroup(const std::string &name, IniGroupType type);
41
42 const IniItem *GetItem(const std::string &name) const;
43 IniItem &GetOrCreateItem(const std::string &name);
44 IniItem &CreateItem(const std::string &name);
45 void RemoveItem(const std::string &name);
46 void Clear();
47 };
48
50 struct IniLoadFile {
51 using IniGroupNameList = std::initializer_list<std::string_view>;
52
53 std::list<IniGroup> groups;
54 std::string comment;
55 const IniGroupNameList list_group_names;
56 const IniGroupNameList seq_group_names;
57
58 IniLoadFile(const IniGroupNameList &list_group_names = {}, const IniGroupNameList &seq_group_names = {});
59 virtual ~IniLoadFile() { }
60
61 const IniGroup *GetGroup(const std::string &name) const;
62 IniGroup *GetGroup(const std::string &name);
63 IniGroup &GetOrCreateGroup(const std::string &name);
64 IniGroup &CreateGroup(const std::string &name);
65 void RemoveGroup(const std::string &name);
66
67 void LoadFromDisk(const std::string &filename, Subdirectory subdir);
68
76 virtual FILE *OpenFile(const std::string &filename, Subdirectory subdir, size_t *size) = 0;
77
84 virtual void ReportFileError(const char * const pre, const char * const buffer, const char * const post) = 0;
85 };
86
88 struct IniFile : IniLoadFile {
89 IniFile(const IniGroupNameList &list_group_names = {});
90
91 bool SaveToDisk(const std::string &filename);
92
93 FILE *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;
95 };
96
97 #endif /* INI_TYPE_H */
IniItem::SetValue
void SetValue(const std::string_view value)
Replace the current value with another value.
Definition: ini_load.cpp:32
IniLoadFile::comment
std::string comment
last comment in file
Definition: ini_type.h:54
IGT_LIST
@ IGT_LIST
A list of values, separated by and terminated by the next group block.
Definition: ini_type.h:18
IniItem
A single "line" in an ini file.
Definition: ini_type.h:23
IniGroup::GetItem
const IniItem * GetItem(const std::string &name) const
Get the item with the given name.
Definition: ini_load.cpp:52
IniGroup
A group within an ini file.
Definition: ini_type.h:34
IniGroup::RemoveItem
void RemoveItem(const std::string &name)
Remove the item with the given name.
Definition: ini_load.cpp:90
IniLoadFile::GetOrCreateGroup
IniGroup & GetOrCreateGroup(const std::string &name)
Get the group with the given name, and if it doesn't exist create a new group.
Definition: ini_load.cpp:147
IniGroup::items
std::list< IniItem > items
all items in the group
Definition: ini_type.h:35
IniLoadFile::seq_group_names
const IniGroupNameList seq_group_names
list of group names that are sequences.
Definition: ini_type.h:56
IniGroup::Clear
void Clear()
Clear all items in the group.
Definition: ini_load.cpp:98
IniLoadFile::list_group_names
const IniGroupNameList list_group_names
list of group names that are lists
Definition: ini_type.h:55
IniLoadFile
Ini file that only supports loading.
Definition: ini_type.h:50
IniLoadFile::RemoveGroup
void RemoveGroup(const std::string &name)
Remove the group with the given name.
Definition: ini_load.cpp:175
IniItem::IniItem
IniItem(const std::string &name)
Construct a new in-memory item of an Ini file.
Definition: ini_load.cpp:23
IniLoadFile::CreateGroup
IniGroup & CreateGroup(const std::string &name)
Create an group with the given name.
Definition: ini_load.cpp:162
IniLoadFile::OpenFile
virtual FILE * OpenFile(const std::string &filename, Subdirectory subdir, size_t *size)=0
Open the INI file.
IniGroup::GetOrCreateItem
IniItem & GetOrCreateItem(const std::string &name)
Get the item with the given name, and if it doesn't exist create a new item.
Definition: ini_load.cpp:66
IniItem::value
std::optional< std::string > value
The value of this item.
Definition: ini_type.h:25
IniFile::SaveToDisk
bool SaveToDisk(const std::string &filename)
Save the Ini file's data to the disk.
Definition: ini.cpp:42
IniLoadFile::GetGroup
const IniGroup * GetGroup(const std::string &name) const
Get the group with the given name.
Definition: ini_load.cpp:119
IniGroupType
IniGroupType
Types of groups.
Definition: ini_type.h:16
IniGroup::type
IniGroupType type
type of group
Definition: ini_type.h:36
IGT_VARIABLES
@ IGT_VARIABLES
Values of the form "landscape = hilly".
Definition: ini_type.h:17
IniGroup::comment
std::string comment
comment for group
Definition: ini_type.h:38
IniGroup::name
std::string name
name of group
Definition: ini_type.h:37
IniFile
Ini file that supports both loading and saving.
Definition: ini_type.h:88
IniLoadFile::ReportFileError
virtual void ReportFileError(const char *const pre, const char *const buffer, const char *const post)=0
Report an error about the file contents.
IniLoadFile::IniLoadFile
IniLoadFile(const IniGroupNameList &list_group_names={}, const IniGroupNameList &seq_group_names={})
Construct a new in-memory Ini file representation.
Definition: ini_load.cpp:108
IniGroup::CreateItem
IniItem & CreateItem(const std::string &name)
Create an item with the given name.
Definition: ini_load.cpp:81
IGT_SEQUENCE
@ IGT_SEQUENCE
A list of uninterpreted lines, terminated by the next group block.
Definition: ini_type.h:19
IniGroup::IniGroup
IniGroup(const std::string &name, IniGroupType type)
Construct a new in-memory group of an Ini file.
Definition: ini_load.cpp:42
IniFile::IniFile
IniFile(const IniGroupNameList &list_group_names={})
Create a new ini file with given group names.
Definition: ini.cpp:33
fileio_type.h
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:108
IniLoadFile::groups
std::list< IniGroup > groups
all groups in the ini
Definition: ini_type.h:53
IniItem::name
std::string name
The name of this item.
Definition: ini_type.h:24
IniFile::OpenFile
FILE * OpenFile(const std::string &filename, Subdirectory subdir, size_t *size) override
Open the INI file.
Definition: ini.cpp:103
IniItem::comment
std::string comment
The comment associated with this item.
Definition: ini_type.h:26
IniLoadFile::LoadFromDisk
void LoadFromDisk(const std::string &filename, Subdirectory subdir)
Load the Ini file's data from the disk.
Definition: ini_load.cpp:187
IniFile::ReportFileError
void ReportFileError(const char *const pre, const char *const buffer, const char *const post) override
Report an error about the file contents.
Definition: ini.cpp:110