2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00: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 10:50:05 +00:00
|
|
|
/** @file fileio_func.h Functions for Standard In/Out file operations */
|
2007-03-01 01:24:44 +00:00
|
|
|
|
2008-08-31 10:50:05 +00:00
|
|
|
#ifndef FILEIO_FUNC_H
|
|
|
|
#define FILEIO_FUNC_H
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2011-11-14 21:30:37 +00:00
|
|
|
#include "core/enum_type.hpp"
|
2008-08-31 10:50:05 +00:00
|
|
|
#include "fileio_type.h"
|
2007-06-17 15:48:57 +00:00
|
|
|
|
2008-05-27 21:41:00 +00:00
|
|
|
void FioSeekTo(size_t pos, int mode);
|
|
|
|
void FioSeekToFile(uint8 slot, size_t pos);
|
|
|
|
size_t FioGetPos();
|
2008-01-22 07:27:06 +00:00
|
|
|
const char *FioGetFilename(uint8 slot);
|
2007-03-07 11:47:46 +00:00
|
|
|
byte FioReadByte();
|
|
|
|
uint16 FioReadWord();
|
|
|
|
uint32 FioReadDword();
|
|
|
|
void FioCloseAll();
|
2011-08-24 13:42:06 +00:00
|
|
|
void FioOpenFile(int slot, const char *filename, Subdirectory subdir);
|
2008-05-08 13:20:54 +00:00
|
|
|
void FioReadBlock(void *ptr, size_t size);
|
2004-08-09 17:04:08 +00:00
|
|
|
void FioSkipBytes(int n);
|
2007-03-12 15:25:33 +00:00
|
|
|
|
2007-06-17 15:48:57 +00:00
|
|
|
/**
|
2011-05-14 18:35:40 +00:00
|
|
|
* The search paths OpenTTD could search through.
|
2007-06-17 15:48:57 +00:00
|
|
|
* At least one of the slots has to be filled with a path.
|
|
|
|
* NULL paths tell that there is no such path for the
|
|
|
|
* current operating system.
|
|
|
|
*/
|
|
|
|
extern const char *_searchpaths[NUM_SEARCHPATHS];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the given search path is a valid search path
|
|
|
|
* @param sp the search path to check
|
|
|
|
* @return true if the search path is valid
|
|
|
|
*/
|
|
|
|
static inline bool IsValidSearchPath(Searchpath sp)
|
|
|
|
{
|
|
|
|
return sp < NUM_SEARCHPATHS && _searchpaths[sp] != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Iterator for all the search paths */
|
2007-06-18 11:04:12 +00:00
|
|
|
#define FOR_ALL_SEARCHPATHS(sp) for (sp = SP_FIRST_DIR; sp < NUM_SEARCHPATHS; sp++) if (IsValidSearchPath(sp))
|
2007-06-17 15:48:57 +00:00
|
|
|
|
2007-09-16 18:10:52 +00:00
|
|
|
void FioFCloseFile(FILE *f);
|
2011-08-24 13:53:34 +00:00
|
|
|
FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize = NULL);
|
|
|
|
bool FioCheckFileExists(const char *filename, Subdirectory subdir);
|
2014-04-23 21:23:21 +00:00
|
|
|
char *FioGetFullPath(char *buf, const char *last, Searchpath sp, Subdirectory subdir, const char *filename);
|
|
|
|
char *FioFindFullPath(char *buf, const char *last, Subdirectory subdir, const char *filename);
|
|
|
|
char *FioAppendDirectory(char *buf, const char *last, Searchpath sp, Subdirectory subdir);
|
|
|
|
char *FioGetDirectory(char *buf, const char *last, Subdirectory subdir);
|
2018-04-29 18:07:22 +00:00
|
|
|
void FioCreateDirectory(const char *name);
|
2007-06-17 15:48:57 +00:00
|
|
|
|
2012-12-09 16:52:43 +00:00
|
|
|
const char *FiosGetScreenshotDir();
|
|
|
|
|
2007-06-12 15:46:34 +00:00
|
|
|
void SanitizeFilename(char *filename);
|
2014-04-23 21:23:21 +00:00
|
|
|
bool AppendPathSeparator(char *buf, const char *last);
|
2007-03-17 11:36:04 +00:00
|
|
|
void DeterminePaths(const char *exe);
|
2007-12-25 11:26:07 +00:00
|
|
|
void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize);
|
|
|
|
bool FileExists(const char *filename);
|
2011-11-14 21:28:43 +00:00
|
|
|
const char *FioTarFirstDir(const char *tarname, Subdirectory subdir);
|
|
|
|
void FioTarAddLink(const char *src, const char *dest, Subdirectory subdir);
|
|
|
|
bool ExtractTar(const char *tar_filename, Subdirectory subdir);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2013-11-13 18:57:25 +00:00
|
|
|
extern const char *_personal_dir; ///< custom directory for personal settings, saves, newgrf, etc.
|
2007-06-17 15:48:57 +00:00
|
|
|
|
2008-08-31 08:38:30 +00:00
|
|
|
/** Helper for scanning for files with a given name */
|
2011-08-25 10:31:35 +00:00
|
|
|
class FileScanner {
|
|
|
|
protected:
|
|
|
|
Subdirectory subdir; ///< The current sub directory we are searching through
|
2008-08-31 08:38:30 +00:00
|
|
|
public:
|
2008-08-31 19:56:52 +00:00
|
|
|
/** Destruct the proper one... */
|
|
|
|
virtual ~FileScanner() {}
|
|
|
|
|
2009-03-04 00:17:51 +00:00
|
|
|
uint Scan(const char *extension, Subdirectory sd, bool tars = true, bool recursive = true);
|
|
|
|
uint Scan(const char *extension, const char *directory, bool recursive = true);
|
2008-08-31 08:38:30 +00: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 09:48:52 +00:00
|
|
|
* @param tar_filename the name of the tar file the file is read from.
|
2008-08-31 08:38:30 +00:00
|
|
|
* @return true if the file is added.
|
|
|
|
*/
|
2011-09-08 09:48:52 +00:00
|
|
|
virtual bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) = 0;
|
2008-08-31 08:38:30 +00:00
|
|
|
};
|
|
|
|
|
2010-05-10 09:37:17 +00:00
|
|
|
/** Helper for scanning for files with tar as extension */
|
|
|
|
class TarScanner : FileScanner {
|
2011-11-14 21:28:43 +00:00
|
|
|
uint DoScan(Subdirectory sd);
|
2010-05-10 09:37:17 +00:00
|
|
|
public:
|
2011-11-14 21:30:37 +00: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 20:54:37 +00:00
|
|
|
GAME = 1 << 4, ///< Scan for game scripts.
|
|
|
|
ALL = BASESET | NEWGRF | AI | SCENARIO | GAME, ///< Scan for everything.
|
2011-11-14 21:30:37 +00:00
|
|
|
};
|
|
|
|
|
2019-03-03 22:25:13 +00:00
|
|
|
bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename = NULL) override;
|
2010-05-10 09:37:17 +00:00
|
|
|
|
2011-11-16 16:54:37 +00:00
|
|
|
bool AddFile(Subdirectory sd, const char *filename);
|
|
|
|
|
2010-05-10 09:37:17 +00:00
|
|
|
/** Do the scan for Tars. */
|
2011-11-14 21:30:37 +00:00
|
|
|
static uint DoScan(TarScanner::Mode mode);
|
2010-05-10 09:37:17 +00:00
|
|
|
};
|
2008-08-31 10:50:05 +00:00
|
|
|
|
2011-11-14 21:30:37 +00:00
|
|
|
DECLARE_ENUM_AS_BIT_SET(TarScanner::Mode)
|
|
|
|
|
2008-08-31 10:50:05 +00:00
|
|
|
/* Implementation of opendir/readdir/closedir for Windows */
|
2018-12-09 01:28:14 +00:00
|
|
|
#if defined(_WIN32)
|
2008-08-31 10:50:05 +00:00
|
|
|
struct DIR;
|
|
|
|
|
|
|
|
struct dirent { // XXX - only d_name implemented
|
|
|
|
TCHAR *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 TCHAR *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>
|
2018-12-09 01:28:14 +00:00
|
|
|
#endif /* defined(_WIN32) */
|
2008-08-31 10:50:05 +00: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)
|
|
|
|
{
|
|
|
|
return opendir(OTTD2FS(path));
|
|
|
|
}
|
|
|
|
|
2018-05-20 01:55:06 +00:00
|
|
|
|
|
|
|
/** Auto-close a file upon scope exit. */
|
|
|
|
class FileCloser {
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
public:
|
|
|
|
FileCloser(FILE *_f) : f(_f) {}
|
|
|
|
~FileCloser()
|
|
|
|
{
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-08-31 10:50:05 +00:00
|
|
|
#endif /* FILEIO_FUNC_H */
|