OpenTTD Source  20241108-master-g80f628063a
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 
22 
23  FT_INVALID = 7,
24  FT_NUMBITS = 3,
25  FT_MASK = (1 << FT_NUMBITS) - 1,
26 };
27 
30  /* Save game and scenario files. */
33 
34  /* Heightmap files. */
37 
38  /* Town data files. */
40 
41  /* fios 'files' */
46 
48 
49  DFT_INVALID = 255,
50 };
51 
57 
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),
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 
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
153  NUM_SEARCHPATHS
154 };
155 
157 
158 class FileHandle {
159 public:
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 
170 private:
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. */
185 template <> 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.
Definition: fileio_type.h:107
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.
Definition: fileio_type.h:139
@ SP_SHARED_DIR
Search in the shared directory, like 'Shared Files' under Windows.
Definition: fileio_type.h:146
@ SP_INSTALLATION_DIR
Search in the installation directory.
Definition: fileio_type.h:148
@ 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
Search in the directory where the binary resides.
Definition: fileio_type.h:147
@ SP_AUTODOWNLOAD_PERSONAL_DIR
Search within the autodownload directory located in the personal directory.
Definition: fileio_type.h:151
@ SP_PERSONAL_DIR
Search in the personal directory.
Definition: fileio_type.h:145
@ SP_WORKING_DIR
Search in the working directory.
Definition: fileio_type.h:141
@ SP_APPLICATION_BUNDLE_DIR
Search within the application bundle.
Definition: fileio_type.h:149
@ SP_AUTODOWNLOAD_DIR
Search within the autodownload directory.
Definition: fileio_type.h:150
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:115
@ OLD_DATA_DIR
Old subdirectory for the data.
Definition: fileio_type.h:122
@ NO_DIRECTORY
A path without any base directory.
Definition: fileio_type.h:133
@ AI_LIBRARY_DIR
Subdirectory for all AI libraries.
Definition: fileio_type.h:127
@ SCREENSHOT_DIR
Subdirectory for all screenshots.
Definition: fileio_type.h:130
@ SOCIAL_INTEGRATION_DIR
Subdirectory for all social integration plugins.
Definition: fileio_type.h:131
@ GAME_LIBRARY_DIR
Subdirectory for all GS libraries.
Definition: fileio_type.h:129
@ AI_DIR
Subdirectory for all AI files.
Definition: fileio_type.h:126
@ OLD_GM_DIR
Old subdirectory for the music.
Definition: fileio_type.h:121
@ SCENARIO_DIR
Base directory for all scenarios.
Definition: fileio_type.h:119
@ BASE_DIR
Base directory for all subdirectories.
Definition: fileio_type.h:116
@ SAVE_DIR
Base directory for all savegames.
Definition: fileio_type.h:117
@ NUM_SUBDIRS
Number of subdirectories.
Definition: fileio_type.h:132
@ HEIGHTMAP_DIR
Subdirectory of scenario for heightmaps.
Definition: fileio_type.h:120
@ NEWGRF_DIR
Subdirectory for all NewGRFs.
Definition: fileio_type.h:124
@ AUTOSAVE_DIR
Subdirectory of save for autosaves.
Definition: fileio_type.h:118
@ LANG_DIR
Subdirectory for all translation files.
Definition: fileio_type.h:125
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
Definition: fileio_type.h:123
@ GAME_DIR
Subdirectory for all game scripts.
Definition: fileio_type.h:128
Helper to close a FILE * with a std::unique_ptr.
Definition: fileio_type.h:172