2009-08-21 22:21:05 +02:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-08-31 12:50:05 +02:00
|
|
|
/** @file fileio_func.h Functions for Standard In/Out file operations */
|
2007-03-01 02:24:44 +01:00
|
|
|
|
2008-08-31 12:50:05 +02:00
|
|
|
#ifndef FILEIO_FUNC_H
|
|
|
|
#define FILEIO_FUNC_H
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2011-11-14 22:30:37 +01:00
|
|
|
#include "core/enum_type.hpp"
|
2008-08-31 12:50:05 +02:00
|
|
|
#include "fileio_type.h"
|
2007-06-17 17:48:57 +02:00
|
|
|
|
2007-09-16 20:10:52 +02:00
|
|
|
void FioFCloseFile(FILE *f);
|
2020-12-06 21:11:47 +01:00
|
|
|
FILE *FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize = nullptr);
|
2020-12-06 21:11:45 +01:00
|
|
|
bool FioCheckFileExists(const std::string &filename, Subdirectory subdir);
|
2023-04-18 22:21:17 +02:00
|
|
|
std::string FioFindFullPath(Subdirectory subdir, const std::string &filename);
|
2020-12-06 21:11:44 +01:00
|
|
|
std::string FioGetDirectory(Searchpath sp, Subdirectory subdir);
|
|
|
|
std::string FioFindDirectory(Subdirectory subdir);
|
2020-12-06 21:11:45 +01:00
|
|
|
void FioCreateDirectory(const std::string &name);
|
2007-06-17 17:48:57 +02:00
|
|
|
|
2012-12-09 17:52:43 +01:00
|
|
|
const char *FiosGetScreenshotDir();
|
|
|
|
|
2023-05-25 18:02:59 +02:00
|
|
|
void SanitizeFilename(std::string &filename);
|
2020-12-06 21:11:44 +01:00
|
|
|
void AppendPathSeparator(std::string &buf);
|
2021-06-10 23:13:34 +02:00
|
|
|
void DeterminePaths(const char *exe, bool only_local_path);
|
2021-03-28 00:12:32 +01:00
|
|
|
std::unique_ptr<char[]> ReadFileToMem(const std::string &filename, size_t &lenp, size_t maxsize);
|
2020-12-06 21:11:45 +01:00
|
|
|
bool FileExists(const std::string &filename);
|
2020-12-06 21:11:47 +01:00
|
|
|
bool ExtractTar(const std::string &tar_filename, Subdirectory subdir);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2020-12-06 21:11:42 +01:00
|
|
|
extern std::string _personal_dir; ///< custom directory for personal settings, saves, newgrf, etc.
|
2021-04-30 15:41:58 +02:00
|
|
|
extern std::vector<Searchpath> _valid_searchpaths;
|
2007-06-17 17:48:57 +02:00
|
|
|
|
2008-08-31 10:38:30 +02:00
|
|
|
/** Helper for scanning for files with a given name */
|
2011-08-25 12:31:35 +02:00
|
|
|
class FileScanner {
|
|
|
|
protected:
|
|
|
|
Subdirectory subdir; ///< The current sub directory we are searching through
|
2008-08-31 10:38:30 +02:00
|
|
|
public:
|
2008-08-31 21:56:52 +02:00
|
|
|
/** Destruct the proper one... */
|
2023-05-14 23:31:03 +02:00
|
|
|
virtual ~FileScanner() = default;
|
2008-08-31 21:56:52 +02:00
|
|
|
|
2009-03-04 01:17:51 +01:00
|
|
|
uint Scan(const char *extension, Subdirectory sd, bool tars = true, bool recursive = true);
|
2023-05-06 13:22:16 +02:00
|
|
|
uint Scan(const char *extension, const std::string &directory, bool recursive = true);
|
2008-08-31 10:38:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2011-09-08 11:48:52 +02:00
|
|
|
* @param tar_filename the name of the tar file the file is read from.
|
2008-08-31 10:38:30 +02:00
|
|
|
* @return true if the file is added.
|
|
|
|
*/
|
2020-12-06 21:11:50 +01:00
|
|
|
virtual bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) = 0;
|
2008-08-31 10:38:30 +02:00
|
|
|
};
|
|
|
|
|
2010-05-10 11:37:17 +02:00
|
|
|
/** Helper for scanning for files with tar as extension */
|
|
|
|
class TarScanner : FileScanner {
|
2011-11-14 22:28:43 +01:00
|
|
|
uint DoScan(Subdirectory sd);
|
2010-05-10 11:37:17 +02:00
|
|
|
public:
|
2011-11-14 22:30:37 +01:00
|
|
|
/** 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.
|
2011-12-19 21:54:37 +01:00
|
|
|
GAME = 1 << 4, ///< Scan for game scripts.
|
|
|
|
ALL = BASESET | NEWGRF | AI | SCENARIO | GAME, ///< Scan for everything.
|
2011-11-14 22:30:37 +01:00
|
|
|
};
|
|
|
|
|
2020-12-27 19:10:40 +01:00
|
|
|
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename = {}) override;
|
2010-05-10 11:37:17 +02:00
|
|
|
|
2020-12-06 21:11:50 +01:00
|
|
|
bool AddFile(Subdirectory sd, const std::string &filename);
|
2011-11-16 17:54:37 +01:00
|
|
|
|
2010-05-10 11:37:17 +02:00
|
|
|
/** Do the scan for Tars. */
|
2011-11-14 22:30:37 +01:00
|
|
|
static uint DoScan(TarScanner::Mode mode);
|
2010-05-10 11:37:17 +02:00
|
|
|
};
|
2008-08-31 12:50:05 +02:00
|
|
|
|
2011-11-14 22:30:37 +01:00
|
|
|
DECLARE_ENUM_AS_BIT_SET(TarScanner::Mode)
|
|
|
|
|
2008-08-31 12:50:05 +02:00
|
|
|
/* Implementation of opendir/readdir/closedir for Windows */
|
2018-12-09 02:28:14 +01:00
|
|
|
#if defined(_WIN32)
|
2008-08-31 12:50:05 +02:00
|
|
|
struct DIR;
|
|
|
|
|
|
|
|
struct dirent { // XXX - only d_name implemented
|
2021-02-21 20:48:21 +01:00
|
|
|
wchar_t *d_name; // name of found file
|
2008-08-31 12:50:05 +02:00
|
|
|
/* 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;
|
|
|
|
};
|
|
|
|
|
2021-02-21 20:48:21 +01:00
|
|
|
DIR *opendir(const wchar_t *path);
|
2008-08-31 12:50:05 +02:00
|
|
|
struct dirent *readdir(DIR *d);
|
|
|
|
int closedir(DIR *d);
|
|
|
|
#else
|
|
|
|
/* Use system-supplied opendir/readdir/closedir functions */
|
|
|
|
# include <sys/types.h>
|
|
|
|
# include <dirent.h>
|
2018-12-09 02:28:14 +01:00
|
|
|
#endif /* defined(_WIN32) */
|
2008-08-31 12:50:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2021-02-21 17:03:19 +01:00
|
|
|
return opendir(OTTD2FS(path).c_str());
|
2008-08-31 12:50:05 +02:00
|
|
|
}
|
|
|
|
|
2018-05-20 03:55:06 +02:00
|
|
|
|
|
|
|
/** Auto-close a file upon scope exit. */
|
|
|
|
class FileCloser {
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
public:
|
|
|
|
FileCloser(FILE *_f) : f(_f) {}
|
|
|
|
~FileCloser()
|
|
|
|
{
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-17 23:32:10 +02:00
|
|
|
/** Helper to manage a FILE with a \c std::unique_ptr. */
|
|
|
|
struct FileDeleter {
|
|
|
|
void operator()(FILE *f)
|
|
|
|
{
|
|
|
|
if (f) fclose(f);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-08-31 12:50:05 +02:00
|
|
|
#endif /* FILEIO_FUNC_H */
|