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"
|
2005-07-21 22:15:02 +00:00
|
|
|
#include "macros.h"
|
2005-07-21 18:44:27 +00:00
|
|
|
#include "variables.h"
|
2004-12-27 10:40:04 +00:00
|
|
|
#if defined(UNIX) || defined(__OS2__)
|
2004-08-09 17:04:08 +00:00
|
|
|
#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;
|
2006-05-25 09:57:27 +00:00
|
|
|
FILE *handles[64];
|
2004-08-09 17:04:08 +00:00
|
|
|
byte buffer_start[512];
|
|
|
|
} 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;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2005-01-28 08:56:43 +00:00
|
|
|
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
|
2006-02-01 07:36:15 +00:00
|
|
|
for (s = buf + strlen(_path.data_dir) - 1; *s != 0; s++)
|
2005-01-28 08:56:43 +00:00
|
|
|
*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);
|
2006-02-01 07:36:15 +00:00
|
|
|
for (s = buf + strlen(_path.second_data_dir) - 1; *s != 0; s++)
|
2005-01-28 08:56:43 +00:00
|
|
|
*s = tolower(*s);
|
|
|
|
f = fopen(buf, "rb");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-06-27 21:25:53 +00:00
|
|
|
if (f == NULL) {
|
2005-01-28 08:56:43 +00:00
|
|
|
return false;
|
2006-06-27 21:25:53 +00:00
|
|
|
} else {
|
2005-01-28 08:56:43 +00:00
|
|
|
fclose(f);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
char *s;
|
|
|
|
// Make lower case and try again
|
2006-02-01 07:36:15 +00:00
|
|
|
for (s = buf + strlen(_path.data_dir) - 1; *s != 0; s++)
|
2005-12-10 11:16:45 +00:00
|
|
|
*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);
|
2006-02-01 07:36:15 +00:00
|
|
|
for (s = buf + strlen(_path.second_data_dir) - 1; *s != 0; s++)
|
2005-12-10 11:16:45 +00:00
|
|
|
*s = tolower(*s);
|
|
|
|
f = fopen(buf, "rb");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
void FioOpenFile(int slot, const char *filename)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
char buf[MAX_PATH];
|
|
|
|
|
|
|
|
sprintf(buf, "%s%s", _path.data_dir, filename);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
f = fopen(buf, "rb");
|
|
|
|
#if !defined(WIN32)
|
|
|
|
if (f == NULL) {
|
|
|
|
char *s;
|
|
|
|
// Make lower case and try again
|
2006-02-01 07:36:15 +00:00
|
|
|
for (s = buf + strlen(_path.data_dir) - 1; *s != 0; s++)
|
2004-08-09 17:04:08 +00:00
|
|
|
*s = tolower(*s);
|
|
|
|
f = fopen(buf, "rb");
|
2005-01-06 18:45:28 +00:00
|
|
|
|
2004-12-07 17:16:57 +00:00
|
|
|
#if defined SECOND_DATA_DIR
|
|
|
|
// tries in the 2nd data directory
|
|
|
|
if (f == NULL) {
|
|
|
|
sprintf(buf, "%s%s", _path.second_data_dir, filename);
|
2006-02-01 07:36:15 +00:00
|
|
|
for (s = buf + strlen(_path.second_data_dir) - 1; *s != 0; s++)
|
2004-12-07 17:16:57 +00:00
|
|
|
*s = tolower(*s);
|
|
|
|
f = fopen(buf, "rb");
|
|
|
|
}
|
2005-07-24 20:47:42 +00:00
|
|
|
|
|
|
|
if (f == NULL)
|
|
|
|
sprintf(buf, "%s%s", _path.data_dir, filename); //makes it print the primary datadir path instead of the secundary one
|
|
|
|
|
2004-12-07 17:16:57 +00:00
|
|
|
#endif
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (f == NULL)
|
|
|
|
error("Cannot open file '%s'", buf);
|
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);
|
|
|
|
}
|