2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2005-01-22 20:23:18 +00:00
|
|
|
#include "fileio.h"
|
2005-07-22 07:02:20 +00:00
|
|
|
#include "functions.h"
|
2006-07-31 22:11:34 +00:00
|
|
|
#include "string.h"
|
2005-07-21 22:15:02 +00:00
|
|
|
#include "macros.h"
|
2005-07-21 18:44:27 +00:00
|
|
|
#include "variables.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/*************************************************/
|
|
|
|
/* FILE IO ROUTINES ******************************/
|
|
|
|
/*************************************************/
|
|
|
|
|
|
|
|
#define FIO_BUFFER_SIZE 512
|
|
|
|
|
|
|
|
typedef struct {
|
2006-07-31 22:26:11 +00:00
|
|
|
byte *buffer, *buffer_end; ///< position pointer in local buffer and last valid byte of buffer
|
|
|
|
uint32 pos; ///< current (system) position in file
|
|
|
|
FILE *cur_fh; ///< current file handle
|
|
|
|
FILE *handles[64]; ///< array of file handles we can have open
|
|
|
|
byte buffer_start[FIO_BUFFER_SIZE]; ///< local buffer when read from file
|
2004-08-09 17:04:08 +00:00
|
|
|
} Fio;
|
|
|
|
|
|
|
|
static Fio _fio;
|
|
|
|
|
|
|
|
// Get current position in file
|
2005-01-22 20:23:18 +00:00
|
|
|
uint32 FioGetPos(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
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;
|
2006-07-31 22:26:11 +00:00
|
|
|
_fio.pos = pos;
|
|
|
|
fseek(_fio.cur_fh, _fio.pos, SEEK_SET);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Seek to a file and a position
|
|
|
|
void FioSeekToFile(uint32 pos)
|
|
|
|
{
|
|
|
|
FILE *f = _fio.handles[pos >> 24];
|
|
|
|
assert(f != NULL);
|
|
|
|
_fio.cur_fh = f;
|
2006-07-31 22:26:11 +00:00
|
|
|
FioSeekTo(GB(pos, 0, 24), SEEK_SET);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
byte FioReadByte(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2006-02-01 07:36:15 +00:00
|
|
|
for (;;) {
|
2004-08-09 17:04:08 +00:00
|
|
|
int m = min(_fio.buffer_end - _fio.buffer, n);
|
|
|
|
_fio.buffer += m;
|
|
|
|
n -= m;
|
|
|
|
if (n == 0) break;
|
|
|
|
FioReadByte();
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
uint16 FioReadWord(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
byte b = FioReadByte();
|
|
|
|
return (FioReadByte() << 8) | b;
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
uint32 FioReadDword(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2005-01-16 12:40:25 +00:00
|
|
|
static inline void FioCloseFile(int slot)
|
|
|
|
{
|
|
|
|
if (_fio.handles[slot] != NULL) {
|
2005-01-23 13:09:35 +00:00
|
|
|
fclose(_fio.handles[slot]);
|
2005-01-16 12:40:25 +00:00
|
|
|
_fio.handles[slot] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-16 12:14:52 +00:00
|
|
|
void FioCloseAll(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2005-01-16 12:40:25 +00:00
|
|
|
for (i = 0; i != lengthof(_fio.handles); i++)
|
|
|
|
FioCloseFile(i);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-07-31 22:15:39 +00:00
|
|
|
bool FioCheckFileExists(const char *filename)
|
2005-01-28 08:56:43 +00:00
|
|
|
{
|
2006-07-31 22:24:09 +00:00
|
|
|
FILE *f = FioFOpenFile(filename);
|
|
|
|
if (f == NULL) return false;
|
2005-01-28 08:56:43 +00:00
|
|
|
|
2006-07-31 22:24:09 +00:00
|
|
|
fclose(f);
|
|
|
|
return true;
|
2005-01-28 08:56:43 +00:00
|
|
|
}
|
|
|
|
|
2005-12-10 11:16:45 +00:00
|
|
|
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) {
|
2006-07-31 22:11:34 +00:00
|
|
|
strtolower(buf + strlen(_path.data_dir) - 1);
|
2005-12-10 11:16:45 +00:00
|
|
|
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);
|
2006-07-31 22:11:34 +00:00
|
|
|
strtolower(buf + strlen(_path.second_data_dir) - 1);
|
2005-12-10 11:16:45 +00:00
|
|
|
f = fopen(buf, "rb");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
void FioOpenFile(int slot, const char *filename)
|
|
|
|
{
|
2006-07-31 22:24:09 +00:00
|
|
|
FILE *f = FioFOpenFile(filename);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-07-31 22:24:09 +00:00
|
|
|
if (f == NULL) error("Cannot open file '%s%s'", _path.data_dir, filename);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-01-16 12:40:25 +00:00
|
|
|
FioCloseFile(slot); // if file was opened before, close it
|
2004-08-09 17:04:08 +00:00
|
|
|
_fio.handles[slot] = f;
|
|
|
|
FioSeekToFile(slot << 24);
|
|
|
|
}
|