Simple header parser
This commit is contained in:
parent
0bd2c64f73
commit
bb4cafd680
@ -36,61 +36,34 @@
|
||||
# define RDI_EXTERN
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Library's functions declaration
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
namespace RDI
|
||||
{
|
||||
|
||||
class RDI_EXTERN RDI {
|
||||
private:
|
||||
std::string libVersion;
|
||||
std::string workingDir = ".";
|
||||
|
||||
public:
|
||||
RDI();
|
||||
virtual ~RDI();
|
||||
|
||||
/**
|
||||
* @brief Get the current library version.
|
||||
* @return String of the version.
|
||||
*/
|
||||
inline std::string getLibVersion() {
|
||||
return libVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get the current working directory of the library instance.
|
||||
* @return Path string of the current working directory.
|
||||
*/
|
||||
std::string getWorkingDirectory() {
|
||||
return workingDir;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set the current working directory of the library instance.
|
||||
* @param[in] newPath Path string without final slash.
|
||||
*/
|
||||
void setWorkingDirectory( std::string newPath ) {
|
||||
workingDir = newPath;
|
||||
}
|
||||
|
||||
};
|
||||
namespace RDI {
|
||||
|
||||
/**
|
||||
* @brief Search for Rogue data file, try to open it and map it in memory.
|
||||
* @return Error status, return RDI_SUCCESS in nominal case.
|
||||
* @brief Get the current library version.
|
||||
* @return String of the version.
|
||||
*/
|
||||
RDI_EXTERN unsigned short OpenRogueDat( void );
|
||||
RDI_EXTERN std::string getLibVersion( void );
|
||||
|
||||
/**
|
||||
* @brief Try to open Rogue data file and map it in memory.
|
||||
* @brief Create a new Rogue Data file instance.
|
||||
* @details Try to open DATA.DAT file at location specified by roguePath.
|
||||
* If file can be opened, it's mapped in memory and process files list.
|
||||
*
|
||||
* @param[in] filePath Path to Rogue data file.
|
||||
* @param[in] roguePath Path to DATA.DAT and DATA.HDR location.
|
||||
*
|
||||
* @return Error status, return RDI_SUCCESS in nominal case.
|
||||
* @return Handler of RogueData file, should be used with other function of this lib.
|
||||
*/
|
||||
RDI_EXTERN unsigned short OpenRogueDat( const char* filePath );
|
||||
RDI_EXTERN void CreateRDatHandler( std::string roguePath );
|
||||
|
||||
RDI_EXTERN unsigned char getSectionCount( void );
|
||||
RDI_EXTERN std::string getSectionName( unsigned char id );
|
||||
RDI_EXTERN unsigned int getSectionOffset( unsigned char id );
|
||||
|
||||
/**
|
||||
* @brief Clean up global resources.
|
||||
*/
|
||||
RDI_EXTERN void DestroyRDatHandler( void );
|
||||
|
||||
}
|
||||
|
||||
|
50
src/RDI.cpp
50
src/RDI.cpp
@ -18,35 +18,39 @@
|
||||
#include "RDat.h"
|
||||
#include "RDI.h"
|
||||
|
||||
using namespace std;
|
||||
using RDI::RDat;
|
||||
|
||||
|
||||
namespace RDI {
|
||||
|
||||
RDI::RDI() {
|
||||
this->libVersion = PRG_VERSION;
|
||||
}
|
||||
|
||||
RDI::~RDI() {}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Internal variable
|
||||
*/
|
||||
static RDI::RDat *hRDat = nullptr;
|
||||
|
||||
/*
|
||||
* Libs interface
|
||||
*/
|
||||
|
||||
unsigned short OpenRogueDat( void ) {
|
||||
return 0;
|
||||
std::string RDI::getLibVersion() { return PRG_VERSION; }
|
||||
|
||||
void RDI::CreateRDatHandler( std::string roguePath ){
|
||||
if (hRDat == nullptr) hRDat = new RDI::RDat(roguePath);
|
||||
|
||||
//return hRDat;
|
||||
}
|
||||
|
||||
unsigned short OpenRogueDat( const char* filePath ){
|
||||
auto rogueData = new RDat(filePath);
|
||||
|
||||
|
||||
delete rogueData;
|
||||
return 0;
|
||||
unsigned char RDI::getSectionCount() {
|
||||
if (hRDat == nullptr) return 0;
|
||||
else return hRDat->getDataSectionCount();
|
||||
}
|
||||
|
||||
std::string RDI::getSectionName( unsigned char id ) {
|
||||
if (hRDat == nullptr) return "";
|
||||
else return hRDat->getDataSectionName(id);
|
||||
}
|
||||
|
||||
unsigned int RDI::getSectionOffset( unsigned char id ) {
|
||||
if (hRDat == nullptr) return 0;
|
||||
else return hRDat->getDataSectionOffset(id);
|
||||
}
|
||||
|
||||
void RDI::DestroyRDatHandler(){
|
||||
if (hRDat) delete hRDat;
|
||||
}
|
||||
|
85
src/RDat.cpp
85
src/RDat.cpp
@ -10,6 +10,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <RSPModel.h>
|
||||
#include <RSPTerrain.h>
|
||||
@ -17,40 +18,74 @@
|
||||
#include "data_struct.h"
|
||||
#include "RDat.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
using std::string;
|
||||
using std::ios;
|
||||
using std::vector;
|
||||
using boost::filesystem::path;
|
||||
using boost::filesystem::file_size;
|
||||
|
||||
|
||||
namespace RDI {
|
||||
|
||||
RDat::RDat( string fPath ) {
|
||||
string datFile, hdrFile;
|
||||
fstream rdf;
|
||||
streampos size;
|
||||
struct dataSection {
|
||||
std::string name = "";
|
||||
unsigned int offset = 0;
|
||||
};
|
||||
|
||||
datFile = fPath + "/DATA.DAT";
|
||||
hdrFile = fPath + "/DATA.HDR";
|
||||
RDat::RDat( string fPath ) {
|
||||
unsigned int i, size;
|
||||
path fp(fPath);
|
||||
std::fstream rdf;
|
||||
|
||||
rdf.open(datFile, ios::in | ios::binary);
|
||||
if (rdf.is_open()) {
|
||||
size = rdf.tellg();
|
||||
rDatPtr = (MEMFILE)malloc(size);
|
||||
rdf.seekg(0, ios::beg);
|
||||
rdf.read(rDatPtr, size);
|
||||
rdf.close();
|
||||
// If input as a directory, assume it's contain DATA.DAT/HDR.
|
||||
if (boost::filesystem::is_directory(fp)) fp.append("DATA.HDR");
|
||||
|
||||
// If file specified in input is the "datas" file, process the header (HDR) before.
|
||||
if (fp.extension() == ".DAT") fp.replace_extension(".HDR");
|
||||
|
||||
// Open and parse data header file.
|
||||
rdf.open(fp.string(), ios::in | ios::binary);
|
||||
|
||||
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<struct dataSection>(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;
|
||||
}
|
||||
}
|
||||
|
||||
rdf.open(hdrFile, ios::in | ios::binary);
|
||||
if (rdf.is_open()) {
|
||||
size = rdf.tellg();
|
||||
rDatPtr = new char[size];
|
||||
rdf.seekg(0, ios::beg);
|
||||
rdf.read(rDatPtr, size);
|
||||
rdf.close();
|
||||
RDat::~RDat() {
|
||||
free(rDatPtr);
|
||||
delete pDatSection;
|
||||
}
|
||||
}
|
||||
|
||||
RDat::~RDat() {
|
||||
delete rDatPtr;
|
||||
}
|
||||
std::string RDat::getDataSectionName( unsigned char id ) { return pDatSection->at(id).name; }
|
||||
unsigned int RDat::getDataSectionOffset( unsigned char id ) { return pDatSection->at(id).offset; }
|
||||
|
||||
} /* namespace RDI */
|
||||
|
18
src/RDat.h
18
src/RDat.h
@ -7,27 +7,39 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include <vector>
|
||||
|
||||
|
||||
#ifndef RDAT_H_
|
||||
#define RDAT_H_
|
||||
|
||||
|
||||
namespace RDI {
|
||||
|
||||
typedef char* MEMFILE;
|
||||
|
||||
|
||||
class RDat {
|
||||
private:
|
||||
std::string workingDir = ".";
|
||||
|
||||
unsigned char cDatSection = 0;
|
||||
std::vector<struct dataSection> *pDatSection;
|
||||
MEMFILE rDatPtr = nullptr;
|
||||
|
||||
public:
|
||||
RDat( std::string fPath );
|
||||
virtual ~RDat();
|
||||
~RDat();
|
||||
|
||||
MEMFILE getRDat() {
|
||||
return rDatPtr;
|
||||
}
|
||||
|
||||
std::string getWorkingDirectory() { return workingDir; }
|
||||
void setWorkingDirectory( std::string newPath ) { workingDir = newPath; }
|
||||
|
||||
unsigned char getDataSectionCount() { return cDatSection; }
|
||||
std::string getDataSectionName( unsigned char id );
|
||||
unsigned int getDataSectionOffset( unsigned char id );
|
||||
|
||||
};
|
||||
|
||||
} /* namespace RDI */
|
||||
|
@ -11,16 +11,24 @@
|
||||
#include <RDI.h>
|
||||
|
||||
|
||||
using namespace std;
|
||||
using namespace RDI;
|
||||
|
||||
|
||||
int main( int argc, char *argv[] ) {
|
||||
RDI::RDI *hRDI = nullptr;
|
||||
unsigned int i;
|
||||
|
||||
hRDI = new RDI::RDI();
|
||||
printf("Using RDI lib v%s\n\n", RDI::getLibVersion().c_str());
|
||||
//cout << "Using RDI lib v" << RDI::getLibVersion() << endl << endl;
|
||||
|
||||
printf("Using RDI lib v%s\n", hRDI->getLibVersion().c_str());
|
||||
if ( argc > 1 )
|
||||
RDI::CreateRDatHandler(argv[1]);
|
||||
else
|
||||
RDI::CreateRDatHandler(".");
|
||||
|
||||
delete hRDI;
|
||||
printf("> Section found: %d\n", RDI::getSectionCount());
|
||||
for ( i = 0; i < RDI::getSectionCount(); i++ ) {
|
||||
printf(" -Section %d name: %s\n", i, RDI::getSectionName(i).c_str());
|
||||
printf(" -Section %d offset: 0x%X\n", i, RDI::getSectionOffset(i));
|
||||
}
|
||||
|
||||
RDI::DestroyRDatHandler();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user