2013-10-24 16:44:38 +00:00
|
|
|
/******************************************************************************
|
|
|
|
** libDXFrw - Library to read/write DXF files (ascii & binary) **
|
|
|
|
** **
|
|
|
|
** Copyright (C) 2011 Rallaz, rallazz@gmail.com **
|
|
|
|
** **
|
|
|
|
** This library is free software, licensed under the terms of the GNU **
|
|
|
|
** General Public License as published by the Free Software Foundation, **
|
|
|
|
** either version 2 of the License, or (at your option) any later version. **
|
|
|
|
** You should have received a copy of the GNU General Public License **
|
|
|
|
** along with this program. If not, see <http://www.gnu.org/licenses/>. **
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
#include "dxfreader.h"
|
|
|
|
#include "drw_textcodec.h"
|
|
|
|
|
|
|
|
#ifdef DRW_DBG
|
2013-10-28 08:06:38 +00:00
|
|
|
#include <iostream> // for debug
|
|
|
|
#define DBG( a ) std::cerr << a
|
2013-10-24 16:44:38 +00:00
|
|
|
#else
|
2013-10-28 08:06:38 +00:00
|
|
|
#define DBG( a )
|
2013-10-24 16:44:38 +00:00
|
|
|
#endif
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
bool dxfReader::readRec( int* codeData, bool skip )
|
|
|
|
{
|
|
|
|
// std::string text;
|
2013-10-24 16:44:38 +00:00
|
|
|
int code;
|
|
|
|
|
|
|
|
#ifdef DRW_DBG
|
2013-10-28 08:06:38 +00:00
|
|
|
count = count + 2; // DBG
|
2013-10-24 16:44:38 +00:00
|
|
|
/* if (count > 10250)
|
2013-10-28 08:06:38 +00:00
|
|
|
* DBG("line 10256");*/
|
2013-10-24 16:44:38 +00:00
|
|
|
#endif
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
if( !readCode( &code ) )
|
2013-10-24 16:44:38 +00:00
|
|
|
return false;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
2013-10-24 16:44:38 +00:00
|
|
|
*codeData = code;
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
if( code < 10 )
|
2013-10-24 16:44:38 +00:00
|
|
|
readString();
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( code < 60 )
|
2013-10-24 16:44:38 +00:00
|
|
|
readDouble();
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( code < 80 )
|
2013-10-24 16:44:38 +00:00
|
|
|
readInt();
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( code > 89 && code < 100 ) // TODO this is an int 32b
|
2013-10-24 16:44:38 +00:00
|
|
|
readInt32();
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( code == 100 || code == 102 || code == 105 )
|
2013-10-24 16:44:38 +00:00
|
|
|
readString();
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( code > 109 && code < 150 ) // skip not used at the v2012
|
2013-10-24 16:44:38 +00:00
|
|
|
readDouble();
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( code > 159 && code < 170 ) // skip not used at the v2012
|
2013-10-24 16:44:38 +00:00
|
|
|
readInt64();
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( code < 180 )
|
2013-10-24 16:44:38 +00:00
|
|
|
readInt();
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( code > 209 && code < 240 ) // skip not used at the v2012
|
2013-10-24 16:44:38 +00:00
|
|
|
readDouble();
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( code > 269 && code < 290 ) // skip not used at the v2012
|
2013-10-24 16:44:38 +00:00
|
|
|
readInt();
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( code < 300 ) // TODO this is a boolean indicator, int in Binary?
|
2013-10-24 16:44:38 +00:00
|
|
|
readBool();
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( code < 370 )
|
2013-10-24 16:44:38 +00:00
|
|
|
readString();
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( code < 390 )
|
2013-10-24 16:44:38 +00:00
|
|
|
readInt();
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( code < 400 )
|
2013-10-24 16:44:38 +00:00
|
|
|
readString();
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( code < 410 )
|
2013-10-24 16:44:38 +00:00
|
|
|
readInt();
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( code < 420 )
|
2013-10-24 16:44:38 +00:00
|
|
|
readString();
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( code < 430 ) // TODO this is an int 32b
|
2013-10-24 16:44:38 +00:00
|
|
|
readInt32();
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( code < 440 )
|
2013-10-24 16:44:38 +00:00
|
|
|
readString();
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( code < 450 ) // TODO this is an int 32b
|
2013-10-24 16:44:38 +00:00
|
|
|
readInt32();
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( code < 460 ) // TODO this is long??
|
2013-10-24 16:44:38 +00:00
|
|
|
readInt();
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( code < 470 ) // TODO this is a floating point double precision??
|
2013-10-24 16:44:38 +00:00
|
|
|
readDouble();
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( code < 481 )
|
2013-10-24 16:44:38 +00:00
|
|
|
readString();
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( code > 998 && code < 1009 ) // skip not used at the v2012
|
2013-10-24 16:44:38 +00:00
|
|
|
readString();
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( code < 1060 ) // TODO this is a floating point double precision??
|
2013-10-24 16:44:38 +00:00
|
|
|
readDouble();
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( code < 1071 )
|
2013-10-24 16:44:38 +00:00
|
|
|
readInt();
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( code == 1071 ) // TODO this is an int 32b
|
2013-10-24 16:44:38 +00:00
|
|
|
readInt32();
|
2013-10-28 08:06:38 +00:00
|
|
|
else if( skip )
|
|
|
|
// skip safely this dxf entry ( ok for ascii dxf)
|
2013-10-24 16:44:38 +00:00
|
|
|
readString();
|
|
|
|
else
|
2013-10-28 08:06:38 +00:00
|
|
|
// break in binary files because the conduct is unpredictable
|
2013-10-24 16:44:38 +00:00
|
|
|
return false;
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
return filestr->good();
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
int dxfReader::getHandleString()
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
int res;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
2013-10-24 16:44:38 +00:00
|
|
|
#if defined(__APPLE__)
|
2013-10-28 08:06:38 +00:00
|
|
|
int Succeeded = sscanf( strData.c_str(), "%x", &res );
|
|
|
|
|
|
|
|
if( !Succeeded || Succeeded == EOF )
|
2013-10-24 16:44:38 +00:00
|
|
|
res = 0;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
2013-10-24 16:44:38 +00:00
|
|
|
#else
|
2013-10-28 08:06:38 +00:00
|
|
|
std::istringstream Convert( strData );
|
|
|
|
|
|
|
|
if( !(Convert >> std::hex >> res) )
|
2013-10-24 16:44:38 +00:00
|
|
|
res = 0;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
2013-10-24 16:44:38 +00:00
|
|
|
#endif
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
bool dxfReaderBinary::readCode( int* code )
|
|
|
|
{
|
|
|
|
unsigned short* int16p;
|
2015-02-24 09:07:40 +00:00
|
|
|
char buffer[2];
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
filestr->read( buffer, 2 );
|
|
|
|
int16p = (unsigned short*) buffer;
|
|
|
|
|
|
|
|
// exist a 32bits int (code 90) with 2 bytes???
|
|
|
|
if( (*code == 90) && (*int16p>2000) )
|
|
|
|
{
|
|
|
|
DBG( *code ); DBG( " de 16bits\n" );
|
|
|
|
filestr->seekg( -4, std::ios_base::cur );
|
|
|
|
filestr->read( buffer, 2 );
|
|
|
|
int16p = (unsigned short*) buffer;
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
2013-10-28 08:06:38 +00:00
|
|
|
|
2013-10-24 16:44:38 +00:00
|
|
|
*code = *int16p;
|
2013-10-28 08:06:38 +00:00
|
|
|
DBG( *code ); DBG( "\n" );
|
2013-10-24 16:44:38 +00:00
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
return filestr->good();
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
bool dxfReaderBinary::readString()
|
|
|
|
{
|
|
|
|
std::getline( *filestr, strData, '\0' );
|
|
|
|
|
|
|
|
DBG( strData ); DBG( "\n" );
|
|
|
|
return filestr->good();
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
bool dxfReaderBinary::readString( std::string* text )
|
|
|
|
{
|
|
|
|
std::getline( *filestr, *text, '\0' );
|
|
|
|
|
|
|
|
DBG( *text ); DBG( "\n" );
|
|
|
|
return filestr->good();
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
bool dxfReaderBinary::readInt()
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
char buffer[2];
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
filestr->read( buffer, 2 );
|
|
|
|
intData = (int) ( (buffer[1] << 8) | buffer[0] );
|
|
|
|
DBG( intData ); DBG( "\n" );
|
|
|
|
return filestr->good();
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
bool dxfReaderBinary::readInt32()
|
|
|
|
{
|
2015-02-24 09:07:40 +00:00
|
|
|
unsigned int* int32p;
|
|
|
|
char buffer[4];
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
filestr->read( buffer, 4 );
|
|
|
|
int32p = (unsigned int*) buffer;
|
2013-10-24 16:44:38 +00:00
|
|
|
intData = *int32p;
|
2013-10-28 08:06:38 +00:00
|
|
|
DBG( intData ); DBG( "\n" );
|
|
|
|
return filestr->good();
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
bool dxfReaderBinary::readInt64()
|
|
|
|
{
|
|
|
|
unsigned long long int* int64p; // 64 bits integer pointer
|
2013-10-24 16:44:38 +00:00
|
|
|
char buffer[8];
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
filestr->read( buffer, 8 );
|
|
|
|
int64p = (unsigned long long int*) buffer;
|
|
|
|
int64 = *int64p;
|
|
|
|
DBG( int64 ); DBG( " int64\n" );
|
|
|
|
return filestr->good();
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
bool dxfReaderBinary::readDouble()
|
|
|
|
{
|
|
|
|
double* result;
|
2015-02-24 09:07:40 +00:00
|
|
|
char buffer[8];
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
filestr->read( buffer, 8 );
|
2015-02-24 09:07:40 +00:00
|
|
|
result = (double*) buffer;
|
|
|
|
doubleData = *result;
|
2013-10-28 08:06:38 +00:00
|
|
|
DBG( doubleData ); DBG( "\n" );
|
|
|
|
return filestr->good();
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
// saved as int or add a bool member??
|
|
|
|
bool dxfReaderBinary::readBool()
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
char buffer[1];
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
filestr->read( buffer, 1 );
|
|
|
|
intData = (int) (buffer[0]);
|
|
|
|
DBG( intData ); DBG( "\n" );
|
|
|
|
return filestr->good();
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
bool dxfReaderAscii::readCode( int* code )
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
std::string text;
|
2013-10-28 08:06:38 +00:00
|
|
|
std::getline( *filestr, text );
|
|
|
|
|
|
|
|
*code = atoi( text.c_str() );
|
|
|
|
DBG( *code ); DBG( "\n" );
|
|
|
|
return filestr->good();
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
bool dxfReaderAscii::readString( std::string* text )
|
|
|
|
{
|
|
|
|
std::getline( *filestr, *text );
|
|
|
|
|
|
|
|
if( !text->empty() && text->at( text->size() - 1 ) == '\r' )
|
|
|
|
text->erase( text->size() - 1 );
|
|
|
|
|
|
|
|
return filestr->good();
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
bool dxfReaderAscii::readString()
|
|
|
|
{
|
|
|
|
std::getline( *filestr, strData );
|
|
|
|
|
|
|
|
if( !strData.empty() && strData.at( strData.size() - 1 ) == '\r' )
|
|
|
|
strData.erase( strData.size() - 1 );
|
|
|
|
|
|
|
|
DBG( strData ); DBG( "\n" );
|
|
|
|
return filestr->good();
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
bool dxfReaderAscii::readInt()
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
std::string text;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
if( readString( &text ) )
|
|
|
|
{
|
|
|
|
intData = atoi( text.c_str() );
|
|
|
|
DBG( intData ); DBG( "\n" );
|
2013-10-24 16:44:38 +00:00
|
|
|
return true;
|
2013-10-28 08:06:38 +00:00
|
|
|
}
|
|
|
|
else
|
2013-10-24 16:44:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
bool dxfReaderAscii::readInt32()
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
return readInt();
|
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
bool dxfReaderAscii::readInt64()
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
return readInt();
|
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
bool dxfReaderAscii::readDouble()
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
std::string text;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
if( readString( &text ) )
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
#if defined(__APPLE__)
|
2013-10-28 08:06:38 +00:00
|
|
|
int succeeded = sscanf( &(text[0]), "%lg", &doubleData );
|
|
|
|
|
|
|
|
if( succeeded != 1 )
|
|
|
|
{
|
|
|
|
DBG( "dxfReaderAscii::readDouble(): reading double error: " );
|
|
|
|
DBG( text );
|
|
|
|
DBG( '\n' );
|
2013-10-24 16:44:38 +00:00
|
|
|
}
|
2013-10-28 08:06:38 +00:00
|
|
|
|
2013-10-24 16:44:38 +00:00
|
|
|
#else
|
2013-10-28 08:06:38 +00:00
|
|
|
std::istringstream sd( text );
|
2013-10-24 16:44:38 +00:00
|
|
|
sd >> doubleData;
|
2013-10-28 08:06:38 +00:00
|
|
|
DBG( doubleData ); DBG( '\n' );
|
2013-10-24 16:44:38 +00:00
|
|
|
#endif
|
|
|
|
return true;
|
2013-10-28 08:06:38 +00:00
|
|
|
}
|
|
|
|
else
|
2013-10-24 16:44:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
// saved as int or add a bool member??
|
|
|
|
bool dxfReaderAscii::readBool()
|
|
|
|
{
|
2013-10-24 16:44:38 +00:00
|
|
|
std::string text;
|
2013-10-28 08:06:38 +00:00
|
|
|
|
|
|
|
if( readString( &text ) )
|
|
|
|
{
|
|
|
|
intData = atoi( text.c_str() );
|
|
|
|
DBG( intData ); DBG( "\n" );
|
2013-10-24 16:44:38 +00:00
|
|
|
return true;
|
2013-10-28 08:06:38 +00:00
|
|
|
}
|
|
|
|
else
|
2013-10-24 16:44:38 +00:00
|
|
|
return false;
|
|
|
|
}
|