/* build physfs: cmake . && make * gcc -ggdb -I src test_memio.c libphysfs.a * valgrind --leak-check=full ./a.out */ #include #include #include #include #define __PHYSICSFS_INTERNAL__ #include "physfs_internal.h" typedef struct { PHYSFS_Io *io; } my_state; static void *ABC_openArchive(PHYSFS_Io *io, const char *name, int forWriting, int *claimed) { my_state *s = allocator.Malloc(sizeof(my_state)); s->io = io; printf("init %p\n", s); return s; } static void ABC_closeArchive(void *opaque) { printf("free %p\n", opaque); my_state *s = opaque; if (s->io) { s->io->destroy(s->io); } allocator.Free(s); } static PHYSFS_Io *ABC_openRead(void *opaque, const char *filename) { const char *hello = "hello"; char *buf = allocator.Malloc(6); strcpy(buf, hello); return __PHYSFS_createMemoryIo(buf, 5, allocator.Free); } static PHYSFS_EnumerateCallbackResult ABC_enumerate(void *opaque, const char *dname, PHYSFS_EnumerateCallback cb, const char *origdir, void *callbackdata) { assert(0); } static int ABC_stat(void *opaque, const char *filename, PHYSFS_Stat *stat) { BAIL(PHYSFS_ERR_UNSUPPORTED, 0); } static int ABC_mkdir(void *opaque, const char *name) { BAIL(PHYSFS_ERR_UNSUPPORTED, 0); } static int ABC_remove(void *opaque, const char *name) { BAIL(PHYSFS_ERR_UNSUPPORTED, 0); } static PHYSFS_Io *ABC_openWrite(void *opaque, const char *filename) { BAIL(PHYSFS_ERR_UNSUPPORTED, 0); } static PHYSFS_Io *ABC_openAppend(void *opaque, const char *filename) { BAIL(PHYSFS_ERR_UNSUPPORTED, 0); } const PHYSFS_Archiver PHYSFS_Archiver_ABC = { CURRENT_PHYSFS_ARCHIVER_API_VERSION, { "ABC", "", "" "", "", 0, }, ABC_openArchive, ABC_enumerate, ABC_openRead, ABC_openWrite, ABC_openAppend, ABC_remove, ABC_mkdir, ABC_stat, ABC_closeArchive }; #if 0 354 /* we _are_ the parent. */ 355 assert(info->refcount > 0); /* even in a race, we hold a reference. */ 356 357 if (__PHYSFS_ATOMIC_DECR(&info->refcount) == 0) #endif // change: // #define __PHYSFS_ATOMIC_DECR(ptrval) __sync_fetch_and_add(ptrval, -1) // to: // #define __PHYSFS_ATOMIC_DECR(ptrval) __sync_add_and_fetch(ptrval, -1) int main(int argc, char *argv[]) { PHYSFS_init(argv[0]); PHYSFS_registerArchiver(&PHYSFS_Archiver_ABC); const char *container = "dummy.abc"; FILE *c = fopen(container, "w"); fclose(c); PHYSFS_mount(container, "/", 1); PHYSFS_mount(".", "/", 1); PHYSFS_File *f = PHYSFS_openRead("hello.txt"); if (f) { char buf[11] = {0}; ssize_t r = PHYSFS_readBytes(f, buf, 10); printf("read %ld => %s\n", r, buf); PHYSFS_close(f); } PHYSFS_unmount(container); PHYSFS_deregisterArchiver("ABC"); PHYSFS_deinit(); unlink(container); return 0; }