changeset 7574:62a4ffc139c0 draft

(svn r11099) -Codechange: allow on opening of a file via FioFOpenFile to request the size of the file, so we can keep that in mind
author truelight <truelight@openttd.org>
date Thu, 13 Sep 2007 18:46:29 +0000
parents 85fa5b92adef
children 83da0ffcda47
files src/fileio.cpp src/fileio.h src/gfxinit.cpp src/newgrf_config.cpp
diffstat 4 files changed, 19 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/fileio.cpp
+++ b/src/fileio.cpp
@@ -267,7 +267,7 @@
 	return buf;
 }
 
-FILE *FioFOpenFileSp(const char *filename, const char *mode, Searchpath sp, Subdirectory subdir)
+FILE *FioFOpenFileSp(const char *filename, const char *mode, Searchpath sp, Subdirectory subdir, size_t *filesize)
 {
 #if defined(WIN32) && defined(UNICODE)
 	/* fopen is implemented as a define with ellipses for
@@ -293,11 +293,17 @@
 		f = fopen(buf, mode);
 	}
 #endif
+	if (f != NULL && filesize != NULL) {
+		/* Find the size of the file */
+		fseek(f, 0, SEEK_END);
+		*filesize = ftell(f);
+		fseek(f, 0, SEEK_SET);
+	}
 	return f;
 }
 
 /** Opens OpenTTD files somewhere in a personal or global directory */
-FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir)
+FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *size)
 {
 	FILE *f = NULL;
 	Searchpath sp;
@@ -305,7 +311,7 @@
 	assert(subdir < NUM_SUBDIRS || subdir == NO_DIRECTORY);
 
 	FOR_ALL_SEARCHPATHS(sp) {
-		f = FioFOpenFileSp(filename, mode, sp, subdir);
+		f = FioFOpenFileSp(filename, mode, sp, subdir, size);
 		if (f != NULL || subdir == NO_DIRECTORY) break;
 	}
 
--- a/src/fileio.h
+++ b/src/fileio.h
@@ -73,7 +73,7 @@
 /** Iterator for all the search paths */
 #define FOR_ALL_SEARCHPATHS(sp) for (sp = SP_FIRST_DIR; sp < NUM_SEARCHPATHS; sp++) if (IsValidSearchPath(sp))
 
-FILE *FioFOpenFile(const char *filename, const char *mode = "rb", Subdirectory subdir = DATA_DIR);
+FILE *FioFOpenFile(const char *filename, const char *mode = "rb", Subdirectory subdir = DATA_DIR, size_t *filesize = NULL);
 bool FioCheckFileExists(const char *filename, Subdirectory subdir = DATA_DIR);
 char *FioGetFullPath(char *buf, size_t buflen, Searchpath sp, Subdirectory subdir, const char *filename);
 char *FioFindFullPath(char *buf, size_t buflen, Subdirectory subdir, const char *filename);
--- a/src/gfxinit.cpp
+++ b/src/gfxinit.cpp
@@ -114,7 +114,8 @@
  * returns true if the checksum is correct */
 static bool FileMD5(const MD5File file, bool warn)
 {
-	FILE *f = FioFOpenFile(file.filename);
+	size_t size;
+	FILE *f = FioFOpenFile(file.filename, "rb", DATA_DIR, &size);
 
 	if (f != NULL) {
 		md5_state_t filemd5state;
@@ -123,8 +124,10 @@
 		size_t len;
 
 		md5_init(&filemd5state);
-		while ((len = fread(buffer, 1, sizeof(buffer), f)) != 0)
+		while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
+			size -= len;
 			md5_append(&filemd5state, buffer, len);
+		}
 
 		if (ferror(f) && warn) ShowInfoF("Error Reading from %s \n", file.filename);
 		fclose(f);
--- a/src/newgrf_config.cpp
+++ b/src/newgrf_config.cpp
@@ -37,15 +37,16 @@
 	FILE *f;
 	md5_state_t md5state;
 	md5_byte_t buffer[1024];
-	size_t len;
+	size_t len, size;
 
 	/* open the file */
-	f = FioFOpenFile(config->filename);
+	f = FioFOpenFile(config->filename, "rb", DATA_DIR, &size);
 	if (f == NULL) return false;
 
 	/* calculate md5sum */
 	md5_init(&md5state);
-	while ((len = fread(buffer, 1, sizeof(buffer), f)) != 0) {
+	while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
+		size -= len;
 		md5_append(&md5state, buffer, len);
 	}
 	md5_finish(&md5state, config->md5sum);