Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef VTK_MESH_DRIVER_HXX
00024 #define VTK_MESH_DRIVER_HXX
00025
00026 #include <cstring>
00027
00028 #include "MEDMEM.hxx"
00029
00030 #include <string>
00031 #include <vector>
00032 #include "MEDMEM_define.hxx"
00033 #include "MEDMEM_GenDriver.hxx"
00034
00035 #include "MEDMEM_STRING.hxx"
00036 #include "MEDMEM_Exception.hxx"
00037 #include "MEDMEM_Utilities.hxx"
00038
00039 #include <fstream>
00040
00041
00049 namespace MEDMEM {
00050 class GMESH;
00051 class FAMILY;
00052 class GROUP;
00053 class CONNECTIVITY;
00054 class MEDMEM_EXPORT _VTK_BinaryWriter;
00055
00056 class MEDMEM_EXPORT VTK_MESH_DRIVER : public GENDRIVER
00057 {
00058 protected:
00059
00060 const GMESH * _ptrMesh;
00061 std::string _meshName;
00062 mutable std::ofstream * _vtkFile ;
00063 mutable _VTK_BinaryWriter* _binaryFile;
00064
00065 public :
00066
00070 VTK_MESH_DRIVER() ;
00074 VTK_MESH_DRIVER(const std::string & fileName, const GMESH * ptrMesh) ;
00078 VTK_MESH_DRIVER(const VTK_MESH_DRIVER & driver) ;
00079
00083 ~VTK_MESH_DRIVER() ;
00084
00085 void open() ;
00086 void close() ;
00087
00088 void openConst() const throw (MEDEXCEPTION);
00089 void closeConst() const throw (MEDEXCEPTION);
00090
00091 void write( void ) const throw (MEDEXCEPTION) ;
00092 void read ( void ) throw (MEDEXCEPTION) ;
00093
00099 void setMeshName(const string & meshName) ;
00103 string getMeshName() const ;
00104
00105 private:
00106 GENDRIVER * copy ( void ) const;
00107
00108 template <typename T>
00109 void writeBinary(const T* data, int nbValues) const throw (MEDEXCEPTION);
00110 };
00111
00112 class MEDMEM_EXPORT _VTK_BinaryWriter
00113 {
00114 std::string _fileName;
00115 int _binaryFile;
00116 public:
00117 _VTK_BinaryWriter(const std::string file);
00118 bool open(bool append=false) const;
00119 bool close() const;
00120
00121 template <typename T>
00122 void write(const T* data, int nbValues) const throw (MEDEXCEPTION)
00123 {
00124
00125
00126
00127
00128
00129
00130 const void* toWrite = (const void* ) data;
00131 T* swappedData = 0;
00132 if ( sizeof(T) != sizeof(char))
00133 {
00134
00135 toWrite = (const void* )( swappedData = new T[ nbValues ]);
00136 memcpy( swappedData, data, nbValues * sizeof(T));
00137 int* intBuf = ((int*) swappedData) - 1;
00138 int* bufEnd = (int*)((char*) swappedData + nbValues * sizeof(T));
00139 while ( ++intBuf < bufEnd )
00140 *intBuf = swapBytes( *intBuf );
00141 }
00142 #ifdef WNT
00143 ssize_t nbWritten = ::_write( _binaryFile, toWrite, nbValues * sizeof(T));
00144 #else
00145 ssize_t nbWritten = ::write( _binaryFile, toWrite, nbValues * sizeof(T));
00146 #endif
00147 if ( swappedData )
00148 delete [] swappedData;
00149 if ( nbWritten < 0 )
00150 throw MEDEXCEPTION(LOCALIZED(STRING("_VTK_BinaryWriter::Failed to write into ")<< _fileName));
00151 }
00152 };
00153
00154 template <typename T>
00155 void VTK_MESH_DRIVER::writeBinary(const T* data, int nbValues) const throw (MEDEXCEPTION)
00156 {
00157 _binaryFile->write( data, nbValues );
00158 }
00159
00160 }
00161
00162
00163 #endif