Added an API to get the size of a file - WARNING! ABI CHANGE!

This commit is contained in:
Sam Lantinga 2012-11-03 18:43:36 -07:00
parent 732a5d8165
commit 9bd6a89040
7 changed files with 102 additions and 38 deletions

View file

@ -121,11 +121,29 @@ windows_file_open(SDL_RWops * context, const char *filename, const char *mode)
return 0; /* ok */
}
static long SDLCALL
windows_file_seek(SDL_RWops * context, long offset, int whence)
static Sint64 SDLCALL
windows_file_size(SDL_RWops * context)
{
LARGE_INTEGER size;
if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE) {
SDL_SetError("windows_file_size: invalid context/file not opened");
return -1;
}
if (!GetFileSizeEx(context->hidden.windowsio.h, &size)) {
WIN_SetError("windows_file_size");
return -1;
}
return size.QuadPart;
}
static Sint64 SDLCALL
windows_file_seek(SDL_RWops * context, Sint64 offset, int whence)
{
DWORD windowswhence;
long file_pos;
LARGE_INTEGER windowsoffset;
if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE) {
SDL_SetError("windows_file_seek: invalid context/file not opened");
@ -153,14 +171,12 @@ windows_file_seek(SDL_RWops * context, long offset, int whence)
return -1;
}
file_pos =
SetFilePointer(context->hidden.windowsio.h, offset, NULL, windowswhence);
if (file_pos != INVALID_SET_FILE_POINTER)
return file_pos; /* success */
SDL_Error(SDL_EFSEEK);
return -1; /* error */
windowsoffset.QuadPart = offset;
if (!SetFilePointerEx(context->hidden.windowsio.h, windowsoffset, &windowsoffset, windowswhence)) {
WIN_SetError("windows_file_seek");
return -1;
}
return windowsoffset.QuadPart;
}
static size_t SDLCALL
@ -281,15 +297,39 @@ windows_file_close(SDL_RWops * context)
/* Functions to read/write stdio file pointers */
static long SDLCALL
stdio_seek(SDL_RWops * context, long offset, int whence)
static Sint64 SDLCALL
stdio_size(SDL_RWops * context)
{
Sint64 pos, size;
pos = SDL_RWseek(context, 0, RW_SEEK_CUR);
if (pos < 0) {
return -1;
}
size = SDL_RWseek(context, 0, RW_SEEK_END);
SDL_RWseek(context, pos, RW_SEEK_SET);
return size;
}
static Sint64 SDLCALL
stdio_seek(SDL_RWops * context, Sint64 offset, int whence)
{
#ifdef HAVE_FSEEKO64
if (fseek64o(context->hidden.stdio.fp, (off64_t)offset, whence) == 0) {
return ftell64o(context->hidden.stdio.fp);
}
#elif defined(HAVE_FSEEKO)
if (fseeko(context->hidden.stdio.fp, (off_t)offset, whence) == 0) {
return ftello(context->hidden.stdio.fp);
}
#else
if (fseek(context->hidden.stdio.fp, offset, whence) == 0) {
return (ftell(context->hidden.stdio.fp));
} else {
SDL_Error(SDL_EFSEEK);
return (-1);
}
#endif
SDL_Error(SDL_EFSEEK);
return (-1);
}
static size_t SDLCALL
@ -336,8 +376,14 @@ stdio_close(SDL_RWops * context)
/* Functions to read/write memory pointers */
static long SDLCALL
mem_seek(SDL_RWops * context, long offset, int whence)
static Sint64 SDLCALL
mem_size(SDL_RWops * context)
{
return (Sint64)(context->hidden.mem.stop - context->hidden.mem.base);
}
static Sint64 SDLCALL
mem_seek(SDL_RWops * context, Sint64 offset, int whence)
{
Uint8 *newpos;
@ -362,7 +408,7 @@ mem_seek(SDL_RWops * context, long offset, int whence)
newpos = context->hidden.mem.stop;
}
context->hidden.mem.here = newpos;
return (long)(context->hidden.mem.here - context->hidden.mem.base);
return (Sint64)(context->hidden.mem.here - context->hidden.mem.base);
}
static size_t SDLCALL
@ -460,6 +506,7 @@ SDL_RWFromFile(const char *file, const char *mode)
SDL_FreeRW(rwops);
return NULL;
}
rwops->size = Android_JNI_FileSize;
rwops->seek = Android_JNI_FileSeek;
rwops->read = Android_JNI_FileRead;
rwops->write = Android_JNI_FileWrite;
@ -473,6 +520,7 @@ SDL_RWFromFile(const char *file, const char *mode)
SDL_FreeRW(rwops);
return NULL;
}
rwops->size = windows_file_size;
rwops->seek = windows_file_seek;
rwops->read = windows_file_read;
rwops->write = windows_file_write;
@ -513,6 +561,7 @@ SDL_RWFromFP(FILE * fp, SDL_bool autoclose)
rwops = SDL_AllocRW();
if (rwops != NULL) {
rwops->size = stdio_size;
rwops->seek = stdio_seek;
rwops->read = stdio_read;
rwops->write = stdio_write;
@ -538,6 +587,7 @@ SDL_RWFromMem(void *mem, int size)
rwops = SDL_AllocRW();
if (rwops != NULL) {
rwops->size = mem_size;
rwops->seek = mem_seek;
rwops->read = mem_read;
rwops->write = mem_write;
@ -556,6 +606,7 @@ SDL_RWFromConstMem(const void *mem, int size)
rwops = SDL_AllocRW();
if (rwops != NULL) {
rwops->size = mem_size;
rwops->seek = mem_seek;
rwops->read = mem_read;
rwops->write = mem_writeconst;