OpenTTD Source: src/fileio_type.h Source File

OpenTTD Source 15.0-beta1

fileio_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 FILEIO_TYPE_H
11#define FILEIO_TYPE_H
12
13#include "core/enum_type.hpp"
14
16 enum AbstractFileType {
17 FT_NONE,
18 FT_SAVEGAME,
19 FT_SCENARIO,
20 FT_HEIGHTMAP,
21 FT_TOWN_DATA,
22
23 FT_INVALID = 7,
24 FT_NUMBITS = 3,
25 FT_MASK = (1 << FT_NUMBITS) - 1,
26};
27
29 enum DetailedFileType {
30 /* Save game and scenario files. */
31 DFT_OLD_GAME_FILE,
32 DFT_GAME_FILE,
33
34 /* Heightmap files. */
35 DFT_HEIGHTMAP_BMP,
36 DFT_HEIGHTMAP_PNG,
37
38 /* Town data files. */
39 DFT_TOWN_DATA_JSON,
40
41 /* fios 'files' */
42 DFT_FIOS_DRIVE,
43 DFT_FIOS_PARENT,
44 DFT_FIOS_DIR,
45 DFT_FIOS_DIRECT,
46
47 DFT_END,
48
49 DFT_INVALID = 255,
50};
51
53 enum SaveLoadOperation {
54 SLO_CHECK,
55 SLO_LOAD,
56 SLO_SAVE,
57
58 SLO_INVALID,
59};
60
66#define MAKE_FIOS_TYPE(abstract, detailed) ((abstract) | ((detailed) << FT_NUMBITS))
67
73 enum FiosType {
74 FIOS_TYPE_DRIVE = MAKE_FIOS_TYPE(FT_NONE, DFT_FIOS_DRIVE),
75 FIOS_TYPE_PARENT = MAKE_FIOS_TYPE(FT_NONE, DFT_FIOS_PARENT),
76 FIOS_TYPE_DIR = MAKE_FIOS_TYPE(FT_NONE, DFT_FIOS_DIR),
77 FIOS_TYPE_DIRECT = MAKE_FIOS_TYPE(FT_NONE, DFT_FIOS_DIRECT),
78
79 FIOS_TYPE_FILE = MAKE_FIOS_TYPE(FT_SAVEGAME, DFT_GAME_FILE),
80 FIOS_TYPE_OLDFILE = MAKE_FIOS_TYPE(FT_SAVEGAME, DFT_OLD_GAME_FILE),
81 FIOS_TYPE_SCENARIO = MAKE_FIOS_TYPE(FT_SCENARIO, DFT_GAME_FILE),
82 FIOS_TYPE_OLD_SCENARIO = MAKE_FIOS_TYPE(FT_SCENARIO, DFT_OLD_GAME_FILE),
83 FIOS_TYPE_PNG = MAKE_FIOS_TYPE(FT_HEIGHTMAP, DFT_HEIGHTMAP_PNG),
84 FIOS_TYPE_BMP = MAKE_FIOS_TYPE(FT_HEIGHTMAP, DFT_HEIGHTMAP_BMP),
85 FIOS_TYPE_JSON = MAKE_FIOS_TYPE(FT_TOWN_DATA, DFT_TOWN_DATA_JSON),
86
87 FIOS_TYPE_INVALID = MAKE_FIOS_TYPE(FT_INVALID, DFT_INVALID),
88};
89
90#undef MAKE_FIOS_TYPE
91
97 inline AbstractFileType GetAbstractFileType(FiosType fios_type)
98{
99 return static_cast<AbstractFileType>(static_cast<uint>(fios_type) & FT_MASK);
100}
101
107 inline DetailedFileType GetDetailedFileType(FiosType fios_type)
108{
109 return static_cast<DetailedFileType>(fios_type >> FT_NUMBITS);
110}
111
115 enum Subdirectory {
116 BASE_DIR,
117 SAVE_DIR,
118 AUTOSAVE_DIR,
119 SCENARIO_DIR,
120 HEIGHTMAP_DIR,
121 OLD_GM_DIR,
122 OLD_DATA_DIR,
123 BASESET_DIR,
124 NEWGRF_DIR,
125 LANG_DIR,
126 AI_DIR,
127 AI_LIBRARY_DIR,
128 GAME_DIR,
129 GAME_LIBRARY_DIR,
130 SCREENSHOT_DIR,
131 SOCIAL_INTEGRATION_DIR,
132 NUM_SUBDIRS,
133 NO_DIRECTORY,
134};
135
139 enum Searchpath : unsigned {
140 SP_FIRST_DIR,
141 SP_WORKING_DIR = SP_FIRST_DIR,
142#ifdef USE_XDG
143 SP_PERSONAL_DIR_XDG,
144#endif
145 SP_PERSONAL_DIR,
146 SP_SHARED_DIR,
147 SP_BINARY_DIR,
148 SP_INSTALLATION_DIR,
149 SP_APPLICATION_BUNDLE_DIR,
150 SP_AUTODOWNLOAD_DIR,
151 SP_AUTODOWNLOAD_PERSONAL_DIR,
152 SP_AUTODOWNLOAD_PERSONAL_DIR_XDG,
153 NUM_SEARCHPATHS
154};
155
156 DECLARE_POSTFIX_INCREMENT(Searchpath)
157
158class FileHandle {
159public:
160 static std::optional<FileHandle> Open(const std::string &filename, const std::string &mode);
161
162 inline void Close() { this->f.reset(); }
163
164 inline operator FILE *()
165 {
166 assert(this->f != nullptr);
167 return this->f.get();
168 }
169
170private:
172 struct FileDeleter {
173 void operator ()(FILE *f)
174 {
175 if (f != nullptr) fclose(f);
176 }
177 };
178
179 std::unique_ptr<FILE, FileDeleter> f;
180
181 FileHandle(FILE *f) : f(f) { assert(this->f != nullptr); }
182};
183
184/* Ensure has_value() is used consistently. */
185template <> constexpr std::optional<FileHandle>::operator bool() const noexcept = delete;
186
187#endif /* FILEIO_TYPE_H */
FileHandle
Definition fileio_type.h:158
enum_type.hpp
Type (helpers) for enums.
DECLARE_POSTFIX_INCREMENT
#define DECLARE_POSTFIX_INCREMENT(enum_type)
Some enums need to have allowed incrementing (i.e.
Definition enum_type.hpp:18
GetDetailedFileType
DetailedFileType GetDetailedFileType(FiosType fios_type)
Extract the detailed file type from a FiosType.
Definition fileio_type.h:107
GetAbstractFileType
AbstractFileType GetAbstractFileType(FiosType fios_type)
Extract the abstract file type from a FiosType.
Definition fileio_type.h:97
AbstractFileType
AbstractFileType
The different abstract types of files that the system knows about.
Definition fileio_type.h:16
FT_SCENARIO
@ FT_SCENARIO
old or new scenario
Definition fileio_type.h:19
FT_HEIGHTMAP
@ FT_HEIGHTMAP
heightmap file
Definition fileio_type.h:20
FT_NONE
@ FT_NONE
nothing to do
Definition fileio_type.h:17
FT_SAVEGAME
@ FT_SAVEGAME
old or new savegame
Definition fileio_type.h:18
FT_NUMBITS
@ FT_NUMBITS
Number of bits required for storing a AbstractFileType value.
Definition fileio_type.h:24
FT_INVALID
@ FT_INVALID
Invalid or unknown file type.
Definition fileio_type.h:23
FT_TOWN_DATA
@ FT_TOWN_DATA
town data file
Definition fileio_type.h:21
FT_MASK
@ FT_MASK
Bitmask for extracting an abstract file type.
Definition fileio_type.h:25
FiosType
FiosType
Elements of a file system that are recognized.
Definition fileio_type.h:73
SaveLoadOperation
SaveLoadOperation
Operation performed on the file.
Definition fileio_type.h:53
SLO_CHECK
@ SLO_CHECK
Load file for checking and/or preview.
Definition fileio_type.h:54
SLO_SAVE
@ SLO_SAVE
File is being saved.
Definition fileio_type.h:56
SLO_LOAD
@ SLO_LOAD
File is being loaded.
Definition fileio_type.h:55
SLO_INVALID
@ SLO_INVALID
Unknown file operation.
Definition fileio_type.h:58
MAKE_FIOS_TYPE
#define MAKE_FIOS_TYPE(abstract, detailed)
Construct an enum value for FiosType as a combination of an abstract and a detailed file type.
Definition fileio_type.h:66
DetailedFileType
DetailedFileType
Kinds of files in each AbstractFileType.
Definition fileio_type.h:29
DFT_FIOS_DRIVE
@ DFT_FIOS_DRIVE
A drive (letter) entry.
Definition fileio_type.h:42
DFT_GAME_FILE
@ DFT_GAME_FILE
Save game or scenario file.
Definition fileio_type.h:32
DFT_HEIGHTMAP_BMP
@ DFT_HEIGHTMAP_BMP
BMP file.
Definition fileio_type.h:35
DFT_HEIGHTMAP_PNG
@ DFT_HEIGHTMAP_PNG
PNG file.
Definition fileio_type.h:36
DFT_FIOS_DIR
@ DFT_FIOS_DIR
A directory entry.
Definition fileio_type.h:44
DFT_FIOS_PARENT
@ DFT_FIOS_PARENT
A parent directory entry.
Definition fileio_type.h:43
DFT_INVALID
@ DFT_INVALID
Unknown or invalid file.
Definition fileio_type.h:49
DFT_FIOS_DIRECT
@ DFT_FIOS_DIRECT
Direct filename.
Definition fileio_type.h:45
DFT_TOWN_DATA_JSON
@ DFT_TOWN_DATA_JSON
JSON file.
Definition fileio_type.h:39
DFT_END
@ DFT_END
End of this enum. Supports a compile time size check against _fios_colours in fios_gui....
Definition fileio_type.h:47
DFT_OLD_GAME_FILE
@ DFT_OLD_GAME_FILE
Old save game or scenario file.
Definition fileio_type.h:31
Searchpath
Searchpath
Types of searchpaths OpenTTD might use.
Definition fileio_type.h:139
SP_SHARED_DIR
@ SP_SHARED_DIR
Search in the shared directory, like 'Shared Files' under Windows.
Definition fileio_type.h:146
SP_INSTALLATION_DIR
@ SP_INSTALLATION_DIR
Search in the installation directory.
Definition fileio_type.h:148
SP_AUTODOWNLOAD_PERSONAL_DIR_XDG
@ SP_AUTODOWNLOAD_PERSONAL_DIR_XDG
Search within the autodownload directory located in the personal directory (XDG variant)
Definition fileio_type.h:152
SP_BINARY_DIR
@ SP_BINARY_DIR
Search in the directory where the binary resides.
Definition fileio_type.h:147
SP_AUTODOWNLOAD_PERSONAL_DIR
@ SP_AUTODOWNLOAD_PERSONAL_DIR
Search within the autodownload directory located in the personal directory.
Definition fileio_type.h:151
SP_PERSONAL_DIR
@ SP_PERSONAL_DIR
Search in the personal directory.
Definition fileio_type.h:145
SP_WORKING_DIR
@ SP_WORKING_DIR
Search in the working directory.
Definition fileio_type.h:141
SP_APPLICATION_BUNDLE_DIR
@ SP_APPLICATION_BUNDLE_DIR
Search within the application bundle.
Definition fileio_type.h:149
SP_AUTODOWNLOAD_DIR
@ SP_AUTODOWNLOAD_DIR
Search within the autodownload directory.
Definition fileio_type.h:150
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:115
OLD_DATA_DIR
@ OLD_DATA_DIR
Old subdirectory for the data.
Definition fileio_type.h:122
NO_DIRECTORY
@ NO_DIRECTORY
A path without any base directory.
Definition fileio_type.h:133
AI_LIBRARY_DIR
@ AI_LIBRARY_DIR
Subdirectory for all AI libraries.
Definition fileio_type.h:127
SCREENSHOT_DIR
@ SCREENSHOT_DIR
Subdirectory for all screenshots.
Definition fileio_type.h:130
SOCIAL_INTEGRATION_DIR
@ SOCIAL_INTEGRATION_DIR
Subdirectory for all social integration plugins.
Definition fileio_type.h:131
GAME_LIBRARY_DIR
@ GAME_LIBRARY_DIR
Subdirectory for all GS libraries.
Definition fileio_type.h:129
AI_DIR
@ AI_DIR
Subdirectory for all AI files.
Definition fileio_type.h:126
OLD_GM_DIR
@ OLD_GM_DIR
Old subdirectory for the music.
Definition fileio_type.h:121
SCENARIO_DIR
@ SCENARIO_DIR
Base directory for all scenarios.
Definition fileio_type.h:119
BASE_DIR
@ BASE_DIR
Base directory for all subdirectories.
Definition fileio_type.h:116
SAVE_DIR
@ SAVE_DIR
Base directory for all savegames.
Definition fileio_type.h:117
NUM_SUBDIRS
@ NUM_SUBDIRS
Number of subdirectories.
Definition fileio_type.h:132
HEIGHTMAP_DIR
@ HEIGHTMAP_DIR
Subdirectory of scenario for heightmaps.
Definition fileio_type.h:120
NEWGRF_DIR
@ NEWGRF_DIR
Subdirectory for all NewGRFs.
Definition fileio_type.h:124
AUTOSAVE_DIR
@ AUTOSAVE_DIR
Subdirectory of save for autosaves.
Definition fileio_type.h:118
LANG_DIR
@ LANG_DIR
Subdirectory for all translation files.
Definition fileio_type.h:125
BASESET_DIR
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
Definition fileio_type.h:123
GAME_DIR
@ GAME_DIR
Subdirectory for all game scripts.
Definition fileio_type.h:128
FileHandle::FileDeleter
Helper to close a FILE * with a std::unique_ptr.
Definition fileio_type.h:172