OpenTTD Source: src/fileio_type.h Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#ifndef FILEIO_TYPE_H
11#define FILEIO_TYPE_H
12
13#include "core/enum_type.hpp"
14#include "misc/autorelease.hpp"
15
17 enum AbstractFileType : uint8_t {
18 FT_NONE,
19 FT_SAVEGAME,
20 FT_SCENARIO,
21 FT_HEIGHTMAP,
22 FT_TOWN_DATA,
23
24 FT_INVALID = 7,
25};
26
28 enum DetailedFileType : uint8_t {
29 /* Save game and scenario files. */
30 DFT_OLD_GAME_FILE,
31 DFT_GAME_FILE,
32
33 /* Heightmap files. */
34 DFT_HEIGHTMAP_BMP,
35 DFT_HEIGHTMAP_PNG,
36
37 /* Town data files. */
38 DFT_TOWN_DATA_JSON,
39
40 /* fios 'files' */
41 DFT_FIOS_DRIVE,
42 DFT_FIOS_PARENT,
43 DFT_FIOS_DIR,
44 DFT_FIOS_DIRECT,
45
46 DFT_END,
47
48 DFT_INVALID = 255,
49};
50
52 enum SaveLoadOperation : uint8_t {
53 SLO_CHECK,
54 SLO_LOAD,
55 SLO_SAVE,
56
57 SLO_INVALID,
58};
59
63 struct FiosType {
64 AbstractFileType abstract;
65 DetailedFileType detailed;
66
67 constexpr bool operator==(const FiosType &) const noexcept = default;
68};
69
70constexpr FiosType FIOS_TYPE_DRIVE{FT_NONE, DFT_FIOS_DRIVE};
71constexpr FiosType FIOS_TYPE_PARENT{FT_NONE, DFT_FIOS_PARENT};
72constexpr FiosType FIOS_TYPE_DIR{FT_NONE, DFT_FIOS_DIR};
73constexpr FiosType FIOS_TYPE_DIRECT{FT_NONE, DFT_FIOS_DIRECT};
74
75constexpr FiosType FIOS_TYPE_FILE{FT_SAVEGAME, DFT_GAME_FILE};
76constexpr FiosType FIOS_TYPE_OLDFILE{FT_SAVEGAME, DFT_OLD_GAME_FILE};
77constexpr FiosType FIOS_TYPE_SCENARIO{FT_SCENARIO, DFT_GAME_FILE};
78constexpr FiosType FIOS_TYPE_OLD_SCENARIO{FT_SCENARIO, DFT_OLD_GAME_FILE};
79constexpr FiosType FIOS_TYPE_PNG{FT_HEIGHTMAP, DFT_HEIGHTMAP_PNG};
80constexpr FiosType FIOS_TYPE_BMP{FT_HEIGHTMAP, DFT_HEIGHTMAP_BMP};
81constexpr FiosType FIOS_TYPE_JSON{FT_TOWN_DATA, DFT_TOWN_DATA_JSON};
82
83constexpr FiosType FIOS_TYPE_INVALID{FT_INVALID, DFT_INVALID};
84
88 enum Subdirectory : uint8_t {
89 BASE_DIR,
90 SAVE_DIR,
91 AUTOSAVE_DIR,
92 SCENARIO_DIR,
93 HEIGHTMAP_DIR,
94 OLD_GM_DIR,
95 OLD_DATA_DIR,
96 BASESET_DIR,
97 NEWGRF_DIR,
98 LANG_DIR,
99 AI_DIR,
100 AI_LIBRARY_DIR,
101 GAME_DIR,
102 GAME_LIBRARY_DIR,
103 SCREENSHOT_DIR,
104 SOCIAL_INTEGRATION_DIR,
105 DOCS_DIR,
106 NUM_SUBDIRS,
107 NO_DIRECTORY,
108};
109
113 enum Searchpath : uint8_t {
114 SP_FIRST_DIR,
115 SP_WORKING_DIR = SP_FIRST_DIR,
116#ifdef USE_XDG
117 SP_PERSONAL_DIR_XDG,
118#endif
119 SP_PERSONAL_DIR,
120 SP_SHARED_DIR,
121 SP_BINARY_DIR,
122 SP_INSTALLATION_DIR,
123 SP_APPLICATION_BUNDLE_DIR,
124 SP_AUTODOWNLOAD_DIR,
125 SP_AUTODOWNLOAD_PERSONAL_DIR,
126 SP_AUTODOWNLOAD_PERSONAL_DIR_XDG,
127 NUM_SEARCHPATHS
128};
129
130 DECLARE_INCREMENT_DECREMENT_OPERATORS(Searchpath)
131
132class FileHandle {
133public:
134 static std::optional<FileHandle> Open(const std::string &filename, std::string_view mode);
135 static std::optional<FileHandle> Open(std::string_view filename, std::string_view mode) { return FileHandle::Open(std::string{filename}, mode); }
136
137 inline void Close() { this->f.reset(); }
138
139 inline operator FILE *()
140 {
141 assert(this->f != nullptr);
142 return this->f.get();
143 }
144
145private:
146 AutoRelease<FILE, fclose> f;
147
148 FileHandle(FILE *f) : f(f) { assert(this->f != nullptr); }
149};
150
151/* Ensure has_value() is used consistently. */
152template <> constexpr std::optional<FileHandle>::operator bool() const noexcept = delete;
153
154#endif /* FILEIO_TYPE_H */
autorelease.hpp
Helper for std::unique_ptr to use an arbitrary function as the deleter.
AutoRelease
std::unique_ptr< T, DeleterFromFunc< Tfunc > > AutoRelease
Specialisation of std::unique_ptr for objects which must be deleted by calling a function.
Definition autorelease.hpp:25
FileHandle
Definition fileio_type.h:132
FileHandle::Open
static std::optional< FileHandle > Open(const std::string &filename, std::string_view mode)
Open an RAII file handle if possible.
Definition fileio.cpp:1171
enum_type.hpp
Type (helpers) for enums.
DECLARE_INCREMENT_DECREMENT_OPERATORS
#define DECLARE_INCREMENT_DECREMENT_OPERATORS(enum_type)
For some enums it is useful to have pre/post increment/decrement operators.
Definition enum_type.hpp:63
SaveLoadOperation
SaveLoadOperation
Operation performed on the file.
Definition fileio_type.h:52
SLO_CHECK
@ SLO_CHECK
Load file for checking and/or preview.
Definition fileio_type.h:53
SLO_SAVE
@ SLO_SAVE
File is being saved.
Definition fileio_type.h:55
SLO_LOAD
@ SLO_LOAD
File is being loaded.
Definition fileio_type.h:54
SLO_INVALID
@ SLO_INVALID
Unknown file operation.
Definition fileio_type.h:57
DetailedFileType
DetailedFileType
Kinds of files in each AbstractFileType.
Definition fileio_type.h:28
DFT_FIOS_DRIVE
@ DFT_FIOS_DRIVE
A drive (letter) entry.
Definition fileio_type.h:41
DFT_GAME_FILE
@ DFT_GAME_FILE
Save game or scenario file.
Definition fileio_type.h:31
DFT_HEIGHTMAP_BMP
@ DFT_HEIGHTMAP_BMP
BMP file.
Definition fileio_type.h:34
DFT_HEIGHTMAP_PNG
@ DFT_HEIGHTMAP_PNG
PNG file.
Definition fileio_type.h:35
DFT_FIOS_DIR
@ DFT_FIOS_DIR
A directory entry.
Definition fileio_type.h:43
DFT_FIOS_PARENT
@ DFT_FIOS_PARENT
A parent directory entry.
Definition fileio_type.h:42
DFT_INVALID
@ DFT_INVALID
Unknown or invalid file.
Definition fileio_type.h:48
DFT_FIOS_DIRECT
@ DFT_FIOS_DIRECT
Direct filename.
Definition fileio_type.h:44
DFT_TOWN_DATA_JSON
@ DFT_TOWN_DATA_JSON
JSON file.
Definition fileio_type.h:38
DFT_END
@ DFT_END
End of this enum. Supports a compile time size check against _fios_colours in fios_gui....
Definition fileio_type.h:46
DFT_OLD_GAME_FILE
@ DFT_OLD_GAME_FILE
Old save game or scenario file.
Definition fileio_type.h:30
Searchpath
Searchpath
Types of searchpaths OpenTTD might use.
Definition fileio_type.h:113
SP_SHARED_DIR
@ SP_SHARED_DIR
Search in the shared directory, like 'Shared Files' under Windows.
Definition fileio_type.h:120
SP_INSTALLATION_DIR
@ SP_INSTALLATION_DIR
Search in the installation directory.
Definition fileio_type.h:122
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:126
SP_BINARY_DIR
@ SP_BINARY_DIR
Search in the directory where the binary resides.
Definition fileio_type.h:121
SP_AUTODOWNLOAD_PERSONAL_DIR
@ SP_AUTODOWNLOAD_PERSONAL_DIR
Search within the autodownload directory located in the personal directory.
Definition fileio_type.h:125
SP_PERSONAL_DIR
@ SP_PERSONAL_DIR
Search in the personal directory.
Definition fileio_type.h:119
SP_WORKING_DIR
@ SP_WORKING_DIR
Search in the working directory.
Definition fileio_type.h:115
SP_APPLICATION_BUNDLE_DIR
@ SP_APPLICATION_BUNDLE_DIR
Search within the application bundle.
Definition fileio_type.h:123
SP_AUTODOWNLOAD_DIR
@ SP_AUTODOWNLOAD_DIR
Search within the autodownload directory.
Definition fileio_type.h:124
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:88
OLD_DATA_DIR
@ OLD_DATA_DIR
Old subdirectory for the data.
Definition fileio_type.h:95
NO_DIRECTORY
@ NO_DIRECTORY
A path without any base directory.
Definition fileio_type.h:107
AI_LIBRARY_DIR
@ AI_LIBRARY_DIR
Subdirectory for all AI libraries.
Definition fileio_type.h:100
SCREENSHOT_DIR
@ SCREENSHOT_DIR
Subdirectory for all screenshots.
Definition fileio_type.h:103
SOCIAL_INTEGRATION_DIR
@ SOCIAL_INTEGRATION_DIR
Subdirectory for all social integration plugins.
Definition fileio_type.h:104
GAME_LIBRARY_DIR
@ GAME_LIBRARY_DIR
Subdirectory for all GS libraries.
Definition fileio_type.h:102
AI_DIR
@ AI_DIR
Subdirectory for all AI files.
Definition fileio_type.h:99
OLD_GM_DIR
@ OLD_GM_DIR
Old subdirectory for the music.
Definition fileio_type.h:94
SCENARIO_DIR
@ SCENARIO_DIR
Base directory for all scenarios.
Definition fileio_type.h:92
BASE_DIR
@ BASE_DIR
Base directory for all subdirectories.
Definition fileio_type.h:89
SAVE_DIR
@ SAVE_DIR
Base directory for all savegames.
Definition fileio_type.h:90
DOCS_DIR
@ DOCS_DIR
Subdirectory for documentation.
Definition fileio_type.h:105
NUM_SUBDIRS
@ NUM_SUBDIRS
Number of subdirectories.
Definition fileio_type.h:106
HEIGHTMAP_DIR
@ HEIGHTMAP_DIR
Subdirectory of scenario for heightmaps.
Definition fileio_type.h:93
NEWGRF_DIR
@ NEWGRF_DIR
Subdirectory for all NewGRFs.
Definition fileio_type.h:97
AUTOSAVE_DIR
@ AUTOSAVE_DIR
Subdirectory of save for autosaves.
Definition fileio_type.h:91
LANG_DIR
@ LANG_DIR
Subdirectory for all translation files.
Definition fileio_type.h:98
BASESET_DIR
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
Definition fileio_type.h:96
GAME_DIR
@ GAME_DIR
Subdirectory for all game scripts.
Definition fileio_type.h:101
AbstractFileType
AbstractFileType
The different abstract types of files that the system knows about.
Definition fileio_type.h:17
FT_SCENARIO
@ FT_SCENARIO
old or new scenario
Definition fileio_type.h:20
FT_HEIGHTMAP
@ FT_HEIGHTMAP
heightmap file
Definition fileio_type.h:21
FT_NONE
@ FT_NONE
nothing to do
Definition fileio_type.h:18
FT_SAVEGAME
@ FT_SAVEGAME
old or new savegame
Definition fileio_type.h:19
FT_INVALID
@ FT_INVALID
Invalid or unknown file type.
Definition fileio_type.h:24
FT_TOWN_DATA
@ FT_TOWN_DATA
town data file
Definition fileio_type.h:22
FiosType
Elements of a file system that are recognized.
Definition fileio_type.h:63
FiosType::detailed
DetailedFileType detailed
Detailed file type.
Definition fileio_type.h:65
FiosType::abstract
AbstractFileType abstract
Abstract file type.
Definition fileio_type.h:64