Update: Translations from eints
[openttd-github.git] / src / fileio_func.h
blob0c2cff30f9396a0a96c7383311b2f1764ce94c80
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 */
8 /** @file fileio_func.h Functions for Standard In/Out file operations */
10 #ifndef FILEIO_FUNC_H
11 #define FILEIO_FUNC_H
13 #include "core/enum_type.hpp"
14 #include "fileio_type.h"
16 std::optional<FileHandle> FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize = nullptr);
17 bool FioCheckFileExists(const std::string &filename, Subdirectory subdir);
18 std::string FioFindFullPath(Subdirectory subdir, const std::string &filename);
19 std::string FioGetDirectory(Searchpath sp, Subdirectory subdir);
20 std::string FioFindDirectory(Subdirectory subdir);
21 void FioCreateDirectory(const std::string &name);
22 bool FioRemove(const std::string &filename);
24 const char *FiosGetScreenshotDir();
26 void SanitizeFilename(std::string &filename);
27 void AppendPathSeparator(std::string &buf);
28 void DeterminePaths(const char *exe, bool only_local_path);
29 std::unique_ptr<char[]> ReadFileToMem(const std::string &filename, size_t &lenp, size_t maxsize);
30 bool FileExists(const std::string &filename);
31 bool ExtractTar(const std::string &tar_filename, Subdirectory subdir);
33 extern std::string _personal_dir; ///< custom directory for personal settings, saves, newgrf, etc.
34 extern std::vector<Searchpath> _valid_searchpaths;
36 /** Helper for scanning for files with a given name */
37 class FileScanner {
38 protected:
39 Subdirectory subdir; ///< The current sub directory we are searching through
40 public:
41 /** Destruct the proper one... */
42 virtual ~FileScanner() = default;
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);
47 /**
48 * Add a file with the given filename.
49 * @param filename the full path to the file to read
50 * @param basepath_length amount of characters to chop of before to get a
51 * filename relative to the search path.
52 * @param tar_filename the name of the tar file the file is read from.
53 * @return true if the file is added.
55 virtual bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) = 0;
58 /** Helper for scanning for files with tar as extension */
59 class TarScanner : FileScanner {
60 uint DoScan(Subdirectory sd);
61 public:
62 /** The mode of tar scanning. */
63 enum Mode {
64 NONE = 0, ///< Scan nothing.
65 BASESET = 1 << 0, ///< Scan for base sets.
66 NEWGRF = 1 << 1, ///< Scan for non-base sets.
67 AI = 1 << 2, ///< Scan for AIs and its libraries.
68 SCENARIO = 1 << 3, ///< Scan for scenarios and heightmaps.
69 GAME = 1 << 4, ///< Scan for game scripts.
70 ALL = BASESET | NEWGRF | AI | SCENARIO | GAME, ///< Scan for everything.
73 bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename = {}) override;
75 bool AddFile(Subdirectory sd, const std::string &filename);
77 /** Do the scan for Tars. */
78 static uint DoScan(TarScanner::Mode mode);
81 DECLARE_ENUM_AS_BIT_SET(TarScanner::Mode)
83 #endif /* FILEIO_FUNC_H */