//---------------------------------------------------------------------------- // EgalTech 2014-2014 //---------------------------------------------------------------------------- // File : NgeWriter.cpp Data : 13.04.14 Versione : 1.5d5 // Contenuto : Implementazione della classe NgeWriter. // // // // Modifiche : 13.04.14 DS Creazione modulo. // // //---------------------------------------------------------------------------- //--------------------------- Include ---------------------------------------- #include "stdafx.h" #include "NgeWriter.h" #include "NgeKeyW.h" #include "/EgtDev/Include/EGkGdbConst.h" #include "/EgtDev/Include/EGkStringUtils3d.h" #include "/EgtDev/Include/EgtStringConverter.h" #include "/EgtDev/Extern/zlib/Include/zlib.h" using namespace std ; //---------------------------------------------------------------------------- static bool WriteStringOutTxt( gzFile OutFile, const char* szVal, const char* szSep, bool bEndL) { // verifico apertura file if ( OutFile == nullptr) return false ; // scrivo stringa if ( gzputs( OutFile, szVal) == Z_ERRNO) return false ; // se fornito, scrivo separatore if ( szSep != nullptr && szSep[0] != '\0') { if ( gzputs( OutFile, szSep) == Z_ERRNO) return false ; } // se richiesto, scrivo fine linea if ( bEndL) { if ( gzputs( OutFile, "\r\n") == Z_ERRNO) return false ; } return true ; } //---------------------------------------------------------------------------- bool NgeWriter::Init( const string& sFileOut, int nFlag) { // salvo tipo file m_bBinary = ( nFlag == GDB_SV_BIN || nFlag == GDB_SV_CMPBIN) ; // apertura del file di uscita if ( nFlag == GDB_SV_TXT || nFlag == GDB_SV_BIN) { m_OutFile = gzopen_w( stringtoW( sFileOut), "wbT") ; return ( m_OutFile != nullptr) ; } else { // GDB_SV_CMPTXT o GDB_SV_CMPBIN m_OutFile = gzopen_w( stringtoW( sFileOut), "wb") ; if ( m_OutFile == nullptr) return false ; const int DIM_BUFFER = 65536 ; if ( gzbuffer( m_OutFile, DIM_BUFFER) != Z_OK) return false ; const int COMPR_LEVEL = 3 ; // 0 = no compression ... 9 = max compression if ( gzsetparams( m_OutFile, COMPR_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK) return false ; return true ; } } //---------------------------------------------------------------------------- bool NgeWriter::Close( void) { if ( m_OutFile != nullptr) { bool bOk = ( gzclose( m_OutFile) == Z_OK) ; m_OutFile = nullptr ; return bOk ; } return true ; } //---------------------------------------------------------------------------- bool NgeWriter::WriteUchar( unsigned char ucVal, const char* szSep, bool bEndL) { if ( m_bBinary) { if ( m_OutFile == nullptr) return false ; return ( gzwrite( m_OutFile, &ucVal, sizeof( ucVal)) > 0) ; } else { return WriteStringOutTxt( m_OutFile, ToString( ucVal).c_str(), szSep, bEndL) ; } } //---------------------------------------------------------------------------- bool NgeWriter::WriteBool( bool bVal, const char* szSep, bool bEndL) { if ( m_bBinary) { if ( m_OutFile == nullptr) return false ; return ( gzwrite( m_OutFile, &bVal, sizeof( bVal)) > 0) ; } else { return WriteStringOutTxt( m_OutFile, ToString( bVal).c_str(), szSep, bEndL) ; } } //---------------------------------------------------------------------------- bool NgeWriter::WriteInt( int nVal, const char* szSep, bool bEndL) { if ( m_bBinary) { if ( m_OutFile == nullptr) return false ; return ( gzwrite( m_OutFile, &nVal, sizeof( nVal)) > 0) ; } else { return WriteStringOutTxt( m_OutFile, ToString( nVal).c_str(), szSep, bEndL) ; } } //---------------------------------------------------------------------------- bool NgeWriter::WriteDouble( double dVal, const char* szSep, bool bEndL, int nPrec) { if ( m_bBinary) { if ( m_OutFile == nullptr) return false ; return ( gzwrite( m_OutFile, &dVal, sizeof( dVal)) > 0) ; } else { return WriteStringOutTxt( m_OutFile, ToString( dVal, nPrec).c_str(), szSep, bEndL) ; } } //---------------------------------------------------------------------------- bool NgeWriter::WriteString( const string& sVal, const char* szSep, bool bEndL) { if ( m_bBinary) { if ( m_OutFile == nullptr) return false ; int nDim = ssize( sVal) ; return ( gzwrite( m_OutFile, &nDim, sizeof( nDim)) > 0 && ( nDim == 0 || gzwrite( m_OutFile, sVal.c_str(), sVal.size()) > 0)) ; } else { return WriteStringOutTxt( m_OutFile, sVal.c_str(), szSep, bEndL) ; } } //---------------------------------------------------------------------------- bool NgeWriter::WriteVector( const Vector3d& vtV, const char* szSep, bool bEndL, int nPrec) { if ( m_bBinary) { if ( m_OutFile == nullptr) return false ; return ( gzwrite( m_OutFile, &vtV.v, sizeof( vtV.v)) > 0) ; } else { return WriteStringOutTxt( m_OutFile, ToString( vtV, nPrec).c_str(), szSep, bEndL) ; } } //---------------------------------------------------------------------------- bool NgeWriter::WritePoint( const Point3d& ptP, const char* szSep, bool bEndL, int nPrec) { if ( m_bBinary) { if ( m_OutFile == nullptr) return false ; return ( gzwrite( m_OutFile, &ptP.v, sizeof( ptP.v)) > 0) ; } else { return WriteStringOutTxt( m_OutFile, ToString( ptP, nPrec).c_str(), szSep, bEndL) ; } } //---------------------------------------------------------------------------- bool NgeWriter::WritePointW( const Point3d& ptP, double dW, const char* szSep, bool bEndL, int nPrecP, int nPrecW) { if ( m_bBinary) { if ( m_OutFile == nullptr) return false ; return ( gzwrite( m_OutFile, &ptP.v, sizeof( ptP.v)) > 0 && gzwrite( m_OutFile, &dW, sizeof( dW)) > 0) ; } else { return WriteStringOutTxt( m_OutFile, ToString( ptP, dW, nPrecP, nPrecW).c_str(), szSep, bEndL) ; } } //---------------------------------------------------------------------------- bool NgeWriter::WriteFrame( const Frame3d& frF, const char* szSep, bool bEndL, int nPrecP, int nPrecV) { if ( m_bBinary) { if ( m_OutFile == nullptr) return false ; return ( gzwrite( m_OutFile, &frF.Orig().v, sizeof( frF.Orig().v)) > 0 && gzwrite( m_OutFile, &frF.VersX().v, sizeof( frF.VersX().v)) > 0 && gzwrite( m_OutFile, &frF.VersY().v, sizeof( frF.VersY().v)) > 0 && gzwrite( m_OutFile, &frF.VersZ().v, sizeof( frF.VersZ().v)) > 0) ; } else { return WriteStringOutTxt( m_OutFile, ToString( frF, nPrecP, nPrecV).c_str(), szSep, bEndL) ; } } //---------------------------------------------------------------------------- bool NgeWriter::WriteKey( int nKey) { if ( nKey < 0 || nKey > NGE_LAST_ID) return false ; if ( m_bBinary) { if ( m_OutFile == nullptr) return false ; return ( gzwrite( m_OutFile, &NgeBinKeyW[nKey], sizeof( int)) > 0) ; } else { return WriteStringOutTxt( m_OutFile, NgeAscKeyW[nKey].c_str(), nullptr, true) ; } } //---------------------------------------------------------------------------- bool NgeWriter::WriteCol( const Color& cCol, const char* szSep, bool bEndL) { if ( m_bBinary) { if ( m_OutFile == nullptr) return false ; unsigned char ucCol[4] ; ucCol[0] = cCol.GetIntRed() ; ucCol[1] = cCol.GetIntGreen() ; ucCol[2] = cCol.GetIntBlue() ; ucCol[3] = cCol.GetIntAlpha() ; return ( gzwrite( m_OutFile, ucCol, sizeof( ucCol)) > 0) ; } else { return WriteStringOutTxt( m_OutFile, ToString( cCol).c_str(), szSep, bEndL) ; } } //---------------------------------------------------------------------------- bool NgeWriter::WriteRemark( const string& sVal) { if ( m_bBinary) { return true ; } else { return ( WriteStringOutTxt( m_OutFile, "//", nullptr, false) && WriteStringOutTxt( m_OutFile, sVal.c_str(), nullptr, true)) ; } }