OpenTTD Source: src/ini_type.h Source File

OpenTTD Source 15.0-RC2

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#ifndef INI_TYPE_H
11#define INI_TYPE_H
12
13#include "fileio_type.h"
14
16 enum IniGroupType : uint8_t {
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(std::string_view name);
29
30 void SetValue(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(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<const 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(std::string_view filename, Subdirectory subdir);
68
76 virtual std::optional<FileHandle> OpenFile(std::string_view filename, Subdirectory subdir, size_t *size) = 0;
77
82 virtual void ReportFileError(std::string_view message) = 0;
83};
84
86 struct IniFile : IniLoadFile {
87 IniFile(const IniGroupNameList &list_group_names = {});
88
89 bool SaveToDisk(const std::string &filename);
90
91 std::optional<FileHandle> OpenFile(std::string_view filename, Subdirectory subdir, size_t *size) override;
92 void ReportFileError(std::string_view message) override;
93};
94
95#endif /* INI_TYPE_H */
fileio_type.h
Types for Standard In/Out file operations.
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:88
IniGroupType
IniGroupType
Types of groups.
Definition ini_type.h:16
IGT_SEQUENCE
@ IGT_SEQUENCE
A list of uninterpreted lines, terminated by the next group block.
Definition ini_type.h:19
IGT_VARIABLES
@ IGT_VARIABLES
Values of the form "landscape = hilly".
Definition ini_type.h:17
IGT_LIST
@ IGT_LIST
A list of values, separated by and terminated by the next group block.
Definition ini_type.h:18
IniFile
Ini file that supports both loading and saving.
Definition ini_type.h:86
IniFile::SaveToDisk
bool SaveToDisk(const std::string &filename)
Save the Ini file's data to the disk.
Definition ini.cpp:42
IniFile::OpenFile
std::optional< FileHandle > OpenFile(std::string_view filename, Subdirectory subdir, size_t *size) override
Open the INI file.
Definition ini.cpp:104
IniFile::ReportFileError
void ReportFileError(std::string_view message) override
Report an error about the file contents.
Definition ini.cpp:111
IniGroup
A group within an ini file.
Definition ini_type.h:34
IniGroup::GetItem
const IniItem * GetItem(std::string_view name) const
Get the item with the given name.
Definition ini_load.cpp:51
IniGroup::comment
std::string comment
comment for group
Definition ini_type.h:38
IniGroup::type
IniGroupType type
type of group
Definition ini_type.h:36
IniGroup::Clear
void Clear()
Clear all items in the group.
Definition ini_load.cpp:97
IniGroup::RemoveItem
void RemoveItem(std::string_view name)
Remove the item with the given name.
Definition ini_load.cpp:89
IniGroup::name
std::string name
name of group
Definition ini_type.h:37
IniGroup::CreateItem
IniItem & CreateItem(std::string_view name)
Create an item with the given name.
Definition ini_load.cpp:80
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:65
IniGroup::items
std::list< IniItem > items
all items in the group
Definition ini_type.h:35
IniItem
A single "line" in an ini file.
Definition ini_type.h:23
IniItem::value
std::optional< std::string > value
The value of this item.
Definition ini_type.h:25
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
IniItem::SetValue
void SetValue(std::string_view value)
Replace the current value with another value.
Definition ini_load.cpp:31
IniLoadFile
Ini file that only supports loading.
Definition ini_type.h:50
IniLoadFile::groups
std::list< IniGroup > groups
all groups in the ini
Definition ini_type.h:53
IniLoadFile::RemoveGroup
void RemoveGroup(std::string_view name)
Remove the group with the given name.
Definition ini_load.cpp:174
IniLoadFile::seq_group_names
const IniGroupNameList seq_group_names
list of group names that are sequences.
Definition ini_type.h:56
IniLoadFile::OpenFile
virtual std::optional< FileHandle > OpenFile(std::string_view filename, Subdirectory subdir, size_t *size)=0
Open the INI file.
IniLoadFile::GetGroup
const IniGroup * GetGroup(std::string_view name) const
Get the group with the given name.
Definition ini_load.cpp:118
IniLoadFile::ReportFileError
virtual void ReportFileError(std::string_view message)=0
Report an error about the file contents.
IniLoadFile::comment
std::string comment
last comment in file
Definition ini_type.h:54
IniLoadFile::LoadFromDisk
void LoadFromDisk(std::string_view filename, Subdirectory subdir)
Load the Ini file's data from the disk.
Definition ini_load.cpp:185
IniLoadFile::CreateGroup
IniGroup & CreateGroup(std::string_view name)
Create an group with the given name.
Definition ini_load.cpp:161
IniLoadFile::list_group_names
const IniGroupNameList list_group_names
list of group names that are lists
Definition ini_type.h:55
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:146