Back to index

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

Reader of ASCII files. More...

#include <MEDMEM_EnsightUtils.hxx>

List of all members.

Public Member Functions

 _ASCIIFileReader (const string &fileName) throw (MEDEXCEPTION)
 Constructor of ASCIIFileReader.
 ~_ASCIIFileReader ()
 Destructor of ASCIIFileReader closes its file.
bool eof ()
 Return true if the whole file has been read.
string getWord ()
 never throws
int getInt () throw (MEDEXCEPTION)
float getReal () throw (MEDEXCEPTION)
 needed after getWord(), getInt() or getReal() to get a next line
void toNextLine ()
char * getLine () throw (MEDEXCEPTION)
 Read a next line from the file.
const char * getCurrentPtr () const
bool lookAt (const char *text)
bool isTimeStepBeginning ()
 Return true if TIME_STEP_BEG follows and skips TIME_STEP_BEG line.
bool isTimeStepEnd ()
 read out given data
void skip (int nbVals, int nbPerLine, int valWidth)
 read out width chars and nbLines line-ends
void skip (int width, int nbLines)
 read out width chars and nbLines line-ends
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)

Static Public Member Functions

static int split (const string &str, string &part1, string &part2, const char separator=' ', const bool fromBack=false)
 < divide a string into two parts
static int split (const string &str, std::list< string > &parts, const char separator=' ', const bool fromBack=false)
 check if string contains only digits
static bool isDigit (const string &str, const bool real=false)
 check if a string contains only digits

Private Attributes

int _file
char * _start
char * _ptr
char * _eptr
bool _isWin

Detailed Description

Reader of ASCII files.

Definition at line 594 of file MEDMEM_EnsightUtils.hxx.


Constructor & Destructor Documentation

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

Constructor of ASCIIFileReader.

Definition at line 2395 of file MEDMEM_EnsightUtils.cxx.

{
#ifdef WNT
  _file = ::_open (fileName.c_str(), _O_RDONLY|_O_BINARY);
#else
  _file = ::open (fileName.c_str(), O_RDONLY);
#endif
  if (_file >= 0)
  {
    _start = new char [BUFFER_SIZE];
    _ptr   = _start;
    _eptr  = _start;
  }
  else
  {
    throw MEDEXCEPTION(STRING("Can't read from ")<<fileName);
  }
  if ( eof() )
    throw MEDEXCEPTION(STRING("Empty file ")<<fileName);

  // there must be end-of-line in ASCII file
  char* ptr = _start + MAX_LINE_LENGTH;
  bool isASCII = false;
  while ( !isASCII && ptr >= _start )
    isASCII = (*ptr-- == '\n');
  _isWin = ( *ptr == '\r');
  if ( !isASCII )
    throw MEDEXCEPTION(STRING("Not ASCII file ")<<fileName);
}

Destructor of ASCIIFileReader closes its file.

Definition at line 2764 of file MEDMEM_EnsightUtils.cxx.

{
  if (_file >= 0)
  {
    ::close (_file);
    delete [] _start;
  }
}

Member Function Documentation

template<class T >
char* MEDMEM_ENSIGHT::_ASCIIFileReader::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 637 of file MEDMEM_EnsightUtils.hxx.

  {
    T* result = new T[ nbValues ];
    T* ptrT = result;
    if ( undefValue ) // fill undefIndices
    {
      undefIndices->clear();
      float undef = atof( undefValue );
      for ( int i = 0; i < nbValues; ++i, ++ptrT ) {
        float value = getReal();
        (*ptrT) = (T) value;
        if ( value == undef )
          undefIndices->insert( undefIndices->end(), i+1 );
      }
    }
    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) getReal();
        shift += nbValues;
      }
    }
    else
    {
      for ( int i = 0; i < nbValues; ++i, ++ptrT )
        (*ptrT) = (T) getReal();
    }
    return (char*) result;
  }

Return true if the whole file has been read.

Definition at line 2431 of file MEDMEM_EnsightUtils.cxx.

{
  // Check the state of the buffer;
  // if there is too little left, read the next portion of data
  int nBytesRest = _eptr - _ptr;
  if (nBytesRest < 2 * MAX_LINE_LENGTH)
  {
    if (nBytesRest > 0) {
      char* tmpBuf = new char [nBytesRest];
      memcpy (tmpBuf, _ptr, nBytesRest);
      memcpy (_start, tmpBuf, nBytesRest);
      delete [] tmpBuf;
    } else {
      nBytesRest = 0;
    }
    _ptr = _start;
    const int nBytesRead = ::read (_file,
                                   &_start [nBytesRest],
                                   BUFFER_SIZE - nBytesRest);
    nBytesRest += nBytesRead;
    _eptr = &_start [nBytesRest];

    // skip spaces at file end
    if ( nBytesRest < MAX_LINE_LENGTH ) {
      while ( isspace( *_ptr )) _ptr++;
      nBytesRest = _eptr - _ptr;
    }
  }
  return nBytesRest < 1;
}

Here is the caller graph for this function:

const char* MEDMEM_ENSIGHT::_ASCIIFileReader::getCurrentPtr ( ) const [inline]

Definition at line 622 of file MEDMEM_EnsightUtils.hxx.

{ return _ptr; }
int MEDMEM_ENSIGHT::_ASCIIFileReader::getInt ( ) throw (MEDEXCEPTION) [inline]

Definition at line 605 of file MEDMEM_EnsightUtils.hxx.

                                        {
    if ( eof() ) throw MEDEXCEPTION("Unexpected EOF");
    return strtol(_ptr, &_ptr, 10);
  }
char * MEDMEM_ENSIGHT::_ASCIIFileReader::getLine ( ) throw (MEDEXCEPTION)

Read a next line from the file.

Definition at line 2503 of file MEDMEM_EnsightUtils.cxx.

{
  if ( eof() )
    throw MEDEXCEPTION("Unexpected EOF");

  // Check the buffer for the end-of-line
  char * ptr = _ptr;
  while (true)
  {
    // Check for end-of-the-buffer, the ultimate criterion for termination
    if (ptr >= _eptr)
    {
      //_eptr[-1] = '\0';
      _eptr[0] = '\0';
      break;
    }
    // seek the line-feed character
    if (ptr[0] == '\n')
    {
      if ( ptr > _start && // avoid "Invalid read" error by valgrind
           ptr[-1] == '\r')
        ptr[-1] = '\0';
      ptr[0] = '\0';
      ++ptr;
      break;
    }
    ++ptr;
  }
  // Output the result
  char * line = _ptr;
  _ptr = ptr;

  return line;
}

Here is the call graph for this function:

Here is the caller graph for this function:

float MEDMEM_ENSIGHT::_ASCIIFileReader::getReal ( ) throw (MEDEXCEPTION) [inline]

needed after getWord(), getInt() or getReal() to get a next line

Definition at line 609 of file MEDMEM_EnsightUtils.hxx.

never throws

Read a next word from the file.

Definition at line 2556 of file MEDMEM_EnsightUtils.cxx.

{
  if ( eof() )
    return "";

  // skip spaces
  while ( isspace(*_ptr)) ++_ptr;
  if ( _ptr >= _eptr )
    return "";

  // skip not spaces
  char* word = _ptr++;
  while ( !isspace(*_ptr)) ++_ptr;

  return string( word, _ptr - word );
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool MEDMEM_ENSIGHT::_ASCIIFileReader::isDigit ( const string &  str,
const bool  real = false 
) [static]

check if a string contains only digits

Definition at line 2739 of file MEDMEM_EnsightUtils.cxx.

{
  const char* s = str.c_str();
  if ( real ) {
    while ( *s ) {
      char c = *s++;
      if ( !isdigit(c) && c!='-' && c!='+' && c!='.' && c!=',' && c!='E' && c!='e')
        return false;
    }
  }
  else {
    while ( *s ) {
      if ( !isdigit( *s++ ))
        return false;
    }
  }
  return true;
}

Here is the caller graph for this function:

Return true if TIME_STEP_BEG follows and skips TIME_STEP_BEG line.

Definition at line 2579 of file MEDMEM_EnsightUtils.cxx.

{
  if ( eof() ) throw MEDEXCEPTION(LOCALIZED("Unexpected EOF"));

  while ( isspace(*_ptr)) ++_ptr;

  if ( strncmp( _ptr, TIME_STEP_BEG, TIME_STEP_BEG_LEN ) != 0 )
    return false;

  _ptr += TIME_STEP_BEG_LEN;
  while ( isspace(*_ptr)) ++_ptr;
  return true;
}

Here is the call graph for this function:

read out given data

Return true if TIME_STEP_END follows and skips TIME_STEP_END line.

Definition at line 2599 of file MEDMEM_EnsightUtils.cxx.

{
  if ( eof() ) return true;

  while ( isspace(*_ptr)) ++_ptr;

  if ( strncmp( _ptr, TIME_STEP_END, TIME_STEP_END_LEN ) != 0 )
    return false;

  _ptr += TIME_STEP_END_LEN;
  while ( isspace(*_ptr)) ++_ptr;
  return true;
}

Here is the call graph for this function:

bool MEDMEM_ENSIGHT::_ASCIIFileReader::lookAt ( const char *  text)

Definition at line 2544 of file MEDMEM_EnsightUtils.cxx.

{
  while ( isspace(*_ptr)) ++_ptr;
  return ( strncmp( _ptr, text, strlen( text )) == 0 );
}
void MEDMEM_ENSIGHT::_ASCIIFileReader::skip ( int  nbVals,
int  nbPerLine,
int  valWidth 
)

read out width chars and nbLines line-ends

read out given data

Definition at line 2468 of file MEDMEM_EnsightUtils.cxx.

{
  int nbLines = (nbVals + nbPerLine - 1) / nbPerLine;
  int width = nbVals * valWidth;
  skip( width, nbLines );
}
void MEDMEM_ENSIGHT::_ASCIIFileReader::skip ( int  width,
int  nbLines 
)

read out width chars and nbLines line-ends

Definition at line 2481 of file MEDMEM_EnsightUtils.cxx.

{
  width += nbLines; // to skip '\n'
  if ( _isWin )
    width += nbLines; // to skip '\r'

  _ptr += width;
  int nBytesRest = _eptr - _ptr;
  while ( nBytesRest < 0 ) {
    width = -nBytesRest;
    if ( eof() ) return;
    _ptr += width;
    nBytesRest = _eptr - _ptr;
  }
}

Here is the call graph for this function:

int MEDMEM_ENSIGHT::_ASCIIFileReader::split ( const string &  str,
string &  part1,
string &  part2,
const char  separator = ' ',
const bool  fromBack = false 
) [static]

< divide a string into two parts

divide a string into two parts

divide a string into parts, return nb of parts

Definition at line 2620 of file MEDMEM_EnsightUtils.cxx.

{
  int nbParts = 0;
  string parts[2];
  const char* ptr1 = str.c_str();
  const char* back = ptr1 + str.size();
  for (nbParts = 0; nbParts < 2; ++nbParts ) {
    // skip spaces before the current part
    while ( isspace(*ptr1)) ++ptr1;
    if ( !*ptr1) break;
    // find end of the part and beginning of the next part
    const char* ptr2 = ptr1;
    const char* nextBeg = back;
    if ( nbParts > 0 ) {
      ptr2 = back;
    }
    else if ( fromBack ) {
      ptr2 = back;
      string::size_type pos = str.rfind( separator );
      if ( pos != str.npos ) {
        ptr2 = & str[ pos ];
        nextBeg = ptr2 + 1;
        if ( separator != ' ')
          while ( *nextBeg && *nextBeg == separator ) ++nextBeg;
      }
    }
    else if ( separator == ' ' ) {
      while ( *ptr2 && !isspace(*ptr2)) ++ptr2;
      if ( *ptr2 ) nextBeg = ptr2 + 1;
    }
    else {
      while ( *ptr2 && *ptr2 != separator ) ++ptr2;
      if ( *ptr2 ) {
        nextBeg = ptr2 + 1;
        while ( *nextBeg && *nextBeg == separator ) ++nextBeg;
      }
    }
    //if ( !*ptr2) --ptr2;
    // skip spaces after the current part
    while ( ptr2 > ptr1 && isspace(ptr2[-1])) --ptr2;
    parts[ nbParts ] = string( ptr1, ptr2-ptr1 );
    ptr1 = nextBeg;
  }
  part1 = parts[0];
  part2 = parts[1];
  return nbParts;
}

Here is the caller graph for this function:

int MEDMEM_ENSIGHT::_ASCIIFileReader::split ( const string &  str,
std::list< string > &  parts,
const char  separator = ' ',
const bool  fromBack = false 
) [static]

check if string contains only digits

divide a string into parts, return nb of parts

Definition at line 2678 of file MEDMEM_EnsightUtils.cxx.

{
  parts.clear();
  if ( str.empty() )
    return 0;
  int nbParts = 0;
  const char* ptr1 = str.c_str();
  const char* back = ptr1 + str.size();
  if ( fromBack ) {
    swap( ptr1, back );
    while (1) {
      // skip spaces after the current part
      while ( isspace(ptr1[-1])) --ptr1;
      if ( ptr1 <= back ) break;
      // find beginning of the part
      const char* ptr2 = ptr1 - 1;
      if ( separator == ' ' )
        while ( ptr2 > back && !isspace(ptr2[-1])) --ptr2;
      else
        while ( ptr2 > back && ptr2[-1] != separator ) --ptr2;
      //if ( !*ptr2) --ptr2;
      const char* sepPtr = ptr2;
      // skip spaces before the current part
      while ( isspace(*ptr2)) ++ptr2;
      parts.push_back( string( ptr2, ptr1-ptr2 ));
      ++nbParts;
      ptr1 = sepPtr - 1;
    }
  }
  else {
    while (1) {
      // skip spaces before the current part
      while ( isspace(*ptr1)) ++ptr1;
      if ( ptr1 >= back) break;
      // find end of the part
      const char* ptr2 = ptr1 + 1;
      if ( separator == ' ' )
        while ( *ptr2 && !isspace(*ptr2)) ++ptr2;
      else
        while ( *ptr2 && *ptr2 != separator ) ++ptr2;
      //if ( !*ptr2) --ptr2;
      const char* sepPtr = ptr2;
      // skip spaces after the current part
      while ( ptr2 > ptr1 && isspace(ptr2[-1])) --ptr2;
      parts.push_back( string( ptr1, ptr2-ptr1 ));
      ++nbParts;
      ptr1 = sepPtr + int( sepPtr < back );
    }
  }
  return nbParts;
}

Here is the call graph for this function:

Definition at line 617 of file MEDMEM_EnsightUtils.hxx.

                    {
    while (isspace(*_ptr)) if ((++_ptr)[-1]=='\n') break;
  }

Member Data Documentation

Definition at line 700 of file MEDMEM_EnsightUtils.hxx.

Definition at line 697 of file MEDMEM_EnsightUtils.hxx.

Definition at line 702 of file MEDMEM_EnsightUtils.hxx.

Definition at line 699 of file MEDMEM_EnsightUtils.hxx.

Definition at line 698 of file MEDMEM_EnsightUtils.hxx.


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