Back to index

salome-med  6.5.0
Public Member Functions | Private Member Functions | Private Attributes
MEDMEM_ENSIGHT::_BinaryFileReader Class Reference

Reader of binary files. More...

#include <MEDMEM_EnsightUtils.hxx>

List of all members.

Public Member Functions

 _BinaryFileReader (const string &fileName) throw (MEDEXCEPTION)
 Constructor of _BinaryFileReader opens its file.
 ~_BinaryFileReader ()
 Destructor of _BinaryFileWriter closes its file.
void rewind ()
 rewind the file backward
void swapBytes ()
int moreValuesAvailable () const
 size of not read file portion in sizeof(int)
bool eof ()
 Return true if the whole file has been read.
void skip (int size) throw (MEDEXCEPTION)
 Skips given nb of bytes.
void skipTimeStepBeginning () throw (MEDEXCEPTION)
 Read lines until TIME_STEP_BEG encounters.
char * getLine () throw (MEDEXCEPTION)
int * getInt (int nb) throw (MEDEXCEPTION)
float * getFlt (int nb) throw (MEDEXCEPTION)
ssize_t getPosition () const
template<class T >
char * convertReals (const int nbValues, const char *undefValue=0, set< int > *undefIndices=0, const vector< int > *partialIndices=0, const int nbPartialComponents=0) throw (MEDEXCEPTION)

Private Member Functions

template<typename T >
T * get (int nb, bool inverseBytes=false)

Private Attributes

int _file
 descriptor
MEDEXCEPTION _exception
 ready to raise exception
ssize_t _pos
ssize_t _maxPos
 current position and end position
bool _mySwapBytes
 to swap bytes

Detailed Description

Reader of binary files.

Definition at line 713 of file MEDMEM_EnsightUtils.hxx.


Constructor & Destructor Documentation

MEDMEM_ENSIGHT::_BinaryFileReader::_BinaryFileReader ( const string &  fileName) throw (MEDEXCEPTION)

Constructor of _BinaryFileReader opens its file.

Definition at line 2779 of file MEDMEM_EnsightUtils.cxx.

  : _exception(STRING("Unexpected EOF ") << fileName), _mySwapBytes(false)
{
#ifdef WNT
  _file = ::_open (fileName.c_str(), _O_RDONLY|_O_BINARY);
#else
  _file = ::open (fileName.c_str(), O_RDONLY);
#endif

  if (_file < 0)
    throw MEDEXCEPTION(STRING("Can't read from ") << fileName);

  _maxPos = ::lseek( _file, 0, SEEK_END);
  _pos    = ::lseek( _file, 0, SEEK_SET);
}

Destructor of _BinaryFileWriter closes its file.

Definition at line 2801 of file MEDMEM_EnsightUtils.cxx.

{
  if (_file >= 0)
    ::close (_file);
}

Member Function Documentation

template<class T >
char* MEDMEM_ENSIGHT::_BinaryFileReader::convertReals ( const int  nbValues,
const char *  undefValue = 0,
set< int > *  undefIndices = 0,
const vector< int > *  partialIndices = 0,
const int  nbPartialComponents = 0 
) throw (MEDEXCEPTION) [inline]

Definition at line 745 of file MEDMEM_EnsightUtils.hxx.

  {
    T* result = new T[ nbValues ];
    T* ptrT = result, *endT = result + nbValues;
    int nb = partialIndices ? partialIndices->size() * nbPartialComponents : nbValues;
    TFltOwner fltData( getFlt( nb ));
    float* ptrFlt = fltData;
    if ( undefValue ) // fill undefIndices
    {
      undefIndices->clear();
      float undef = atof( undefValue );
      while ( ptrT < endT ) {
        float value = *ptrFlt++;
        *ptrT++ = (T) value;
        if ( std::abs( value - undef ) <= FLT_MIN )
          undefIndices->insert( undefIndices->end(), ptrT - result );
      }
    }
    else if ( partialIndices )
    {
      // partial variables are available in GOLD format only where
      // values are in no-interlace
      int shift = 1;
      for ( int j = 1; j <= nbPartialComponents; ++j ) {
        vector<int>::const_iterator i = partialIndices->begin(), iEnd = partialIndices->end();
        while ( i != iEnd )
          result[ *i++ - shift ] = (T) *ptrFlt++;
        shift += nbValues;
      }
    }
    else
    {
      while ( ptrT < endT )
        *ptrT++ = (T) *ptrFlt++;
    }
    return (char*) result;
  }

Return true if the whole file has been read.

Definition at line 2835 of file MEDMEM_EnsightUtils.cxx.

{
  return _pos >= _maxPos;
}
template<typename T >
T* MEDMEM_ENSIGHT::_BinaryFileReader::get ( int  nb,
bool  inverseBytes = false 
) [inline, private]

Definition at line 795 of file MEDMEM_EnsightUtils.hxx.

  {
    size_t bufSize = nb * sizeof( T );
    if ( int(bufSize) > _maxPos - _pos )
      throw _exception;
    T* buf = new T[ nb ];
#ifdef WNT
#else
    ssize_t nBytesRead = ::read (_file, buf, bufSize );
    _pos += nBytesRead;
    if ( int(nBytesRead) < int(bufSize) ) {
      delete buf;
      throw _exception;
    }
    if ( inverseBytes ) { // swap bytes
      int* intBuf = ((int*) buf) - 1;
      int* bufEnd = (int*)((char*) buf + nBytesRead);
      while ( ++intBuf < bufEnd )
        *intBuf = MEDMEM::swapBytes( *intBuf );
    }
#endif
    return buf;
  }

Here is the call graph for this function:

float* MEDMEM_ENSIGHT::_BinaryFileReader::getFlt ( int  nb) throw (MEDEXCEPTION) [inline]

Definition at line 739 of file MEDMEM_EnsightUtils.hxx.

  { return get<float>(nb,_mySwapBytes); }
int* MEDMEM_ENSIGHT::_BinaryFileReader::getInt ( int  nb) throw (MEDEXCEPTION) [inline]

Definition at line 736 of file MEDMEM_EnsightUtils.hxx.

  { return get<int>(nb,_mySwapBytes); }
char* MEDMEM_ENSIGHT::_BinaryFileReader::getLine ( ) throw (MEDEXCEPTION) [inline]

Definition at line 733 of file MEDMEM_EnsightUtils.hxx.

  { return get<char>(80); }

Here is the caller graph for this function:

Definition at line 742 of file MEDMEM_EnsightUtils.hxx.

{ return _pos; }

size of not read file portion in sizeof(int)

Definition at line 2824 of file MEDMEM_EnsightUtils.cxx.

{
  return (_maxPos - _pos) / sizeof(int); // believe that sizeof(int) == sizeof(float)
}

rewind the file backward

Definition at line 2813 of file MEDMEM_EnsightUtils.cxx.

{
  _pos = ::lseek( _file, 0, SEEK_SET);
}
void MEDMEM_ENSIGHT::_BinaryFileReader::skip ( int  size) throw (MEDEXCEPTION)

Skips given nb of bytes.

Definition at line 2846 of file MEDMEM_EnsightUtils.cxx.

{
  if ( _pos + size > _maxPos )
    throw _exception;
  off_t newPos = ::lseek( _file, size, SEEK_CUR);
  if ( newPos < _pos + size )
    throw _exception;
  _pos = newPos;
}

Read lines until TIME_STEP_BEG encounters.

Definition at line 2862 of file MEDMEM_EnsightUtils.cxx.

{
  bool tsbReached = false;
  while ( !tsbReached ) {
    TStrOwner line( getLine() );
    tsbReached = ( strncmp( line, TIME_STEP_BEG, TIME_STEP_BEG_LEN ) == 0 );
  }
}

Here is the call graph for this function:

Definition at line 722 of file MEDMEM_EnsightUtils.hxx.

  { _mySwapBytes = true; }

Member Data Documentation

ready to raise exception

Definition at line 790 of file MEDMEM_EnsightUtils.hxx.

descriptor

Definition at line 789 of file MEDMEM_EnsightUtils.hxx.

current position and end position

Definition at line 791 of file MEDMEM_EnsightUtils.hxx.

to swap bytes

read any data from file

Definition at line 792 of file MEDMEM_EnsightUtils.hxx.

Definition at line 791 of file MEDMEM_EnsightUtils.hxx.


The documentation for this class was generated from the following files: