author | tron |
Thu, 21 Jul 2005 18:44:27 +0000 | |
changeset 2153 | ecfc674410b4 |
parent 1891 | 862800791170 |
child 2159 | f6284cf5fab0 |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
1891
862800791170
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1198
diff
changeset
|
2 |
#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
|
3 |
#include "fileio.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
|
4 |
#include "variables.h" |
810
a1494b19bd2a
(svn r1281) -Fix: the OS/2 is now finished. Fixes:
truelight
parents:
561
diff
changeset
|
5 |
#if defined(UNIX) || defined(__OS2__) |
0 | 6 |
#include <ctype.h> // required for tolower() |
7 |
#endif |
|
8 |
||
9 |
/*************************************************/ |
|
10 |
/* FILE IO ROUTINES ******************************/ |
|
11 |
/*************************************************/ |
|
12 |
||
13 |
#define FIO_BUFFER_SIZE 512 |
|
14 |
||
15 |
typedef struct { |
|
16 |
byte *buffer, *buffer_end; |
|
17 |
uint32 pos; |
|
18 |
FILE *cur_fh; |
|
19 |
FILE *handles[32]; |
|
20 |
byte buffer_start[512]; |
|
21 |
} Fio; |
|
22 |
||
23 |
static Fio _fio; |
|
24 |
||
25 |
// 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
|
26 |
uint32 FioGetPos(void) |
0 | 27 |
{ |
28 |
return _fio.pos + (_fio.buffer - _fio.buffer_start) - FIO_BUFFER_SIZE; |
|
29 |
} |
|
30 |
||
31 |
void FioSeekTo(uint32 pos, int mode) |
|
32 |
{ |
|
33 |
if (mode == SEEK_CUR) pos += FioGetPos(); |
|
34 |
_fio.buffer = _fio.buffer_end = _fio.buffer_start + FIO_BUFFER_SIZE; |
|
35 |
fseek(_fio.cur_fh, (_fio.pos=pos), SEEK_SET); |
|
36 |
} |
|
37 |
||
38 |
// Seek to a file and a position |
|
39 |
void FioSeekToFile(uint32 pos) |
|
40 |
{ |
|
41 |
FILE *f = _fio.handles[pos >> 24]; |
|
42 |
assert(f != NULL); |
|
43 |
_fio.cur_fh = f; |
|
44 |
FioSeekTo(pos & 0xFFFFFF, SEEK_SET); |
|
45 |
} |
|
46 |
||
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
|
47 |
byte FioReadByte(void) |
0 | 48 |
{ |
49 |
if (_fio.buffer == _fio.buffer_end) { |
|
50 |
_fio.pos += FIO_BUFFER_SIZE; |
|
51 |
fread(_fio.buffer = _fio.buffer_start, 1, FIO_BUFFER_SIZE, _fio.cur_fh); |
|
52 |
} |
|
53 |
return *_fio.buffer++; |
|
54 |
} |
|
55 |
||
56 |
void FioSkipBytes(int n) |
|
57 |
{ |
|
58 |
for(;;) { |
|
59 |
int m = min(_fio.buffer_end - _fio.buffer, n); |
|
60 |
_fio.buffer += m; |
|
61 |
n -= m; |
|
62 |
if (n == 0) break; |
|
63 |
FioReadByte(); |
|
64 |
n--; |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
||
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
|
69 |
uint16 FioReadWord(void) |
0 | 70 |
{ |
71 |
byte b = FioReadByte(); |
|
72 |
return (FioReadByte() << 8) | b; |
|
73 |
} |
|
74 |
||
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
|
75 |
uint32 FioReadDword(void) |
0 | 76 |
{ |
77 |
uint b = FioReadWord(); |
|
78 |
return (FioReadWord() << 16) | b; |
|
79 |
} |
|
80 |
||
81 |
void FioReadBlock(void *ptr, uint size) |
|
82 |
{ |
|
83 |
FioSeekTo(FioGetPos(), SEEK_SET); |
|
84 |
_fio.pos += size; |
|
85 |
fread(ptr, 1, size, _fio.cur_fh); |
|
86 |
} |
|
87 |
||
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
|
88 |
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
|
89 |
{ |
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 |
if (_fio.handles[slot] != NULL) { |
1109
ecb98f43ba2c
(svn r1610) Remove trailing whitespace (last time ever, i hope)
tron
parents:
1093
diff
changeset
|
91 |
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
|
92 |
_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
|
93 |
} |
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 |
} |
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 |
|
1036
8a82ce28b724
(svn r1537) -Fix: Close all and any open filehandles open at shutdown (tamlin)
darkvater
parents:
915
diff
changeset
|
96 |
void FioCloseAll(void) |
0 | 97 |
{ |
98 |
int i; |
|
99 |
||
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
|
100 |
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
|
101 |
FioCloseFile(i); |
0 | 102 |
} |
103 |
||
1198
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
104 |
bool FiosCheckFileExists(const char *filename) |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
105 |
{ |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
106 |
FILE *f; |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
107 |
char buf[MAX_PATH]; |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
108 |
|
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
109 |
sprintf(buf, "%s%s", _path.data_dir, filename); |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
110 |
|
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
111 |
f = fopen(buf, "rb"); |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
112 |
#if !defined(WIN32) |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
113 |
if (f == NULL) { |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
114 |
char *s; |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
115 |
// Make lower case and try again |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
116 |
for(s=buf + strlen(_path.data_dir) - 1; *s != 0; s++) |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
117 |
*s = tolower(*s); |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
118 |
f = fopen(buf, "rb"); |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
119 |
|
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
120 |
#if defined SECOND_DATA_DIR |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
121 |
// tries in the 2nd data directory |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
122 |
if (f == NULL) { |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
123 |
sprintf(buf, "%s%s", _path.second_data_dir, filename); |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
124 |
for(s=buf + strlen(_path.second_data_dir) - 1; *s != 0; s++) |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
125 |
*s = tolower(*s); |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
126 |
f = fopen(buf, "rb"); |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
127 |
} |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
128 |
#endif |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
129 |
} |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
130 |
#endif |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
131 |
|
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
132 |
if (f == NULL) |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
133 |
return false; |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
134 |
else { |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
135 |
fclose(f); |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
136 |
return true; |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
137 |
} |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
138 |
} |
2ad7ac9e1d59
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
139 |
|
0 | 140 |
void FioOpenFile(int slot, const char *filename) |
141 |
{ |
|
142 |
FILE *f; |
|
143 |
char buf[MAX_PATH]; |
|
144 |
||
145 |
sprintf(buf, "%s%s", _path.data_dir, filename); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
146 |
|
0 | 147 |
f = fopen(buf, "rb"); |
148 |
#if !defined(WIN32) |
|
149 |
if (f == NULL) { |
|
150 |
char *s; |
|
151 |
// Make lower case and try again |
|
152 |
for(s=buf + strlen(_path.data_dir) - 1; *s != 0; s++) |
|
153 |
*s = tolower(*s); |
|
154 |
f = fopen(buf, "rb"); |
|
915 | 155 |
|
561
9a0c469a251c
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
193
diff
changeset
|
156 |
#if defined SECOND_DATA_DIR |
9a0c469a251c
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
193
diff
changeset
|
157 |
// tries in the 2nd data directory |
9a0c469a251c
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
193
diff
changeset
|
158 |
if (f == NULL) { |
9a0c469a251c
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
193
diff
changeset
|
159 |
sprintf(buf, "%s%s", _path.second_data_dir, filename); |
9a0c469a251c
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
193
diff
changeset
|
160 |
for(s=buf + strlen(_path.second_data_dir) - 1; *s != 0; s++) |
9a0c469a251c
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
193
diff
changeset
|
161 |
*s = tolower(*s); |
9a0c469a251c
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
193
diff
changeset
|
162 |
f = fopen(buf, "rb"); |
9a0c469a251c
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
193
diff
changeset
|
163 |
} |
9a0c469a251c
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
193
diff
changeset
|
164 |
#endif |
0 | 165 |
} |
166 |
#endif |
|
167 |
||
168 |
if (f == NULL) |
|
169 |
error("Cannot open file '%s'", buf); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
170 |
|
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
|
171 |
FioCloseFile(slot); // if file was opened before, close it |
0 | 172 |
_fio.handles[slot] = f; |
173 |
FioSeekToFile(slot << 24); |
|
174 |
} |
|
175 |
||
176 |