Version: 6.3.1
Public Member Functions | Private Attributes

SMESH_File Class Reference

High level util for effective file reading and other file operations. More...

#include <SMESH_File.hxx>

Public Member Functions

 SMESH_File (const std::string &name, bool open=true)
 Creator opening the file for reading by default.
 ~SMESH_File ()
 Destructor closing the file.
std::string getName () const
bool open ()
 Open file for reading.
void close ()
 Close the file.
bool remove ()
 Remove the file.
int size () const
 Return file size.
 operator const char * () const
bool operator++ ()
void operator+= (int posDelta)
bool eof () const
const char * getPos () const
void setPos (const char *pos)
 Set cursor to the given position.
std::string getLine ()
 Skip till current line end and return the skipped string.
void rewind ()
 Move cursor to the file beginning.
bool getInts (std::vector< int > &ids)
 Fill vector by reading out integers from file.

Private Attributes

std::string _name
 file name
int _size
 file size
int _file
void * _map
const char * _pos
 current position
const char * _end
 position after file end

Detailed Description

High level util for effective file reading and other file operations.

Definition at line 41 of file SMESH_File.hxx.


Constructor & Destructor Documentation

SMESH_File::SMESH_File ( const std::string &  name,
bool  open = true 
)

Creator opening the file for reading by default.

Definition at line 48 of file SMESH_File.cxx.

References open().

  :_name(name), _size(-1), _file(0), _map(0), _pos(0), _end(0)
{
  if ( open ) this->open();
}
SMESH_File::~SMESH_File ( )

Destructor closing the file.

Definition at line 60 of file SMESH_File.cxx.

References close().

{
  close();
}

Member Function Documentation

void SMESH_File::close ( )

Close the file.

Definition at line 119 of file SMESH_File.cxx.

References _end, _file, _map, _pos, and _size.

Referenced by open(), remove(), size(), and ~SMESH_File().

{
  if ( _map != NULL )
  {
#ifdef WNT
    UnmapViewOfFile(_map);
    CloseHandle(_mapObj);
    CloseHandle(_file);
#else
    ::munmap(_map, _size);
    ::close(_file);
#endif
    _map = NULL;
    _pos = _end = 0;
    _size = -1;
  }
}
bool SMESH_File.eof ( ) const

Definition at line 69 of file SMESH_File.hxx.

Referenced by getInts(), and getLine().

{ return _pos >= _end; }
bool SMESH_File::getInts ( std::vector< int > &  ints)

Fill vector by reading out integers from file.

Vector size gives number of integers to read

Definition at line 228 of file SMESH_File.cxx.

References _pos, and eof().

{
  int i = 0;
  while ( i < ints.size() )
  {
    while ( !isdigit( *_pos ) && !eof()) ++_pos;
    if ( eof() ) break;
    if ( _pos[-1] == '-' ) --_pos;
    ints[ i++ ] = strtol( _pos, (char**)&_pos, 10 );
  }
  return ( i == ints.size() );
}
std::string SMESH_File::getLine ( )

Skip till current line end and return the skipped string.

Definition at line 198 of file SMESH_File.cxx.

References _pos, and eof().

{
  std::string line;
  const char* p = _pos;
  while ( !eof() )
    if ( *(++_pos) == '\n' )
      break;
  line.append( p, _pos );
  if ( !eof() ) _pos++;
  return line;
}
std::string SMESH_File.getName ( ) const

Definition at line 49 of file SMESH_File.hxx.

{ return _name; }
const char* SMESH_File.getPos ( ) const

Definition at line 71 of file SMESH_File.hxx.

{ return _pos; }
bool SMESH_File::open ( )

Open file for reading.

Return true if there is something to read

Definition at line 71 of file SMESH_File.cxx.

References _end, _file, _map, _name, _pos, _size, close(), and size().

Referenced by size(), and SMESH_File().

{
  int length = size();
  if ( !_map && length > 0 )
  {
#ifdef WNT
    _file = CreateFile(_name.data(), GENERIC_READ, FILE_SHARE_READ,
                       NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    bool ok = ( _file != INVALID_HANDLE_VALUE );
#else
    _file = ::open(_name.data(), O_RDONLY );
    bool ok = ( _file > 0 );
#endif
    if ( ok )
    {
#ifdef WNT
      _mapObj = CreateFileMapping(_file, NULL, PAGE_READONLY, 0, (DWORD)length, NULL);
      _map = (void*) MapViewOfFile( _mapObj, FILE_MAP_READ, 0, 0, 0 );
#else
      _map = ::mmap(0,length,PROT_READ,MAP_PRIVATE,_file,0);
      if ( _map == MAP_FAILED ) _map = NULL;
#endif
      if ( _map != NULL )
      {
        _size = length;
        _pos = (char*) _map;
        _end = _pos + _size;
      }
      else
      {
#ifdef WNT
        CloseHandle(_mapObj);
        CloseHandle(_file);
#else
        ::close(_file);
#endif
      }
    }
  }
  return _pos;
}
SMESH_File.operator const char * ( ) const

Definition at line 63 of file SMESH_File.hxx.

{ return _pos; }
bool SMESH_File.operator++ ( )

Definition at line 65 of file SMESH_File.hxx.

{ return ++_pos < _end; }
void SMESH_File.operator+= ( int  posDelta)

Definition at line 67 of file SMESH_File.hxx.

{ _pos+=posDelta; }
bool SMESH_File::remove ( )

Remove the file.

Definition at line 143 of file SMESH_File.cxx.

References _name, close(), SMESH_flight_skin.filePath, and MESSAGE.

{
  close();
  try {
    OSD_Path filePath(TCollection_AsciiString((char*)_name.data()));
    OSD_File(filePath).Remove();
  }
  catch ( Standard_ProgramError ) {
    MESSAGE("Can't remove file: " << _name << " ; file does not exist or permission denied");
    return false;
  }
  return true;
}
void SMESH_File::rewind ( )

Move cursor to the file beginning.

Definition at line 216 of file SMESH_File.cxx.

References _map, and _pos.

{
  _pos = (char*) _map;
}
void SMESH_File::setPos ( const char *  pos)

Set cursor to the given position.

Definition at line 186 of file SMESH_File.cxx.

References _end, _map, and _pos.

{
  if ( pos > (const char*)_map && pos < _end )
    _pos = (char*) pos;
}
int SMESH_File::size ( ) const

Return file size.

Definition at line 163 of file SMESH_File.cxx.

References _name, _size, close(), and open().

Referenced by open().

{
  if ( _size >= 0 ) return _size; // size of open file

  int size = -1;
  int file = ::open( _name.data(), O_RDONLY );
  if ( file > 0 )
  {
    struct stat status;
    int err = fstat( file, &status);
    if ( !err )
      size = status.st_size;
    ::close( file );
  }
  return size;
}

Field Documentation

const char* SMESH_File._end [private]

position after file end

Definition at line 92 of file SMESH_File.hxx.

Referenced by close(), open(), and setPos().

int SMESH_File._file [private]

Definition at line 88 of file SMESH_File.hxx.

Referenced by close(), and open().

void* SMESH_File._map [private]

Definition at line 90 of file SMESH_File.hxx.

Referenced by close(), open(), rewind(), and setPos().

std::string SMESH_File._name [private]

file name

Definition at line 83 of file SMESH_File.hxx.

Referenced by open(), remove(), and size().

const char* SMESH_File._pos [private]

current position

Definition at line 91 of file SMESH_File.hxx.

Referenced by close(), getInts(), getLine(), open(), rewind(), and setPos().

int SMESH_File._size [private]

file size

Definition at line 84 of file SMESH_File.hxx.

Referenced by close(), open(), and size().

Copyright © 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
Copyright © 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS