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_func.h Functions for Standard In/Out file operations */
13 #include "core/enum_type.hpp"
14 #include "fileio_type.h"
18 void FioFCloseFile(FILE *f
);
19 FILE *FioFOpenFile(const std::string
&filename
, const char *mode
, Subdirectory subdir
, size_t *filesize
= nullptr);
20 bool FioCheckFileExists(const std::string
&filename
, Subdirectory subdir
);
21 std::string
FioFindFullPath(Subdirectory subdir
, const char *filename
);
22 std::string
FioGetDirectory(Searchpath sp
, Subdirectory subdir
);
23 std::string
FioFindDirectory(Subdirectory subdir
);
24 void FioCreateDirectory(const std::string
&name
);
26 const char *FiosGetScreenshotDir();
28 void SanitizeFilename(char *filename
);
29 void AppendPathSeparator(std::string
&buf
);
30 void DeterminePaths(const char *exe
, bool only_local_path
);
31 std::unique_ptr
<char[]> ReadFileToMem(const std::string
&filename
, size_t &lenp
, size_t maxsize
);
32 bool FileExists(const std::string
&filename
);
33 bool ExtractTar(const std::string
&tar_filename
, Subdirectory subdir
);
35 extern std::string _personal_dir
; ///< custom directory for personal settings, saves, newgrf, etc.
36 extern std::vector
<Searchpath
> _valid_searchpaths
;
38 /** Helper for scanning for files with a given name */
41 Subdirectory subdir
; ///< The current sub directory we are searching through
43 /** Destruct the proper one... */
44 virtual ~FileScanner() {}
46 uint
Scan(const char *extension
, Subdirectory sd
, bool tars
= true, bool recursive
= true);
47 uint
Scan(const char *extension
, const char *directory
, bool recursive
= true);
50 * Add a file with the given filename.
51 * @param filename the full path to the file to read
52 * @param basepath_length amount of characters to chop of before to get a
53 * filename relative to the search path.
54 * @param tar_filename the name of the tar file the file is read from.
55 * @return true if the file is added.
57 virtual bool AddFile(const std::string
&filename
, size_t basepath_length
, const std::string
&tar_filename
) = 0;
60 /** Helper for scanning for files with tar as extension */
61 class TarScanner
: FileScanner
{
62 uint
DoScan(Subdirectory sd
);
64 /** The mode of tar scanning. */
66 NONE
= 0, ///< Scan nothing.
67 BASESET
= 1 << 0, ///< Scan for base sets.
68 NEWGRF
= 1 << 1, ///< Scan for non-base sets.
69 AI
= 1 << 2, ///< Scan for AIs and its libraries.
70 SCENARIO
= 1 << 3, ///< Scan for scenarios and heightmaps.
71 GAME
= 1 << 4, ///< Scan for game scripts.
72 ALL
= BASESET
| NEWGRF
| AI
| SCENARIO
| GAME
, ///< Scan for everything.
75 bool AddFile(const std::string
&filename
, size_t basepath_length
, const std::string
&tar_filename
= {}) override
;
77 bool AddFile(Subdirectory sd
, const std::string
&filename
);
79 /** Do the scan for Tars. */
80 static uint
DoScan(TarScanner::Mode mode
);
83 DECLARE_ENUM_AS_BIT_SET(TarScanner::Mode
)
85 /* Implementation of opendir/readdir/closedir for Windows */
89 struct dirent
{ // XXX - only d_name implemented
90 wchar_t *d_name
; // name of found file
91 /* little hack which will point to parent DIR struct which will
92 * save us a call to GetFileAttributes if we want information
93 * about the file (for example in function fio_bla) */
97 DIR *opendir(const wchar_t *path
);
98 struct dirent
*readdir(DIR *d
);
101 /* Use system-supplied opendir/readdir/closedir functions */
102 # include <sys/types.h>
104 #endif /* defined(_WIN32) */
107 * A wrapper around opendir() which will convert the string from
108 * OPENTTD encoding to that of the filesystem. For all purposes this
109 * function behaves the same as the original opendir function
110 * @param path string to open directory of
111 * @return DIR pointer
113 static inline DIR *ttd_opendir(const char *path
)
115 return opendir(OTTD2FS(path
).c_str());
119 /** Auto-close a file upon scope exit. */
124 FileCloser(FILE *_f
) : f(_f
) {}
131 /** Helper to manage a FILE with a \c std::unique_ptr. */
133 void operator()(FILE *f
)
139 #endif /* FILEIO_FUNC_H */