Here are some simple steps to getting PhysicsFS working in your program.
This tutorial was created by some friendly people in #icculus.org in the
freenode IRC network.
First thing you need to do is initialize the filesystem. You simply call
PHYSFS_init(argv[0]); to do this.
From the docs:
argv0 the argv[0] string passed to your program's mainline. This may be NULL
on most platforms (such as ones without a standard main() function), but you
should always try to pass something in here. Unix-like systems such as Linux
_need_ to pass argv[0] from main() in here.
PHYSFS_init(NULL);
After you have it initialized, then you need to set up any archives that
will be read from. You can do this by using the
PHYSFS_AddToSearchPath(char*, int); function. The 1 makes it added to the
end of the search path so that it is the last looked at. You could instead
put a 0 there and have it be the first thing looked at.
PHYSFS_AddToSearchPath("myzip.zip", 1);
Now that we have initialized physfs and added an archive to its search path,
we can do some file reading. First thing you will want to do is check to
make sure that the file exists. You can do this by calling
PHYSFS_exists(char*);
PHYSFS_exists("myfile.txt");
Then you can use PHYSFS_openRead(char*); to get a PHYSFS_file pointer to
that file. There is also available: PHYSFS_openWrite(char*); and
PHYSFS_openAppend(char*);
NOTE: If you would like to do any writing you need to set a dir to write
too. You can do this by using PHYSFS_setWriteDir(const char *newDir);
PHYSFS_file* myfile = PHYSFS_openRead("myfile.txt");
If you are going to be reading this file into memory, you can use
PHYSFS_fileLength(PHYSFS_file*) to find out how many bytes you need to
allocate for the file. You can also use this to check and make sure the
file isn't larger than the max file size you want to open.
PHYSFS_sint64 file_size = PHYSFS_fileLength(myfile);
Now it is time to read the file into memory. In this tutorial I am simply
going to read it all into a byte array using PHYSFS_read (PHYSFS_file *fp,
your_buffer, size_of_object_to_read, number_of_objects_to_read); and it
returns the number of objects read.
//on a platform where char = 1 byte
char *myBuf;
...
myBuf = new char[PHYSFS_fileLength(myfile)];
int length_read = PHYSFS_read (myfile, myBuf, 1, PHYSFS_fileLength(myfile));
When you are finished with your file you need to close it. You can do so
with the function PHYSFS_close(PHYSFS_file*);
PHYSFS_close(myfile);
When you are finished with PHYSFS completely, you need to call the
PHYSFS_deinit(void) function.
From the docs:
This closes any files opened via PhysicsFS, blanks the search/write paths,
frees memory, and invalidates all of your file handles.
PHYSFS_deinit();
Ok that is it for the tutorial. It should give you a rough overview of how
things are structured and you can check the online docs for more information
on using the rest of the API.