Some work on file I/O
This commit is contained in:
@@ -112,6 +112,7 @@
|
||||
<file name="src/ui/TouchEvent.cpp" />
|
||||
<file name="src/ui/WindowEvent.cpp" />
|
||||
<file name="src/utils/ByteArray.cpp" />
|
||||
<file name="src/utils/FileIO.cpp" />
|
||||
|
||||
</files>
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
27
project/include/utils/FileIO.h
Normal file
27
project/include/utils/FileIO.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef LIME_UTILS_FILEIO_H
|
||||
#define LIME_UTILS_FILEIO_H
|
||||
|
||||
|
||||
extern "C" {
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
@@ -1,14 +1,14 @@
|
||||
extern "C" {
|
||||
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdio.h>
|
||||
#include <jpeglib.h>
|
||||
|
||||
|
||||
}
|
||||
|
||||
#include <graphics/Image.h>
|
||||
#include <graphics/JPEG.h>
|
||||
#include <utils/ByteArray.h>
|
||||
#include <utils/FileIO.h>
|
||||
|
||||
|
||||
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;
|
||||
|
||||
@@ -7,7 +7,7 @@ extern "C" {
|
||||
|
||||
#include <graphics/Image.h>
|
||||
#include <graphics/PNG.h>
|
||||
#include <utils/ByteArray.h>
|
||||
#include <utils/FileIO.h>
|
||||
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <hx/CFFI.h>
|
||||
|
||||
#include <utils/ByteArray.h>
|
||||
#include <utils/FileIO.h>
|
||||
#include <string>
|
||||
|
||||
|
||||
@@ -113,35 +114,11 @@ namespace lime {
|
||||
} DEFINE_PRIM(lime_byte_array_get_native_pointer,1);
|
||||
|
||||
|
||||
#if defined(HX_MACOS)
|
||||
#include <CoreFoundation/CoreFoundation.h>
|
||||
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);
|
||||
|
||||
74
project/src/utils/FileIO.cpp
Normal file
74
project/src/utils/FileIO.cpp
Normal file
@@ -0,0 +1,74 @@
|
||||
#include <utils/FileIO.h>
|
||||
|
||||
#ifdef HX_MACOS
|
||||
#include <CoreFoundation/CoreFoundation.h>
|
||||
#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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user