author | KUDr |
Mon, 26 Mar 2007 20:47:18 +0000 | |
branch | cpp_gui |
changeset 6299 | 7892983044e6 |
parent 6298 | c30fe89622df |
child 6303 | 84c215fc8eb8 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
6285
187e3ef04cc9
(svn r9004) [cpp_gui] -Sync with trunk (r8900..r9003)
KUDr
parents:
6218
diff
changeset
|
3 |
/** @file fileio.cpp Standard In/Out file operations */ |
187e3ef04cc9
(svn r9004) [cpp_gui] -Sync with trunk (r8900..r9003)
KUDr
parents:
6218
diff
changeset
|
4 |
|
0 | 5 |
#include "stdafx.h" |
1891
92a3b0aa0946
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1198
diff
changeset
|
6 |
#include "openttd.h" |
1093
e8d26c7dc42f
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1039
diff
changeset
|
7 |
#include "fileio.h" |
2163
637ec3c361f5
(svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents:
2159
diff
changeset
|
8 |
#include "functions.h" |
4200
a45420ba0c23
(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
|
9 |
#include "string.h" |
2159
3b634157c3b2
(svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents:
2153
diff
changeset
|
10 |
#include "macros.h" |
2153
91e89aa8c299
(svn r2663) Include variables.h only in these files which need it, not globally via openttd.h
tron
parents:
1891
diff
changeset
|
11 |
#include "variables.h" |
6218
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
12 |
#include "debug.h" |
0 | 13 |
|
14 |
/*************************************************/ |
|
15 |
/* FILE IO ROUTINES ******************************/ |
|
16 |
/*************************************************/ |
|
17 |
||
18 |
#define FIO_BUFFER_SIZE 512 |
|
6218
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
19 |
#define MAX_HANDLES 64 |
0 | 20 |
|
6298
c30fe89622df
(svn r9119) [cpp_gui] -Sync with trunk (r9003:9100)
bjarni
parents:
6285
diff
changeset
|
21 |
struct Fio { |
4203
790879e123dd
(svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents:
4202
diff
changeset
|
22 |
byte *buffer, *buffer_end; ///< position pointer in local buffer and last valid byte of buffer |
790879e123dd
(svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents:
4202
diff
changeset
|
23 |
uint32 pos; ///< current (system) position in file |
790879e123dd
(svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents:
4202
diff
changeset
|
24 |
FILE *cur_fh; ///< current file handle |
6218
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
25 |
FILE *handles[MAX_HANDLES]; ///< array of file handles we can have open |
4203
790879e123dd
(svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents:
4202
diff
changeset
|
26 |
byte buffer_start[FIO_BUFFER_SIZE]; ///< local buffer when read from file |
6218
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
27 |
#if defined(LIMITED_FDS) |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
28 |
uint open_handles; ///< current amount of open handles |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
29 |
const char *filename[MAX_HANDLES]; ///< array of filenames we (should) have open |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
30 |
uint usage_count[MAX_HANDLES]; ///< count how many times this file has been opened |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
31 |
#endif /* LIMITED_FDS */ |
6298
c30fe89622df
(svn r9119) [cpp_gui] -Sync with trunk (r9003:9100)
bjarni
parents:
6285
diff
changeset
|
32 |
}; |
0 | 33 |
|
34 |
static Fio _fio; |
|
35 |
||
6285
187e3ef04cc9
(svn r9004) [cpp_gui] -Sync with trunk (r8900..r9003)
KUDr
parents:
6218
diff
changeset
|
36 |
/* Get current position in file */ |
6298
c30fe89622df
(svn r9119) [cpp_gui] -Sync with trunk (r9003:9100)
bjarni
parents:
6285
diff
changeset
|
37 |
uint32 FioGetPos() |
0 | 38 |
{ |
39 |
return _fio.pos + (_fio.buffer - _fio.buffer_start) - FIO_BUFFER_SIZE; |
|
40 |
} |
|
41 |
||
42 |
void FioSeekTo(uint32 pos, int mode) |
|
43 |
{ |
|
44 |
if (mode == SEEK_CUR) pos += FioGetPos(); |
|
45 |
_fio.buffer = _fio.buffer_end = _fio.buffer_start + FIO_BUFFER_SIZE; |
|
4203
790879e123dd
(svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents:
4202
diff
changeset
|
46 |
_fio.pos = pos; |
790879e123dd
(svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents:
4202
diff
changeset
|
47 |
fseek(_fio.cur_fh, _fio.pos, SEEK_SET); |
0 | 48 |
} |
49 |
||
6218
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
50 |
#if defined(LIMITED_FDS) |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
51 |
static void FioRestoreFile(int slot) |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
52 |
{ |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
53 |
/* Do we still have the file open, or should we reopen it? */ |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
54 |
if (_fio.handles[slot] == NULL) { |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
55 |
DEBUG(misc, 6, "Restoring file '%s' in slot '%d' from disk", _fio.filename[slot], slot); |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
56 |
FioOpenFile(slot, _fio.filename[slot]); |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
57 |
} |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
58 |
_fio.usage_count[slot]++; |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
59 |
} |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
60 |
#endif /* LIMITED_FDS */ |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
61 |
|
6285
187e3ef04cc9
(svn r9004) [cpp_gui] -Sync with trunk (r8900..r9003)
KUDr
parents:
6218
diff
changeset
|
62 |
/* Seek to a file and a position */ |
0 | 63 |
void FioSeekToFile(uint32 pos) |
64 |
{ |
|
6218
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
65 |
FILE *f; |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
66 |
#if defined(LIMITED_FDS) |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
67 |
/* Make sure we have this file open */ |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
68 |
FioRestoreFile(pos >> 24); |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
69 |
#endif /* LIMITED_FDS */ |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
70 |
f = _fio.handles[pos >> 24]; |
0 | 71 |
assert(f != NULL); |
72 |
_fio.cur_fh = f; |
|
4203
790879e123dd
(svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents:
4202
diff
changeset
|
73 |
FioSeekTo(GB(pos, 0, 24), SEEK_SET); |
0 | 74 |
} |
75 |
||
6298
c30fe89622df
(svn r9119) [cpp_gui] -Sync with trunk (r9003:9100)
bjarni
parents:
6285
diff
changeset
|
76 |
byte FioReadByte() |
0 | 77 |
{ |
78 |
if (_fio.buffer == _fio.buffer_end) { |
|
79 |
_fio.pos += FIO_BUFFER_SIZE; |
|
80 |
fread(_fio.buffer = _fio.buffer_start, 1, FIO_BUFFER_SIZE, _fio.cur_fh); |
|
81 |
} |
|
82 |
return *_fio.buffer++; |
|
83 |
} |
|
84 |
||
85 |
void FioSkipBytes(int n) |
|
86 |
{ |
|
2952 | 87 |
for (;;) { |
0 | 88 |
int m = min(_fio.buffer_end - _fio.buffer, n); |
89 |
_fio.buffer += m; |
|
90 |
n -= m; |
|
91 |
if (n == 0) break; |
|
92 |
FioReadByte(); |
|
93 |
n--; |
|
94 |
} |
|
95 |
} |
|
96 |
||
6298
c30fe89622df
(svn r9119) [cpp_gui] -Sync with trunk (r9003:9100)
bjarni
parents:
6285
diff
changeset
|
97 |
uint16 FioReadWord() |
0 | 98 |
{ |
99 |
byte b = FioReadByte(); |
|
100 |
return (FioReadByte() << 8) | b; |
|
101 |
} |
|
102 |
||
6298
c30fe89622df
(svn r9119) [cpp_gui] -Sync with trunk (r9003:9100)
bjarni
parents:
6285
diff
changeset
|
103 |
uint32 FioReadDword() |
0 | 104 |
{ |
105 |
uint b = FioReadWord(); |
|
106 |
return (FioReadWord() << 16) | b; |
|
107 |
} |
|
108 |
||
109 |
void FioReadBlock(void *ptr, uint size) |
|
110 |
{ |
|
111 |
FioSeekTo(FioGetPos(), SEEK_SET); |
|
112 |
_fio.pos += size; |
|
113 |
fread(ptr, 1, size, _fio.cur_fh); |
|
114 |
} |
|
115 |
||
1039
8736f759759b
(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
|
116 |
static inline void FioCloseFile(int slot) |
8736f759759b
(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
|
117 |
{ |
8736f759759b
(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
|
118 |
if (_fio.handles[slot] != NULL) { |
1109
1bab892228cd
(svn r1610) Remove trailing whitespace (last time ever, i hope)
tron
parents:
1093
diff
changeset
|
119 |
fclose(_fio.handles[slot]); |
1039
8736f759759b
(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
|
120 |
_fio.handles[slot] = NULL; |
6218
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
121 |
#if defined(LIMITED_FDS) |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
122 |
_fio.open_handles--; |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
123 |
#endif /* LIMITED_FDS */ |
1039
8736f759759b
(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
|
124 |
} |
8736f759759b
(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
|
125 |
} |
8736f759759b
(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
|
126 |
|
6298
c30fe89622df
(svn r9119) [cpp_gui] -Sync with trunk (r9003:9100)
bjarni
parents:
6285
diff
changeset
|
127 |
void FioCloseAll() |
0 | 128 |
{ |
129 |
int i; |
|
130 |
||
1039
8736f759759b
(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
|
131 |
for (i = 0; i != lengthof(_fio.handles); i++) |
8736f759759b
(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
|
132 |
FioCloseFile(i); |
0 | 133 |
} |
134 |
||
4201
3004bdee457d
(svn r5685) - Codechange: s/FiosCheckFileExists/FioCheckFileExists/ to unify the naming of these functions.
Darkvater
parents:
4200
diff
changeset
|
135 |
bool FioCheckFileExists(const char *filename) |
1198
010e5986ed74
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
136 |
{ |
4202
698caf9ac7ff
(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
|
137 |
FILE *f = FioFOpenFile(filename); |
698caf9ac7ff
(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
|
138 |
if (f == NULL) return false; |
1198
010e5986ed74
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
139 |
|
4202
698caf9ac7ff
(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
|
140 |
fclose(f); |
698caf9ac7ff
(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
|
141 |
return true; |
1198
010e5986ed74
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
142 |
} |
010e5986ed74
(svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents:
1109
diff
changeset
|
143 |
|
6218
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
144 |
#if defined(LIMITED_FDS) |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
145 |
static void FioFreeHandle() |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
146 |
{ |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
147 |
/* If we are about to open a file that will exceed the limit, close a file */ |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
148 |
if (_fio.open_handles + 1 == LIMITED_FDS) { |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
149 |
uint i, count; |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
150 |
int slot; |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
151 |
|
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
152 |
count = UINT_MAX; |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
153 |
slot = -1; |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
154 |
/* Find the file that is used the least */ |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
155 |
for (i = 0; i < lengthof(_fio.handles); i++) { |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
156 |
if (_fio.handles[i] != NULL && _fio.usage_count[i] < count) { |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
157 |
count = _fio.usage_count[i]; |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
158 |
slot = i; |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
159 |
} |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
160 |
} |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
161 |
assert(slot != -1); |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
162 |
DEBUG(misc, 6, "Closing filehandler '%s' in slot '%d' because of fd-limit", _fio.filename[slot], slot); |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
163 |
FioCloseFile(slot); |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
164 |
} |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
165 |
} |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
166 |
#endif /* LIMITED_FDS */ |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
167 |
|
2736
1ea068235989
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
168 |
FILE *FioFOpenFile(const char *filename) |
1ea068235989
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
169 |
{ |
1ea068235989
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
170 |
FILE *f; |
1ea068235989
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
171 |
char buf[MAX_PATH]; |
1ea068235989
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
172 |
|
5296
6a4aaa66eed3
(svn r7449) -Codechange: Rename _path to _paths as it is technically more correct, but mainly because
Darkvater
parents:
5170
diff
changeset
|
173 |
snprintf(buf, lengthof(buf), "%s%s", _paths.data_dir, filename); |
2736
1ea068235989
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
174 |
|
1ea068235989
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
175 |
f = fopen(buf, "rb"); |
1ea068235989
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
176 |
#if !defined(WIN32) |
1ea068235989
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
177 |
if (f == NULL) { |
5296
6a4aaa66eed3
(svn r7449) -Codechange: Rename _path to _paths as it is technically more correct, but mainly because
Darkvater
parents:
5170
diff
changeset
|
178 |
strtolower(buf + strlen(_paths.data_dir) - 1); |
2736
1ea068235989
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
179 |
f = fopen(buf, "rb"); |
1ea068235989
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
180 |
|
1ea068235989
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
181 |
#if defined SECOND_DATA_DIR |
6285
187e3ef04cc9
(svn r9004) [cpp_gui] -Sync with trunk (r8900..r9003)
KUDr
parents:
6218
diff
changeset
|
182 |
/* tries in the 2nd data directory */ |
2736
1ea068235989
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
183 |
if (f == NULL) { |
5296
6a4aaa66eed3
(svn r7449) -Codechange: Rename _path to _paths as it is technically more correct, but mainly because
Darkvater
parents:
5170
diff
changeset
|
184 |
snprintf(buf, lengthof(buf), "%s%s", _paths.second_data_dir, filename); |
6a4aaa66eed3
(svn r7449) -Codechange: Rename _path to _paths as it is technically more correct, but mainly because
Darkvater
parents:
5170
diff
changeset
|
185 |
strtolower(buf + strlen(_paths.second_data_dir) - 1); |
2736
1ea068235989
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
186 |
f = fopen(buf, "rb"); |
1ea068235989
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
187 |
} |
1ea068235989
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
188 |
#endif |
1ea068235989
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
189 |
} |
1ea068235989
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
190 |
#endif |
1ea068235989
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
191 |
|
1ea068235989
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
192 |
return f; |
1ea068235989
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
193 |
} |
1ea068235989
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
194 |
|
0 | 195 |
void FioOpenFile(int slot, const char *filename) |
196 |
{ |
|
6218
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
197 |
FILE *f; |
915 | 198 |
|
6218
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
199 |
#if defined(LIMITED_FDS) |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
200 |
FioFreeHandle(); |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
201 |
#endif /* LIMITED_FDS */ |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
202 |
f = FioFOpenFile(filename); |
5296
6a4aaa66eed3
(svn r7449) -Codechange: Rename _path to _paths as it is technically more correct, but mainly because
Darkvater
parents:
5170
diff
changeset
|
203 |
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
|
204 |
|
1039
8736f759759b
(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
|
205 |
FioCloseFile(slot); // if file was opened before, close it |
0 | 206 |
_fio.handles[slot] = f; |
6218
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
207 |
#if defined(LIMITED_FDS) |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
208 |
_fio.filename[slot] = filename; |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
209 |
_fio.usage_count[slot] = 0; |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
210 |
_fio.open_handles++; |
89dc931b8d78
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5835
diff
changeset
|
211 |
#endif /* LIMITED_FDS */ |
0 | 212 |
FioSeekToFile(slot << 24); |
213 |
} |