X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;ds=sidebyside;f=src%2Fandroid%2Fassfile.c;fp=src%2Fandroid%2Fassfile.c;h=ec3d0f96240691d970d61f0dc8e1593c6a1d4972;hb=6c206da1b97ad86e095d383f4ebd3d8f332ad416;hp=0000000000000000000000000000000000000000;hpb=ab58ef7af3d960fd1360002a370ec76ce96ab768;p=andemo diff --git a/src/android/assfile.c b/src/android/assfile.c new file mode 100644 index 0000000..ec3d0f9 --- /dev/null +++ b/src/android/assfile.c @@ -0,0 +1,92 @@ +#include +#include +#include +#include "assfile.h" +#include "android_native_app_glue.h" + +struct android_app *app; /* defined in android/amain.c */ + +static int putback_buf = -1; + +ass_file *ass_fopen(const char *fname, const char *mode) +{ + AAsset *ass; + unsigned int flags = 0; + char prev = 0; + + while(*mode) { + switch(*mode) { + case 'r': + flags |= O_RDONLY; + break; + + case 'w': + flags |= O_WRONLY; + break; + + case 'a': + flags |= O_APPEND; + break; + + case '+': + if(prev == 'w' || prev == 'a') { + flags |= O_CREAT; + } + break; + + default: + break; + } + prev = *mode++; + } + + assert(app); + assert(app->activity); + assert(app->activity->assetManager); + if(!(ass = AAssetManager_open(app->activity->assetManager, fname, flags))) { + return 0; + } + return (ass_file*)ass; +} + +void ass_fclose(ass_file *fp) +{ + AAsset_close((AAsset*)fp); +} + +long ass_fseek(ass_file *fp, long offs, int whence) +{ + return AAsset_seek((AAsset*)fp, offs, whence); +} + +long ass_ftell(ass_file *fp) +{ + return AAsset_seek((AAsset*)fp, 0, SEEK_CUR); +} + +int ass_feof(ass_file *fp) +{ + return AAsset_getRemainingLength((AAsset*)fp) == 0 ? 1 : 0; +} + +size_t ass_fread(void *buf, size_t size, size_t count, ass_file *fp) +{ + size_t nbytes = size * count; + size_t rdbytes = 0; + + if(putback_buf >= 0) { + *(unsigned char*)buf = (unsigned char)putback_buf; + putback_buf = -1; + --nbytes; + ++rdbytes; + buf = (unsigned char*)buf + 1; + } + + return (rdbytes + AAsset_read((AAsset*)fp, buf, nbytes)) / size; +} + +int ass_ungetc(int c, ass_file *fp) +{ + putback_buf = c; + return 0; +}