OpenTTD

fileio_func.h
Go to the documentation of this file.
1 /* $Id: fileio_func.h 26489 2014-04-23 21:23:21Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #ifndef FILEIO_FUNC_H
13 #define FILEIO_FUNC_H
14
15 #include "core/enum_type.hpp"
16 #include "fileio_type.h"
17
18 void FioSeekTo(size_t pos, int mode);
19 void FioSeekToFile(uint8 slot, size_t pos);
20 size_t FioGetPos();
21 const char *FioGetFilename(uint8 slot);
22 byte FioReadByte();
23 uint16 FioReadWord();
24 uint32 FioReadDword();
25 void FioCloseAll();
26 void FioOpenFile(int slot, const char *filename, Subdirectory subdir);
27 void FioReadBlock(void *ptr, size_t size);
28 void FioSkipBytes(int n);
29
36 extern const char *_searchpaths[NUM_SEARCHPATHS];
37
43 static inline bool IsValidSearchPath(Searchpath sp)
44 {
45 return sp < NUM_SEARCHPATHS && _searchpaths[sp] != NULL;
46 }
47
49 #define FOR_ALL_SEARCHPATHS(sp) for (sp = SP_FIRST_DIR; sp < NUM_SEARCHPATHS; sp++) if (IsValidSearchPath(sp))
50
51 void FioFCloseFile(FILE *f);
52 FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize = NULL);
53 bool FioCheckFileExists(const char *filename, Subdirectory subdir);
54 char *FioGetFullPath(char *buf, const char *last, Searchpath sp, Subdirectory subdir, const char *filename);
55 char *FioFindFullPath(char *buf, const char *last, Subdirectory subdir, const char *filename);
56 char *FioAppendDirectory(char *buf, const char *last, Searchpath sp, Subdirectory subdir);
57 char *FioGetDirectory(char *buf, const char *last, Subdirectory subdir);
58
59 const char *FiosGetScreenshotDir();
60
61 void SanitizeFilename(char *filename);
62 bool AppendPathSeparator(char *buf, const char *last);
63 void DeterminePaths(const char *exe);
64 void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize);
65 bool FileExists(const char *filename);
66 const char *FioTarFirstDir(const char *tarname, Subdirectory subdir);
67 void FioTarAddLink(const char *src, const char *dest, Subdirectory subdir);
68 bool ExtractTar(const char *tar_filename, Subdirectory subdir);
69
70 extern const char *_personal_dir;
71
73 class FileScanner {
74 protected:
75 Subdirectory subdir;
76 public:
78 virtual ~FileScanner() {}
79
80 uint Scan(const char *extension, Subdirectory sd, bool tars = true, bool recursive = true);
81 uint Scan(const char *extension, const char *directory, bool recursive = true);
82
91 virtual bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) = 0;
92 };
93
95 class TarScanner : FileScanner {
96 uint DoScan(Subdirectory sd);
97 public:
99 enum Mode {
100 NONE = 0,
101 BASESET = 1 << 0,
102 NEWGRF = 1 << 1,
103 AI = 1 << 2,
104 SCENARIO = 1 << 3,
105 GAME = 1 << 4,
106 ALL = BASESET | NEWGRF | AI | SCENARIO | GAME,
107 };
108
109 /* virtual */ bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename = NULL);
110
111 bool AddFile(Subdirectory sd, const char *filename);
112
114 static uint DoScan(TarScanner::Mode mode);
115 };
116
117 DECLARE_ENUM_AS_BIT_SET(TarScanner::Mode)
118
119 /* Implementation of opendir/readdir/closedir for Windows */
120 #if defined(WIN32)
121 struct DIR;
122
123 struct dirent { // XXX - only d_name implemented
124 TCHAR *d_name; // name of found file
125 /* little hack which will point to parent DIR struct which will
126 * save us a call to GetFileAttributes if we want information
127 * about the file (for example in function fio_bla) */
128 DIR *dir;
129 };
130
131 DIR *opendir(const TCHAR *path);
132 struct dirent *readdir(DIR *d);
133 int closedir(DIR *d);
134 #else
135 /* Use system-supplied opendir/readdir/closedir functions */
136 # include <sys/types.h>
137 # include <dirent.h>
138 #endif /* defined(WIN32) */
139
147 static inline DIR *ttd_opendir(const char *path)
148 {
149 return opendir(OTTD2FS(path));
150 }
151
152 #endif /* FILEIO_FUNC_H */
DECLARE_ENUM_AS_BIT_SET
DECLARE_ENUM_AS_BIT_SET(GenderEthnicity) enum CompanyManagerFaceVariable
Bitgroups of the CompanyManagerFace variable.
Definition: company_manager_face.h:31
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:110
_personal_dir
const char * _personal_dir
custom directory for personal settings, saves, newgrf, etc.
Definition: fileio.cpp:1163
FioCheckFileExists
bool FioCheckFileExists(const char *filename, Subdirectory subdir)
Check whether the given file exists.
Definition: fileio.cpp:312
FileScanner::Scan
uint Scan(const char *extension, Subdirectory sd, bool tars=true, bool recursive=true)
Scan for files with the given extension in the given search path.
Definition: fileio.cpp:1428
FileScanner::~FileScanner
virtual ~FileScanner()
Destruct the proper one...
Definition: fileio_func.h:78
TarScanner
Helper for scanning for files with tar as extension.
Definition: fileio_func.h:95
AppendPathSeparator
bool AppendPathSeparator(char *buf, const char *last)
Appends, if necessary, the path separator character to the end of the string.
Definition: fileio.cpp:573
FioReadWord
uint16 FioReadWord()
Read a word (16 bits) from the file (in low endian format).
Definition: fileio.cpp:166
Searchpath
Searchpath
Types of searchpaths OpenTTD might use.
Definition: fileio_type.h:133
enum_type.hpp
Type (helpers) for enums.
FioGetPos
size_t FioGetPos()
Get position in the current file.
Definition: fileio.cpp:68
FileScanner::AddFile
virtual bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename)=0
Add a file with the given filename.
AI
Main AI class.
Definition: ai.hpp:26
FileScanner
Helper for scanning for files with a given name.
Definition: fileio_func.h:73
FioOpenFile
void FioOpenFile(int slot, const char *filename, Subdirectory subdir)
Open a slotted file.
Definition: fileio.cpp:250
fileio_type.h
Types for Standard In/Out file operations.
FioReadBlock
void FioReadBlock(void *ptr, size_t size)
Read a block.
Definition: fileio.cpp:187
DIR
Definition: win32.cpp:98
FioSeekTo
void FioSeekTo(size_t pos, int mode)
Seek in the current file.
Definition: fileio.cpp:88
DeterminePaths
void DeterminePaths(const char *exe)
Acquire the base paths (personal dir and game data dir), fill all other paths (save dir...
Definition: fileio.cpp:1171
FioCloseAll
void FioCloseAll()
Close all slotted open files.
Definition: fileio.cpp:213
FioSeekToFile
void FioSeekToFile(uint8 slot, size_t pos)
Switch to a different file and seek to a position.
Definition: fileio.cpp:115
FioReadDword
uint32 FioReadDword()
Read a double word (32 bits) from the file (in low endian format).
Definition: fileio.cpp:176
OTTD2FS
const TCHAR * OTTD2FS(const char *name, bool console_cp)
Convert from OpenTTD&#39;s encoding to that of the local environment.
Definition: win32.cpp:631
SanitizeFilename
void SanitizeFilename(char *filename)
Sanitizes a filename, i.e.
Definition: fileio.cpp:1297
FioFindFullPath
char * FioFindFullPath(char *buf, const char *last, Subdirectory subdir, const char *filename)
Find a path to the filename in one of the search directories.
Definition: fileio.cpp:364
_searchpaths
const char * _searchpaths[NUM_SEARCHPATHS]
The search paths OpenTTD could search through.
Definition: fileio.cpp:299
ExtractTar
bool ExtractTar(const char *tar_filename, Subdirectory subdir)
Extract the tar with the given filename in the directory where the tar resides.
Definition: fileio.cpp:924
FileScanner::subdir
Subdirectory subdir
The current sub directory we are searching through.
Definition: fileio_func.h:75
TarScanner::Mode
Mode
The mode of tar scanning.
Definition: fileio_func.h:99
FileExists
bool FileExists(const char *filename)
Test whether the given filename exists.
Definition: fileio.cpp:326
ReadFileToMem
void * ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize)
Load a file into memory.
Definition: fileio.cpp:1319
ttd_opendir
static DIR * ttd_opendir(const char *path)
A wrapper around opendir() which will convert the string from OPENTTD encoding to that of the filesys...
Definition: fileio_func.h:147
FioSkipBytes
void FioSkipBytes(int n)
Skip n bytes ahead in the file.
Definition: fileio.cpp:150
FiosGetScreenshotDir
const char * FiosGetScreenshotDir()
Get the directory for screenshots.
Definition: fios.cpp:644
IsValidSearchPath
static bool IsValidSearchPath(Searchpath sp)
Checks whether the given search path is a valid search path.
Definition: fileio_func.h:43
FioFCloseFile
void FioFCloseFile(FILE *f)
Close a file in a safe way.
Definition: fileio.cpp:342
FioFOpenFile
FILE * FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize=NULL)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition: fileio.cpp:474
FioGetFilename
const char * FioGetFilename(uint8 slot)
Get the filename associated with a slot.
Definition: fileio.cpp:78
FioTarFirstDir
const char * FioTarFirstDir(const char *tarname, Subdirectory subdir)
Find the first directory in a tar archive.
Definition: fileio.cpp:592
FioReadByte
byte FioReadByte()
Read a byte from the file.
Definition: fileio.cpp:133