This is the reading method.
static char* GetBufferFromResource(char *resourcefilename, char *resourcename, int *filesize)
{
//Try to open the resource file in question
int fd = open(resourcefilename, O_RDONLY);
if (fd < 0)
{
perror("Error opening resource file");
exit(1);
}
//Make sure we're at the beginning of the file
lseek(fd, 0, SEEK_SET);
//Read the first INT, which will tell us how many files are in this resource
int numfiles;
read(fd, &numfiles, sizeof(int));
std::cout << "Number of files: " << numfiles << std::endl;
//Get the pointers to the stored files
int *filestart = (int *) malloc(sizeof(int) * numfiles);
read(fd, filestart, sizeof(int) * numfiles);
std::cout << "File starts at: " << *filestart << std::endl;
//Loop through the files, looking for the file in question
int filenamesize;
char *buffer;
int i;
for(i=0;i<numfiles;i++)
{
char *filename;
//Seek to the location
lseek(fd, filestart[i], SEEK_SET);
//Get the filesize value
read(fd, filesize, sizeof(int));
//Get the size of the filename string
read(fd, &filenamesize, sizeof(int));
//Size the buffer and read the filename
filename = (char *) malloc(filenamesize + 1);
read(fd, filename, filenamesize);
//Remember to terminate the string properly!
filename[filenamesize] = '\0';
//Compare to the string we're looking for
std::cout << "Comparing " << resourcename << " to " << filename << std::endl;
if (strcmp(filename, resourcename) == 0)
{
//Get the contents of the file
std::cout << resourcename << " is equal to " << filename << std::endl;
buffer = (char *) malloc(*filesize);
read(fd, buffer, *filesize);
free(filename);
break;
}
//Free the filename buffer
free(filename);
}
//Release memory
free(filestart);
//Close the resource file!
close(fd);
//Did we find the file within the resource that we were looking for?
if (buffer == NULL)
{
printf("Unable to find '%s' in the resource file!\n", resourcename);
exit(1);
}
//Return the buffer
return buffer;
}