OpenTTD Source 20241224-master-gee860a5c8e
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
21
23struct IniItem {
24 std::string name;
25 std::optional<std::string> value;
26 std::string comment;
27
28 IniItem(std::string_view name);
29
30 void SetValue(std::string_view value);
31};
32
34struct IniGroup {
35 std::list<IniItem> items;
37 std::string name;
38 std::string comment;
39
40 IniGroup(std::string_view name, IniGroupType type);
41
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);
46 void Clear();
47};
48
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() = default;
60
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);
66
67 void LoadFromDisk(const std::string &filename, Subdirectory subdir);
68
76 virtual std::optional<FileHandle> 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
89 IniFile(const IniGroupNameList &list_group_names = {});
90
91 bool SaveToDisk(const std::string &filename);
92
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;
95};
96
97#endif /* INI_TYPE_H */
Types for Standard In/Out file operations.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
IniGroupType
Types of groups.
Definition ini_type.h:16
@ IGT_SEQUENCE
A list of uninterpreted lines, terminated by the next group block.
Definition ini_type.h:19
@ IGT_VARIABLES
Values of the form "landscape = hilly".
Definition ini_type.h:17
@ IGT_LIST
A list of values, separated by and terminated by the next group block.
Definition ini_type.h:18
Ini file that supports both loading and saving.
Definition ini_type.h:88
std::optional< FileHandle > OpenFile(const std::string &filename, Subdirectory subdir, size_t *size) override
Open the INI file.
Definition ini.cpp:103
bool SaveToDisk(const std::string &filename)
Save the Ini file's data to the disk.
Definition ini.cpp:42
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
A group within an ini file.
Definition ini_type.h:34
const IniItem * GetItem(std::string_view name) const
Get the item with the given name.
Definition ini_load.cpp:52
std::string comment
comment for group
Definition ini_type.h:38
IniGroupType type
type of group
Definition ini_type.h:36
void Clear()
Clear all items in the group.
Definition ini_load.cpp:98
void RemoveItem(std::string_view name)
Remove the item with the given name.
Definition ini_load.cpp:90
std::string name
name of group
Definition ini_type.h:37
IniItem & CreateItem(std::string_view name)
Create an item with the given name.
Definition ini_load.cpp:81
IniItem & GetOrCreateItem(std::string_view name)
Get the item with the given name, and if it doesn't exist create a new item.
Definition ini_load.cpp:66
std::list< IniItem > items
all items in the group
Definition ini_type.h:35
A single "line" in an ini file.
Definition ini_type.h:23
std::optional< std::string > value
The value of this item.
Definition ini_type.h:25
std::string name
The name of this item.
Definition ini_type.h:24
std::string comment
The comment associated with this item.
Definition ini_type.h:26
void SetValue(std::string_view value)
Replace the current value with another value.
Definition ini_load.cpp:32
Ini file that only supports loading.
Definition ini_type.h:50
std::list< IniGroup > groups
all groups in the ini
Definition ini_type.h:53
void RemoveGroup(std::string_view name)
Remove the group with the given name.
Definition ini_load.cpp:175
const IniGroupNameList seq_group_names
list of group names that are sequences.
Definition ini_type.h:56
void LoadFromDisk(const std::string &filename, Subdirectory subdir)
Load the Ini file's data from the disk.
Definition ini_load.cpp:187
const IniGroup * GetGroup(std::string_view name) const
Get the group with the given name.
Definition ini_load.cpp:119
virtual void ReportFileError(const char *const pre, const char *const buffer, const char *const post)=0
Report an error about the file contents.
std::string comment
last comment in file
Definition ini_type.h:54
virtual std::optional< FileHandle > OpenFile(const std::string &filename, Subdirectory subdir, size_t *size)=0
Open the INI file.
IniGroup & CreateGroup(std::string_view name)
Create an group with the given name.
Definition ini_load.cpp:162
const IniGroupNameList list_group_names
list of group names that are lists
Definition ini_type.h:55
IniGroup & GetOrCreateGroup(std::string_view name)
Get the group with the given name, and if it doesn't exist create a new group.
Definition ini_load.cpp:147