OpenTTD Source  12.0-beta2
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 #include <string>
15 #include <optional>
16 
20  IGT_LIST = 1,
22 };
23 
25 struct IniItem {
27  std::string name;
28  std::optional<std::string> value;
29  std::string comment;
30 
31  IniItem(struct IniGroup *parent, const std::string &name);
32  ~IniItem();
33 
34  void SetValue(const std::string_view value);
35 };
36 
38 struct IniGroup {
43  std::string name;
44  std::string comment;
45 
46  IniGroup(struct IniLoadFile *parent, const std::string &name);
47  ~IniGroup();
48 
49  IniItem *GetItem(const std::string &name, bool create);
50  void RemoveItem(const std::string &name);
51  void Clear();
52 };
53 
55 struct IniLoadFile {
58  std::string comment;
59  const char * const *list_group_names;
60  const char * const *seq_group_names;
61 
62  IniLoadFile(const char * const *list_group_names = nullptr, const char * const *seq_group_names = nullptr);
63  virtual ~IniLoadFile();
64 
65  IniGroup *GetGroup(const std::string &name, bool create_new = true);
66  void RemoveGroup(const char *name);
67 
68  void LoadFromDisk(const std::string &filename, Subdirectory subdir);
69 
77  virtual FILE *OpenFile(const std::string &filename, Subdirectory subdir, size_t *size) = 0;
78 
85  virtual void ReportFileError(const char * const pre, const char * const buffer, const char * const post) = 0;
86 };
87 
89 struct IniFile : IniLoadFile {
90  IniFile(const char * const *list_group_names = nullptr);
91 
92  bool SaveToDisk(const std::string &filename);
93 
94  virtual FILE *OpenFile(const std::string &filename, Subdirectory subdir, size_t *size);
95  virtual void ReportFileError(const char * const pre, const char * const buffer, const char * const post);
96 };
97 
98 #endif /* INI_TYPE_H */
IniLoadFile::RemoveGroup
void RemoveGroup(const char *name)
Remove the group with the given name.
Definition: ini_load.cpp:182
IniLoadFile::~IniLoadFile
virtual ~IniLoadFile()
Free everything we loaded.
Definition: ini_load.cpp:151
IniItem::SetValue
void SetValue(const std::string_view value)
Replace the current value with another value.
Definition: ini_load.cpp:41
IniLoadFile::comment
std::string comment
last comment in file
Definition: ini_type.h:58
IniItem::next
IniItem * next
The next item in this group.
Definition: ini_type.h:26
IGT_LIST
@ IGT_LIST
A list of values, separated by and terminated by the next group block.
Definition: ini_type.h:20
IniItem
A single "line" in an ini file.
Definition: ini_type.h:25
IniGroup
A group within an ini file.
Definition: ini_type.h:38
IniGroup::RemoveItem
void RemoveItem(const std::string &name)
Remove the item with the given name.
Definition: ini_load.cpp:107
IniGroup::Clear
void Clear()
Clear all items in the group.
Definition: ini_load.cpp:130
IniLoadFile
Ini file that only supports loading.
Definition: ini_type.h:55
IniFile::IniFile
IniFile(const char *const *list_group_names=nullptr)
Create a new ini file with given group names.
Definition: ini.cpp:37
IniLoadFile::OpenFile
virtual FILE * OpenFile(const std::string &filename, Subdirectory subdir, size_t *size)=0
Open the INI file.
IniItem::value
std::optional< std::string > value
The value of this item.
Definition: ini_type.h:28
IniFile::SaveToDisk
bool SaveToDisk(const std::string &filename)
Save the Ini file's data to the disk.
Definition: ini.cpp:46
IniGroupType
IniGroupType
Types of groups.
Definition: ini_type.h:18
IniGroup::type
IniGroupType type
type of group
Definition: ini_type.h:40
IGT_VARIABLES
@ IGT_VARIABLES
Values of the form "landscape = hilly".
Definition: ini_type.h:19
IniGroup::last_item
IniItem ** last_item
the last item in the group
Definition: ini_type.h:42
IniFile::ReportFileError
virtual void ReportFileError(const char *const pre, const char *const buffer, const char *const post)
Report an error about the file contents.
Definition: ini.cpp:133
IniLoadFile::list_group_names
const char *const * list_group_names
nullptr terminated list with group names that are lists
Definition: ini_type.h:59
IniLoadFile::group
IniGroup * group
the first group in the ini
Definition: ini_type.h:56
IniGroup::comment
std::string comment
comment for group
Definition: ini_type.h:44
IniGroup::name
std::string name
name of group
Definition: ini_type.h:43
IniFile
Ini file that supports both loading and saving.
Definition: ini_type.h:89
IniItem::~IniItem
~IniItem()
Free everything we loaded.
Definition: ini_load.cpp:32
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.
IniGroup::~IniGroup
~IniGroup()
Free everything we loaded.
Definition: ini_load.cpp:78
IniItem::IniItem
IniItem(struct IniGroup *parent, const std::string &name)
Construct a new in-memory item of an Ini file.
Definition: ini_load.cpp:23
IniFile::OpenFile
virtual FILE * OpenFile(const std::string &filename, Subdirectory subdir, size_t *size)
Open the INI file.
Definition: ini.cpp:126
IGT_SEQUENCE
@ IGT_SEQUENCE
A list of uninterpreted lines, terminated by the next group block.
Definition: ini_type.h:21
fileio_type.h
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:108
IniLoadFile::last_group
IniGroup ** last_group
the last group in the ini
Definition: ini_type.h:57
IniItem::name
std::string name
The name of this item.
Definition: ini_type.h:27
IniGroup::item
IniItem * item
the first item in the group
Definition: ini_type.h:41
IniLoadFile::IniLoadFile
IniLoadFile(const char *const *list_group_names=nullptr, const char *const *seq_group_names=nullptr)
Construct a new in-memory Ini file representation.
Definition: ini_load.cpp:142
IniLoadFile::seq_group_names
const char *const * seq_group_names
nullptr terminated list with group names that are sequences.
Definition: ini_type.h:60
IniItem::comment
std::string comment
The comment associated with this item.
Definition: ini_type.h:29
IniGroup::GetItem
IniItem * GetItem(const std::string &name, bool create)
Get the item with the given name, and if it doesn't exist and create is true it creates a new item.
Definition: ini_load.cpp:91
IniGroup::IniGroup
IniGroup(struct IniLoadFile *parent, const std::string &name)
Construct a new in-memory group of an Ini file.
Definition: ini_load.cpp:51
IniLoadFile::LoadFromDisk
void LoadFromDisk(const std::string &filename, Subdirectory subdir)
Load the Ini file's data from the disk.
Definition: ini_load.cpp:215
IniLoadFile::GetGroup
IniGroup * GetGroup(const std::string &name, bool create_new=true)
Get the group with the given name.
Definition: ini_load.cpp:163
IniGroup::next
IniGroup * next
the next group within this file
Definition: ini_type.h:39