Files
Dario Sassi d0ac226753 EgtGeomKernel :
- aggiunta lettura file Nge binari compressi.
2025-12-29 19:24:56 +01:00

395 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"
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 file
gzFile_s* InFile = gzopen_w( stringtoW( sFile), "rb") ;
if ( InFile == nullptr)
return NGE_ERROR ;
// lettura dei primi caratteri
char szBuff[9] = "\0\0\0\0\0\0\0\0" ;
int nLen = gzread( InFile, &szBuff, 8) ;
if ( gzclose( InFile) != Z_OK || nLen == Z_ERRNO)
return NGE_ERROR ;
// se binario
if ( szBuff[0] == '\x0F' && szBuff[1] == '\x0F')
return NGE_BINARY ;
// se testo
string sBuff{ szBuff} ;
if ( sBuff.find( "START") != string::npos)
return NGE_ASCII ;
// altrimenti errore
return NGE_ERROR ;
}
//----------------------------------------------------------------------------
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) ;
}
}