OpenTTD Source 20261112-master-gbcbec5e63f
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 "misc/autorelease.hpp"
14
25
27enum class DetailedFileType : uint8_t {
28 /* Save game and scenario files. */
31
32 /* Heightmap files. */
35
36 /* Town data files. */
38
39 /* fios 'files' */
44
46
47 Invalid = 255,
48};
49
51enum class SaveLoadOperation : uint8_t {
55
57};
58
62struct FiosType {
65
66 constexpr bool operator==(const FiosType &) const noexcept = default;
67};
68
73
81
83
108
129
130class FileHandle {
131public:
139 static std::optional<FileHandle> Open(const std::string &filename, std::string_view mode);
140
148 static std::optional<FileHandle> Open(std::string_view filename, std::string_view mode) { return FileHandle::Open(std::string{filename}, mode); }
149
150 inline void Close() { this->f.reset(); }
151
152 inline operator FILE *()
153 {
154 assert(this->f != nullptr);
155 return this->f.get();
156 }
157
158private:
160
161 FileHandle(FILE *f) : f(f) { assert(this->f != nullptr); }
162};
163
164/* Ensure has_value() is used consistently. */
165template <> constexpr std::optional<FileHandle>::operator bool() const noexcept = delete;
166
167#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:1218
SaveLoadOperation
Operation performed on the file.
Definition fileio_type.h:51
@ Check
Load file for checking and/or preview.
Definition fileio_type.h:52
@ Save
File is being saved.
Definition fileio_type.h:54
@ Load
File is being loaded.
Definition fileio_type.h:53
DetailedFileType
Kinds of files in each AbstractFileType.
Definition fileio_type.h:27
@ FiosDirect
Direct filename.
Definition fileio_type.h:43
@ HeightmapPng
PNG file.
Definition fileio_type.h:34
@ FiosDrive
A drive (letter) entry.
Definition fileio_type.h:40
@ Invalid
Unknown or invalid file.
Definition fileio_type.h:47
@ HeightmapBmp
BMP file.
Definition fileio_type.h:33
@ OldGameFile
Old save game or scenario file.
Definition fileio_type.h:29
@ GameFile
Save game or scenario file.
Definition fileio_type.h:30
@ End
End marker.
Definition fileio_type.h:45
@ TownDataJson
JSON file.
Definition fileio_type.h:37
@ FiosDirectory
A directory entry.
Definition fileio_type.h:42
@ FiosParent
A parent directory entry.
Definition fileio_type.h:41
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.
@ TransportTycoonDeluxeDir
Search within the Transport Tycoon Deluxe data directory (if installed).
@ 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:87
@ Base
Base directory for all subdirectories.
Definition fileio_type.h:88
@ Autosave
Subdirectory of save for autosaves.
Definition fileio_type.h:90
@ Lang
Subdirectory for all translation files.
Definition fileio_type.h:97
@ NewGrf
Subdirectory for all NewGRFs.
Definition fileio_type.h:96
@ Screenshot
Subdirectory for all screenshots.
@ GsLibrary
Subdirectory for all GS libraries.
@ Ai
Subdirectory for all AI files.
Definition fileio_type.h:98
@ Gs
Subdirectory for all game scripts.
@ Docs
Subdirectory for documentation.
@ Baseset
Subdirectory for all base data (base sets, intro game).
Definition fileio_type.h:95
@ OldData
Old subdirectory for the data.
Definition fileio_type.h:94
@ AiLibrary
Subdirectory for all AI libraries.
Definition fileio_type.h:99
@ OldGm
Old subdirectory for the music.
Definition fileio_type.h:93
AbstractFileType
The different abstract types of files that the system knows about.
Definition fileio_type.h:16
@ Savegame
old or new savegame
Definition fileio_type.h:18
@ Invalid
Invalid or unknown file type.
Definition fileio_type.h:23
@ Scenario
old or new scenario
Definition fileio_type.h:19
@ Heightmap
heightmap file
Definition fileio_type.h:20
@ None
nothing to do
Definition fileio_type.h:17
@ TownData
town data file
Definition fileio_type.h:21
@ Invalid
broken savegame (used internally)
Definition saveload.h:438
Elements of a file system that are recognized.
Definition fileio_type.h:62
DetailedFileType detailed
Detailed file type.
Definition fileio_type.h:64
AbstractFileType abstract
Abstract file type.
Definition fileio_type.h:63