From 8ffe841ac932c7df1466a7757f92f674fd4c95dc Mon Sep 17 00:00:00 2001 From: kervala Date: Sat, 20 Feb 2016 18:56:21 +0100 Subject: [PATCH] Adapt CSTLoader to use nlfopen and some simplifications, issue #261 --- .../common/src/game_share/cst_loader.cpp | 137 +++++++++--------- code/ryzom/common/src/game_share/cst_loader.h | 80 ++++------ 2 files changed, 93 insertions(+), 124 deletions(-) diff --git a/code/ryzom/common/src/game_share/cst_loader.cpp b/code/ryzom/common/src/game_share/cst_loader.cpp index b957d7f34..87e587ba1 100644 --- a/code/ryzom/common/src/game_share/cst_loader.cpp +++ b/code/ryzom/common/src/game_share/cst_loader.cpp @@ -26,11 +26,11 @@ using namespace NLMISC; /****************************************************************\ buildTableFormat() \****************************************************************/ -void CSTLoader::buildTableFormat( string fileName, list >& tableFormat ) +void CSTLoader::buildTableFormat(const string &fileName, list >& tableFormat ) { - _File = new ifstream(fileName.c_str(), ios::in); + _File = nlfopen(fileName, "rb"); - if( !_File->is_open() ) + if (!_File) { nlerror("can't open file : %s\n", fileName.c_str()); } @@ -40,7 +40,7 @@ void CSTLoader::buildTableFormat( string fileName, list > //================ char readBuffer[4096]; char * token; - _File->getline(readBuffer, 4096); + if (fgets(readBuffer, 4096, _File) == NULL) return; // extract first token //==================== @@ -102,13 +102,17 @@ void CSTLoader::readData( list >& data ) char * token; bool firstToken = true; - while( !_File->eof() ) + while( !feof(_File) ) { // list of current object values list lineData; // read a line - _File->getline(readBuffer, 4096); + if (fgets(readBuffer, 4096, _File) == NULL) + { + // EOF + break; + } // check all tokens of the current line do @@ -146,74 +150,44 @@ void CSTLoader::readData( list >& data ) /****************************************************************\ generateDerivedClasses() \****************************************************************/ -void CSTLoader::generateDerivedClasses(ofstream &file, std::list< std::pair > &format, std::list< std::list< std::string> > &data ) +void CSTLoader::generateDerivedClasses(const std::list< std::pair > &format, const std::list< std::list< std::string> > &data ) { + std::string content; - - std::list< std::list< std::string> >::iterator it_dl = data.begin(); + std::list< std::list< std::string> >::const_iterator it_dl = data.begin(); while ( it_dl != data.end() ) { - std::list< std::pair >::iterator it_def = format.begin(); - std::list::iterator it_val = (*it_dl).begin(); + std::list< std::pair >::const_iterator it_def = format.begin(); + std::list::const_iterator it_val = (*it_dl).begin(); // sint32 size = data.size(); // sint32 size2 = (*it_dl).size(); -// std::string name = convertName( *it_val ); +// std::string name = convertName( *it_val ); -// std::string test = *it_val; +// std::string test = *it_val; if ( (*it_dl).size() ) { - file << "From Item : Define " << convertName( *it_val ) << endl; + content += "From Item : Define " + convertName( *it_val ) + "\n"; it_val++; it_def++; - file << "{" << endl; - file << "\tComponent:" << endl; + content += "{\n"; + content += "\tComponent:\n"; } - std::list< std::pair >::iterator it_obj = format.begin(); + std::list< std::pair >::const_iterator it_obj = format.begin(); it_obj++; while ( it_obj != format.end() ) { - file << "\t\t"; - switch ( (*it_obj).second ) - { - case UINT8: - file << "uint8"; - break; - case SINT8: - file << "sint8"; - break; - case UINT16: - file << "uint16"; - break; - case SINT16: - file << "sint16"; - break; - case UINT32: - file << "uint32"; - break; - case SINT32: - file << "sint32"; - break; - case FLOAT: - file << "Float"; - break; - case STRING: - file << "String"; - break; - case BOOL: - file << "Bool"; - break; - } - file << "<'" << (*it_obj).first << "', Static>;" << endl; + content += "\t\t" + convertFromType((*it_obj).second); + content += "<'" + (*it_obj).first + "', Static>;\n"; it_obj++; } - file << "\tEnd" << endl << endl; + content += "\tEnd\n"; - file << "\t StaticInit()" << endl; + content += "\t StaticInit()\n"; while ( it_def != format.end() && it_val != (*it_dl).end() ) { @@ -222,50 +196,52 @@ void CSTLoader::generateDerivedClasses(ofstream &file, std::list< std::pair& fileFormat) +void CSTLoader::init(const string &fileName, const map& fileFormat) { _FileFormat = fileFormat; _FileName = fileName; - _File = new ifstream(fileName.c_str(), ios::in); + _File = nlfopen(fileName, "rb"); - if( !_File->is_open() ) + if (!_File) { nlerror("can't open file : %s\n", fileName.c_str()); } @@ -291,7 +267,8 @@ void CSTLoader::init(string fileName, const map& fileFormat) // read first line char readBuffer[4096]; char * token; - _File->getline(readBuffer, 4096); + + if (fgets(readBuffer, 4096, _File) == NULL) return; // extract first token token = strtok(readBuffer, _Seps.c_str()); @@ -316,7 +293,7 @@ void CSTLoader::init(string fileName, const map& fileFormat) \****************************************************************/ bool CSTLoader::readLine() { - if( _File->eof() ) + if (feof(_File)) { return false; } @@ -333,7 +310,7 @@ bool CSTLoader::readLine() _Tokens.clear(); // read a line - _File->getline(readBuffer, 4096); + if (fgets(readBuffer, 4096, _File) == NULL) return false; // if the line is empty we consider we are at end of file if( strlen(readBuffer) == 0) @@ -429,3 +406,21 @@ bool CSTLoader::readLine() return true; } +std::string CSTLoader::convertFromType(TDataType type) +{ + switch (type) + { + case UINT8: return "uint8"; + case SINT8: return "sint8"; + case UINT16: return "uint16"; + case SINT16: return "sint16"; + case UINT32: return "uint32"; + case SINT32: return "sint32"; + case FLOAT: return "Float"; + case STRING: return "String"; + case BOOL: return "Bool"; + default: break; + } + + return ""; +} diff --git a/code/ryzom/common/src/game_share/cst_loader.h b/code/ryzom/common/src/game_share/cst_loader.h index b80cad107..51fa6e9aa 100644 --- a/code/ryzom/common/src/game_share/cst_loader.h +++ b/code/ryzom/common/src/game_share/cst_loader.h @@ -25,7 +25,6 @@ #include #include #include -#include /** @@ -55,10 +54,10 @@ public: private: /// cst file - std::ifstream * _File; + FILE *_File; /// name of the cst file (used for debug information) - std::string _FileName; + std::string _FileName; /// separators std::string _Seps; @@ -101,7 +100,7 @@ public: * \param fileName the name of the file * \param fileFormat the name of the columns and their data type */ - void buildTableFormat( std::string fileName, std::list >& tableFormat ); + void buildTableFormat( const std::string &fileName, std::list >& tableFormat ); /** @@ -117,7 +116,7 @@ public: * \param fileName the name of the file * \param fileFormat the name of the columns and their data type */ - void init( std::string fileName, const std::map& fileFormat); + void init( const std::string &fileName, const std::map& fileFormat); /** @@ -213,77 +212,50 @@ public: /// close file void close() { - _File->close(); - delete _File; + fclose(_File); + _File = NULL; } - void Load(std::string fileName,std::ofstream &script_file) + void Load(const std::string &fileName) { // Generates the base class std::list< std::pair > format; buildTableFormat( fileName, format ); - generateBaseClass( script_file, format); + generateBaseClass( format); // Generates a derived class for each type of object std::list< std::list > data; readData( data ); - generateDerivedClasses( script_file, format, data ); + generateDerivedClasses( format, data ); } - void generateBaseClass(std::ofstream &file, std::list< std::pair > &/* format */) + void generateBaseClass(const std::list< std::pair > &/* format */) { - file << "From Agent : Define Item" << std::endl; - file << "{" << std::endl; -/* file << "\tComponent:" << std::endl; + std::string content; + content += "From Agent : Define Item\n"; + content += "{\n"; +/* content += "\tComponent:\n"; std::list< std::pair >::iterator it_obj = format.begin(); it_obj++; while ( it_obj != format.end() ) { - file << "\t\t"; - switch ( (*it_obj).second ) - { - case UINT8: - file << "uint8"; - break; - case SINT8: - file << "sint8"; - break; - case UINT16: - file << "uint16"; - break; - case SINT16: - file << "sint16"; - break; - case UINT32: - file << "uint32"; - break; - case SINT32: - file << "sint32"; - break; - case FLOAT: - file << "Float"; - break; - case STRING: - file << "String"; - break; - case BOOL: - file << "Bool"; - break; - } - file << "<'" << (*it_obj).first << "', Static>;" << std::endl; + content += "\t\t" + convertFromType((*it_obj).second); + content += "<'" + (*it_obj).first + "', Static>;\n"; it_obj++; } - file << "\tEnd" << std::endl;*/ - file << "}" << std::endl; - file << std::endl; + content += "\tEnd\n"; */ + content += "}\n"; + content += "\n"; + + fwrite(content.c_str(), 1, content.length(), _File); } - void generateDerivedClasses(std::ofstream &, std::list< std::pair > &, std::list< std::list< std::string> > &); + void generateDerivedClasses(const std::list< std::pair > &, const std::list< std::list< std::string> > &); - TDataType convertType(std::string type_str) + TDataType convertType(const std::string &type_str) { if ( type_str == "UINT8") return UINT8; @@ -306,11 +278,13 @@ public: return (TDataType)0; } - std::string convertName(std::string &name) + std::string convertFromType(TDataType type); + + std::string convertName(const std::string &name) const { int i = 0; char buffer[1024]; - std::string::iterator it_c = name.begin(); + std::string::const_iterator it_c = name.begin(); while ( it_c != name.end() ) { char c = *it_c;