OpenTTD Source: src/fileio_func.h Source File

OpenTTD Source 15.0-RC2

fileio_func.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_FUNC_H
11#define FILEIO_FUNC_H
12
13#include "core/enum_type.hpp"
14#include "fileio_type.h"
15
16std::optional<FileHandle> FioFOpenFile(std::string_view filename, std::string_view mode, Subdirectory subdir, size_t *filesize = nullptr);
17bool FioCheckFileExists(std::string_view filename, Subdirectory subdir);
18std::string FioFindFullPath(Subdirectory subdir, std::string_view filename);
19std::string FioGetDirectory(Searchpath sp, Subdirectory subdir);
20std::string FioFindDirectory(Subdirectory subdir);
21void FioCreateDirectory(const std::string &name);
22bool FioRemove(const std::string &filename);
23
24std::string_view FiosGetScreenshotDir();
25
26void SanitizeFilename(std::string &filename);
27void AppendPathSeparator(std::string &buf);
28void DeterminePaths(std::string_view exe, bool only_local_path);
29std::unique_ptr<char[]> ReadFileToMem(const std::string &filename, size_t &lenp, size_t maxsize);
30bool FileExists(std::string_view filename);
31bool ExtractTar(const std::string &tar_filename, Subdirectory subdir);
32
33extern std::string _personal_dir;
34extern std::vector<Searchpath> _valid_searchpaths;
35
37 class FileScanner {
38protected:
39 Subdirectory subdir;
40public:
42 virtual ~FileScanner() = default;
43
44 uint Scan(std::string_view extension, Subdirectory sd, bool tars = true, bool recursive = true);
45 uint Scan(std::string_view extension, const std::string &directory, bool recursive = true);
46
55 virtual bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) = 0;
56};
57
59 class TarScanner : FileScanner {
60 uint DoScan(Subdirectory sd);
61public:
63 enum class Mode : uint8_t {
64 Baseset,
65 NewGRF,
66 AI,
67 Scenario,
68 Game,
69 };
70 using Modes = EnumBitSet<Mode, uint8_t>;
71
72 static constexpr Modes MODES_ALL = {Mode::Baseset, Mode::NewGRF, Mode::AI, Mode::Scenario, Mode::Game};
73
74 bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename = {}) override;
75
76 bool AddFile(Subdirectory sd, const std::string &filename);
77
79 static uint DoScan(TarScanner::Modes modes);
80};
81
82#endif /* FILEIO_FUNC_H */
EnumBitSet
Enum-as-bit-set wrapper.
Definition enum_type.hpp:177
FileScanner
Helper for scanning for files with a given name.
Definition fileio_func.h:37
FileScanner::Scan
uint Scan(std::string_view extension, Subdirectory sd, bool tars=true, bool recursive=true)
Scan for files with the given extension in the given search path.
Definition fileio.cpp:1115
FileScanner::~FileScanner
virtual ~FileScanner()=default
Destruct the proper one...
FileScanner::AddFile
virtual bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename)=0
Add a file with the given filename.
FileScanner::subdir
Subdirectory subdir
The current sub directory we are searching through.
Definition fileio_func.h:39
TarScanner
Helper for scanning for files with tar as extension.
Definition fileio_func.h:59
TarScanner::Mode
Mode
The mode of tar scanning.
Definition fileio_func.h:63
TarScanner::Mode::AI
@ AI
Scan for AIs and its libraries.
TarScanner::Mode::Scenario
@ Scenario
Scan for scenarios and heightmaps.
TarScanner::Mode::Game
@ Game
Scan for game scripts.
TarScanner::Mode::Baseset
@ Baseset
Scan for base sets.
TarScanner::Mode::NewGRF
@ NewGRF
Scan for non-base sets.
TarScanner::DoScan
uint DoScan(Subdirectory sd)
Perform the scanning of a particular subdirectory.
Definition fileio.cpp:377
TarScanner::MODES_ALL
static constexpr Modes MODES_ALL
Scan for everything.
Definition fileio_func.h:72
TarScanner::AddFile
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename={}) override
Add a file with the given filename.
Definition fileio.cpp:440
enum_type.hpp
Type (helpers) for enums.
ExtractTar
bool ExtractTar(const std::string &tar_filename, Subdirectory subdir)
Extract the tar with the given filename in the directory where the tar resides.
Definition fileio.cpp:590
DeterminePaths
void DeterminePaths(std::string_view exe, bool only_local_path)
Acquire the base paths (personal dir and game data dir), fill all other paths (save dir,...
Definition fileio.cpp:880
FioRemove
bool FioRemove(const std::string &filename)
Remove a file.
Definition fileio.cpp:327
SanitizeFilename
void SanitizeFilename(std::string &filename)
Sanitizes a filename, i.e.
Definition fileio.cpp:1004
AppendPathSeparator
void AppendPathSeparator(std::string &buf)
Appends, if necessary, the path separator character to the end of the string.
Definition fileio.cpp:348
_personal_dir
std::string _personal_dir
custom directory for personal settings, saves, newgrf, etc.
Definition fileio.cpp:871
FioCreateDirectory
void FioCreateDirectory(const std::string &name)
Create a directory with the given name If the parent directory does not exist, it will try to create ...
Definition fileio.cpp:315
FileExists
bool FileExists(std::string_view filename)
Test whether the given filename exists.
Definition fileio.cpp:132
FioFOpenFile
std::optional< FileHandle > FioFOpenFile(std::string_view filename, std::string_view mode, Subdirectory subdir, size_t *filesize=nullptr)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition fileio.cpp:242
FioCheckFileExists
bool FioCheckFileExists(std::string_view filename, Subdirectory subdir)
Check whether the given file exists.
Definition fileio.cpp:121
FiosGetScreenshotDir
std::string_view FiosGetScreenshotDir()
Get the directory for screenshots.
Definition fios.cpp:581
FioFindFullPath
std::string FioFindFullPath(Subdirectory subdir, std::string_view filename)
Find a path to the filename in one of the search directories.
Definition fileio.cpp:144
ReadFileToMem
std::unique_ptr< char[]> ReadFileToMem(const std::string &filename, size_t &lenp, size_t maxsize)
Load a file into memory.
Definition fileio.cpp:1026
fileio_type.h
Types for Standard In/Out file operations.
Searchpath
Searchpath
Types of searchpaths OpenTTD might use.
Definition fileio_type.h:113
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:88