Adapt CSTLoader to use nlfopen and some simplifications, issue #261
This commit is contained in:
parent
64af254ece
commit
35570eb0a3
2 changed files with 93 additions and 124 deletions
|
@ -26,11 +26,11 @@ using namespace NLMISC;
|
|||
/****************************************************************\
|
||||
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());
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ void CSTLoader::buildTableFormat( string fileName, list<pair<string,TDataType> >
|
|||
//================
|
||||
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<list<string> >& data )
|
|||
char * token;
|
||||
bool firstToken = true;
|
||||
|
||||
while( !_File->eof() )
|
||||
while( !feof(_File) )
|
||||
{
|
||||
// list of current object values
|
||||
list<string> 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,16 +150,16 @@ void CSTLoader::readData( list<list<string> >& data )
|
|||
/****************************************************************\
|
||||
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> >::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<std::string, TDataType> >::iterator it_def = format.begin();
|
||||
std::list<std::string>::iterator it_val = (*it_dl).begin();
|
||||
std::list< std::pair<std::string, TDataType> >::const_iterator it_def = format.begin();
|
||||
std::list<std::string>::const_iterator it_val = (*it_dl).begin();
|
||||
// sint32 size = data.size();
|
||||
// sint32 size2 = (*it_dl).size();
|
||||
|
||||
|
@ -165,55 +169,25 @@ void CSTLoader::generateDerivedClasses(ofstream &file, std::list< std::pair<std:
|
|||
|
||||
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<std::string,TDataType> >::iterator it_obj = format.begin();
|
||||
std::list< std::pair<std::string,TDataType> >::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<std:
|
|||
std::string test2 = (*it_def).first;
|
||||
#endif
|
||||
|
||||
file << "\t\t" << (*it_def).first << " = ";
|
||||
content += "\t\t" + (*it_def).first + " = ";
|
||||
switch ( (*it_def).second )
|
||||
{
|
||||
case UINT8:
|
||||
file << "new uint8(" << convertName(*it_val);
|
||||
content += "new uint8(" + convertName(*it_val);
|
||||
break;
|
||||
case SINT8:
|
||||
file << "new sint8(" << convertName(*it_val);
|
||||
content += "new sint8(" + convertName(*it_val);
|
||||
break;
|
||||
case UINT16:
|
||||
file << "new uint16(" << convertName(*it_val);
|
||||
content += "new uint16(" + convertName(*it_val);
|
||||
break;
|
||||
case SINT16:
|
||||
file << "new sint16(" << convertName(*it_val);
|
||||
content += "new sint16(" + convertName(*it_val);
|
||||
break;
|
||||
case UINT32:
|
||||
file << "new uint32(" << convertName(*it_val);
|
||||
content += "new uint32(" + convertName(*it_val);
|
||||
break;
|
||||
case SINT32:
|
||||
file << "new sint32(" << convertName(*it_val);
|
||||
content += "new sint32(" + convertName(*it_val);
|
||||
break;
|
||||
case FLOAT:
|
||||
file << "new Float(" <<convertName(*it_val);
|
||||
content += "new Float(" + convertName(*it_val);
|
||||
break;
|
||||
case STRING:
|
||||
file << "'" << (*it_val) << "'";
|
||||
content += "'" + (*it_val) + "'";
|
||||
break;
|
||||
case BOOL:
|
||||
file << "new Bool(" << (*it_val);
|
||||
content += "new Bool(" + (*it_val);
|
||||
break;
|
||||
default:
|
||||
file << "ERROR: unsuported type " << (*it_def).second << std::endl;
|
||||
content += "ERROR: unsuported type " + toString((uint)(*it_def).second) + "\n";
|
||||
break;
|
||||
}
|
||||
file << ");" << endl;
|
||||
content += ");\n";
|
||||
|
||||
it_def++;
|
||||
it_val++;
|
||||
}
|
||||
file << "\tEnd" << endl;
|
||||
file << "}" << endl;
|
||||
content += "\tEnd\n";
|
||||
content += "}\n";
|
||||
|
||||
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()
|
||||
\****************************************************************/
|
||||
void CSTLoader::init(string fileName, const map<string,TDataType>& fileFormat)
|
||||
void CSTLoader::init(const string &fileName, const map<string,TDataType>& 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<string,TDataType>& 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<string,TDataType>& 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 "";
|
||||
}
|
||||
|
|
|
@ -25,7 +25,6 @@
|
|||
#include <map>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
|
||||
|
||||
/**
|
||||
|
@ -55,7 +54,7 @@ public:
|
|||
private:
|
||||
|
||||
/// cst file
|
||||
std::ifstream * _File;
|
||||
FILE *_File;
|
||||
|
||||
/// name of the cst file (used for debug information)
|
||||
std::string _FileName;
|
||||
|
@ -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<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 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
|
||||
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<std::string,TDataType> > format;
|
||||
buildTableFormat( fileName, format );
|
||||
generateBaseClass( script_file, format);
|
||||
generateBaseClass( format);
|
||||
|
||||
// Generates a derived class for each type of object
|
||||
std::list< std::list<std::string> > 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;
|
||||
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<std::string,TDataType> >::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::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")
|
||||
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;
|
||||
|
|
Loading…
Reference in a new issue