Fix Windows file I/O
This commit is contained in:
@@ -4,6 +4,12 @@
|
||||
#include <CoreFoundation/CoreFoundation.h>
|
||||
#endif
|
||||
|
||||
#ifdef HX_WINDOWS
|
||||
#include <stdio.h>
|
||||
//#include <io.h>
|
||||
//#include <fcntl.h>
|
||||
#endif
|
||||
|
||||
#include <SDL_rwops.h>
|
||||
#include <SDL_timer.h>
|
||||
|
||||
@@ -20,41 +26,84 @@ namespace lime {
|
||||
|
||||
FILE* FILE_HANDLE::getFile () {
|
||||
|
||||
if (((SDL_RWops*)handle)->type == SDL_RWOPS_STDFILE) {
|
||||
#ifndef HX_WINDOWS
|
||||
|
||||
switch (((SDL_RWops*)handle)->type) {
|
||||
|
||||
return ((SDL_RWops*)handle)->hidden.stdio.fp;
|
||||
case SDL_RWOPS_STDFILE:
|
||||
|
||||
return ((SDL_RWops*)handle)->hidden.stdio.fp;
|
||||
|
||||
} else if (((SDL_RWops*)handle)->type == SDL_RWOPS_JNIFILE) {
|
||||
|
||||
#ifdef ANDROID
|
||||
FILE* file = ::fdopen (((SDL_RWops*)handle)->hidden.androidio.fd, "rb");
|
||||
::fseek (file, ((SDL_RWops*)handle)->hidden.androidio.offset, 0);
|
||||
return file;
|
||||
#endif
|
||||
case SDL_RWOPS_JNIFILE:
|
||||
{
|
||||
#ifdef ANDROID
|
||||
FILE* file = ::fdopen (((SDL_RWops*)handle)->hidden.androidio.fd, "rb");
|
||||
::fseek (file, ((SDL_RWops*)handle)->hidden.androidio.offset, 0);
|
||||
return file;
|
||||
#endif
|
||||
}
|
||||
|
||||
case SDL_RWOPS_WINFILE:
|
||||
{
|
||||
/*#ifdef HX_WINDOWS
|
||||
printf("SDKFLJDSLFKJ\n");
|
||||
int fd = _open_osfhandle ((intptr_t)((SDL_RWops*)handle)->hidden.windowsio.h, _O_RDONLY);
|
||||
|
||||
if (fd != -1) {
|
||||
printf("SDKFLJDSLFKJ\n");
|
||||
return ::fdopen (fd, "rb");
|
||||
|
||||
}
|
||||
#endif*/
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
#else
|
||||
|
||||
return (FILE*)handle;
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
int FILE_HANDLE::getLength () {
|
||||
|
||||
#ifndef HX_WINDOWS
|
||||
|
||||
return SDL_RWsize (((SDL_RWops*)handle));
|
||||
|
||||
#else
|
||||
|
||||
return 0;
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
bool FILE_HANDLE::isFile () {
|
||||
|
||||
#ifndef HX_WINDOWS
|
||||
|
||||
return ((SDL_RWops*)handle)->type == SDL_RWOPS_STDFILE;
|
||||
|
||||
#else
|
||||
|
||||
return true;
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
int fclose (FILE_HANDLE *stream) {
|
||||
|
||||
#ifndef HX_WINDOWS
|
||||
|
||||
if (stream) {
|
||||
|
||||
return SDL_RWclose ((SDL_RWops*)stream->handle);
|
||||
@@ -63,11 +112,19 @@ namespace lime {
|
||||
|
||||
return 0;
|
||||
|
||||
#else
|
||||
|
||||
return ::fclose ((FILE*)stream->handle);
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
FILE_HANDLE *fdopen (int fd, const char *mode) {
|
||||
|
||||
#ifndef HX_WINDOWS
|
||||
|
||||
FILE* fp = ::fdopen (fd, mode);
|
||||
SDL_RWops *result = SDL_RWFromFP (fp, SDL_TRUE);
|
||||
|
||||
@@ -79,11 +136,27 @@ namespace lime {
|
||||
|
||||
return NULL;
|
||||
|
||||
#else
|
||||
|
||||
FILE* result = ::fdopen (fd, mode);
|
||||
|
||||
if (result) {
|
||||
|
||||
return new FILE_HANDLE (result);
|
||||
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
FILE_HANDLE *fopen (const char *filename, const char *mode) {
|
||||
|
||||
#ifndef HX_WINDOWS
|
||||
|
||||
SDL_RWops *result;
|
||||
|
||||
#ifdef HX_MACOS
|
||||
@@ -127,34 +200,80 @@ namespace lime {
|
||||
|
||||
return NULL;
|
||||
|
||||
#else
|
||||
|
||||
FILE* result = ::fopen (filename, mode);
|
||||
|
||||
if (result) {
|
||||
|
||||
return new FILE_HANDLE (result);
|
||||
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
size_t fread (void *ptr, size_t size, size_t count, FILE_HANDLE *stream) {
|
||||
|
||||
#ifndef HX_WINDOWS
|
||||
|
||||
return SDL_RWread (stream ? (SDL_RWops*)stream->handle : NULL, ptr, size, count);
|
||||
|
||||
#else
|
||||
|
||||
return ::fread (ptr, size, count, (FILE*)stream->handle);
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
int fseek (FILE_HANDLE *stream, long int offset, int origin) {
|
||||
|
||||
#ifndef HX_WINDOWS
|
||||
|
||||
return SDL_RWseek (stream ? (SDL_RWops*)stream->handle : NULL, offset, origin);
|
||||
|
||||
#else
|
||||
|
||||
return ::fseek ((FILE*)stream->handle, offset, origin);
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
long int ftell (FILE_HANDLE *stream) {
|
||||
|
||||
#ifndef HX_WINDOWS
|
||||
|
||||
return SDL_RWtell (stream ? (SDL_RWops*)stream->handle : NULL);
|
||||
|
||||
#else
|
||||
|
||||
return ::ftell ((FILE*)stream->handle);
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
size_t fwrite (const void *ptr, size_t size, size_t count, FILE_HANDLE *stream) {
|
||||
|
||||
#ifndef HX_WINDOWS
|
||||
|
||||
return SDL_RWwrite (stream ? (SDL_RWops*)stream->handle : NULL, ptr, size, count);
|
||||
|
||||
#else
|
||||
|
||||
return ::fwrite (ptr, size, count, (FILE*)stream->handle);
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user