diff --git a/conanfile.txt b/conanfile.txt index 5fe2a90..1f3d10b 100644 --- a/conanfile.txt +++ b/conanfile.txt @@ -11,6 +11,7 @@ cmake_find_package [options] boost:zlib=False boost:bzip2=False +boost:without_test=True [imports] bin, *.dll -> ./bin \ No newline at end of file diff --git a/include/RDI.h b/include/RDI.h index 1be9cae..053a509 100644 --- a/include/RDI.h +++ b/include/RDI.h @@ -7,6 +7,9 @@ * */ +#include "RDI_Datatypes.h" + + #ifndef RDI_H_ #define RDI_H_ diff --git a/include/RDI_Datatypes.h b/include/RDI_Datatypes.h new file mode 100644 index 0000000..7c040a6 --- /dev/null +++ b/include/RDI_Datatypes.h @@ -0,0 +1,28 @@ +/** + * @file RDI_Datatypes.h + * @date 20/09/2022 + * @author JackCarterSmith + * @copyright GPL-v3.0 + * @brief Rogue Data Interface library variables type definitions. + * + */ + +#ifndef RDI_DATATYPES_H_ +#define RDI_DATATYPES_H_ + +namespace RDI { + +typedef enum e_rdi_result { + RDI_OK, + + RDI_ERROR_GENERIC, + RDI_ERROR_PROCESS, + RDI_ERROR_MEMORY, + RDI_ERROR_FILESYSTEM, + + RDI_ERROR_UNEXPECTED +} RDI_RESULT; + +} + +#endif /* RDI_DATATYPES_H_ */ diff --git a/src/RDat.cpp b/src/RDat.cpp index a52b79d..a695c3c 100644 --- a/src/RDat.cpp +++ b/src/RDat.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include "data_struct.h" #include "RDat.h" @@ -28,15 +29,42 @@ using boost::filesystem::file_size; namespace RDI { - struct dataSection { - std::string name = ""; - unsigned int offset = 0; - }; - RDat::RDat( string fPath ) { - unsigned int i, size; - path fp(fPath); - std::fstream rdf; + RDI_RESULT errCode = RDI_OK; + + this->workingDir = fPath; + + // Process header file and dump data file in memory. + errCode = DumpLegacyFiles(); + if (errCode == RDI_ERROR_FILESYSTEM) { + std::cout << "Data files (DATA.DAT/DATA.HDR) not found! Interrupt." << std::endl; + return; + } else if (errCode == RDI_ERROR_MEMORY) { + std::cout << "Memory allocation or file access failed! Interrupt." << std::endl; + return; + } + + // Process data file tree and filter file subtype into list. + ProcessFilesTree(); + } + + RDat::~RDat() { + free(rDatPtr); + delete pDatSection; + } + + string RDat::getDataSectionName( unsigned char id ) { return pDatSection->at(id).name; } + unsigned int RDat::getDataSectionOffset( unsigned char id ) { return pDatSection->at(id).offset; } + + /** + * Helper function to search for legacy DATA.DAT/HDR and dump it in memory. + * + * @return Error status. + */ + RDI_RESULT RDat::DumpLegacyFiles() { + unsigned int i; + path fp(workingDir); + MEMFILE pTmpFile; // If input as a directory, assume it's contain DATA.DAT/HDR. if (boost::filesystem::is_directory(fp)) fp.append("DATA.HDR"); @@ -45,47 +73,61 @@ namespace RDI { if (fp.extension() == ".DAT") fp.replace_extension(".HDR"); // Open and parse data header file. - rdf.open(fp.string(), ios::in | ios::binary); + //if (boost::filesystem::exists(fp)) return RDI_ERROR_FILESYSTEM; + pTmpFile = MallocFile(fp); + if (!pTmpFile) return RDI_ERROR_MEMORY; - if (rdf.is_open()) { - size = file_size(fp); - rDatPtr = (MEMFILE)malloc(size); - rdf.read(rDatPtr, size); - rdf.close(); - - cDatSection = size / sizeof(T_HDR_ENTRY); - pDatSection = new vector(cDatSection); - for ( i = 0; i < pDatSection->size(); i++ ) { - pDatSection->at(i).name.append(((T_HDR_ENTRY*)(rDatPtr + i * sizeof(T_HDR_ENTRY)))->section_name); - pDatSection->at(i).offset = ((T_HDR_ENTRY*)(rDatPtr + i * sizeof(T_HDR_ENTRY)))->section_offset; - } - - fp.replace_extension(".DAT"); - - // Open and parse file tree in datas file. - rdf.open(fp.string(), ios::in | ios::binary); - - if (rdf.is_open()) { - size = file_size(fp); - rDatPtr = (MEMFILE)realloc(rDatPtr, size); - rdf.read(rDatPtr, size); - rdf.close(); - - //TODO: Process files lists. - } else { - std::cout << "Datas file (DAT) not found!" << std::endl << "Data files should have same exact name." << std::endl; - } - } else { - std::cout << "Header file (HDR) not found!" << std::endl << "Data files should have same exact name." << std::endl; + cDatSection = file_size(fp) / sizeof(T_HDR_ENTRY); + pDatSection = new vector(cDatSection); + for ( i = 0; i < pDatSection->size(); i++ ) { + // Store header infos into structure to make easier access later. + pDatSection->at(i).name.append(((T_HDR_ENTRY*)(pTmpFile + i * sizeof(T_HDR_ENTRY)))->section_name); + pDatSection->at(i).offset = ((T_HDR_ENTRY*)(pTmpFile + i * sizeof(T_HDR_ENTRY)))->section_offset; } + + free(pTmpFile); + + // Dump data file and store it's pointer into class member. + fp.replace_extension(".DAT"); + //if (boost::filesystem::exists(fp)) return RDI_ERROR_FILESYSTEM; + rDatPtr = MallocFile(fp); + if (!rDatPtr) return RDI_ERROR_MEMORY; + + return RDI_OK; } - RDat::~RDat() { - free(rDatPtr); - delete pDatSection; + /** + * Dump file into to newly allocated memory. + * @param filePath File path. + * + * @return Start memory pointer of the file. + */ + MEMFILE RDat::MallocFile( path filePath ) { + const unsigned int size = file_size(filePath); + std::fstream rdf; + MEMFILE fPtr = NULL; + + rdf.open(filePath.string(), ios::in | ios::binary); + if (rdf.is_open()) { + fPtr = (MEMFILE)malloc(size); + rdf.read(fPtr, size); + rdf.close(); + } + + return fPtr; } - std::string RDat::getDataSectionName( unsigned char id ) { return pDatSection->at(id).name; } - unsigned int RDat::getDataSectionOffset( unsigned char id ) { return pDatSection->at(id).offset; } + /** + * Helper function to process files content for further use in this library. + * + * @return Error status. + */ + RDI_RESULT RDat::ProcessFilesTree() { + + + return RDI_OK; + } + + } /* namespace RDI */ diff --git a/src/RDat.h b/src/RDat.h index 68dadcc..6975de6 100644 --- a/src/RDat.h +++ b/src/RDat.h @@ -7,7 +7,9 @@ * */ +#include #include +#include "RDI_Datatypes.h" #ifndef RDAT_H_ @@ -17,14 +19,12 @@ namespace RDI { typedef char* MEMFILE; + struct dataSection { + std::string name = ""; + unsigned int offset = 0; + }; + class RDat { - private: - std::string workingDir = "."; - - unsigned char cDatSection = 0; - std::vector *pDatSection = nullptr; - MEMFILE rDatPtr = nullptr; - public: RDat( std::string fPath ); ~RDat(); @@ -40,6 +40,18 @@ namespace RDI { std::string getDataSectionName( unsigned char id ); unsigned int getDataSectionOffset( unsigned char id ); + private: + std::string workingDir = "."; + + unsigned char cDatSection = 0; + std::vector *pDatSection = nullptr; + MEMFILE rDatPtr = nullptr; + + /* File processing methods */ + RDI_RESULT DumpLegacyFiles(); + MEMFILE MallocFile( boost::filesystem::path filePath ); + RDI_RESULT ProcessFilesTree(); + }; } /* namespace RDI */