author | rubidium |
Thu, 01 Feb 2007 15:49:12 +0000 | |
changeset 5893 | 7e431a4abebb |
parent 5584 | 1111b4d36e35 |
child 5967 | e8c5596608ad |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
0 | 3 |
#include "stdafx.h" |
1891
862800791170
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1198
diff
changeset
|
4 |
#include "openttd.h" |
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1039
diff
changeset
|
5 |
#include "fileio.h" |
2163
b17b313113a0
(svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents:
2159
diff
changeset
|
6 |
#include "functions.h" |
4200
e6ba94085b81
(svn r5684) - Codechange: create an strtolower() function that uses tolower() on a whole string and apply it in the places this was used.
Darkvater
parents:
4077
diff
changeset
|
7 |
#include "string.h" |
2159
f6284cf5fab0
(svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents:
2153
diff
changeset
|
8 |
#include "macros.h" |
2153
ecfc674410b4
(svn r2663) Include variables.h only in these files which need it, not globally via openttd.h
tron
parents:
1891
diff
changeset
|
9 |
#include "variables.h" |
0 | 10 |
|
11 |
/*************************************************/ |
|
12 |
/* FILE IO ROUTINES ******************************/ |
|
13 |
/*************************************************/ |
|
14 |
||
15 |
#define FIO_BUFFER_SIZE 512 |
|
16 |
||
17 |
typedef struct { |
|
4203
9f0dade554c1
(svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents:
4202
diff
changeset
|
18 |
byte *buffer, *buffer_end; ///< position pointer in local buffer and last valid byte of buffer |
9f0dade554c1
(svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents:
4202
diff
changeset
|
19 |
uint32 pos; ///< current (system) position in file |
9f0dade554c1
(svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents:
4202
diff
changeset
|
20 |
FILE *cur_fh; ///< current file handle |
9f0dade554c1
(svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents:
4202
diff
changeset
|
21 |
FILE *handles[64]; ///< array of file handles we can have open |
9f0dade554c1
(svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents:
4202
diff
changeset
|
22 |
byte buffer_start[FIO_BUFFER_SIZE]; ///< local buffer when read from file |
0 | 23 |
} Fio; |
24 |
||
25 |
static Fio _fio; |
|
26 |
||
27 |
// Get current position in file |
|
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1039
diff
changeset
|
28 |
uint32 FioGetPos(void) |
0 | 29 |
{ |
30 |
return _fio.pos + (_fio.buffer - _fio.buffer_start) - FIO_BUFFER_SIZE; |
|
31 |
} |
|
32 |
||
33 |
void FioSeekTo(uint32 pos, int mode) |
|
34 |
{ |
|
35 |
if (mode == SEEK_CUR) pos += FioGetPos(); |
|
36 |
_fio.buffer = _fio.buffer_end = _fio.buffer_start + FIO_BUFFER_SIZE; |
|
4203
9f0dade554c1
(svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents:
4202
diff
changeset
|
37 |
_fio.pos = pos; |
9f0dade554c1
(svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents:
4202
diff
changeset
|
38 |
fseek(_fio.cur_fh, _fio.pos, SEEK_SET); |
0 | 39 |
} |
40 |
||
41 |
// Seek to a file and a position |
|
42 |
void FioSeekToFile(uint32 pos) |
|
43 |
{ |
|
44 |
FILE *f = _fio.handles[pos >> 24]; |
|
45 |
assert(f != NULL); |
|
46 |
_fio.cur_fh = f; |
|
4203
9f0dade554c1
(svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents:
4202
diff
changeset
|
47 |
FioSeekTo(GB(pos, 0, 24), SEEK_SET); |
0 | 48 |
} |
49 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1039
diff
changeset
|
50 |
byte FioReadByte(void) |
0 | 51 |
{ |
52 |
if (_fio.buffer == _fio.buffer_end) { |
|
53 |
_fio.pos += FIO_BUFFER_SIZE; |
|
54 |
fread(_fio.buffer = _fio.buffer_start, 1, FIO_BUFFER_SIZE, _fio.cur_fh); |
|
55 |
} |
|
56 |
return *_fio.buffer++; |
|
57 |
} |
|
58 |
||
59 |
void FioSkipBytes(int n) |
|
60 |
{ |
|
2952 | 61 |
for (;;) { |
0 | 62 |
int m = min(_fio.buffer_end - _fio.buffer, n); |
63 |
_fio.buffer += m; |
|
64 |
n -= m; |
|
65 |
if (n == 0) break; |
|
66 |
FioReadByte(); |
|
67 |
n--; |
|
68 |
} |
|
69 |
} |
|
70 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1039
diff
changeset
|
71 |
uint16 FioReadWord(void) |
0 | 72 |
{ |
73 |
byte b = FioReadByte(); |
|
74 |
return (FioReadByte() << 8) | b; |
|
75 |
} |
|
76 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1039
diff
changeset
|
77 |
uint32 FioReadDword(void) |
0 | 78 |
{ |
79 |
uint b = FioReadWord(); |
|
80 |
return (FioReadWord() << 16) | b; |
|
81 |
} |
|
82 |
||
83 |
void FioReadBlock(void *ptr, uint size) |
|
84 |
{ |
|
85 |
FioSeekTo(FioGetPos(), SEEK_SET); |
|
86 |
_fio.pos += size; |
|
87 |
fread(ptr, 1, size, _fio.cur_fh); |
|
88 |
} |
|
89 |
||
1039
309068e65b16
(svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents:
1036
diff
changeset
|
90 |
static inline void FioCloseFile(int slot) |
309068e65b16
(svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents:
1036
diff
changeset
|
91 |
{ |
309068e65b16
(svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents:
1036
diff
changeset
|
92 |
if (_fio.handles[slot] != NULL) { |
1109
ecb98f43ba2c
(svn r1610) Remove trailing whitespace (last time ever, i hope)
tron
parents:
1093
diff
changeset
|
93 |
fclose(_fio.handles[slot]); |
1039
309068e65b16
(svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents:
1036
diff
changeset
|
94 |
_fio.handles[slot] = NULL; |
309068e65b16
(svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents:
1036
diff
changeset
|
95 |
} |
309068e65b16
(svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents:
1036
diff
changeset
|
96 |
} |
309068e65b16
(svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents:
1036
diff
changeset
|
97 |
|
1036
8a82ce28b724
(svn r1537) -Fix: Close all and any open filehandles open at shutdown (tamlin)
darkvater
parents:
915
diff
changeset
|
98 |
void FioCloseAll(void) |
0 | 99 |
{ |
100 |
int i; |
|
101 |
||
1039
309068e65b16
(svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents:
1036
diff
changeset
|
102 |
for (i = 0; i != lengthof(_fio.handles); i++) |
309068e65b16
(svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents:
1036
diff
changeset
|
103 |
FioCloseFile(i); |
0 | 104 |
} |
105 |
||
4201
5c1b9b3043b1
(svn r5685) - Codechange: s/FiosCheckFileExists/FioCheckFileExists/ to unify the naming of these functions.
Darkvater
parents:
4200
diff
changeset
|
106 |
bool FioCheckFileExists(const char *filename) |
1198
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
107 |
{ |
4202
669c27034153
(svn r5686) - Codechange: Use only FioFOpenFile for opening files, use the other similar functions (FioCheckFileExists and FioOpenFile) as its caller.
Darkvater
parents:
4201
diff
changeset
|
108 |
FILE *f = FioFOpenFile(filename); |
669c27034153
(svn r5686) - Codechange: Use only FioFOpenFile for opening files, use the other similar functions (FioCheckFileExists and FioOpenFile) as its caller.
Darkvater
parents:
4201
diff
changeset
|
109 |
if (f == NULL) return false; |
1198
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
110 |
|
4202
669c27034153
(svn r5686) - Codechange: Use only FioFOpenFile for opening files, use the other similar functions (FioCheckFileExists and FioOpenFile) as its caller.
Darkvater
parents:
4201
diff
changeset
|
111 |
fclose(f); |
669c27034153
(svn r5686) - Codechange: Use only FioFOpenFile for opening files, use the other similar functions (FioCheckFileExists and FioOpenFile) as its caller.
Darkvater
parents:
4201
diff
changeset
|
112 |
return true; |
1198
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
113 |
} |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
114 |
|
2736
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
115 |
FILE *FioFOpenFile(const char *filename) |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
116 |
{ |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
117 |
FILE *f; |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
118 |
char buf[MAX_PATH]; |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
119 |
|
5296
e7acddfdd8a7
(svn r7449) -Codechange: Rename _path to _paths as it is technically more correct, but mainly because
Darkvater
parents:
5170
diff
changeset
|
120 |
snprintf(buf, lengthof(buf), "%s%s", _paths.data_dir, filename); |
2736
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
121 |
|
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
122 |
f = fopen(buf, "rb"); |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
123 |
#if !defined(WIN32) |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
124 |
if (f == NULL) { |
5296
e7acddfdd8a7
(svn r7449) -Codechange: Rename _path to _paths as it is technically more correct, but mainly because
Darkvater
parents:
5170
diff
changeset
|
125 |
strtolower(buf + strlen(_paths.data_dir) - 1); |
2736
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
126 |
f = fopen(buf, "rb"); |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
127 |
|
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
128 |
#if defined SECOND_DATA_DIR |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
129 |
// tries in the 2nd data directory |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
130 |
if (f == NULL) { |
5296
e7acddfdd8a7
(svn r7449) -Codechange: Rename _path to _paths as it is technically more correct, but mainly because
Darkvater
parents:
5170
diff
changeset
|
131 |
snprintf(buf, lengthof(buf), "%s%s", _paths.second_data_dir, filename); |
e7acddfdd8a7
(svn r7449) -Codechange: Rename _path to _paths as it is technically more correct, but mainly because
Darkvater
parents:
5170
diff
changeset
|
132 |
strtolower(buf + strlen(_paths.second_data_dir) - 1); |
2736
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
133 |
f = fopen(buf, "rb"); |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
134 |
} |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
135 |
#endif |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
136 |
} |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
137 |
#endif |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
138 |
|
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
139 |
return f; |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
140 |
} |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
141 |
|
0 | 142 |
void FioOpenFile(int slot, const char *filename) |
143 |
{ |
|
4202
669c27034153
(svn r5686) - Codechange: Use only FioFOpenFile for opening files, use the other similar functions (FioCheckFileExists and FioOpenFile) as its caller.
Darkvater
parents:
4201
diff
changeset
|
144 |
FILE *f = FioFOpenFile(filename); |
915 | 145 |
|
5296
e7acddfdd8a7
(svn r7449) -Codechange: Rename _path to _paths as it is technically more correct, but mainly because
Darkvater
parents:
5170
diff
changeset
|
146 |
if (f == NULL) error("Cannot open file '%s%s'", _paths.data_dir, filename); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
147 |
|
1039
309068e65b16
(svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents:
1036
diff
changeset
|
148 |
FioCloseFile(slot); // if file was opened before, close it |
0 | 149 |
_fio.handles[slot] = f; |
150 |
FioSeekToFile(slot << 24); |
|
151 |
} |