File diff r6178:fc8bd2bde93a → r6179:c0508e7aefec
src/fileio.cpp
Show inline comments
 
/* $Id$ */
 

	
 
/** @file fileio.cpp Standard In/Out file operations*/
 

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

	
 
/*************************************************/
 
/* FILE IO ROUTINES ******************************/
 
/*************************************************/
 
@@ -22,25 +24,25 @@ typedef struct {
 
	FILE *cur_fh;                       ///< current file handle
 
	FILE *handles[MAX_HANDLES];         ///< array of file handles we can have open
 
	byte buffer_start[FIO_BUFFER_SIZE]; ///< local buffer when read from file
 
#if defined(LIMITED_FDS)
 
	uint open_handles;                  ///< current amount of open handles
 
	const char *filename[MAX_HANDLES];  ///< array of filenames we (should) have open
 
	uint usage_count[MAX_HANDLES];      ///< count how many times this file has been opened
 
#endif /* LIMITED_FDS */
 
} Fio;
 

	
 
static Fio _fio;
 

	
 
// Get current position in file
 
/* 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);
 
}
 
@@ -48,25 +50,25 @@ void FioSeekTo(uint32 pos, int mode)
 
#if defined(LIMITED_FDS)
 
static void FioRestoreFile(int slot)
 
{
 
	/* Do we still have the file open, or should we reopen it? */
 
	if (_fio.handles[slot] == NULL) {
 
		DEBUG(misc, 6, "Restoring file '%s' in slot '%d' from disk", _fio.filename[slot], slot);
 
		FioOpenFile(slot, _fio.filename[slot]);
 
	}
 
	_fio.usage_count[slot]++;
 
}
 
#endif /* LIMITED_FDS */
 

	
 
// Seek to a file and a position
 
/* Seek to a file and a position */
 
void FioSeekToFile(uint32 pos)
 
{
 
	FILE *f;
 
#if defined(LIMITED_FDS)
 
	/* Make sure we have this file open */
 
	FioRestoreFile(pos >> 24);
 
#endif /* LIMITED_FDS */
 
	f = _fio.handles[pos >> 24];
 
	assert(f != NULL);
 
	_fio.cur_fh = f;
 
	FioSeekTo(GB(pos, 0, 24), SEEK_SET);
 
}
 
@@ -168,25 +170,25 @@ FILE *FioFOpenFile(const char *filename)
 
	FILE *f;
 
	char buf[MAX_PATH];
 

	
 
	snprintf(buf, lengthof(buf), "%s%s", _paths.data_dir, filename);
 

	
 
	f = fopen(buf, "rb");
 
#if !defined(WIN32)
 
	if (f == NULL) {
 
		strtolower(buf + strlen(_paths.data_dir) - 1);
 
		f = fopen(buf, "rb");
 

	
 
#if defined SECOND_DATA_DIR
 
		// tries in the 2nd data directory
 
		/* tries in the 2nd data directory */
 
		if (f == NULL) {
 
			snprintf(buf, lengthof(buf), "%s%s", _paths.second_data_dir, filename);
 
			strtolower(buf + strlen(_paths.second_data_dir) - 1);
 
			f = fopen(buf, "rb");
 
		}
 
#endif
 
	}
 
#endif
 

	
 
	return f;
 
}