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
|
|
|
|
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();
|
2004-08-09 17:04:08 +00:00
|
|
|
void FioOpenFile(int slot, const char *filename);
|
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);
|
2007-09-13 18:46:29 +00:00
|
|
|
FILE *FioFOpenFile(const char *filename, const char *mode = "rb", Subdirectory subdir = DATA_DIR, size_t *filesize = NULL);
|
2007-06-17 15:48:57 +00:00
|
|
|
bool FioCheckFileExists(const char *filename, Subdirectory subdir = DATA_DIR);
|
|
|
|
char *FioGetFullPath(char *buf, size_t buflen, Searchpath sp, Subdirectory subdir, const char *filename);
|
|
|
|
char *FioFindFullPath(char *buf, size_t buflen, Subdirectory subdir, const char *filename);
|
|
|
|
char *FioAppendDirectory(char *buf, size_t buflen, Searchpath sp, Subdirectory subdir);
|
|
|
|
char *FioGetDirectory(char *buf, size_t buflen, Subdirectory subdir);
|
|
|
|
|
2007-06-12 15:46:34 +00:00
|
|
|
void SanitizeFilename(char *filename);
|
2010-05-10 09:49:02 +00:00
|
|
|
bool AppendPathSeparator(char *buf, size_t buflen);
|
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);
|
2009-01-12 17:11:45 +00:00
|
|
|
const char *FioTarFirstDir(const char *tarname);
|
|
|
|
void FioTarAddLink(const char *src, const char *dest);
|
2010-02-26 00:00:55 +00:00
|
|
|
bool ExtractTar(const char *tar_filename);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-17 15:48:57 +00:00
|
|
|
extern char *_personal_dir; ///< custom directory for personal settings, saves, newgrf, etc.
|
|
|
|
|
2008-08-31 08:38:30 +00:00
|
|
|
/** Helper for scanning for files with a given name */
|
|
|
|
class FileScanner
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
* @return true if the file is added.
|
|
|
|
*/
|
|
|
|
virtual bool AddFile(const char *filename, size_t basepath_length) = 0;
|
|
|
|
};
|
|
|
|
|
2010-05-10 09:37:17 +00:00
|
|
|
/** Helper for scanning for files with tar as extension */
|
|
|
|
class TarScanner : FileScanner {
|
|
|
|
public:
|
|
|
|
/* virtual */ bool AddFile(const char *filename, size_t basepath_length);
|
|
|
|
|
|
|
|
/** Do the scan for Tars. */
|
|
|
|
static uint DoScan();
|
|
|
|
};
|
2008-08-31 10:50:05 +00:00
|
|
|
|
|
|
|
/* Implementation of opendir/readdir/closedir for Windows */
|
|
|
|
#if defined(WIN32)
|
|
|
|
#include <windows.h>
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DIR {
|
|
|
|
HANDLE hFind;
|
|
|
|
/* the dirent returned by readdir.
|
|
|
|
* note: having only one global instance is not possible because
|
|
|
|
* multiple independent opendir/readdir sequences must be supported. */
|
|
|
|
dirent ent;
|
|
|
|
WIN32_FIND_DATA fd;
|
|
|
|
/* since opendir calls FindFirstFile, we need a means of telling the
|
|
|
|
* first call to readdir that we already have a file.
|
|
|
|
* that's the case iff this is true */
|
|
|
|
bool at_first_entry;
|
|
|
|
};
|
|
|
|
|
|
|
|
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>
|
|
|
|
#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));
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* FILEIO_FUNC_H */
|