OpenTTD Source  1.11.2
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 char *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 Clear();
51 };
52 
54 struct IniLoadFile {
57  std::string comment;
58  const char * const *list_group_names;
59  const char * const *seq_group_names;
60 
61  IniLoadFile(const char * const *list_group_names = nullptr, const char * const *seq_group_names = nullptr);
62  virtual ~IniLoadFile();
63 
64  IniGroup *GetGroup(const std::string &name, bool create_new = true);
65  void RemoveGroup(const char *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 char * const *list_group_names = nullptr);
90 
91  bool SaveToDisk(const std::string &filename);
92 
93  virtual FILE *OpenFile(const std::string &filename, Subdirectory subdir, size_t *size);
94  virtual void ReportFileError(const char * const pre, const char * const buffer, const char * const post);
95 };
96 
97 #endif /* INI_TYPE_H */
IniLoadFile::RemoveGroup
void RemoveGroup(const char *name)
Remove the group with the given name.
Definition: ini_load.cpp:162
IniLoadFile::~IniLoadFile
virtual ~IniLoadFile()
Free everything we loaded.
Definition: ini_load.cpp:131
IniLoadFile::comment
std::string comment
last comment in file
Definition: ini_type.h:57
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::Clear
void Clear()
Clear all items in the group.
Definition: ini_load.cpp:110
IniLoadFile
Ini file that only supports loading.
Definition: ini_type.h:54
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:58
IniLoadFile::group
IniGroup * group
the first group in the ini
Definition: ini_type.h:55
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:88
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:82
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:56
IniItem::SetValue
void SetValue(const char *value)
Replace the current value with another value.
Definition: ini_load.cpp:41
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:122
IniLoadFile::seq_group_names
const char *const * seq_group_names
nullptr terminated list with group names that are sequences.
Definition: ini_type.h:59
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:95
IniGroup::IniGroup
IniGroup(struct IniLoadFile *parent, const std::string &name)
Construct a new in-memory group of an Ini file.
Definition: ini_load.cpp:55
IniLoadFile::LoadFromDisk
void LoadFromDisk(const std::string &filename, Subdirectory subdir)
Load the Ini file's data from the disk.
Definition: ini_load.cpp:195
IniLoadFile::GetGroup
IniGroup * GetGroup(const std::string &name, bool create_new=true)
Get the group with the given name.
Definition: ini_load.cpp:143
IniGroup::next
IniGroup * next
the next group within this file
Definition: ini_type.h:39