Files
@ r27009:2a13cdb5a0cf
Branch filter:
Location: cpp/openttd-patchpack/source/src/fileio_func.h - annotation
r27009:2a13cdb5a0cf
4.7 KiB
text/x-c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r10039:727fb45b0424 r10039:727fb45b0424 r10039:727fb45b0424 r10039:727fb45b0424 r10039:727fb45b0424 r18371:8ec67d29c1db r10039:727fb45b0424 r24522:0c6c6ad8ded0 r25378:49a072fb8b4d r10039:727fb45b0424 r10039:727fb45b0424 r24526:74cfd45960ed r24525:3150e1a8a4b3 r24524:a57f35a240ed r24524:a57f35a240ed r24524:a57f35a240ed r24525:3150e1a8a4b3 r10039:727fb45b0424 r19849:42900960d059 r19849:42900960d059 r10039:727fb45b0424 r24524:a57f35a240ed r25625:c14e95bbb24a r25103:79e816f12199 r24525:3150e1a8a4b3 r24526:74cfd45960ed r10039:727fb45b0424 r24522:0c6c6ad8ded0 r25378:49a072fb8b4d r10039:727fb45b0424 r10039:727fb45b0424 r18016:ed7b28b213ba r18016:ed7b28b213ba r18016:ed7b28b213ba r10039:727fb45b0424 r10047:088b8b265ea0 r10047:088b8b265ea0 r10047:088b8b265ea0 r11252:706b96dca92b r11252:706b96dca92b r10039:727fb45b0424 r10039:727fb45b0424 r10039:727fb45b0424 r10039:727fb45b0424 r10039:727fb45b0424 r10039:727fb45b0424 r18073:853f8705886d r10039:727fb45b0424 r10039:727fb45b0424 r24529:3dec691db49a r10039:727fb45b0424 r10039:727fb45b0424 r15138:94bac05d24df r15138:94bac05d24df r18370:7fbb35cad336 r15138:94bac05d24df r18371:8ec67d29c1db r18371:8ec67d29c1db r18371:8ec67d29c1db r18371:8ec67d29c1db r18371:8ec67d29c1db r18371:8ec67d29c1db r18371:8ec67d29c1db r18747:12d6e8d373e7 r18747:12d6e8d373e7 r18371:8ec67d29c1db r18371:8ec67d29c1db r24533:a813d0309c92 r15138:94bac05d24df r24529:3dec691db49a r18388:b05150903c57 r15138:94bac05d24df r18371:8ec67d29c1db r15138:94bac05d24df r10039:727fb45b0424 r18371:8ec67d29c1db r18371:8ec67d29c1db r10039:727fb45b0424 r23088:6710c4c79ac6 r10039:727fb45b0424 r10039:727fb45b0424 r10039:727fb45b0424 r24928:f1d8013662df r10039:727fb45b0424 r10039:727fb45b0424 r10039:727fb45b0424 r10039:727fb45b0424 r10039:727fb45b0424 r10039:727fb45b0424 r24928:f1d8013662df r10039:727fb45b0424 r10039:727fb45b0424 r10039:727fb45b0424 r10039:727fb45b0424 r10039:727fb45b0424 r10039:727fb45b0424 r23088:6710c4c79ac6 r10039:727fb45b0424 r10039:727fb45b0424 r10039:727fb45b0424 r10039:727fb45b0424 r10039:727fb45b0424 r10039:727fb45b0424 r10039:727fb45b0424 r10039:727fb45b0424 r10039:727fb45b0424 r10039:727fb45b0424 r25127:b131dacb88fa r10039:727fb45b0424 r10039:727fb45b0424 r22875:0dc0685c0eb6 r22875:0dc0685c0eb6 r22875:0dc0685c0eb6 r22875:0dc0685c0eb6 r22875:0dc0685c0eb6 r22875:0dc0685c0eb6 r22875:0dc0685c0eb6 r22875:0dc0685c0eb6 r22875:0dc0685c0eb6 r22875:0dc0685c0eb6 r22875:0dc0685c0eb6 r22875:0dc0685c0eb6 r22875:0dc0685c0eb6 r24219:58395a92e207 r24219:58395a92e207 r24219:58395a92e207 r24219:58395a92e207 r24219:58395a92e207 r24219:58395a92e207 r24219:58395a92e207 r24219:58395a92e207 r10039:727fb45b0424 | /*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file fileio_func.h Functions for Standard In/Out file operations */
#ifndef FILEIO_FUNC_H
#define FILEIO_FUNC_H
#include "core/enum_type.hpp"
#include "fileio_type.h"
#include <string>
#include <vector>
void FioFCloseFile(FILE *f);
FILE *FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize = nullptr);
bool FioCheckFileExists(const std::string &filename, Subdirectory subdir);
std::string FioFindFullPath(Subdirectory subdir, const char *filename);
std::string FioGetDirectory(Searchpath sp, Subdirectory subdir);
std::string FioFindDirectory(Subdirectory subdir);
void FioCreateDirectory(const std::string &name);
const char *FiosGetScreenshotDir();
void SanitizeFilename(char *filename);
void AppendPathSeparator(std::string &buf);
void DeterminePaths(const char *exe, bool only_local_path);
std::unique_ptr<char[]> ReadFileToMem(const std::string &filename, size_t &lenp, size_t maxsize);
bool FileExists(const std::string &filename);
bool ExtractTar(const std::string &tar_filename, Subdirectory subdir);
extern std::string _personal_dir; ///< custom directory for personal settings, saves, newgrf, etc.
extern std::vector<Searchpath> _valid_searchpaths;
/** Helper for scanning for files with a given name */
class FileScanner {
protected:
Subdirectory subdir; ///< The current sub directory we are searching through
public:
/** Destruct the proper one... */
virtual ~FileScanner() {}
uint Scan(const char *extension, Subdirectory sd, bool tars = true, bool recursive = true);
uint Scan(const char *extension, const char *directory, bool recursive = true);
/**
* Add a file with the given filename.
* @param filename the full path to the file to read
* @param basepath_length amount of characters to chop of before to get a
* filename relative to the search path.
* @param tar_filename the name of the tar file the file is read from.
* @return true if the file is added.
*/
virtual bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) = 0;
};
/** Helper for scanning for files with tar as extension */
class TarScanner : FileScanner {
uint DoScan(Subdirectory sd);
public:
/** The mode of tar scanning. */
enum Mode {
NONE = 0, ///< Scan nothing.
BASESET = 1 << 0, ///< Scan for base sets.
NEWGRF = 1 << 1, ///< Scan for non-base sets.
AI = 1 << 2, ///< Scan for AIs and its libraries.
SCENARIO = 1 << 3, ///< Scan for scenarios and heightmaps.
GAME = 1 << 4, ///< Scan for game scripts.
ALL = BASESET | NEWGRF | AI | SCENARIO | GAME, ///< Scan for everything.
};
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename = {}) override;
bool AddFile(Subdirectory sd, const std::string &filename);
/** Do the scan for Tars. */
static uint DoScan(TarScanner::Mode mode);
};
DECLARE_ENUM_AS_BIT_SET(TarScanner::Mode)
/* Implementation of opendir/readdir/closedir for Windows */
#if defined(_WIN32)
struct DIR;
struct dirent { // XXX - only d_name implemented
wchar_t *d_name; // name of found file
/* little hack which will point to parent DIR struct which will
* save us a call to GetFileAttributes if we want information
* about the file (for example in function fio_bla) */
DIR *dir;
};
DIR *opendir(const wchar_t *path);
struct dirent *readdir(DIR *d);
int closedir(DIR *d);
#else
/* Use system-supplied opendir/readdir/closedir functions */
# include <sys/types.h>
# include <dirent.h>
#endif /* defined(_WIN32) */
/**
* A wrapper around opendir() which will convert the string from
* OPENTTD encoding to that of the filesystem. For all purposes this
* function behaves the same as the original opendir function
* @param path string to open directory of
* @return DIR pointer
*/
static inline DIR *ttd_opendir(const char *path)
{
return opendir(OTTD2FS(path).c_str());
}
/** Auto-close a file upon scope exit. */
class FileCloser {
FILE *f;
public:
FileCloser(FILE *_f) : f(_f) {}
~FileCloser()
{
fclose(f);
}
};
/** Helper to manage a FILE with a \c std::unique_ptr. */
struct FileDeleter {
void operator()(FILE *f)
{
if (f) fclose(f);
}
};
#endif /* FILEIO_FUNC_H */
|