//---------------------------------------------------------------------------- // 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 ; //---------------------------------------------------------------------------- inline bool WriteStringOutTxt( gzFile OutTxtFile, const char* szVal, const char* szSep, bool bEndL) { // verifico apertura file if ( OutTxtFile == nullptr) return false ; // scrivo stringa if ( gzputs( OutTxtFile, szVal) == Z_ERRNO) return false ; // se fornito, scrivo separatore if ( szSep != nullptr && szSep[0] != '\0') { if ( gzputs( OutTxtFile, szSep) == Z_ERRNO) return false ; } // se richiesto, scrivo fine linea if ( bEndL) { if ( gzputs( OutTxtFile, "\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) ; // apertura del file di uscita if ( m_bBinary) { ios_base::openmode nMode = ios::out | ( m_bBinary ? ios::binary : 0) ; int nProt = _SH_DENYWR ; m_OutBinFile.open( stringtoW( sFileOut), nMode, nProt) ; return m_OutBinFile.good() ; } else { if ( nFlag == GDB_SV_TXT) m_OutTxtFile = gzopen_w( stringtoW( sFileOut), "wbT") ; else // GDB_SV_CMPTXT m_OutTxtFile = gzopen_w( stringtoW( sFileOut), "wb") ; if ( m_OutTxtFile == nullptr) return false ; const int DIM_BUFFER = 65536 ; if ( gzbuffer( m_OutTxtFile, DIM_BUFFER) != Z_OK) return false ; const int COMPR_LEVEL = 3 ; // 0 = no compression ... 9 = max compression if ( gzsetparams( m_OutTxtFile, COMPR_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK) return false ; return true ; } } //---------------------------------------------------------------------------- bool NgeWriter::Close( void) { if ( m_bBinary) { bool bOk = ( m_OutBinFile.good() && m_OutBinFile.is_open()) ; if ( m_OutBinFile.is_open()) m_OutBinFile.close() ; return bOk ; } else { if ( m_OutTxtFile != nullptr) { bool bOk = ( gzclose( m_OutTxtFile) == Z_OK) ; m_OutTxtFile = nullptr ; return bOk ; } return true ; } } //---------------------------------------------------------------------------- bool NgeWriter::WriteUchar( unsigned char ucVal, const char* szSep, bool bEndL) { if ( m_bBinary) { if ( ! m_OutBinFile.is_open()) return false ; m_OutBinFile.write( (char*) &ucVal, sizeof( ucVal)) ; return m_OutBinFile.good() ; } else { return WriteStringOutTxt( m_OutTxtFile, ToString( ucVal).c_str(), szSep, bEndL) ; } } //---------------------------------------------------------------------------- bool NgeWriter::WriteBool( bool bVal, const char* szSep, bool bEndL) { if ( m_bBinary) { if ( ! m_OutBinFile.is_open()) return false ; m_OutBinFile.write( (char*) &bVal, sizeof( bVal)) ; return m_OutBinFile.good() ; } else { return WriteStringOutTxt( m_OutTxtFile, ToString( bVal).c_str(), szSep, bEndL) ; } } //---------------------------------------------------------------------------- bool NgeWriter::WriteInt( int nVal, const char* szSep, bool bEndL) { if ( m_bBinary) { if ( ! m_OutBinFile.is_open()) return false ; m_OutBinFile.write( (char*) &nVal, sizeof( nVal)) ; return m_OutBinFile.good() ; } else { return WriteStringOutTxt( m_OutTxtFile, ToString( nVal).c_str(), szSep, bEndL) ; } } //---------------------------------------------------------------------------- bool NgeWriter::WriteDouble( double dVal, const char* szSep, bool bEndL, int nPrec) { if ( m_bBinary) { if ( ! m_OutBinFile.is_open()) return false ; m_OutBinFile.write( (char*) &dVal, sizeof( dVal)) ; return m_OutBinFile.good() ; } else { return WriteStringOutTxt( m_OutTxtFile, ToString( dVal, nPrec).c_str(), szSep, bEndL) ; } } //---------------------------------------------------------------------------- bool NgeWriter::WriteString( const string& sVal, const char* szSep, bool bEndL) { if ( m_bBinary) { if ( ! m_OutBinFile.is_open()) return false ; int nDim = int( sVal.size()) ; m_OutBinFile.write( (char*) &nDim, sizeof( nDim)) ; m_OutBinFile.write( sVal.c_str(), sVal.size()) ; return m_OutBinFile.good() ; } else { return WriteStringOutTxt( m_OutTxtFile, sVal.c_str(), szSep, bEndL) ; } } //---------------------------------------------------------------------------- bool NgeWriter::WriteVector( const Vector3d& vtV, const char* szSep, bool bEndL, int nPrec) { if ( m_bBinary) { if ( ! m_OutBinFile.is_open()) return false ; m_OutBinFile.write( (char*) &vtV.v, sizeof( vtV.v)) ; return m_OutBinFile.good() ; } else { return WriteStringOutTxt( m_OutTxtFile, 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_OutBinFile.is_open()) return false ; m_OutBinFile.write( (char*) &ptP.v, sizeof( ptP.v)) ; return m_OutBinFile.good() ; } else { return WriteStringOutTxt( m_OutTxtFile, 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_OutBinFile.is_open()) return false ; m_OutBinFile.write( (char*) &ptP.v, sizeof( ptP.v)) ; m_OutBinFile.write( (char*) &dW, sizeof( dW)) ; return m_OutBinFile.good() ; } else { return WriteStringOutTxt( m_OutTxtFile, 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_OutBinFile.is_open()) return false ; m_OutBinFile.write( (char*) &frF.Orig().v, sizeof( frF.Orig().v)) ; m_OutBinFile.write( (char*) &frF.VersX().v, sizeof( frF.VersX().v)) ; m_OutBinFile.write( (char*) &frF.VersY().v, sizeof( frF.VersY().v)) ; m_OutBinFile.write( (char*) &frF.VersZ().v, sizeof( frF.VersZ().v)) ; return m_OutBinFile.good() ; } else { return WriteStringOutTxt( m_OutTxtFile, 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_OutBinFile.is_open()) return false ; m_OutBinFile.write( (char*) &NgeBinKeyW[nKey], sizeof( int)) ; return m_OutBinFile.good() ; } else { return WriteStringOutTxt( m_OutTxtFile, NgeAscKeyW[nKey].c_str(), nullptr, true) ; } } //---------------------------------------------------------------------------- bool NgeWriter::WriteCol( const Color& cCol, const char* szSep, bool bEndL) { if ( m_bBinary) { if ( ! m_OutBinFile.is_open()) return false ; unsigned char ucCol[4] ; ucCol[0] = cCol.GetIntRed() ; ucCol[1] = cCol.GetIntGreen() ; ucCol[2] = cCol.GetIntBlue() ; ucCol[3] = cCol.GetIntAlpha() ; m_OutBinFile.write( (char*) ucCol, sizeof( ucCol)) ; return m_OutBinFile.good() ; } else { return WriteStringOutTxt( m_OutTxtFile, ToString( cCol).c_str(), szSep, bEndL) ; } } //---------------------------------------------------------------------------- bool NgeWriter::WriteRemark( const string& sVal) { if ( m_bBinary) return true ; else return ( WriteStringOutTxt( m_OutTxtFile, "//", nullptr, false) && WriteStringOutTxt( m_OutTxtFile, sVal.c_str(), nullptr, true)) ; }