/*------------------------------------------------------------------*\ author: mathias gumz date: 040129 23:12:50 about: demo of howto bundle fopencookie/custom streams of the gnu-libc and physicsfs file is trying to read the content of physfs.zip !!! COMPILES ONLY WORK WITH -D_GNU_SOURCE !!! gcc -D_GNU_SOURCE -g -c cookiephysfs.c -I. \ -Wmissing-declarations -g -o cookiephysfs -lphysfs \*------------------------------------------------------------------*/ #include #include #include #include #include static size_t physfs_Read(void* file, char* buffer, size_t size) { return PHYSFS_read((PHYSFS_file*)file, buffer, 1, size); } static size_t physfs_Write(void* file, char* buffer, size_t size) { return PHYSFS_write((PHYSFS_file*)file, buffer, 1, size); } static int physfs_Seek(void* file, fpos_t* position, int whence) { PHYSFS_file* f= (PHYSFS_file*)file; if ( whence == SEEK_SET ) return PHYSFS_seek(f, *(unsigned int*)position); else if ( whence == SEEK_CUR ) return PHYSFS_seek(f, *(unsigned int*)position + PHYSFS_tell(f)); else return PHYSFS_seek(f, PHYSFS_fileLength(f) - *(unsigned int*)position); } static int physfs_Close(void* file) { return PHYSFS_close((PHYSFS_file*)file); } cookie_io_functions_t physfs_Functions= { (cookie_read_function_t*)physfs_Read, (cookie_write_function_t*)physfs_Write, (cookie_seek_function_t*)physfs_Seek, (cookie_close_function_t*)physfs_Close }; /*------------------------------------------------------------------*\ \*------------------------------------------------------------------*/ int main(int argc, char* argv[]) { char line[128]; PHYSFS_file* file; FILE* py_file; memset(line, 0, 128); printf("init physfs\n"); PHYSFS_init(argv[0]); PHYSFS_addToSearchPath("physfs.zip", 1); PHYSFS_addToSearchPath("./", 1); file= PHYSFS_openRead("demo.txt"); py_file= fopencookie(file, "r", physfs_Functions); if ( py_file) { printf("--- content of demo.txt .. inside physfs.zip ---- \n"); fgets(line, 128, py_file); printf("%s", line); fgets(line, 128, py_file); printf("%s", line); fgets(line, 128, py_file); printf("%s", line); { float a, b, c= 0.0f; fscanf(py_file, "%f %f %f", &a, &b, &c); printf("a= %f b= %f c= %f\n", a, b, c); } fgets(line, 128, py_file); printf("%s", line); fgets(line, 128, py_file); printf("%s", line); fgets(line, 128, py_file); printf("%s", line); printf("\n--- content of demo.txt .. inside physfs.zip ---- \n"); fflush(stdout); } else printf("error while trying to open file.\n"); fclose(py_file); printf("deinit physfs\n"); PHYSFS_deinit(); return 0; }