diff --git a/project/Build.xml b/project/Build.xml index 4acb14c10..70fb6e682 100644 --- a/project/Build.xml +++ b/project/Build.xml @@ -112,6 +112,7 @@ + diff --git a/project/include/utils/ByteArray.h b/project/include/utils/ByteArray.h index 2403c11e6..922a640f4 100644 --- a/project/include/utils/ByteArray.h +++ b/project/include/utils/ByteArray.h @@ -62,19 +62,13 @@ namespace lime { //#define val_os_string val_string #if defined(IPHONE) - FILE *OpenRead(const char *inName); - FILE *OpenOverwrite(const char *inName); // [ddc] extern int gFixedOrientation; #elif defined(HX_MACOS) - FILE *OpenRead(const char *inName); - #define OpenOverwrite(x) fopen(x,"wb") #else #ifdef TIZEN extern int gFixedOrientation; #endif - #define OpenRead(x) fopen(x,"rb") - #define OpenOverwrite(x) fopen(x,"wb") // [ddc] #endif //#endif diff --git a/project/include/utils/FileIO.h b/project/include/utils/FileIO.h new file mode 100644 index 000000000..8f238bac6 --- /dev/null +++ b/project/include/utils/FileIO.h @@ -0,0 +1,27 @@ +#ifndef LIME_UTILS_FILEIO_H +#define LIME_UTILS_FILEIO_H + + +extern "C" { + + #include + +} + + +namespace lime { + + + extern int fclose (FILE *stream); + extern FILE* 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); + + +} + + +#endif \ No newline at end of file diff --git a/project/src/graphics/JPEG.cpp b/project/src/graphics/JPEG.cpp index 7ef8bc55c..619eb9d9d 100644 --- a/project/src/graphics/JPEG.cpp +++ b/project/src/graphics/JPEG.cpp @@ -1,14 +1,14 @@ extern "C" { - + #include #include #include - + } #include #include -#include +#include namespace lime { @@ -19,7 +19,7 @@ namespace lime { struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; - FILE *file = OpenRead (path); + FILE *file = lime::fopen (path, "rb"); cinfo.err = jpeg_std_error (&jerr); jpeg_create_decompress (&cinfo); @@ -29,7 +29,7 @@ namespace lime { jpeg_start_decompress (&cinfo); int components = cinfo.num_components; - image->Resize(cinfo.output_width, cinfo.output_height); + image->Resize (cinfo.output_width, cinfo.output_height); unsigned char *bytes = image->data->Bytes (); unsigned char *scanline = new unsigned char [image->width * image->height * components]; @@ -59,7 +59,7 @@ namespace lime { } - fclose (file); + lime::fclose (file); jpeg_destroy_decompress (&cinfo); return true; diff --git a/project/src/graphics/PNG.cpp b/project/src/graphics/PNG.cpp index e5a2c7ef2..8eaedb678 100644 --- a/project/src/graphics/PNG.cpp +++ b/project/src/graphics/PNG.cpp @@ -7,7 +7,7 @@ extern "C" { #include #include -#include +#include namespace lime { @@ -21,21 +21,21 @@ namespace lime { png_uint_32 width, height; int bit_depth, color_type; - FILE *file = OpenRead (path); + FILE *file = lime::fopen (path, "rb"); if (!file) return false; // verify the PNG signature - int read = fread (png_sig, PNG_SIG_SIZE, 1, file); + int read = lime::fread (png_sig, PNG_SIG_SIZE, 1, file); if (png_sig_cmp (png_sig, 0, PNG_SIG_SIZE)) { - fclose (file); + lime::fclose (file); return false; } if ((png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)) == NULL) { - fclose (file); + lime::fclose (file); return false; } @@ -43,7 +43,7 @@ namespace lime { if ((info_ptr = png_create_info_struct (png_ptr)) == NULL) { png_destroy_read_struct (&png_ptr, (png_infopp)NULL, (png_infopp)NULL); - fclose (file); + lime::fclose (file); return false; } @@ -52,7 +52,7 @@ namespace lime { if (setjmp (png_jmpbuf (png_ptr))) { png_destroy_read_struct (&png_ptr, &info_ptr, (png_infopp)NULL); - fclose (file); + lime::fclose (file); return false; } diff --git a/project/src/utils/ByteArray.cpp b/project/src/utils/ByteArray.cpp index 061536cba..5356ae400 100644 --- a/project/src/utils/ByteArray.cpp +++ b/project/src/utils/ByteArray.cpp @@ -2,6 +2,7 @@ #include #include +#include #include @@ -113,35 +114,11 @@ namespace lime { } DEFINE_PRIM(lime_byte_array_get_native_pointer,1); -#if defined(HX_MACOS) -#include -FILE *OpenRead(const char *inName) -{ - FILE *result = fopen(inName,"rb"); - if (!result) { - CFStringRef str = CFStringCreateWithCString(NULL, inName, 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); - } - } - return result; -} -#endif ByteArray ByteArray::FromFile(const OSChar *inFilename) { - FILE *file = OpenRead(inFilename); + FILE *file = lime::fopen (inFilename, "rb"); if (!file) { #ifdef ANDROID @@ -150,13 +127,13 @@ FILE *OpenRead(const char *inName) return ByteArray(); } - fseek(file,0,SEEK_END); - int len = ftell(file); - fseek(file,0,SEEK_SET); + lime::fseek(file,0,SEEK_END); + int len = lime::ftell(file); + lime::fseek(file,0,SEEK_SET); ByteArray result(len); - int status = fread(result.Bytes(),len,1,file); - fclose(file); + int status = lime::fread(result.Bytes(),len,1,file); + lime::fclose(file); return result; } @@ -166,7 +143,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 = OpenOverwrite(val_os_string(inFilename)); + FILE *file = lime::fopen (val_os_string(inFilename), "wb"); if (!file) { #ifdef ANDROID @@ -181,9 +158,9 @@ value lime_byte_array_overwrite_file(value inFilename, value inBytes) { // stream pointed to by stream, obtaining them from the location given by // ptr. // fwrite(const void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream); - fwrite( array.Bytes() , 1, array.Size() , file); + lime::fwrite( array.Bytes() , 1, array.Size() , file); - fclose(file); + lime::fclose(file); return alloc_null(); } DEFINE_PRIM(lime_byte_array_overwrite_file, 2); diff --git a/project/src/utils/FileIO.cpp b/project/src/utils/FileIO.cpp new file mode 100644 index 000000000..3dc5aa12e --- /dev/null +++ b/project/src/utils/FileIO.cpp @@ -0,0 +1,74 @@ +#include + +#ifdef HX_MACOS +#include +#endif + + +namespace lime { + + + int fclose (FILE *stream) { + + return ::fclose (stream); + + } + + + FILE* fopen (const char *filename, const char *mode) { + + #ifdef HX_MACOS + FILE *result = ::fopen (filename, "rb"); + if (!result) { + CFStringRef str = CFStringCreateWithCString (NULL, inName, 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); + } + } + return result; + #else + return ::fopen (filename, mode); + #endif + + } + + + size_t fread (void *ptr, size_t size, size_t count, FILE *stream) { + + return ::fread (ptr, size, count, stream); + + } + + + int fseek (FILE *stream, long int offset, int origin) { + + return ::fseek (stream, offset, origin); + + } + + + long int ftell (FILE *stream) { + + return ::ftell (stream); + + } + + + size_t fwrite (const void *ptr, size_t size, size_t count, FILE *stream) { + + return ::fwrite (ptr, size, count, stream); + + } + + +} \ No newline at end of file