mirror of https://github.com/OpenTTD/OpenTTD
(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
parent
c0c12bca3d
commit
3d0ac92267
|
@ -267,7 +267,7 @@ char *FioGetDirectory(char *buf, size_t buflen, Subdirectory subdir)
|
||||||
return buf;
|
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)
|
#if defined(WIN32) && defined(UNICODE)
|
||||||
/* fopen is implemented as a define with ellipses for
|
/* fopen is implemented as a define with ellipses for
|
||||||
|
@ -293,11 +293,17 @@ FILE *FioFOpenFileSp(const char *filename, const char *mode, Searchpath sp, Subd
|
||||||
f = fopen(buf, mode);
|
f = fopen(buf, mode);
|
||||||
}
|
}
|
||||||
#endif
|
#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;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Opens OpenTTD files somewhere in a personal or global directory */
|
/** 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;
|
FILE *f = NULL;
|
||||||
Searchpath sp;
|
Searchpath sp;
|
||||||
|
@ -305,7 +311,7 @@ FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir)
|
||||||
assert(subdir < NUM_SUBDIRS || subdir == NO_DIRECTORY);
|
assert(subdir < NUM_SUBDIRS || subdir == NO_DIRECTORY);
|
||||||
|
|
||||||
FOR_ALL_SEARCHPATHS(sp) {
|
FOR_ALL_SEARCHPATHS(sp) {
|
||||||
f = FioFOpenFileSp(filename, mode, sp, subdir);
|
f = FioFOpenFileSp(filename, mode, sp, subdir, size);
|
||||||
if (f != NULL || subdir == NO_DIRECTORY) break;
|
if (f != NULL || subdir == NO_DIRECTORY) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -73,7 +73,7 @@ static inline bool IsValidSearchPath(Searchpath sp)
|
||||||
/** Iterator for all the search paths */
|
/** Iterator for all the search paths */
|
||||||
#define FOR_ALL_SEARCHPATHS(sp) for (sp = SP_FIRST_DIR; sp < NUM_SEARCHPATHS; sp++) if (IsValidSearchPath(sp))
|
#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);
|
bool FioCheckFileExists(const char *filename, Subdirectory subdir = DATA_DIR);
|
||||||
char *FioGetFullPath(char *buf, size_t buflen, Searchpath sp, Subdirectory subdir, const char *filename);
|
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);
|
char *FioFindFullPath(char *buf, size_t buflen, Subdirectory subdir, const char *filename);
|
||||||
|
|
|
@ -114,7 +114,8 @@ static bool CheckMD5Digest(const MD5File file, md5_byte_t *digest, bool warn)
|
||||||
* returns true if the checksum is correct */
|
* returns true if the checksum is correct */
|
||||||
static bool FileMD5(const MD5File file, bool warn)
|
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) {
|
if (f != NULL) {
|
||||||
md5_state_t filemd5state;
|
md5_state_t filemd5state;
|
||||||
|
@ -123,8 +124,10 @@ static bool FileMD5(const MD5File file, bool warn)
|
||||||
size_t len;
|
size_t len;
|
||||||
|
|
||||||
md5_init(&filemd5state);
|
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);
|
md5_append(&filemd5state, buffer, len);
|
||||||
|
}
|
||||||
|
|
||||||
if (ferror(f) && warn) ShowInfoF("Error Reading from %s \n", file.filename);
|
if (ferror(f) && warn) ShowInfoF("Error Reading from %s \n", file.filename);
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
|
|
@ -37,15 +37,16 @@ static bool CalcGRFMD5Sum(GRFConfig *config)
|
||||||
FILE *f;
|
FILE *f;
|
||||||
md5_state_t md5state;
|
md5_state_t md5state;
|
||||||
md5_byte_t buffer[1024];
|
md5_byte_t buffer[1024];
|
||||||
size_t len;
|
size_t len, size;
|
||||||
|
|
||||||
/* open the file */
|
/* open the file */
|
||||||
f = FioFOpenFile(config->filename);
|
f = FioFOpenFile(config->filename, "rb", DATA_DIR, &size);
|
||||||
if (f == NULL) return false;
|
if (f == NULL) return false;
|
||||||
|
|
||||||
/* calculate md5sum */
|
/* calculate md5sum */
|
||||||
md5_init(&md5state);
|
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_append(&md5state, buffer, len);
|
||||||
}
|
}
|
||||||
md5_finish(&md5state, config->md5sum);
|
md5_finish(&md5state, config->md5sum);
|
||||||
|
|
Loading…
Reference in New Issue