diff --git a/project/Build.xml b/project/Build.xml
index 6bee43798..9a8f4f355 100644
--- a/project/Build.xml
+++ b/project/Build.xml
@@ -133,6 +133,7 @@
+
@@ -149,7 +150,7 @@
-
+
diff --git a/project/include/utils/FileIO.h b/project/include/utils/FileIO.h
index 12c8f6f19..3debd00ef 100644
--- a/project/include/utils/FileIO.h
+++ b/project/include/utils/FileIO.h
@@ -1,25 +1,30 @@
#ifndef LIME_UTILS_FILEIO_H
#define LIME_UTILS_FILEIO_H
-
-extern "C" {
-
- #include
-
-}
+#include
namespace lime {
- extern int fclose (FILE *stream);
- extern FILE *fdopen (int fd, const char *mode);
- extern FILE* fopen (const char *filename, const char *mode);
+ struct FILE_HANDLE {
+
+ void *handle;
+
+ FILE_HANDLE (void* handle) : handle (handle) {}
+
+ FILE* getFile ();
+
+ };
+
+ extern int fclose (FILE_HANDLE *stream);
+ extern FILE_HANDLE *fdopen (int fd, const char *mode);
+ extern FILE_HANDLE *fopen (const char *filename, const char *mode);
//extern FILE* freopen (const char *filename, const char *mode, FILE *stream);
- extern size_t fread (void *ptr, size_t size, size_t count, FILE *stream);
- extern int fseek (FILE *stream, long int offset, int origin);
- extern long int ftell (FILE *stream);
- extern size_t fwrite (const void *ptr, size_t size, size_t count, FILE *stream);
+ extern size_t fread (void *ptr, size_t size, size_t count, FILE_HANDLE *stream);
+ extern int fseek (FILE_HANDLE *stream, long int offset, int origin);
+ extern long int ftell (FILE_HANDLE *stream);
+ extern size_t fwrite (const void *ptr, size_t size, size_t count, FILE_HANDLE *stream);
}
diff --git a/project/src/audio/format/OGG.cpp b/project/src/audio/format/OGG.cpp
index 673829bb8..4f7f08a9c 100644
--- a/project/src/audio/format/OGG.cpp
+++ b/project/src/audio/format/OGG.cpp
@@ -110,7 +110,7 @@ namespace lime {
if (resource->path) {
- FILE *file;
+ FILE_HANDLE *file;
#ifdef ANDROID
FileInfo info = AndroidGetAssetFD (resource->path);
@@ -130,7 +130,7 @@ namespace lime {
#ifdef ANDROID
ov_open (file, &oggFile, NULL, info.length);
#else
- ov_open (file, &oggFile, NULL, 0);
+ ov_open (file->getFile (), &oggFile, NULL, 0);
#endif
} else {
diff --git a/project/src/audio/format/WAV.cpp b/project/src/audio/format/WAV.cpp
index be336943a..e29221ae0 100644
--- a/project/src/audio/format/WAV.cpp
+++ b/project/src/audio/format/WAV.cpp
@@ -49,7 +49,7 @@ namespace lime {
WAVE_Data wave_data;
unsigned char* data;
- FILE *file = NULL;
+ FILE_HANDLE *file = NULL;
if (resource->path) {
diff --git a/project/src/backend/sdl/SDLFileIO.cpp b/project/src/backend/sdl/SDLFileIO.cpp
new file mode 100644
index 000000000..2f5dbce8d
--- /dev/null
+++ b/project/src/backend/sdl/SDLFileIO.cpp
@@ -0,0 +1,111 @@
+#include
+
+#ifdef HX_MACOS
+#include
+#endif
+
+#include
+
+
+namespace lime {
+
+
+ FILE* FILE_HANDLE::getFile () {
+
+ if (((SDL_RWops*)handle)->type == SDL_RWOPS_STDFILE) {
+
+ return ((SDL_RWops*)handle)->hidden.stdio.fp;
+
+ }
+
+ return NULL;
+
+ }
+
+
+ int fclose (FILE_HANDLE *stream) {
+
+ if (stream) {
+
+ return SDL_RWclose ((SDL_RWops*)stream->handle);
+
+ }
+
+ return 0;
+
+ }
+
+
+ FILE_HANDLE *fdopen (int fd, const char *mode) {
+
+ return 0;
+ //return SDL_RWFromFP (fd, mode);
+
+ }
+
+
+ FILE_HANDLE *fopen (const char *filename, const char *mode) {
+
+ SDL_RWops *result;
+ #ifdef HX_MACOS
+ result = SDL_RWFromFile (filename, "rb");
+ if (!result) {
+ CFStringRef str = CFStringCreateWithCString (NULL, filename, kCFStringEncodingUTF8);
+ CFURLRef path = CFBundleCopyResourceURL (CFBundleGetMainBundle (), str, NULL, NULL);
+ CFRelease (str);
+ if (path) {
+ str = CFURLCopyPath (path);
+ CFIndex maxSize = CFStringGetMaximumSizeForEncoding (CFStringGetLength (str), kCFStringEncodingUTF8);
+ char *buffer = (char *)malloc (maxSize);
+ if (CFStringGetCString (str, buffer, maxSize, kCFStringEncodingUTF8)) {
+ result = ::fopen (buffer,"rb");
+ free (buffer);
+ }
+ CFRelease (str);
+ CFRelease (path);
+ }
+ }
+ #else
+ result = SDL_RWFromFile (filename, mode);
+ #endif
+
+ if (result) {
+
+ return new FILE_HANDLE (result);
+
+ }
+
+ return NULL;
+
+ }
+
+
+ size_t fread (void *ptr, size_t size, size_t count, FILE_HANDLE *stream) {
+
+ return SDL_RWread (stream ? (SDL_RWops*)stream->handle : NULL, ptr, size, count);
+
+ }
+
+
+ int fseek (FILE_HANDLE *stream, long int offset, int origin) {
+
+ return SDL_RWseek (stream ? (SDL_RWops*)stream->handle : NULL, offset, origin);
+
+ }
+
+
+ long int ftell (FILE_HANDLE *stream) {
+
+ return SDL_RWtell (stream ? (SDL_RWops*)stream->handle : NULL);
+
+ }
+
+
+ size_t fwrite (const void *ptr, size_t size, size_t count, FILE_HANDLE *stream) {
+
+ return SDL_RWwrite (stream ? (SDL_RWops*)stream->handle : NULL, ptr, size, count);
+
+ }
+
+
+}
\ No newline at end of file
diff --git a/project/src/graphics/format/JPEG.cpp b/project/src/graphics/format/JPEG.cpp
index a51d2b991..871b45a4f 100644
--- a/project/src/graphics/format/JPEG.cpp
+++ b/project/src/graphics/format/JPEG.cpp
@@ -190,7 +190,7 @@ namespace lime {
jpegError.base.error_exit = OnError;
jpegError.base.output_message = OnOutput;
- FILE *file = NULL;
+ FILE_HANDLE *file = NULL;
if (setjmp (jpegError.on_error)) {
@@ -210,7 +210,7 @@ namespace lime {
if (resource->path) {
file = lime::fopen (resource->path, "rb");
- jpeg_stdio_src (&cinfo, file);
+ jpeg_stdio_src (&cinfo, file->getFile ());
} else {
diff --git a/project/src/graphics/format/PNG.cpp b/project/src/graphics/format/PNG.cpp
index 710fc375e..08258048a 100644
--- a/project/src/graphics/format/PNG.cpp
+++ b/project/src/graphics/format/PNG.cpp
@@ -83,7 +83,7 @@ namespace lime {
png_uint_32 width, height;
int bit_depth, color_type, interlace_type;
- FILE *file = NULL;
+ FILE_HANDLE *file = NULL;
if (resource->path) {
@@ -136,7 +136,7 @@ namespace lime {
if (file) {
- png_init_io (png_ptr, file);
+ png_init_io (png_ptr, file->getFile ());
png_set_sig_bytes (png_ptr, PNG_SIG_SIZE);
} else {
diff --git a/project/src/utils/ByteArray.cpp b/project/src/utils/ByteArray.cpp
index 334ea2599..634b45774 100644
--- a/project/src/utils/ByteArray.cpp
+++ b/project/src/utils/ByteArray.cpp
@@ -145,7 +145,7 @@ namespace lime {
ByteArray ByteArray::FromFile(const OSChar *inFilename)
{
- FILE *file = lime::fopen (inFilename, "rb");
+ FILE_HANDLE *file = lime::fopen (inFilename, "rb");
if (!file)
{
#ifdef ANDROID
@@ -170,7 +170,7 @@ value lime_byte_array_overwrite_file(value inFilename, value inBytes) {
// file is created if it doesn't exist,
// if it exists, it is truncated to zero
- FILE *file = lime::fopen (val_os_string(inFilename), "wb");
+ FILE_HANDLE *file = lime::fopen (val_os_string(inFilename), "wb");
if (!file)
{
#ifdef ANDROID
diff --git a/project/src/utils/FileIO.cpp b/project/src/utils/FileIO.cpp
index 87a56fbd7..e0407c787 100644
--- a/project/src/utils/FileIO.cpp
+++ b/project/src/utils/FileIO.cpp
@@ -8,24 +8,48 @@
namespace lime {
- int fclose (FILE *stream) {
+ FILE* FILE_HANDLE::getFile () {
- return ::fclose (stream);
+ return (FILE*)handle;
}
- FILE *fdopen (int fd, const char *mode) {
+ int fclose (FILE_HANDLE *stream) {
- return ::fdopen (fd, mode);
+ if (stream) {
+
+ int value = ::fclose ((FILE*)stream->handle);
+ if (stream) delete stream;
+ return value;
+
+ }
+
+ return 0;
}
- FILE* fopen (const char *filename, const char *mode) {
+ FILE_HANDLE *fdopen (int fd, const char *mode) {
+ FILE* result = ::fdopen (fd, mode);
+
+ if (result) {
+
+ return new FILE_HANDLE (result);
+
+ }
+
+ return NULL;
+
+ }
+
+
+ FILE_HANDLE *fopen (const char *filename, const char *mode) {
+
+ FILE* result;
#ifdef HX_MACOS
- FILE *result = ::fopen (filename, "rb");
+ result = ::fopen (filename, "rb");
if (!result) {
CFStringRef str = CFStringCreateWithCString (NULL, filename, kCFStringEncodingUTF8);
CFURLRef path = CFBundleCopyResourceURL (CFBundleGetMainBundle (), str, NULL, NULL);
@@ -42,38 +66,45 @@ namespace lime {
CFRelease (path);
}
}
- return result;
#else
- return ::fopen (filename, mode);
+ result = ::fopen (filename, mode);
#endif
- }
-
-
- size_t fread (void *ptr, size_t size, size_t count, FILE *stream) {
+ if (result) {
+
+ return new FILE_HANDLE (result);
+
+ }
- return ::fread (ptr, size, count, stream);
+ return NULL;
}
- int fseek (FILE *stream, long int offset, int origin) {
+ size_t fread (void *ptr, size_t size, size_t count, FILE_HANDLE *stream) {
- return ::fseek (stream, offset, origin);
+ return ::fread (ptr, size, count, stream ? (FILE*)stream->handle : NULL);
}
- long int ftell (FILE *stream) {
+ int fseek (FILE_HANDLE *stream, long int offset, int origin) {
- return ::ftell (stream);
+ return ::fseek (stream ? (FILE*)stream->handle : NULL, offset, origin);
}
- size_t fwrite (const void *ptr, size_t size, size_t count, FILE *stream) {
+ long int ftell (FILE_HANDLE *stream) {
- return ::fwrite (ptr, size, count, stream);
+ return ::ftell (stream ? (FILE*)stream->handle : NULL);
+
+ }
+
+
+ size_t fwrite (const void *ptr, size_t size, size_t count, FILE_HANDLE *stream) {
+
+ return ::fwrite (ptr, size, count, (FILE*)stream->handle);
}