d92344f2bb
- - modifiche a NgeReader per semplificare e velocizzare lettura file binari.
403 lines
11 KiB
C++
403 lines
11 KiB
C++
//----------------------------------------------------------------------------
|
|
// EgalTech 2014-2014
|
|
//----------------------------------------------------------------------------
|
|
// File : NgeReader.cpp Data : 14.04.14 Versione : 1.5d5
|
|
// Contenuto : Implementazione della classe NgeReader.
|
|
//
|
|
//
|
|
//
|
|
// Modifiche : 14.04.14 DS Creazione modulo.
|
|
//
|
|
//
|
|
//----------------------------------------------------------------------------
|
|
|
|
//--------------------------- Include ----------------------------------------
|
|
#include "stdafx.h"
|
|
#include "NgeReader.h"
|
|
#include "NgeKeyW.h"
|
|
#include "/EgtDev/Include/EGkStringUtils3d.h"
|
|
#include "/EgtDev/Include/EgtStringConverter.h"
|
|
#include "/EgtDev/Extern/zlib/Include/zlib.h"
|
|
#include <fstream>
|
|
|
|
using namespace std ;
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
NgeReader::Init( const string& sFileIn)
|
|
{
|
|
switch ( NgeType( sFileIn)) {
|
|
case NGE_ASCII :
|
|
m_bBinary = false ;
|
|
m_iPosStart = string::npos ;
|
|
m_sLine.reserve( 128) ;
|
|
m_sToken.reserve( 48) ;
|
|
return m_Scan.Init( sFileIn) ;
|
|
break ;
|
|
case NGE_BINARY :
|
|
m_bBinary = true ;
|
|
m_InFile = gzopen_w( stringtoW( sFileIn), "rb") ;
|
|
if ( m_InFile == nullptr)
|
|
return false ;
|
|
const int DIM_BUFFER = 65536 ;
|
|
gzbuffer( m_InFile, DIM_BUFFER) ;
|
|
return true ;
|
|
break ;
|
|
}
|
|
return false ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
NgeReader::Close( void)
|
|
{
|
|
if ( m_bBinary) {
|
|
if ( m_InFile != nullptr) {
|
|
bool bOk = ( gzclose( m_InFile) == Z_OK) ;
|
|
m_InFile = nullptr ;
|
|
return bOk ;
|
|
}
|
|
return true ;
|
|
}
|
|
else
|
|
return m_Scan.Terminate() ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
int
|
|
NgeReader::NgeType( const string& sFile)
|
|
{
|
|
// apertura del file di ingresso
|
|
ifstream InFile ;
|
|
InFile.open( stringtoW( sFile), ios::in | ios::binary) ;
|
|
if ( InFile.fail())
|
|
return NGE_ERROR ;
|
|
|
|
// lettura dei primi 31 byte
|
|
char cBuff[32] ;
|
|
InFile.read( cBuff, 31) ;
|
|
cBuff[InFile.gcount()] = '\0' ;
|
|
|
|
// chiusura del file
|
|
InFile.close() ;
|
|
|
|
// verifico se file compresso (gz)
|
|
if ( cBuff[0] == '\x1F' && cBuff[1] == '\x8B')
|
|
return NGE_ASCII ;
|
|
|
|
// verifico se iniziano con "START"
|
|
string sBuff = cBuff ;
|
|
size_t nPos = sBuff.find( "START") ;
|
|
if ( nPos != string::npos && nPos < 10)
|
|
return NGE_ASCII ;
|
|
else
|
|
return NGE_BINARY ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
int
|
|
NgeReader::GetCurrPos( void)
|
|
{
|
|
if ( m_bBinary)
|
|
return int( gztell( m_InFile)) ;
|
|
else
|
|
return m_Scan.GetCurrLineNbr() ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
NgeReader::GetToken( string& sToken, const char* szSep, bool bEndL)
|
|
{
|
|
// se necessario, lettura nuova linea
|
|
if ( m_iPosStart == string::npos) {
|
|
if ( ! m_Scan.GetLine( m_sLine))
|
|
return false ;
|
|
m_iPosStart = 0 ;
|
|
}
|
|
// se richiesta ricerca separatore
|
|
if ( szSep != nullptr) {
|
|
// cerco il primo separatore nella linea
|
|
string::size_type iPosEnd = m_sLine.find_first_of( szSep, m_iPosStart) ;
|
|
sToken = m_sLine.substr( m_iPosStart, iPosEnd - m_iPosStart) ;
|
|
// passo al prossimo
|
|
m_iPosStart = m_sLine.find_first_not_of( szSep, iPosEnd) ;
|
|
if ( bEndL)
|
|
return ( m_iPosStart == string::npos) ;
|
|
else
|
|
return ( m_iPosStart != string::npos) ;
|
|
}
|
|
// altrimenti, deve essere tutto con endl obbligatorio
|
|
else {
|
|
sToken = m_sLine.substr( m_iPosStart, string::npos) ;
|
|
m_iPosStart = string::npos ;
|
|
return bEndL ;
|
|
}
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
NgeReader::ReadUchar( unsigned char& ucVal, const char* szSep, bool bEndL)
|
|
{
|
|
if ( m_bBinary) {
|
|
if ( m_InFile == nullptr)
|
|
return false ;
|
|
return ( gzread( m_InFile, &ucVal, sizeof( ucVal)) != Z_ERRNO) ;
|
|
}
|
|
else {
|
|
// recupero il token
|
|
if ( ! GetToken( m_sToken, szSep, bEndL))
|
|
return false ;
|
|
// ricavo il valore
|
|
int nVal ;
|
|
if ( ! FromString( m_sToken, nVal))
|
|
return false ;
|
|
ucVal = nVal ;
|
|
return true ;
|
|
}
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
NgeReader::ReadBool( bool& bVal, const char* szSep, bool bEndL)
|
|
{
|
|
if ( m_bBinary) {
|
|
if ( m_InFile == nullptr)
|
|
return false ;
|
|
return ( gzread( m_InFile, &bVal, sizeof( bVal)) != Z_ERRNO) ;
|
|
}
|
|
else {
|
|
// recupero il token
|
|
if ( ! GetToken( m_sToken, szSep, bEndL))
|
|
return false ;
|
|
// ricavo il valore
|
|
return FromString( m_sToken, bVal) ;
|
|
}
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
NgeReader::ReadInt( int& nVal, const char* szSep, bool bEndL)
|
|
{
|
|
if ( m_bBinary) {
|
|
if ( m_InFile == nullptr)
|
|
return false ;
|
|
return ( gzread( m_InFile, &nVal, sizeof( nVal)) != Z_ERRNO) ;
|
|
}
|
|
else {
|
|
// recupero il token
|
|
if ( ! GetToken( m_sToken, szSep, bEndL))
|
|
return false ;
|
|
// ricavo il valore
|
|
return FromString( m_sToken, nVal) ;
|
|
}
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
NgeReader::ReadInt( unsigned int& nVal, const char* szSep, bool bEndL)
|
|
{
|
|
int nTemp ;
|
|
if ( ! ReadInt( nTemp, szSep, bEndL))
|
|
return false ;
|
|
nVal = ( unsigned int) ( nTemp) ;
|
|
return true ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
NgeReader::ReadDouble( double& dVal, const char* szSep, bool bEndL)
|
|
{
|
|
if ( m_bBinary) {
|
|
if ( m_InFile == nullptr)
|
|
return false ;
|
|
return ( gzread( m_InFile, &dVal, sizeof( dVal)) != Z_ERRNO) ;
|
|
}
|
|
else {
|
|
// recupero il token
|
|
if ( ! GetToken( m_sToken, szSep, bEndL))
|
|
return false ;
|
|
// ricavo il valore
|
|
return FromString( m_sToken, dVal) ;
|
|
}
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
NgeReader::ReadVector( Vector3d& vtV, const char* szSep, bool bEndL)
|
|
{
|
|
if ( m_bBinary) {
|
|
if ( m_InFile == nullptr)
|
|
return false ;
|
|
return ( gzread( m_InFile, &vtV.v, sizeof( vtV.v)) != Z_ERRNO) ;
|
|
}
|
|
else {
|
|
// recupero il token
|
|
if ( ! GetToken( m_sToken, szSep, bEndL))
|
|
return false ;
|
|
// ricavo il valore
|
|
return FromString( m_sToken, vtV) ;
|
|
}
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
NgeReader::ReadPoint( Point3d& ptP, const char* szSep, bool bEndL)
|
|
{
|
|
if ( m_bBinary) {
|
|
if ( m_InFile == nullptr)
|
|
return false ;
|
|
return ( gzread( m_InFile, &ptP.v, sizeof( ptP.v)) != Z_ERRNO) ;
|
|
}
|
|
else {
|
|
// recupero il token
|
|
if ( ! GetToken( m_sToken, szSep, bEndL))
|
|
return false ;
|
|
// ricavo il valore
|
|
return FromString( m_sToken, ptP) ;
|
|
}
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
NgeReader::ReadPointW( Point3d& ptP, double& dW, const char* szSep, bool bEndL)
|
|
{
|
|
if ( m_bBinary) {
|
|
if ( m_InFile == nullptr)
|
|
return false ;
|
|
return ( gzread( m_InFile, &ptP.v, sizeof( ptP.v)) != Z_ERRNO &&
|
|
gzread( m_InFile, &dW, sizeof( dW)) != Z_ERRNO) ;
|
|
}
|
|
else {
|
|
// recupero il token
|
|
if ( ! GetToken( m_sToken, szSep, bEndL))
|
|
return false ;
|
|
// ricavo il valore
|
|
return FromString( m_sToken, ptP, dW) ;
|
|
}
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
NgeReader::ReadFrame( Frame3d& frF, const char* szSep, bool bEndL)
|
|
{
|
|
if ( m_bBinary) {
|
|
if ( m_InFile == nullptr)
|
|
return false ;
|
|
Point3d ptOrig ; Vector3d vtDirX, vtDirY, vtDirZ ;
|
|
if ( gzread( m_InFile, &ptOrig.v, sizeof( ptOrig.v)) == Z_ERRNO ||
|
|
gzread( m_InFile, &vtDirX.v, sizeof( vtDirX.v)) == Z_ERRNO ||
|
|
gzread( m_InFile, &vtDirY.v, sizeof( vtDirY.v)) == Z_ERRNO ||
|
|
gzread( m_InFile, &vtDirZ.v, sizeof( vtDirZ.v)) == Z_ERRNO)
|
|
return false ;
|
|
return frF.Set( ptOrig, vtDirX, vtDirY, vtDirZ) ;
|
|
}
|
|
else {
|
|
// recupero il token
|
|
if ( ! GetToken( m_sToken, szSep, bEndL))
|
|
return false ;
|
|
// ricavo il valore
|
|
return FromString( m_sToken, frF) ;
|
|
}
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
NgeReader::ReadString( string& sVal, const char* szSep, bool bEndL)
|
|
{
|
|
if ( m_bBinary) {
|
|
if ( m_InFile == nullptr)
|
|
return false ;
|
|
const int MAX_STR_DIM = 65535 ;
|
|
int nDim ;
|
|
if ( gzread( m_InFile, &nDim, sizeof( nDim)) == Z_ERRNO || nDim > MAX_STR_DIM)
|
|
return false ;
|
|
if ( nDim == 0) {
|
|
sVal = "" ;
|
|
return true ;
|
|
}
|
|
char* szBuff = new( nothrow) char[ nDim + 1] ;
|
|
if ( szBuff == nullptr)
|
|
return false ;
|
|
if ( gzread( m_InFile, szBuff, nDim) == Z_ERRNO) {
|
|
delete[] szBuff ;
|
|
return false ;
|
|
}
|
|
szBuff[nDim] = '\0' ;
|
|
sVal = szBuff ;
|
|
delete[] szBuff ;
|
|
return true ;
|
|
}
|
|
else {
|
|
// recupero il token
|
|
if ( ! GetToken( m_sToken, szSep, bEndL))
|
|
return false ;
|
|
// lo copio nella stringa
|
|
sVal = m_sToken ;
|
|
return true ;
|
|
}
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
NgeReader::ReadKey( int& nKey)
|
|
{
|
|
if ( m_bUngetKey) {
|
|
m_bUngetKey = false ;
|
|
nKey = m_nLastKey ;
|
|
return true ;
|
|
}
|
|
if ( m_bBinary) {
|
|
if ( m_InFile == nullptr)
|
|
return false ;
|
|
// leggo il dato
|
|
int nVal ;
|
|
if ( gzread( m_InFile, &nVal, sizeof( nVal)) == Z_ERRNO)
|
|
return false ;
|
|
// ricavo l'indice
|
|
for ( int i = 0 ; i <= NGE_LAST_ID ; ++ i) {
|
|
if ( nVal == NgeBinKeyW[i]) {
|
|
nKey = i ;
|
|
m_nLastKey = nKey ;
|
|
return true ;
|
|
}
|
|
}
|
|
return false ;
|
|
}
|
|
else {
|
|
// recupero la linea
|
|
if ( ! GetToken( m_sToken, nullptr, true))
|
|
return false ;
|
|
// ricavo l'indice
|
|
for ( int i = 0 ; i <= NGE_LAST_ID ; ++ i) {
|
|
if ( m_sToken == NgeAscKeyW[i]) {
|
|
nKey = i ;
|
|
m_nLastKey = nKey ;
|
|
return true ;
|
|
}
|
|
}
|
|
return false ;
|
|
}
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
NgeReader::ReadCol( Color& cCol, const char* szSep, bool bEndL)
|
|
{
|
|
if ( m_bBinary) {
|
|
if ( m_InFile == nullptr)
|
|
return false ;
|
|
unsigned char ucCol[4] ;
|
|
if ( gzread( m_InFile, &ucCol, sizeof( ucCol)) == Z_ERRNO)
|
|
return false ;
|
|
cCol.Set( ucCol[0], ucCol[1], ucCol[2], ucCol[3]) ;
|
|
return true ;
|
|
}
|
|
else {
|
|
// recupero il token
|
|
if ( ! GetToken( m_sToken, szSep, bEndL))
|
|
return false ;
|
|
// ricavo il valore
|
|
return FromString( m_sToken, cCol) ;
|
|
}
|
|
}
|