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/>.
10 /** @file fileio_func.h Functions for Standard In/Out file operations */
15 #include "core/enum_type.hpp"
16 #include "fileio_type.h"
18 void FioSeekTo(size_t pos
, int mode
);
19 void FioSeekToFile(uint8 slot
, size_t pos
);
21 const char *FioGetFilename(uint8 slot
);
24 uint32
FioReadDword();
26 void FioOpenFile(int slot
, const char *filename
, Subdirectory subdir
);
27 void FioReadBlock(void *ptr
, size_t size
);
28 void FioSkipBytes(int n
);
31 * The search paths OpenTTD could search through.
32 * At least one of the slots has to be filled with a path.
33 * NULL paths tell that there is no such path for the
34 * current operating system.
36 extern const char *_searchpaths
[NUM_SEARCHPATHS
];
39 * Checks whether the given search path is a valid search path
40 * @param sp the search path to check
41 * @return true if the search path is valid
43 static inline bool IsValidSearchPath(Searchpath sp
)
45 return sp
< NUM_SEARCHPATHS
&& _searchpaths
[sp
] != NULL
;
48 /** Iterator for all the search paths */
49 #define FOR_ALL_SEARCHPATHS(sp) for (sp = SP_FIRST_DIR; sp < NUM_SEARCHPATHS; sp++) if (IsValidSearchPath(sp))
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 int FioGetFullPath (char *buf
, size_t buflen
, Searchpath sp
, Subdirectory subdir
, const char *filename
= NULL
);
55 char *FioFindFullPath(char *buf
, size_t buflen
, Subdirectory subdir
, const char *filename
);
56 char *FioGetDirectory(char *buf
, size_t buflen
, Subdirectory subdir
);
58 const char *FiosGetScreenshotDir();
60 void SanitizeFilename(char *filename
);
62 char *BuildDirPath (uint n
, const char *const *parts
);
64 static inline char *BuildDirPath (const char *part0
)
66 return BuildDirPath (1, &part0
);
69 static inline char *BuildDirPath (const char *part0
, const char *part1
)
71 const char *const parts
[2] = { part0
, part1
};
72 return BuildDirPath (2, parts
);
75 void DeterminePaths(const char *exe
);
76 void *ReadFileToMem(const char *filename
, size_t *lenp
, size_t maxsize
);
77 bool FileExists(const char *filename
);
79 extern const char *_personal_dir
; ///< custom directory for personal settings, saves, newgrf, etc.
81 /** Helper for scanning for files with a given name */
84 Subdirectory subdir
; ///< The current sub directory we are searching through
86 /** Destruct the proper one... */
87 virtual ~FileScanner() {}
89 uint
Scan(const char *extension
, Subdirectory sd
, bool tars
= true, bool recursive
= true);
90 uint
Scan(const char *extension
, const char *directory
, const char *dirend
, bool recursive
);
93 * Add a file with the given filename.
94 * @param filename the full path to the file to read
95 * @param basepath_length amount of characters to chop of before to get a
96 * filename relative to the search path.
97 * @param tar_filename the name of the tar file the file is read from.
98 * @return true if the file is added.
100 virtual bool AddFile(const char *filename
, size_t basepath_length
, const char *tar_filename
) = 0;
103 /** Helper for scanning for files with tar as extension */
104 class TarScanner
: FileScanner
{
105 uint
DoScan(Subdirectory sd
);
107 /** The mode of tar scanning. */
109 NONE
= 0, ///< Scan nothing.
110 BASESET
= 1 << 0, ///< Scan for base sets.
111 NEWGRF
= 1 << 1, ///< Scan for non-base sets.
112 AI
= 1 << 2, ///< Scan for AIs and its libraries.
113 SCENARIO
= 1 << 3, ///< Scan for scenarios and heightmaps.
114 GAME
= 1 << 4, ///< Scan for game scripts.
115 ALL
= BASESET
| NEWGRF
| AI
| SCENARIO
| GAME
, ///< Scan for everything.
119 * Scan for tar files in the given search path.
120 * @param sd the sub directory to search in.
121 * @return the number of found files, i.e. the number of times that
122 * AddFile returned true.
124 uint
Scan (Subdirectory sd
)
126 return this->FileScanner::Scan (".tar", sd
, false);
129 /* virtual */ bool AddFile(const char *filename
, size_t basepath_length
, const char *tar_filename
= NULL
);
131 /** Do the scan for Tars. */
132 static uint
DoScan(TarScanner::Mode mode
);
135 DECLARE_ENUM_AS_BIT_SET(TarScanner::Mode
)
137 /* Implementation of opendir/readdir/closedir for Windows */
141 struct dirent
{ // XXX - only d_name implemented
142 TCHAR
*d_name
; // name of found file
143 /* little hack which will point to parent DIR struct which will
144 * save us a call to GetFileAttributes if we want information
145 * about the file (for example in function fio_bla) */
149 DIR *opendir(const TCHAR
*path
);
150 struct dirent
*readdir(DIR *d
);
151 int closedir(DIR *d
);
153 /* Use system-supplied opendir/readdir/closedir functions */
154 # include <sys/types.h>
156 #endif /* defined(WIN32) */
159 * A wrapper around opendir() which will convert the string from
160 * OPENTTD encoding to that of the filesystem. For all purposes this
161 * function behaves the same as the original opendir function
162 * @param path string to open directory of
163 * @return DIR pointer
165 static inline DIR *ttd_opendir(const char *path
)
167 return opendir(OTTD2FS(path
));
170 #endif /* FILEIO_FUNC_H */