diff --git a/data.jrf b/data.jrf index 3e87df1..13b4212 100644 Binary files a/data.jrf and b/data.jrf differ diff --git a/fileManager.cpp b/fileManager.cpp index fe3353c..4f6dd8f 100644 --- a/fileManager.cpp +++ b/fileManager.cpp @@ -1,90 +1,100 @@ #include "fileManager.h" -#include "stdio.h" -#include "stdlib.h" -#include "string.h" -#include "fcntl.h" -#ifdef _WIN32 -#include "io.h" -#else -#include "unistd.h" -#endif +#include +#include -char *resourceFileName = "data.jrf"; +#pragma pack(push,1) -void setResourceFile(char *p_resourceFileName) { +struct DATA_Header { + char magic[4]; + Uint32 num_files; + Uint32 index_offset; +}; + +struct DATA_Info { + Uint32 offset; + Uint32 length; + char name[13]; +}; + +struct DATA_Index { + DATA_Info* file_info; +}; + +struct DATA_File { + DATA_Header header; + DATA_Index index; +}; + +#pragma pack(pop) + +static const char *resourceFileName = "data.jrf"; +static DATA_File *data_file = NULL; + +void file_setResourceFile(const char *p_resourceFileName) { resourceFileName = p_resourceFileName; } -char *GetBufferFromResource(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; - int resultat = read(fd, &numfiles, sizeof(int)); - -#ifdef _WIN32 - int final = eof(fd); -#endif +static void getDataFile() { + std::ifstream fd( resourceFileName, std::ios::in | std::ios::binary ); - //Get the pointers to the stored files - int *filestart = (int *) malloc(sizeof(int) * numfiles); - resultat = read(fd, filestart, sizeof(int) * numfiles); - - //Loop through the files, looking for the file in question - int filenamesize; - char *buffer; - int i; - for(i=0;iheader, sizeof( DATA_Header ) ); + + fd.seekg( data_file->header.index_offset ); + + data_file->index.file_info = (DATA_Info*)malloc( data_file->header.num_files * sizeof( DATA_Info ) ); + + fd.read( (char*)data_file->index.file_info, data_file->header.num_files * sizeof( DATA_Info ) ); + + fd.close(); +} + +const char *file_setBufferFromResource(const char *resourcename, int& filesize) { + + if( data_file == NULL ) { + getDataFile(); + } + + bool found = false; + int count = 0; + while( !found && count < data_file->header.num_files ) { + found = ( strcmp( resourcename, data_file->index.file_info[count].name ) == 0 ); + if( !found ) count++; + } + + if( !found ) { + perror("El recurs no s'ha trobat en l'arxiu de recursos"); + exit(1); + } + + filesize = data_file->index.file_info[count].length; + + std::ifstream fd( resourceFileName, std::ios::in | std::ios::binary ); + + if( fd.fail() ) { + perror("No s'ha pogut obrir l'arxiu de recursos"); + exit(1); + } + + fd.seekg( data_file->index.file_info[count].offset ); + + char* buffer = (char*)malloc( filesize ); + fd.read( buffer, filesize ); + + fd.close(); + return buffer; } + +void file_quit() { + if( data_file != NULL ) { + free( data_file->index.file_info ); + free( data_file ); + } +} diff --git a/fileManager.h b/fileManager.h index e1d49f8..c34e7c4 100644 --- a/fileManager.h +++ b/fileManager.h @@ -1,5 +1,7 @@ #pragma once -void setResourceFile(char *p_resourceFileName); +void file_setResourceFile(const char *p_resourceFileName); -char *GetBufferFromResource(char *resourcename, int *filesize); +const char *file_getBufferFromResource(const char *resourcename, int& filesize); + +void file_quit();