fileio_func.h

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #ifndef FILEIO_FUNC_H
00013 #define FILEIO_FUNC_H
00014 
00015 #include "core/enum_type.hpp"
00016 #include "fileio_type.h"
00017 
00018 void FioSeekTo(size_t pos, int mode);
00019 void FioSeekToFile(uint8 slot, size_t pos);
00020 size_t FioGetPos();
00021 const char *FioGetFilename(uint8 slot);
00022 byte FioReadByte();
00023 uint16 FioReadWord();
00024 uint32 FioReadDword();
00025 void FioCloseAll();
00026 void FioOpenFile(int slot, const char *filename, Subdirectory subdir);
00027 void FioReadBlock(void *ptr, size_t size);
00028 void FioSkipBytes(int n);
00029 
00036 extern const char *_searchpaths[NUM_SEARCHPATHS];
00037 
00043 static inline bool IsValidSearchPath(Searchpath sp)
00044 {
00045   return sp < NUM_SEARCHPATHS && _searchpaths[sp] != NULL;
00046 }
00047 
00049 #define FOR_ALL_SEARCHPATHS(sp) for (sp = SP_FIRST_DIR; sp < NUM_SEARCHPATHS; sp++) if (IsValidSearchPath(sp))
00050 
00051 void FioFCloseFile(FILE *f);
00052 FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize = NULL);
00053 bool FioCheckFileExists(const char *filename, Subdirectory subdir);
00054 char *FioGetFullPath(char *buf, size_t buflen, Searchpath sp, Subdirectory subdir, const char *filename);
00055 char *FioFindFullPath(char *buf, size_t buflen, Subdirectory subdir, const char *filename);
00056 char *FioAppendDirectory(char *buf, size_t buflen, Searchpath sp, Subdirectory subdir);
00057 char *FioGetDirectory(char *buf, size_t buflen, Subdirectory subdir);
00058 
00059 void SanitizeFilename(char *filename);
00060 bool AppendPathSeparator(char *buf, size_t buflen);
00061 void DeterminePaths(const char *exe);
00062 void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize);
00063 bool FileExists(const char *filename);
00064 const char *FioTarFirstDir(const char *tarname, Subdirectory subdir);
00065 void FioTarAddLink(const char *src, const char *dest, Subdirectory subdir);
00066 bool ExtractTar(const char *tar_filename, Subdirectory subdir);
00067 
00068 extern char *_personal_dir; 
00069 
00071 class FileScanner {
00072 protected:
00073   Subdirectory subdir; 
00074 public:
00076   virtual ~FileScanner() {}
00077 
00078   uint Scan(const char *extension, Subdirectory sd, bool tars = true, bool recursive = true);
00079   uint Scan(const char *extension, const char *directory, bool recursive = true);
00080 
00089   virtual bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) = 0;
00090 };
00091 
00093 class TarScanner : FileScanner {
00094   uint DoScan(Subdirectory sd);
00095 public:
00097   enum Mode {
00098     NONE     = 0,      
00099     BASESET  = 1 << 0, 
00100     NEWGRF   = 1 << 1, 
00101     AI       = 1 << 2, 
00102     SCENARIO = 1 << 3, 
00103     GAME     = 1 << 4, 
00104     ALL      = BASESET | NEWGRF | AI | SCENARIO | GAME, 
00105   };
00106 
00107   /* virtual */ bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename = NULL);
00108 
00109   bool AddFile(Subdirectory sd, const char *filename);
00110 
00112   static uint DoScan(TarScanner::Mode mode);
00113 };
00114 
00115 DECLARE_ENUM_AS_BIT_SET(TarScanner::Mode)
00116 
00117 /* Implementation of opendir/readdir/closedir for Windows */
00118 #if defined(WIN32)
00119 struct DIR;
00120 
00121 struct dirent { // XXX - only d_name implemented
00122   TCHAR *d_name; // name of found file
00123   /* little hack which will point to parent DIR struct which will
00124    * save us a call to GetFileAttributes if we want information
00125    * about the file (for example in function fio_bla) */
00126   DIR *dir;
00127 };
00128 
00129 DIR *opendir(const TCHAR *path);
00130 struct dirent *readdir(DIR *d);
00131 int closedir(DIR *d);
00132 #else
00133 /* Use system-supplied opendir/readdir/closedir functions */
00134 # include <sys/types.h>
00135 # include <dirent.h>
00136 #endif /* defined(WIN32) */
00137 
00145 static inline DIR *ttd_opendir(const char *path)
00146 {
00147   return opendir(OTTD2FS(path));
00148 }
00149 
00150 #endif /* FILEIO_FUNC_H */