OpenTTD Source 20260421-master-gc2fbc6fdeb
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
9
10#ifndef FILEIO_TYPE_H
11#define FILEIO_TYPE_H
12
13#include "core/enum_type.hpp"
14#include "misc/autorelease.hpp"
15
26
28enum class DetailedFileType : uint8_t {
29 /* Save game and scenario files. */
32
33 /* Heightmap files. */
36
37 /* Town data files. */
39
40 /* fios 'files' */
45
47
48 Invalid = 255,
49};
50
52enum class SaveLoadOperation : uint8_t {
56
58};
59
63struct FiosType {
66
67 constexpr bool operator==(const FiosType &) const noexcept = default;
68};
69
74
82
84
109
129
131
132class FileHandle {
133public:
141 static std::optional<FileHandle> Open(const std::string &filename, std::string_view mode);
142
150 static std::optional<FileHandle> Open(std::string_view filename, std::string_view mode) { return FileHandle::Open(std::string{filename}, mode); }
151
152 inline void Close() { this->f.reset(); }
153
154 inline operator FILE *()
155 {
156 assert(this->f != nullptr);
157 return this->f.get();
158 }
159
160private:
162
163 FileHandle(FILE *f) : f(f) { assert(this->f != nullptr); }
164};
165
166/* Ensure has_value() is used consistently. */
167template <> constexpr std::optional<FileHandle>::operator bool() const noexcept = delete;
168
169#endif /* FILEIO_TYPE_H */
Helper for std::unique_ptr to use an arbitrary function as the deleter.
std::unique_ptr< T, DeleterFromFunc< Tfunc > > AutoRelease
Specialisation of std::unique_ptr for objects which must be deleted by calling a function.
static std::optional< FileHandle > Open(std::string_view filename, std::string_view mode)
Open an RAII file handle if possible.
static std::optional< FileHandle > Open(const std::string &filename, std::string_view mode)
Open an RAII file handle if possible.
Definition fileio.cpp:1172
Type (helpers) for enums.
#define DECLARE_INCREMENT_DECREMENT_OPERATORS(enum_type)
For some enums it is useful to have pre/post increment/decrement operators.
Definition enum_type.hpp:86
SaveLoadOperation
Operation performed on the file.
Definition fileio_type.h:52
@ Check
Load file for checking and/or preview.
Definition fileio_type.h:53
@ Save
File is being saved.
Definition fileio_type.h:55
@ Load
File is being loaded.
Definition fileio_type.h:54
DetailedFileType
Kinds of files in each AbstractFileType.
Definition fileio_type.h:28
@ FiosDirect
Direct filename.
Definition fileio_type.h:44
@ HeightmapPng
PNG file.
Definition fileio_type.h:35
@ FiosDrive
A drive (letter) entry.
Definition fileio_type.h:41
@ Invalid
Unknown or invalid file.
Definition fileio_type.h:48
@ HeightmapBmp
BMP file.
Definition fileio_type.h:34
@ OldGameFile
Old save game or scenario file.
Definition fileio_type.h:30
@ GameFile
Save game or scenario file.
Definition fileio_type.h:31
@ End
End marker.
Definition fileio_type.h:46
@ TownDataJson
JSON file.
Definition fileio_type.h:38
@ FiosDirectory
A directory entry.
Definition fileio_type.h:43
@ FiosParent
A parent directory entry.
Definition fileio_type.h:42
Searchpath
Types of searchpaths OpenTTD might use.
@ Begin
The lowest valid value.
@ WorkingDir
Search in the working directory.
@ AutodownloadDir
Search within the autodownload directory.
@ ApplicationBundleDir
Search within the application bundle.
@ InstallationDir
Search in the installation directory.
@ AutodownloadPersonalDir
Search within the autodownload directory located in the personal directory.
@ SharedDir
Search in the shared directory, like 'Shared Files' under Windows.
@ BinaryDir
Search in the directory where the binary resides.
@ AutodownloadPersonalDirXdg
Search within the autodownload directory located in the personal directory (XDG variant).
@ PersonalDir
Search in the personal directory.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:88
@ Base
Base directory for all subdirectories.
Definition fileio_type.h:89
@ Autosave
Subdirectory of save for autosaves.
Definition fileio_type.h:91
@ Lang
Subdirectory for all translation files.
Definition fileio_type.h:98
@ NewGrf
Subdirectory for all NewGRFs.
Definition fileio_type.h:97
@ Screenshot
Subdirectory for all screenshots.
@ GsLibrary
Subdirectory for all GS libraries.
@ Ai
Subdirectory for all AI files.
Definition fileio_type.h:99
@ Gs
Subdirectory for all game scripts.
@ Docs
Subdirectory for documentation.
@ Baseset
Subdirectory for all base data (base sets, intro game).
Definition fileio_type.h:96
@ OldData
Old subdirectory for the data.
Definition fileio_type.h:95
@ AiLibrary
Subdirectory for all AI libraries.
@ OldGm
Old subdirectory for the music.
Definition fileio_type.h:94
AbstractFileType
The different abstract types of files that the system knows about.
Definition fileio_type.h:17
@ Savegame
old or new savegame
Definition fileio_type.h:19
@ Invalid
Invalid or unknown file type.
Definition fileio_type.h:24
@ Scenario
old or new scenario
Definition fileio_type.h:20
@ Heightmap
heightmap file
Definition fileio_type.h:21
@ None
nothing to do
Definition fileio_type.h:18
@ TownData
town data file
Definition fileio_type.h:22
Elements of a file system that are recognized.
Definition fileio_type.h:63
DetailedFileType detailed
Detailed file type.
Definition fileio_type.h:65
AbstractFileType abstract
Abstract file type.
Definition fileio_type.h:64