Files @ r4855:00332ec5d245
Branch filter:

Location: cpp/openttd-patchpack/source/fileio.c - annotation

peter1138
(svn r6781) - Fix (r5926): Some house introduction dates were incorrectly adjusted in the move to 32bit dates.
r2186:5ee653b1b5e1
r2186:5ee653b1b5e1
r0:d63b455452f6
r1891:c5c5466afa35
r1093:18f56ef2d029
r2163:ae001e2aa5b0
r4200:4e5f339514ce
r2159:2c706fe6b0a7
r2153:b45e3461c6c4
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r4203:9d25b5b76bf1
r4203:9d25b5b76bf1
r4203:9d25b5b76bf1
r4203:9d25b5b76bf1
r4203:9d25b5b76bf1
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
r4203:9d25b5b76bf1
r4203:9d25b5b76bf1
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r4203:9d25b5b76bf1
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
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
r4201:fa67baceccee
r1198:e4060ade398c
r4202:920e67dcb031
r4202:920e67dcb031
r1198:e4060ade398c
r4202:920e67dcb031
r4202:920e67dcb031
r1198:e4060ade398c
r1198:e4060ade398c
r2736:03cd6438b59f
r2736:03cd6438b59f
r2736:03cd6438b59f
r2736:03cd6438b59f
r2736:03cd6438b59f
r2736:03cd6438b59f
r2736:03cd6438b59f
r2736:03cd6438b59f
r2736:03cd6438b59f
r2736:03cd6438b59f
r4200:4e5f339514ce
r2736:03cd6438b59f
r2736:03cd6438b59f
r2736:03cd6438b59f
r2736:03cd6438b59f
r2736:03cd6438b59f
r2736:03cd6438b59f
r4200:4e5f339514ce
r2736:03cd6438b59f
r2736:03cd6438b59f
r2736:03cd6438b59f
r2736:03cd6438b59f
r2736:03cd6438b59f
r2736:03cd6438b59f
r2736:03cd6438b59f
r2736:03cd6438b59f
r2736:03cd6438b59f
r0:d63b455452f6
r0:d63b455452f6
r4202:920e67dcb031
r915:dabba47e3ed1
r4202:920e67dcb031
r193:6aa65dc5a4b4
r1039:b5be4ec22469
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
/* $Id$ */

#include "stdafx.h"
#include "openttd.h"
#include "fileio.h"
#include "functions.h"
#include "string.h"
#include "macros.h"
#include "variables.h"

/*************************************************/
/* FILE IO ROUTINES ******************************/
/*************************************************/

#define FIO_BUFFER_SIZE 512

typedef struct {
	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
} 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;
	_fio.pos = pos;
	fseek(_fio.cur_fh, _fio.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(GB(pos, 0, 24), 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 FioCheckFileExists(const char *filename)
{
	FILE *f = FioFOpenFile(filename);
	if (f == NULL) return false;

	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) {
		strtolower(buf + strlen(_path.data_dir) - 1);
		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);
			strtolower(buf + strlen(_path.second_data_dir) - 1);
			f = fopen(buf, "rb");
		}
#endif
	}
#endif

	return f;
}

void FioOpenFile(int slot, const char *filename)
{
	FILE *f = FioFOpenFile(filename);

	if (f == NULL) error("Cannot open file '%s%s'", _path.data_dir, filename);

	FioCloseFile(slot); // if file was opened before, close it
	_fio.handles[slot] = f;
	FioSeekToFile(slot << 24);
}