equal
deleted
inserted
replaced
3 /** @file fileio.cpp Standard In/Out file operations */ |
3 /** @file fileio.cpp Standard In/Out file operations */ |
4 |
4 |
5 #include "stdafx.h" |
5 #include "stdafx.h" |
6 #include "openttd.h" |
6 #include "openttd.h" |
7 #include "fileio.h" |
7 #include "fileio.h" |
8 #include "functions.h" |
|
9 #include "string.h" |
|
10 #include "macros.h" |
|
11 #include "variables.h" |
8 #include "variables.h" |
12 #include "debug.h" |
9 #include "debug.h" |
13 #include "fios.h" |
10 #include "fios.h" |
|
11 #include "core/alloc_func.hpp" |
|
12 #include "core/math_func.hpp" |
|
13 #include "string_func.h" |
14 #ifdef WIN32 |
14 #ifdef WIN32 |
15 #include <windows.h> |
15 #include <windows.h> |
16 #else |
16 #else |
17 #include <pwd.h> |
17 #include <pwd.h> |
18 #include <unistd.h> |
18 #include <unistd.h> |
31 FILE *cur_fh; ///< current file handle |
31 FILE *cur_fh; ///< current file handle |
32 const char *filename; ///< current filename |
32 const char *filename; ///< current filename |
33 FILE *handles[MAX_FILE_SLOTS]; ///< array of file handles we can have open |
33 FILE *handles[MAX_FILE_SLOTS]; ///< array of file handles we can have open |
34 byte buffer_start[FIO_BUFFER_SIZE]; ///< local buffer when read from file |
34 byte buffer_start[FIO_BUFFER_SIZE]; ///< local buffer when read from file |
35 const char *filenames[MAX_FILE_SLOTS]; ///< array of filenames we (should) have open |
35 const char *filenames[MAX_FILE_SLOTS]; ///< array of filenames we (should) have open |
|
36 char *shortnames[MAX_FILE_SLOTS];///< array of short names for spriteloader's use |
36 #if defined(LIMITED_FDS) |
37 #if defined(LIMITED_FDS) |
37 uint open_handles; ///< current amount of open handles |
38 uint open_handles; ///< current amount of open handles |
38 uint usage_count[MAX_FILE_SLOTS]; ///< count how many times this file has been opened |
39 uint usage_count[MAX_FILE_SLOTS]; ///< count how many times this file has been opened |
39 #endif /* LIMITED_FDS */ |
40 #endif /* LIMITED_FDS */ |
40 }; |
41 }; |
45 uint32 FioGetPos() |
46 uint32 FioGetPos() |
46 { |
47 { |
47 return _fio.pos + (_fio.buffer - _fio.buffer_start) - FIO_BUFFER_SIZE; |
48 return _fio.pos + (_fio.buffer - _fio.buffer_start) - FIO_BUFFER_SIZE; |
48 } |
49 } |
49 |
50 |
50 const char *FioGetFilename() |
51 const char *FioGetFilename(uint8 slot) |
51 { |
52 { |
52 return _fio.filename; |
53 return _fio.shortnames[slot]; |
53 } |
54 } |
54 |
55 |
55 void FioSeekTo(uint32 pos, int mode) |
56 void FioSeekTo(uint32 pos, int mode) |
56 { |
57 { |
57 if (mode == SEEK_CUR) pos += FioGetPos(); |
58 if (mode == SEEK_CUR) pos += FioGetPos(); |
129 |
130 |
130 static inline void FioCloseFile(int slot) |
131 static inline void FioCloseFile(int slot) |
131 { |
132 { |
132 if (_fio.handles[slot] != NULL) { |
133 if (_fio.handles[slot] != NULL) { |
133 fclose(_fio.handles[slot]); |
134 fclose(_fio.handles[slot]); |
|
135 |
|
136 free(_fio.shortnames[slot]); |
|
137 _fio.shortnames[slot] = NULL; |
|
138 |
134 _fio.handles[slot] = NULL; |
139 _fio.handles[slot] = NULL; |
135 #if defined(LIMITED_FDS) |
140 #if defined(LIMITED_FDS) |
136 _fio.open_handles--; |
141 _fio.open_handles--; |
137 #endif /* LIMITED_FDS */ |
142 #endif /* LIMITED_FDS */ |
138 } |
143 } |
182 uint32 pos = ftell(f); |
187 uint32 pos = ftell(f); |
183 |
188 |
184 FioCloseFile(slot); // if file was opened before, close it |
189 FioCloseFile(slot); // if file was opened before, close it |
185 _fio.handles[slot] = f; |
190 _fio.handles[slot] = f; |
186 _fio.filenames[slot] = filename; |
191 _fio.filenames[slot] = filename; |
|
192 |
|
193 /* Store the filename without path and extension */ |
|
194 const char *t = strrchr(filename, PATHSEPCHAR); |
|
195 _fio.shortnames[slot] = strdup(t == NULL ? filename : t); |
|
196 char *t2 = strrchr(_fio.shortnames[slot], '.'); |
|
197 if (t2 != NULL) *t2 = '\0'; |
|
198 strtolower(_fio.shortnames[slot]); |
|
199 |
187 #if defined(LIMITED_FDS) |
200 #if defined(LIMITED_FDS) |
188 _fio.usage_count[slot] = 0; |
201 _fio.usage_count[slot] = 0; |
189 _fio.open_handles++; |
202 _fio.open_handles++; |
190 #endif /* LIMITED_FDS */ |
203 #endif /* LIMITED_FDS */ |
191 FioSeekToFile(slot, pos); |
204 FioSeekToFile(slot, pos); |
346 TarFileList::iterator it = _tar_filelist.find(lcfilename); |
359 TarFileList::iterator it = _tar_filelist.find(lcfilename); |
347 free(lcfilename); |
360 free(lcfilename); |
348 if (it != _tar_filelist.end()) { |
361 if (it != _tar_filelist.end()) { |
349 f = FioFOpenFileTar(&((*it).second), filesize); |
362 f = FioFOpenFileTar(&((*it).second), filesize); |
350 } |
363 } |
|
364 } |
|
365 |
|
366 /* Sometimes a full path is given. To support |
|
367 * the 'subdirectory' must be 'removed'. */ |
|
368 if (f == NULL && subdir != NO_DIRECTORY) { |
|
369 f = FioFOpenFile(filename, mode, NO_DIRECTORY, filesize); |
351 } |
370 } |
352 |
371 |
353 return f; |
372 return f; |
354 } |
373 } |
355 |
374 |
638 snprintf(tmp, MAX_PATH, "%s" PATHSEP "%s", homedir, PERSONAL_DIR); |
657 snprintf(tmp, MAX_PATH, "%s" PATHSEP "%s", homedir, PERSONAL_DIR); |
639 AppendPathSeparator(tmp, MAX_PATH); |
658 AppendPathSeparator(tmp, MAX_PATH); |
640 |
659 |
641 _searchpaths[SP_PERSONAL_DIR] = strdup(tmp); |
660 _searchpaths[SP_PERSONAL_DIR] = strdup(tmp); |
642 #endif |
661 #endif |
|
662 |
|
663 #if defined(WITH_SHARED_DIR) |
|
664 snprintf(tmp, MAX_PATH, "%s", SHARED_DIR); |
|
665 AppendPathSeparator(tmp, MAX_PATH); |
|
666 _searchpaths[SP_SHARED_DIR] = strdup(tmp); |
|
667 #else |
643 _searchpaths[SP_SHARED_DIR] = NULL; |
668 _searchpaths[SP_SHARED_DIR] = NULL; |
|
669 #endif |
644 |
670 |
645 #if defined(__MORPHOS__) || defined(__AMIGA__) |
671 #if defined(__MORPHOS__) || defined(__AMIGA__) |
646 _searchpaths[SP_WORKING_DIR] = NULL; |
672 _searchpaths[SP_WORKING_DIR] = NULL; |
647 #else |
673 #else |
648 getcwd(tmp, MAX_PATH); |
674 getcwd(tmp, MAX_PATH); |
756 *filename = '_'; |
782 *filename = '_'; |
757 break; |
783 break; |
758 } |
784 } |
759 } |
785 } |
760 } |
786 } |
|
787 |
|
788 void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize) |
|
789 { |
|
790 FILE *in; |
|
791 byte *mem; |
|
792 size_t len; |
|
793 |
|
794 in = fopen(filename, "rb"); |
|
795 if (in == NULL) return NULL; |
|
796 |
|
797 fseek(in, 0, SEEK_END); |
|
798 len = ftell(in); |
|
799 fseek(in, 0, SEEK_SET); |
|
800 if (len > maxsize || (mem = MallocT<byte>(len + 1)) == NULL) { |
|
801 fclose(in); |
|
802 return NULL; |
|
803 } |
|
804 mem[len] = 0; |
|
805 if (fread(mem, len, 1, in) != 1) { |
|
806 fclose(in); |
|
807 free(mem); |
|
808 return NULL; |
|
809 } |
|
810 fclose(in); |
|
811 |
|
812 *lenp = len; |
|
813 return mem; |
|
814 } |