Adapt CSTLoader to use nlfopen and some simplifications, issue #261

This commit is contained in:
kervala 2016-02-20 18:56:21 +01:00
parent 4fccafe9fd
commit 8ffe841ac9
2 changed files with 93 additions and 124 deletions

View file

@ -26,11 +26,11 @@ using namespace NLMISC;
/****************************************************************\ /****************************************************************\
buildTableFormat() buildTableFormat()
\****************************************************************/ \****************************************************************/
void CSTLoader::buildTableFormat( string fileName, list<pair<string,TDataType> >& tableFormat ) void CSTLoader::buildTableFormat(const string &fileName, list<pair<string,TDataType> >& 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()); nlerror("can't open file : %s\n", fileName.c_str());
} }
@ -40,7 +40,7 @@ void CSTLoader::buildTableFormat( string fileName, list<pair<string,TDataType> >
//================ //================
char readBuffer[4096]; char readBuffer[4096];
char * token; char * token;
_File->getline(readBuffer, 4096); if (fgets(readBuffer, 4096, _File) == NULL) return;
// extract first token // extract first token
//==================== //====================
@ -102,13 +102,17 @@ void CSTLoader::readData( list<list<string> >& data )
char * token; char * token;
bool firstToken = true; bool firstToken = true;
while( !_File->eof() ) while( !feof(_File) )
{ {
// list of current object values // list of current object values
list<string> lineData; list<string> lineData;
// read a line // read a line
_File->getline(readBuffer, 4096); if (fgets(readBuffer, 4096, _File) == NULL)
{
// EOF
break;
}
// check all tokens of the current line // check all tokens of the current line
do do
@ -146,16 +150,16 @@ void CSTLoader::readData( list<list<string> >& data )
/****************************************************************\ /****************************************************************\
generateDerivedClasses() generateDerivedClasses()
\****************************************************************/ \****************************************************************/
void CSTLoader::generateDerivedClasses(ofstream &file, std::list< std::pair<std::string, TDataType> > &format, std::list< std::list< std::string> > &data ) void CSTLoader::generateDerivedClasses(const std::list< std::pair<std::string, TDataType> > &format, const std::list< std::list< std::string> > &data )
{ {
std::string content;
std::list< std::list< std::string> >::const_iterator it_dl = data.begin();
std::list< std::list< std::string> >::iterator it_dl = data.begin();
while ( it_dl != data.end() ) while ( it_dl != data.end() )
{ {
std::list< std::pair<std::string, TDataType> >::iterator it_def = format.begin(); std::list< std::pair<std::string, TDataType> >::const_iterator it_def = format.begin();
std::list<std::string>::iterator it_val = (*it_dl).begin(); std::list<std::string>::const_iterator it_val = (*it_dl).begin();
// sint32 size = data.size(); // sint32 size = data.size();
// sint32 size2 = (*it_dl).size(); // sint32 size2 = (*it_dl).size();
@ -165,55 +169,25 @@ void CSTLoader::generateDerivedClasses(ofstream &file, std::list< std::pair<std:
if ( (*it_dl).size() ) if ( (*it_dl).size() )
{ {
file << "From Item : Define " << convertName( *it_val ) << endl; content += "From Item : Define " + convertName( *it_val ) + "\n";
it_val++; it_val++;
it_def++; it_def++;
file << "{" << endl; content += "{\n";
file << "\tComponent:" << endl; content += "\tComponent:\n";
} }
std::list< std::pair<std::string,TDataType> >::iterator it_obj = format.begin(); std::list< std::pair<std::string,TDataType> >::const_iterator it_obj = format.begin();
it_obj++; it_obj++;
while ( it_obj != format.end() ) while ( it_obj != format.end() )
{ {
file << "\t\t"; content += "\t\t" + convertFromType((*it_obj).second);
switch ( (*it_obj).second ) content += "<'" + (*it_obj).first + "', Static>;\n";
{
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;
it_obj++; 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() ) while ( it_def != format.end() && it_val != (*it_dl).end() )
{ {
@ -222,50 +196,52 @@ void CSTLoader::generateDerivedClasses(ofstream &file, std::list< std::pair<std:
std::string test2 = (*it_def).first; std::string test2 = (*it_def).first;
#endif #endif
file << "\t\t" << (*it_def).first << " = "; content += "\t\t" + (*it_def).first + " = ";
switch ( (*it_def).second ) switch ( (*it_def).second )
{ {
case UINT8: case UINT8:
file << "new uint8(" << convertName(*it_val); content += "new uint8(" + convertName(*it_val);
break; break;
case SINT8: case SINT8:
file << "new sint8(" << convertName(*it_val); content += "new sint8(" + convertName(*it_val);
break; break;
case UINT16: case UINT16:
file << "new uint16(" << convertName(*it_val); content += "new uint16(" + convertName(*it_val);
break; break;
case SINT16: case SINT16:
file << "new sint16(" << convertName(*it_val); content += "new sint16(" + convertName(*it_val);
break; break;
case UINT32: case UINT32:
file << "new uint32(" << convertName(*it_val); content += "new uint32(" + convertName(*it_val);
break; break;
case SINT32: case SINT32:
file << "new sint32(" << convertName(*it_val); content += "new sint32(" + convertName(*it_val);
break; break;
case FLOAT: case FLOAT:
file << "new Float(" <<convertName(*it_val); content += "new Float(" + convertName(*it_val);
break; break;
case STRING: case STRING:
file << "'" << (*it_val) << "'"; content += "'" + (*it_val) + "'";
break; break;
case BOOL: case BOOL:
file << "new Bool(" << (*it_val); content += "new Bool(" + (*it_val);
break; break;
default: default:
file << "ERROR: unsuported type " << (*it_def).second << std::endl; content += "ERROR: unsuported type " + toString((uint)(*it_def).second) + "\n";
break; break;
} }
file << ");" << endl; content += ");\n";
it_def++; it_def++;
it_val++; it_val++;
} }
file << "\tEnd" << endl; content += "\tEnd\n";
file << "}" << endl; content += "}\n";
it_dl++; it_dl++;
} }
fwrite(content.c_str(), 1, content.length(), _File);
} }
@ -274,15 +250,15 @@ void CSTLoader::generateDerivedClasses(ofstream &file, std::list< std::pair<std:
/****************************************************************\ /****************************************************************\
init() init()
\****************************************************************/ \****************************************************************/
void CSTLoader::init(string fileName, const map<string,TDataType>& fileFormat) void CSTLoader::init(const string &fileName, const map<string,TDataType>& fileFormat)
{ {
_FileFormat = fileFormat; _FileFormat = fileFormat;
_FileName = fileName; _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()); nlerror("can't open file : %s\n", fileName.c_str());
} }
@ -291,7 +267,8 @@ void CSTLoader::init(string fileName, const map<string,TDataType>& fileFormat)
// read first line // read first line
char readBuffer[4096]; char readBuffer[4096];
char * token; char * token;
_File->getline(readBuffer, 4096);
if (fgets(readBuffer, 4096, _File) == NULL) return;
// extract first token // extract first token
token = strtok(readBuffer, _Seps.c_str()); token = strtok(readBuffer, _Seps.c_str());
@ -316,7 +293,7 @@ void CSTLoader::init(string fileName, const map<string,TDataType>& fileFormat)
\****************************************************************/ \****************************************************************/
bool CSTLoader::readLine() bool CSTLoader::readLine()
{ {
if( _File->eof() ) if (feof(_File))
{ {
return false; return false;
} }
@ -333,7 +310,7 @@ bool CSTLoader::readLine()
_Tokens.clear(); _Tokens.clear();
// read a line // 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 the line is empty we consider we are at end of file
if( strlen(readBuffer) == 0) if( strlen(readBuffer) == 0)
@ -429,3 +406,21 @@ bool CSTLoader::readLine()
return true; 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 "";
}

View file

@ -25,7 +25,6 @@
#include <map> #include <map>
#include <vector> #include <vector>
#include <string> #include <string>
#include <fstream>
/** /**
@ -55,7 +54,7 @@ public:
private: private:
/// cst file /// cst file
std::ifstream * _File; FILE *_File;
/// name of the cst file (used for debug information) /// name of the cst file (used for debug information)
std::string _FileName; std::string _FileName;
@ -101,7 +100,7 @@ public:
* \param fileName the name of the file * \param fileName the name of the file
* \param fileFormat the name of the columns and their data type * \param fileFormat the name of the columns and their data type
*/ */
void buildTableFormat( std::string fileName, std::list<std::pair< std::string,TDataType> >& tableFormat ); void buildTableFormat( const std::string &fileName, std::list<std::pair< std::string,TDataType> >& tableFormat );
/** /**
@ -117,7 +116,7 @@ public:
* \param fileName the name of the file * \param fileName the name of the file
* \param fileFormat the name of the columns and their data type * \param fileFormat the name of the columns and their data type
*/ */
void init( std::string fileName, const std::map<std::string,TDataType>& fileFormat); void init( const std::string &fileName, const std::map<std::string,TDataType>& fileFormat);
/** /**
@ -213,77 +212,50 @@ public:
/// close file /// close file
void close() void close()
{ {
_File->close(); fclose(_File);
delete _File; _File = NULL;
} }
void Load(std::string fileName,std::ofstream &script_file) void Load(const std::string &fileName)
{ {
// Generates the base class // Generates the base class
std::list< std::pair<std::string,TDataType> > format; std::list< std::pair<std::string,TDataType> > format;
buildTableFormat( fileName, format ); buildTableFormat( fileName, format );
generateBaseClass( script_file, format); generateBaseClass( format);
// Generates a derived class for each type of object // Generates a derived class for each type of object
std::list< std::list<std::string> > data; std::list< std::list<std::string> > data;
readData( data ); readData( data );
generateDerivedClasses( script_file, format, data ); generateDerivedClasses( format, data );
} }
void generateBaseClass(std::ofstream &file, std::list< std::pair<std::string,TDataType> > &/* format */) void generateBaseClass(const std::list< std::pair<std::string,TDataType> > &/* format */)
{ {
file << "From Agent : Define Item" << std::endl; std::string content;
file << "{" << std::endl; content += "From Agent : Define Item\n";
/* file << "\tComponent:" << std::endl; content += "{\n";
/* content += "\tComponent:\n";
std::list< std::pair<std::string,TDataType> >::iterator it_obj = format.begin(); std::list< std::pair<std::string,TDataType> >::iterator it_obj = format.begin();
it_obj++; it_obj++;
while ( it_obj != format.end() ) while ( it_obj != format.end() )
{ {
file << "\t\t"; content += "\t\t" + convertFromType((*it_obj).second);
switch ( (*it_obj).second ) content += "<'" + (*it_obj).first + "', Static>;\n";
{
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;
it_obj++; it_obj++;
} }
file << "\tEnd" << std::endl;*/ content += "\tEnd\n"; */
file << "}" << std::endl; content += "}\n";
file << std::endl; content += "\n";
fwrite(content.c_str(), 1, content.length(), _File);
} }
void generateDerivedClasses(std::ofstream &, std::list< std::pair<std::string, TDataType> > &, std::list< std::list< std::string> > &); void generateDerivedClasses(const std::list< std::pair<std::string, TDataType> > &, const std::list< std::list< std::string> > &);
TDataType convertType(std::string type_str) TDataType convertType(const std::string &type_str)
{ {
if ( type_str == "UINT8") if ( type_str == "UINT8")
return UINT8; return UINT8;
@ -306,11 +278,13 @@ public:
return (TDataType)0; 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; int i = 0;
char buffer[1024]; char buffer[1024];
std::string::iterator it_c = name.begin(); std::string::const_iterator it_c = name.begin();
while ( it_c != name.end() ) while ( it_c != name.end() )
{ {
char c = *it_c; char c = *it_c;