Files
@ r2961:2e84ef887234
Branch filter:
Location: cpp/openttd-patchpack/source/fileio.c - annotation
r2961:2e84ef887234
4.2 KiB
text/x-c
(svn r3523) Undefine helper macros after using them
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | r2186:5ee653b1b5e1 r2186:5ee653b1b5e1 r0:d63b455452f6 r1891:c5c5466afa35 r1093:18f56ef2d029 r2163:ae001e2aa5b0 r2159:2c706fe6b0a7 r2153:b45e3461c6c4 r810:08bcb4bf8186 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r1093:18f56ef2d029 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r1093:18f56ef2d029 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r2952:e97f823d2dbe r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r1093:18f56ef2d029 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r1093:18f56ef2d029 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r1039:b5be4ec22469 r1039:b5be4ec22469 r1039:b5be4ec22469 r1109:6c0cc4a443a2 r1039:b5be4ec22469 r1039:b5be4ec22469 r1039:b5be4ec22469 r1039:b5be4ec22469 r1036:2914343c4007 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r1039:b5be4ec22469 r1039:b5be4ec22469 r0:d63b455452f6 r0:d63b455452f6 r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r2952:e97f823d2dbe r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r2952:e97f823d2dbe r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r1198:e4060ade398c r2736:03cd6438b59f r2736:03cd6438b59f r2736:03cd6438b59f r2736:03cd6438b59f r2736:03cd6438b59f r2736:03cd6438b59f r2736:03cd6438b59f r2736:03cd6438b59f r2736:03cd6438b59f r2736:03cd6438b59f r2736:03cd6438b59f r2736:03cd6438b59f r2952:e97f823d2dbe r2736:03cd6438b59f r2736:03cd6438b59f r2736:03cd6438b59f r2736:03cd6438b59f r2736:03cd6438b59f r2736:03cd6438b59f r2736:03cd6438b59f r2952:e97f823d2dbe r2736:03cd6438b59f r2736:03cd6438b59f r2736:03cd6438b59f r2736:03cd6438b59f r2736:03cd6438b59f r2736:03cd6438b59f r2736:03cd6438b59f r2736:03cd6438b59f r2736:03cd6438b59f r2736:03cd6438b59f r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r193:6aa65dc5a4b4 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r2952:e97f823d2dbe r0:d63b455452f6 r0:d63b455452f6 r915:dabba47e3ed1 r561:f113dc55d8ed r561:f113dc55d8ed r561:f113dc55d8ed r561:f113dc55d8ed r2952:e97f823d2dbe r561:f113dc55d8ed r561:f113dc55d8ed r561:f113dc55d8ed r2188:6f92a4485e32 r2188:6f92a4485e32 r2188:6f92a4485e32 r2188:6f92a4485e32 r561:f113dc55d8ed r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r193:6aa65dc5a4b4 r1039:b5be4ec22469 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 | /* $Id$ */
#include "stdafx.h"
#include "openttd.h"
#include "fileio.h"
#include "functions.h"
#include "macros.h"
#include "variables.h"
#if defined(UNIX) || defined(__OS2__)
#include <ctype.h> // required for tolower()
#endif
/*************************************************/
/* FILE IO ROUTINES ******************************/
/*************************************************/
#define FIO_BUFFER_SIZE 512
typedef struct {
byte *buffer, *buffer_end;
uint32 pos;
FILE *cur_fh;
FILE *handles[32];
byte buffer_start[512];
} Fio;
static Fio _fio;
// Get current position in file
uint32 FioGetPos(void)
{
return _fio.pos + (_fio.buffer - _fio.buffer_start) - FIO_BUFFER_SIZE;
}
void FioSeekTo(uint32 pos, int mode)
{
if (mode == SEEK_CUR) pos += FioGetPos();
_fio.buffer = _fio.buffer_end = _fio.buffer_start + FIO_BUFFER_SIZE;
fseek(_fio.cur_fh, (_fio.pos=pos), SEEK_SET);
}
// Seek to a file and a position
void FioSeekToFile(uint32 pos)
{
FILE *f = _fio.handles[pos >> 24];
assert(f != NULL);
_fio.cur_fh = f;
FioSeekTo(pos & 0xFFFFFF, SEEK_SET);
}
byte FioReadByte(void)
{
if (_fio.buffer == _fio.buffer_end) {
_fio.pos += FIO_BUFFER_SIZE;
fread(_fio.buffer = _fio.buffer_start, 1, FIO_BUFFER_SIZE, _fio.cur_fh);
}
return *_fio.buffer++;
}
void FioSkipBytes(int n)
{
for (;;) {
int m = min(_fio.buffer_end - _fio.buffer, n);
_fio.buffer += m;
n -= m;
if (n == 0) break;
FioReadByte();
n--;
}
}
uint16 FioReadWord(void)
{
byte b = FioReadByte();
return (FioReadByte() << 8) | b;
}
uint32 FioReadDword(void)
{
uint b = FioReadWord();
return (FioReadWord() << 16) | b;
}
void FioReadBlock(void *ptr, uint size)
{
FioSeekTo(FioGetPos(), SEEK_SET);
_fio.pos += size;
fread(ptr, 1, size, _fio.cur_fh);
}
static inline void FioCloseFile(int slot)
{
if (_fio.handles[slot] != NULL) {
fclose(_fio.handles[slot]);
_fio.handles[slot] = NULL;
}
}
void FioCloseAll(void)
{
int i;
for (i = 0; i != lengthof(_fio.handles); i++)
FioCloseFile(i);
}
bool FiosCheckFileExists(const char *filename)
{
FILE *f;
char buf[MAX_PATH];
sprintf(buf, "%s%s", _path.data_dir, filename);
f = fopen(buf, "rb");
#if !defined(WIN32)
if (f == NULL) {
char *s;
// Make lower case and try again
for (s = buf + strlen(_path.data_dir) - 1; *s != 0; s++)
*s = tolower(*s);
f = fopen(buf, "rb");
#if defined SECOND_DATA_DIR
// tries in the 2nd data directory
if (f == NULL) {
sprintf(buf, "%s%s", _path.second_data_dir, filename);
for (s = buf + strlen(_path.second_data_dir) - 1; *s != 0; s++)
*s = tolower(*s);
f = fopen(buf, "rb");
}
#endif
}
#endif
if (f == NULL)
return false;
else {
fclose(f);
return true;
}
}
FILE *FioFOpenFile(const char *filename)
{
FILE *f;
char buf[MAX_PATH];
sprintf(buf, "%s%s", _path.data_dir, filename);
f = fopen(buf, "rb");
#if !defined(WIN32)
if (f == NULL) {
char *s;
// Make lower case and try again
for (s = buf + strlen(_path.data_dir) - 1; *s != 0; s++)
*s = tolower(*s);
f = fopen(buf, "rb");
#if defined SECOND_DATA_DIR
// tries in the 2nd data directory
if (f == NULL) {
sprintf(buf, "%s%s", _path.second_data_dir, filename);
for (s = buf + strlen(_path.second_data_dir) - 1; *s != 0; s++)
*s = tolower(*s);
f = fopen(buf, "rb");
}
#endif
}
#endif
return f;
}
void FioOpenFile(int slot, const char *filename)
{
FILE *f;
char buf[MAX_PATH];
sprintf(buf, "%s%s", _path.data_dir, filename);
f = fopen(buf, "rb");
#if !defined(WIN32)
if (f == NULL) {
char *s;
// Make lower case and try again
for (s = buf + strlen(_path.data_dir) - 1; *s != 0; s++)
*s = tolower(*s);
f = fopen(buf, "rb");
#if defined SECOND_DATA_DIR
// tries in the 2nd data directory
if (f == NULL) {
sprintf(buf, "%s%s", _path.second_data_dir, filename);
for (s = buf + strlen(_path.second_data_dir) - 1; *s != 0; s++)
*s = tolower(*s);
f = fopen(buf, "rb");
}
if (f == NULL)
sprintf(buf, "%s%s", _path.data_dir, filename); //makes it print the primary datadir path instead of the secundary one
#endif
}
#endif
if (f == NULL)
error("Cannot open file '%s'", buf);
FioCloseFile(slot); // if file was opened before, close it
_fio.handles[slot] = f;
FioSeekToFile(slot << 24);
}
|