OpenTTD Source 20241224-master-gf74b0cf984
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
27
30 /* Save game and scenario files. */
33
34 /* Heightmap files. */
37
38 /* Town data files. */
40
41 /* fios 'files' */
46
48
50};
51
60
66#define MAKE_FIOS_TYPE(abstract, detailed) ((abstract) | ((detailed) << FT_NUMBITS))
67
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),
86
87 FIOS_TYPE_INVALID = MAKE_FIOS_TYPE(FT_INVALID, DFT_INVALID),
88};
89
90#undef MAKE_FIOS_TYPE
91
98{
99 return static_cast<AbstractFileType>(static_cast<uint>(fios_type) & FT_MASK);
100}
101
108{
109 return static_cast<DetailedFileType>(fios_type >> FT_NUMBITS);
110}
111
135
155
157
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 */
Type (helpers) for enums.
#define DECLARE_POSTFIX_INCREMENT(enum_type)
Some enums need to have allowed incrementing (i.e.
Definition enum_type.hpp:18
DetailedFileType GetDetailedFileType(FiosType fios_type)
Extract the detailed file type from a FiosType.
AbstractFileType GetAbstractFileType(FiosType fios_type)
Extract the abstract file type from a FiosType.
Definition fileio_type.h:97
AbstractFileType
The different abstract types of files that the system knows about.
Definition fileio_type.h:16
@ FT_SCENARIO
old or new scenario
Definition fileio_type.h:19
@ FT_HEIGHTMAP
heightmap file
Definition fileio_type.h:20
@ FT_NONE
nothing to do
Definition fileio_type.h:17
@ FT_SAVEGAME
old or new savegame
Definition fileio_type.h:18
@ FT_NUMBITS
Number of bits required for storing a AbstractFileType value.
Definition fileio_type.h:24
@ FT_INVALID
Invalid or unknown file type.
Definition fileio_type.h:23
@ FT_TOWN_DATA
town data file
Definition fileio_type.h:21
@ FT_MASK
Bitmask for extracting an abstract file type.
Definition fileio_type.h:25
FiosType
Elements of a file system that are recognized.
Definition fileio_type.h:73
SaveLoadOperation
Operation performed on the file.
Definition fileio_type.h:53
@ SLO_CHECK
Load file for checking and/or preview.
Definition fileio_type.h:54
@ SLO_SAVE
File is being saved.
Definition fileio_type.h:56
@ SLO_LOAD
File is being loaded.
Definition fileio_type.h:55
@ SLO_INVALID
Unknown file operation.
Definition fileio_type.h:58
#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
Kinds of files in each AbstractFileType.
Definition fileio_type.h:29
@ DFT_FIOS_DRIVE
A drive (letter) entry.
Definition fileio_type.h:42
@ DFT_GAME_FILE
Save game or scenario file.
Definition fileio_type.h:32
@ DFT_HEIGHTMAP_BMP
BMP file.
Definition fileio_type.h:35
@ DFT_HEIGHTMAP_PNG
PNG file.
Definition fileio_type.h:36
@ DFT_FIOS_DIR
A directory entry.
Definition fileio_type.h:44
@ DFT_FIOS_PARENT
A parent directory entry.
Definition fileio_type.h:43
@ DFT_INVALID
Unknown or invalid file.
Definition fileio_type.h:49
@ DFT_FIOS_DIRECT
Direct filename.
Definition fileio_type.h:45
@ DFT_TOWN_DATA_JSON
JSON file.
Definition fileio_type.h:39
@ 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
Old save game or scenario file.
Definition fileio_type.h:31
Searchpath
Types of searchpaths OpenTTD might use.
@ SP_SHARED_DIR
Search in the shared directory, like 'Shared Files' under Windows.
@ SP_INSTALLATION_DIR
Search in the installation directory.
@ SP_AUTODOWNLOAD_PERSONAL_DIR_XDG
Search within the autodownload directory located in the personal directory (XDG variant)
@ SP_BINARY_DIR
Search in the directory where the binary resides.
@ SP_AUTODOWNLOAD_PERSONAL_DIR
Search within the autodownload directory located in the personal directory.
@ SP_PERSONAL_DIR
Search in the personal directory.
@ SP_WORKING_DIR
Search in the working directory.
@ SP_APPLICATION_BUNDLE_DIR
Search within the application bundle.
@ SP_AUTODOWNLOAD_DIR
Search within the autodownload directory.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
@ OLD_DATA_DIR
Old subdirectory for the data.
@ NO_DIRECTORY
A path without any base directory.
@ AI_LIBRARY_DIR
Subdirectory for all AI libraries.
@ SCREENSHOT_DIR
Subdirectory for all screenshots.
@ SOCIAL_INTEGRATION_DIR
Subdirectory for all social integration plugins.
@ GAME_LIBRARY_DIR
Subdirectory for all GS libraries.
@ AI_DIR
Subdirectory for all AI files.
@ OLD_GM_DIR
Old subdirectory for the music.
@ SCENARIO_DIR
Base directory for all scenarios.
@ BASE_DIR
Base directory for all subdirectories.
@ SAVE_DIR
Base directory for all savegames.
@ NUM_SUBDIRS
Number of subdirectories.
@ HEIGHTMAP_DIR
Subdirectory of scenario for heightmaps.
@ NEWGRF_DIR
Subdirectory for all NewGRFs.
@ AUTOSAVE_DIR
Subdirectory of save for autosaves.
@ LANG_DIR
Subdirectory for all translation files.
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
@ GAME_DIR
Subdirectory for all game scripts.
Helper to close a FILE * with a std::unique_ptr.