Files
EgtGeomKernel/NgeWriter.cpp
T
Dario Sassi 5a49a486bf EgtGeomKernel 1.5d6 :
- aggiunta lettura dei file Nge binari.
2014-04-15 14:21:42 +00:00

261 lines
7.0 KiB
C++

//----------------------------------------------------------------------------
// 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/EGkStringUtils3d.h"
#include "/EgtDev/Include/EgnStringConverter.h"
using namespace std ;
//----------------------------------------------------------------------------
bool
NgeWriter::Init( const std::string& sFileOut, bool bBinary)
{
// salvo tipo file
m_bBinary = bBinary ;
// apertura del file di uscita
ios_base::openmode nMode = ios::out | ( m_bBinary ? ios::binary : 0) ;
int nProt = _SH_DENYWR ;
m_OutFile.open( stringtoW( sFileOut), nMode, nProt) ;
return m_OutFile.good() ;
}
//----------------------------------------------------------------------------
bool
NgeWriter::Close( void)
{
bool bOk = ( m_OutFile.good() && m_OutFile.is_open()) ;
if ( m_OutFile.is_open())
m_OutFile.close() ;
return bOk ;
}
//----------------------------------------------------------------------------
void
NgeWriter::WriteUchar( unsigned char ucVal, const char* szSep, bool bEndL)
{
if ( ! m_OutFile.is_open())
return ;
if ( m_bBinary)
m_OutFile.write( (char*) &ucVal, sizeof( ucVal)) ;
else {
m_OutFile << ToString( ucVal) ;
if ( szSep != nullptr)
m_OutFile << szSep ;
if ( bEndL)
m_OutFile << endl ;
}
}
//----------------------------------------------------------------------------
void
NgeWriter::WriteBool( bool bVal, const char* szSep, bool bEndL)
{
if ( ! m_OutFile.is_open())
return ;
if ( m_bBinary)
m_OutFile.write( (char*) &bVal, sizeof( bVal)) ;
else {
m_OutFile << ToString( bVal) ;
if ( szSep != nullptr)
m_OutFile << szSep ;
if ( bEndL)
m_OutFile << endl ;
}
}
//----------------------------------------------------------------------------
void
NgeWriter::WriteInt( int nVal, const char* szSep, bool bEndL)
{
if ( ! m_OutFile.is_open())
return ;
if ( m_bBinary)
m_OutFile.write( (char*) &nVal, sizeof( nVal)) ;
else {
m_OutFile << ToString( nVal) ;
if ( szSep != nullptr)
m_OutFile << szSep ;
if ( bEndL)
m_OutFile << endl ;
}
}
//----------------------------------------------------------------------------
void
NgeWriter::WriteDouble( double dVal, const char* szSep, bool bEndL, int nPrec)
{
if ( ! m_OutFile.is_open())
return ;
if ( m_bBinary)
m_OutFile.write( (char*) &dVal, sizeof( dVal)) ;
else {
m_OutFile << ToString( dVal, nPrec) ;
if ( szSep != nullptr)
m_OutFile << szSep ;
if ( bEndL)
m_OutFile << endl ;
}
}
//----------------------------------------------------------------------------
void
NgeWriter::WriteString( const std::string& sVal, const char* szSep, bool bEndL)
{
if ( ! m_OutFile.is_open())
return ;
if ( m_bBinary) {
int nDim = int( sVal.size()) ;
m_OutFile.write( (char*) &nDim, sizeof( nDim)) ;
m_OutFile.write( sVal.c_str(), sVal.size()) ;
}
else {
m_OutFile << sVal ;
if ( szSep != nullptr)
m_OutFile << szSep ;
if ( bEndL)
m_OutFile << endl ;
}
}
//----------------------------------------------------------------------------
void
NgeWriter::WriteVector( const Vector3d& vtV, const char* szSep, bool bEndL, int nPrec)
{
if ( ! m_OutFile.is_open())
return ;
if ( m_bBinary)
m_OutFile.write( (char*) &vtV.v, sizeof( vtV.v)) ;
else {
m_OutFile << ToString( vtV, nPrec) ;
if ( szSep != nullptr)
m_OutFile << szSep ;
if ( bEndL)
m_OutFile << endl ;
}
}
//----------------------------------------------------------------------------
void
NgeWriter::WritePoint( const Point3d& ptP, const char* szSep, bool bEndL, int nPrec)
{
if ( ! m_OutFile.is_open())
return ;
if ( m_bBinary)
m_OutFile.write( (char*) &ptP.v, sizeof( ptP.v)) ;
else {
m_OutFile << ToString( ptP, nPrec) ;
if ( szSep != nullptr)
m_OutFile << szSep ;
if ( bEndL)
m_OutFile << endl ;
}
}
//----------------------------------------------------------------------------
void
NgeWriter::WritePointW( const Point3d& ptP, double dW, const char* szSep, bool bEndL, int nPrecP, int nPrecW)
{
if ( ! m_OutFile.is_open())
return ;
if ( m_bBinary) {
m_OutFile.write( (char*) &ptP.v, sizeof( ptP.v)) ;
m_OutFile.write( (char*) &dW, sizeof( dW)) ;
}
else {
m_OutFile << ToString( ptP, dW, nPrecP, nPrecW) ;
if ( szSep != nullptr)
m_OutFile << szSep ;
if ( bEndL)
m_OutFile << endl ;
}
}
//----------------------------------------------------------------------------
void
NgeWriter::WriteFrame( const Frame3d& frF, const char* szSep, bool bEndL, int nPrecP, int nPrecV)
{
if ( ! m_OutFile.is_open())
return ;
if ( m_bBinary) {
m_OutFile.write( (char*) &frF.Orig().v, sizeof( frF.Orig().v)) ;
m_OutFile.write( (char*) &frF.VersX().v, sizeof( frF.VersX().v)) ;
m_OutFile.write( (char*) &frF.VersY().v, sizeof( frF.VersY().v)) ;
m_OutFile.write( (char*) &frF.VersZ().v, sizeof( frF.VersZ().v)) ;
}
else {
m_OutFile << ToString( frF, nPrecP, nPrecV) ;
if ( szSep != nullptr)
m_OutFile << szSep ;
if ( bEndL)
m_OutFile << endl ;
}
}
//----------------------------------------------------------------------------
void
NgeWriter::WriteKey( int nKey)
{
if ( ! m_OutFile.is_open())
return ;
if ( nKey < 0 || nKey > NGE_LAST_ID)
return ;
if ( m_bBinary)
m_OutFile.write( (char*) &NgeBinKeyW[nKey], sizeof( int)) ;
else {
m_OutFile << NgeAscKeyW[nKey] ;
m_OutFile << endl ;
}
}
//----------------------------------------------------------------------------
void
NgeWriter::WriteCol( const Color& cCol, const char* szSep, bool bEndL)
{
if ( ! m_OutFile.is_open())
return ;
if ( m_bBinary) {
unsigned char ucCol[4] ;
ucCol[0] = cCol.GetIntRed() ;
ucCol[1] = cCol.GetIntGreen() ;
ucCol[2] = cCol.GetIntBlue() ;
ucCol[3] = cCol.GetIntAlpha() ;
m_OutFile.write( (char*) ucCol, sizeof( ucCol)) ;
}
else {
m_OutFile << ToString( cCol) ;
if ( szSep != nullptr)
m_OutFile << szSep ;
if ( bEndL)
m_OutFile << endl ;
}
}