Changeset - r20984:a4da9302878d
[Not reviewed]
master
0 10 0
rubidium - 11 years ago 2013-11-23 13:15:07
rubidium@openttd.org
(svn r26058) -Fix: handle the return value of a number of functions better
10 files changed with 51 insertions and 13 deletions:
0 comments (0 inline, 0 general)
src/bmp.cpp
Show inline comments
 
@@ -22,24 +22,30 @@ void BmpInitializeBuffer(BmpBuffer *buff
 
	buffer->read     = 0;
 
	buffer->real_pos = ftell(file);
 
}
 

	
 
static inline void AdvanceBuffer(BmpBuffer *buffer)
 
{
 
	if (buffer->read < 0) return;
 

	
 
	buffer->read = (int)fread(buffer->data, 1, BMP_BUFFER_SIZE, buffer->file);
 
	buffer->pos  = 0;
 
}
 

	
 
static inline bool EndOfBuffer(BmpBuffer *buffer)
 
{
 
	if (buffer->read < 0) return false;
 

	
 
	if (buffer->pos == buffer->read || buffer->pos < 0) AdvanceBuffer(buffer);
 
	return buffer->pos == buffer->read;
 
}
 

	
 
static inline byte ReadByte(BmpBuffer *buffer)
 
{
 
	if (buffer->read < 0) return 0;
 

	
 
	if (buffer->pos == buffer->read || buffer->pos < 0) AdvanceBuffer(buffer);
 
	buffer->real_pos++;
 
	return buffer->data[buffer->pos++];
 
}
 

	
 
static inline uint16 ReadWord(BmpBuffer *buffer)
 
@@ -59,13 +65,15 @@ static inline void SkipBytes(BmpBuffer *
 
	int i;
 
	for (i = 0; i < bytes; i++) ReadByte(buffer);
 
}
 

	
 
static inline void SetStreamOffset(BmpBuffer *buffer, int offset)
 
{
 
	fseek(buffer->file, offset, SEEK_SET);
 
	if (fseek(buffer->file, offset, SEEK_SET) < 0) {
 
		buffer->read = -1;
 
	}
 
	buffer->pos = -1;
 
	buffer->real_pos = offset;
 
	AdvanceBuffer(buffer);
 
}
 

	
 
/**
src/debug.cpp
Show inline comments
 
@@ -109,12 +109,14 @@ static void debug_print(const char *dbg,
 
{
 
#if defined(ENABLE_NETWORK)
 
	if (_debug_socket != INVALID_SOCKET) {
 
		char buf2[1024 + 32];
 

	
 
		snprintf(buf2, lengthof(buf2), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
 
		/* Sending out an error when this fails would be nice, however... the error
 
		 * would have to be send over this failing socket which won't work. */
 
		send(_debug_socket, buf2, (int)strlen(buf2), 0);
 
		return;
 
	}
 
#endif /* ENABLE_NETWORK */
 
	if (strcmp(dbg, "desync") == 0) {
 
		static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
src/fileio.cpp
Show inline comments
 
@@ -85,13 +85,15 @@ const char *FioGetFilename(uint8 slot)
 
 */
 
void FioSeekTo(size_t 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);
 
	if (fseek(_fio.cur_fh, _fio.pos, SEEK_SET) < 0) {
 
		DEBUG(misc, 0, "Seeking in %s failed", _fio.filename);
 
	}
 
}
 

	
 
#if defined(LIMITED_FDS)
 
static void FioRestoreFile(int slot)
 
{
 
	/* Do we still have the file open, or should we reopen it? */
 
@@ -447,13 +449,17 @@ static FILE *FioFOpenFileSp(const char *
 
 */
 
FILE *FioFOpenFileTar(TarFileListEntry *entry, size_t *filesize)
 
{
 
	FILE *f = fopen(entry->tar_filename, "rb");
 
	if (f == NULL) return f;
 

	
 
	fseek(f, entry->position, SEEK_SET);
 
	if (fseek(f, entry->position, SEEK_SET) < 0) {
 
		fclose(f);
 
		return NULL;
 
	}
 

	
 
	if (filesize != NULL) *filesize = entry->size;
 
	return f;
 
}
 

	
 
/**
 
 * Opens a OpenTTD file somewhere in a personal or global directory.
 
@@ -530,12 +536,14 @@ FILE *FioFOpenFile(const char *filename,
 
/**
 
 * Create a directory with the given name
 
 * @param name the new name of the directory
 
 */
 
static void FioCreateDirectory(const char *name)
 
{
 
	/* Ignore directory creation errors; they'll surface later on, and most
 
	 * of the time they are 'directory already exists' errors anyhow. */
 
#if defined(WIN32) || defined(WINCE)
 
	CreateDirectory(OTTD2FS(name), NULL);
 
#elif defined(OS2) && !defined(__INNOTEK_LIBC__)
 
	mkdir(OTTD2FS(name));
 
#elif defined(__MORPHOS__) || defined(__AMIGAOS__)
 
	char buf[MAX_PATH];
 
@@ -894,13 +902,17 @@ bool TarScanner::AddFile(const char *fil
 
				/* Ignore other types */
 
				break;
 
		}
 

	
 
		/* Skip to the next block.. */
 
		skip = Align(skip, 512);
 
		fseek(f, skip, SEEK_CUR);
 
		if (fseek(f, skip, SEEK_CUR) < 0) {
 
			DEBUG(misc, 0, "The file '%s' can't be read as a valid tar-file", filename);
 
			fclose(f);
 
			return false;
 
		}
 
		pos += skip;
 
	}
 

	
 
	DEBUG(misc, 1, "Found tar '%s' with " PRINTF_SIZE " new files", filename, num);
 
	fclose(f);
 

	
src/ini.cpp
Show inline comments
 
@@ -103,13 +103,15 @@ bool IniFile::SaveToDisk(const char *fil
 
	shfopt.wFunc  = FO_MOVE;
 
	shfopt.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI | FOF_SILENT;
 
	shfopt.pFrom  = tfile_new;
 
	shfopt.pTo    = tfilename;
 
	SHFileOperation(&shfopt);
 
#else
 
	rename(file_new, filename);
 
	if (rename(file_new, filename) < 0) {
 
		DEBUG(misc, 0, "Renaming %s to %s failed; configuration not saved", file_new, filename);
 
	}
 
#endif
 

	
 
	return true;
 
}
 

	
 
/* virtual */ FILE *IniFile::OpenFile(const char *filename, Subdirectory subdir, size_t *size)
src/newgrf_config.cpp
Show inline comments
 
@@ -359,13 +359,17 @@ static bool CalcGRFMD5Sum(GRFConfig *con
 
	/* open the file */
 
	f = FioFOpenFile(config->filename, "rb", subdir, &size);
 
	if (f == NULL) return false;
 

	
 
	size_t start = ftell(f);
 
	size = min(size, GRFGetSizeOfDataSection(f));
 
	fseek(f, start, SEEK_SET);
 

	
 
	if (fseek(f, start, SEEK_SET) < 0) {
 
		FioFCloseFile(f);
 
		return false;
 
	}
 

	
 
	/* calculate md5sum */
 
	while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
 
		size -= len;
 
		checksum.Append(buffer, len);
 
	}
src/pathfinder/yapf/yapf_node_rail.hpp
Show inline comments
 
@@ -193,13 +193,13 @@ struct CYapfRailNodeT
 
		TileIndex cur = base::GetTile();
 
		Trackdir  cur_td = base::GetTrackdir();
 

	
 
		while (cur != GetLastTile() || cur_td != GetLastTrackdir()) {
 
			if (!((obj.*func)(cur, cur_td))) return false;
 

	
 
			ft.Follow(cur, cur_td);
 
			if (!ft.Follow(cur, cur_td)) break;
 
			cur = ft.m_new_tile;
 
			assert(KillFirstBit(ft.m_new_td_bits) == TRACKDIR_BIT_NONE);
 
			cur_td = FindFirstTrackdir(ft.m_new_td_bits);
 
		}
 

	
 
		return (obj.*func)(cur, cur_td);
src/saveload/oldloader.cpp
Show inline comments
 
@@ -247,16 +247,15 @@ static SavegameType DetermineOldSavegame
 
	char temp[TTD_HEADER_SIZE];
 

	
 
	SavegameType type = SGT_TTO;
 

	
 
	/* Can't fseek to 0 as in tar files that is not correct */
 
	long pos = ftell(f);
 
	if (!CheckOldSavegameType(f, temp, lastof(temp), TTO_HEADER_SIZE)) {
 
	if (pos >= 0 && !CheckOldSavegameType(f, temp, lastof(temp), TTO_HEADER_SIZE)) {
 
		type = SGT_TTD;
 
		fseek(f, pos, SEEK_SET);
 
		if (!CheckOldSavegameType(f, temp, lastof(temp), TTD_HEADER_SIZE)) {
 
		if (fseek(f, pos, SEEK_SET) < 0 || !CheckOldSavegameType(f, temp, lastof(temp), TTD_HEADER_SIZE)) {
 
			type = SGT_INVALID;
 
		}
 
	}
 

	
 
	if (title != NULL) {
 
		switch (type) {
src/saveload/saveload.cpp
Show inline comments
 
@@ -1845,13 +1845,15 @@ struct FileReader : LoadFilter {
 
		return fread(buf, 1, size, this->file);
 
	}
 

	
 
	/* virtual */ void Reset()
 
	{
 
		clearerr(this->file);
 
		fseek(this->file, this->begin, SEEK_SET);
 
		if (fseek(this->file, this->begin, SEEK_SET)) {
 
			DEBUG(sl, 1, "Could not reset the file reading");
 
		}
 
	}
 
};
 

	
 
/** Yes, simply writing to a file. */
 
struct FileWriter : SaveFilter {
 
	FILE *file; ///< The file to write to.
src/script/squirrel.cpp
Show inline comments
 
@@ -490,13 +490,18 @@ SQRESULT Squirrel::LoadFile(HSQUIRRELVM 
 
				if (uc != 0xBF) {
 
					FioFCloseFile(file);
 
					return sq_throwerror(vm, _SC("Unrecognized encoding"));
 
				}
 
				func = _io_file_lexfeed_UTF8;
 
				break;
 
			default: func = _io_file_lexfeed_ASCII; fseek(file, -2, SEEK_CUR); break; // ASCII
 
			default: // ASCII
 
				func = _io_file_lexfeed_ASCII;
 
				if (fseek(file, -2, SEEK_CUR) < 0) {
 
					return sq_throwerror(vm, _SC("cannot read the file"));
 
				}
 
				break;
 
		}
 

	
 
		if (SQ_SUCCEEDED(sq_compile(vm, func, &f, OTTD2SQ(filename), printerror))) {
 
			FioFCloseFile(file);
 
			return SQ_OK;
 
		}
src/strgen/strgen.cpp
Show inline comments
 
@@ -350,13 +350,15 @@ struct LanguageFileWriter : LanguageWrit
 
	{
 
		this->Write((const byte *)header, sizeof(*header));
 
	}
 

	
 
	void Finalise()
 
	{
 
		fputc(0, this->fh);
 
		if (fputc(0, this->fh) == EOF) {
 
			error("Could not write to %s", this->filename);
 
		}
 
		this->FileWriter::Finalise();
 
	}
 

	
 
	void Write(const byte *buffer, size_t length)
 
	{
 
		if (fwrite(buffer, sizeof(*buffer), length, this->fh) != length) {
 
@@ -365,12 +367,14 @@ struct LanguageFileWriter : LanguageWrit
 
	}
 
};
 

	
 
/** Multi-OS mkdirectory function */
 
static inline void ottd_mkdir(const char *directory)
 
{
 
	/* Ignore directory creation errors; they'll surface later on, and most
 
	 * of the time they are 'directory already exists' errors anyhow. */
 
#if defined(WIN32) || defined(__WATCOMC__)
 
		mkdir(directory);
 
#else
 
		mkdir(directory, 0755);
 
#endif
 
}
0 comments (0 inline, 0 general)