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/>.
8 /** @file fileio_type.h Types for Standard In/Out file operations */
13 #include "core/enum_type.hpp"
15 /** The different abstract types of files that the system knows about. */
16 enum AbstractFileType
{
17 FT_NONE
, ///< nothing to do
18 FT_SAVEGAME
, ///< old or new savegame
19 FT_SCENARIO
, ///< old or new scenario
20 FT_HEIGHTMAP
, ///< heightmap file
21 FT_TOWN_DATA
, ///< town data file
23 FT_INVALID
= 7, ///< Invalid or unknown file type.
24 FT_NUMBITS
= 3, ///< Number of bits required for storing a #AbstractFileType value.
25 FT_MASK
= (1 << FT_NUMBITS
) - 1, ///< Bitmask for extracting an abstract file type.
28 /** Kinds of files in each #AbstractFileType. */
29 enum DetailedFileType
{
30 /* Save game and scenario files. */
31 DFT_OLD_GAME_FILE
, ///< Old save game or scenario file.
32 DFT_GAME_FILE
, ///< Save game or scenario file.
34 /* Heightmap files. */
35 DFT_HEIGHTMAP_BMP
, ///< BMP file.
36 DFT_HEIGHTMAP_PNG
, ///< PNG file.
38 /* Town data files. */
39 DFT_TOWN_DATA_JSON
, ///< JSON file.
42 DFT_FIOS_DRIVE
, ///< A drive (letter) entry.
43 DFT_FIOS_PARENT
, ///< A parent directory entry.
44 DFT_FIOS_DIR
, ///< A directory entry.
45 DFT_FIOS_DIRECT
, ///< Direct filename.
47 DFT_END
, ///< End of this enum. Supports a compile time size check against _fios_colours in fios_gui.cpp
49 DFT_INVALID
= 255, ///< Unknown or invalid file.
52 /** Operation performed on the file. */
53 enum SaveLoadOperation
{
54 SLO_CHECK
, ///< Load file for checking and/or preview.
55 SLO_LOAD
, ///< File is being loaded.
56 SLO_SAVE
, ///< File is being saved.
58 SLO_INVALID
, ///< Unknown file operation.
62 * Construct an enum value for #FiosType as a combination of an abstract and a detailed file type.
63 * @param abstract Abstract file type (one of #AbstractFileType).
64 * @param detailed Detailed file type (one of #DetailedFileType).
66 #define MAKE_FIOS_TYPE(abstract, detailed) ((abstract) | ((detailed) << FT_NUMBITS))
69 * Elements of a file system that are recognized.
70 * Values are a combination of #AbstractFileType and #DetailedFileType.
71 * @see GetAbstractFileType GetDetailedFileType
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
),
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
),
87 FIOS_TYPE_INVALID
= MAKE_FIOS_TYPE(FT_INVALID
, DFT_INVALID
),
93 * Extract the abstract file type from a #FiosType.
94 * @param fios_type Type to query.
95 * @return The Abstract file type of the \a fios_type.
97 inline AbstractFileType
GetAbstractFileType(FiosType fios_type
)
99 return static_cast<AbstractFileType
>(static_cast<uint
>(fios_type
) & FT_MASK
);
103 * Extract the detailed file type from a #FiosType.
104 * @param fios_type Type to query.
105 * @return The Detailed file type of the \a fios_type.
107 inline DetailedFileType
GetDetailedFileType(FiosType fios_type
)
109 return static_cast<DetailedFileType
>(fios_type
>> FT_NUMBITS
);
113 * The different kinds of subdirectories OpenTTD uses
116 BASE_DIR
, ///< Base directory for all subdirectories
117 SAVE_DIR
, ///< Base directory for all savegames
118 AUTOSAVE_DIR
, ///< Subdirectory of save for autosaves
119 SCENARIO_DIR
, ///< Base directory for all scenarios
120 HEIGHTMAP_DIR
, ///< Subdirectory of scenario for heightmaps
121 OLD_GM_DIR
, ///< Old subdirectory for the music
122 OLD_DATA_DIR
, ///< Old subdirectory for the data.
123 BASESET_DIR
, ///< Subdirectory for all base data (base sets, intro game)
124 NEWGRF_DIR
, ///< Subdirectory for all NewGRFs
125 LANG_DIR
, ///< Subdirectory for all translation files
126 AI_DIR
, ///< Subdirectory for all %AI files
127 AI_LIBRARY_DIR
,///< Subdirectory for all %AI libraries
128 GAME_DIR
, ///< Subdirectory for all game scripts
129 GAME_LIBRARY_DIR
, ///< Subdirectory for all GS libraries
130 SCREENSHOT_DIR
, ///< Subdirectory for all screenshots
131 SOCIAL_INTEGRATION_DIR
, ///< Subdirectory for all social integration plugins
132 NUM_SUBDIRS
, ///< Number of subdirectories
133 NO_DIRECTORY
, ///< A path without any base directory
137 * Types of searchpaths OpenTTD might use
139 enum Searchpath
: unsigned {
141 SP_WORKING_DIR
= SP_FIRST_DIR
, ///< Search in the working directory
143 SP_PERSONAL_DIR_XDG
, ///< Search in the personal directory from the XDG specification
145 SP_PERSONAL_DIR
, ///< Search in the personal directory
146 SP_SHARED_DIR
, ///< Search in the shared directory, like 'Shared Files' under Windows
147 SP_BINARY_DIR
, ///< Search in the directory where the binary resides
148 SP_INSTALLATION_DIR
, ///< Search in the installation directory
149 SP_APPLICATION_BUNDLE_DIR
, ///< Search within the application bundle
150 SP_AUTODOWNLOAD_DIR
, ///< Search within the autodownload directory
151 SP_AUTODOWNLOAD_PERSONAL_DIR
, ///< Search within the autodownload directory located in the personal directory
152 SP_AUTODOWNLOAD_PERSONAL_DIR_XDG
, ///< Search within the autodownload directory located in the personal directory (XDG variant)
156 DECLARE_POSTFIX_INCREMENT(Searchpath
)
160 static std::optional
<FileHandle
> Open(const std::string
&filename
, const std::string
&mode
);
162 inline void Close() { this->f
.reset(); }
164 inline operator FILE *()
166 assert(this->f
!= nullptr);
167 return this->f
.get();
171 /** Helper to close a FILE * with a \c std::unique_ptr. */
173 void operator ()(FILE *f
)
175 if (f
!= nullptr) fclose(f
);
179 std::unique_ptr
<FILE, FileDeleter
> f
;
181 FileHandle(FILE *f
) : f(f
) { assert(this->f
!= nullptr); }
184 /* Ensure has_value() is used consistently. */
185 template <> constexpr std::optional
<FileHandle
>::operator bool() const noexcept
= delete;
187 #endif /* FILEIO_TYPE_H */