OpenTTD Source  20240917-master-g9ab0a47812
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 
18  IGT_LIST = 1,
20 };
21 
23 struct 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 
34 struct 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 
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(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 
88 struct IniFile : IniLoadFile {
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 */
IniGroup::GetOrCreateItem
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
IniItem::SetValue
void SetValue(std::string_view value)
Replace the current value with another value.
Definition: ini_load.cpp:32
IniGroup::IniGroup
IniGroup(std::string_view name, IniGroupType type)
Construct a new in-memory group of an Ini file.
Definition: ini_load.cpp:42
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
A group within an ini file.
Definition: ini_type.h:34
IniGroup::items
std::list< IniItem > items
all items in the group
Definition: ini_type.h:35
IniItem::IniItem
IniItem(std::string_view name)
Construct a new in-memory item of an Ini file.
Definition: ini_load.cpp:23
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
IniGroup::GetItem
const IniItem * GetItem(std::string_view name) const
Get the item with the given name.
Definition: ini_load.cpp:52
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::CreateGroup
IniGroup & CreateGroup(std::string_view name)
Create an group with the given name.
Definition: ini_load.cpp:162
IniLoadFile::RemoveGroup
void RemoveGroup(std::string_view name)
Remove the group with the given name.
Definition: ini_load.cpp:175
IniLoadFile::GetOrCreateGroup
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
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
IniLoadFile::OpenFile
virtual std::optional< FileHandle > OpenFile(const std::string &filename, Subdirectory subdir, size_t *size)=0
Open the INI file.
IniGroup::RemoveItem
void RemoveItem(std::string_view name)
Remove the item with the given name.
Definition: ini_load.cpp:90
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
IGT_SEQUENCE
@ IGT_SEQUENCE
A list of uninterpreted lines, terminated by the next group block.
Definition: ini_type.h:19
IniLoadFile::GetGroup
const IniGroup * GetGroup(std::string_view name) const
Get the group with the given name.
Definition: ini_load.cpp:119
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:115
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
IniItem::comment
std::string comment
The comment associated with this item.
Definition: ini_type.h:26
IniGroup::CreateItem
IniItem & CreateItem(std::string_view name)
Create an item with the given name.
Definition: ini_load.cpp:81
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
IniFile::OpenFile
std::optional< FileHandle > OpenFile(const std::string &filename, Subdirectory subdir, size_t *size) override
Open the INI file.
Definition: ini.cpp:103