From: eap Date: Fri, 12 Mar 2010 08:26:13 +0000 (+0000) Subject: /*! X-Git-Tag: V5_1_4a1~37 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=22395fddea3ed022c43f74f69e560d597b885cc2;p=modules%2Fsmesh.git /*! * \brief High level util for effective file reading and other file operations */ --- diff --git a/src/SMESH/Makefile.am b/src/SMESH/Makefile.am index b286778f9..795ae129c 100644 --- a/src/SMESH/Makefile.am +++ b/src/SMESH/Makefile.am @@ -54,6 +54,7 @@ salomeinclude_HEADERS = \ SMESH_OctreeNode.hxx \ SMESH_Comment.hxx \ SMESH_ComputeError.hxx \ + SMESH_File.hxx \ SMESH_SMESH.hxx # Libraries targets @@ -77,7 +78,8 @@ dist_libSMESHimpl_la_SOURCES = \ SMESH_HypoFilter.cxx \ SMESH_MesherHelper.cxx \ SMESH_Octree.cxx \ - SMESH_OctreeNode.cxx + SMESH_OctreeNode.cxx \ + SMESH_File.cxx # additionnal information to compile and link file libSMESHimpl_la_CPPFLAGS = \ diff --git a/src/SMESH/SMESH_File.cxx b/src/SMESH/SMESH_File.cxx new file mode 100644 index 000000000..5338a6b7f --- /dev/null +++ b/src/SMESH/SMESH_File.cxx @@ -0,0 +1,219 @@ +// File : SMESH_File.cxx +// Created : Wed Mar 10 11:23:25 2010 +// Author : Edward AGAPOV (eap) + + +#include "SMESH_File.hxx" +#include "utilities.h" + +#include +#include +#include +#include + +#include +#include + +#ifdef WIN32 +#include +#else +#include +#endif + +//================================================================================ +/*! + * \brief Creator opening the file for reading by default + */ +//================================================================================ + +SMESH_File::SMESH_File(const std::string& name, bool open) + :_name(name), _size(-1), _file(0), _map(0), _pos(0), _end(0) +{ + if ( open ) this->open(); +} + +//================================================================================ +/*! + * \brief Destructor closing the file + */ +//================================================================================ + +SMESH_File::~SMESH_File() +{ + close(); +} + +//================================================================================ +/*! + * \brief Open file for reading. Return true if there is something to read + */ +//================================================================================ + +bool SMESH_File::open() +{ + 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; +} + +//================================================================================ +/*! + * \brief Close the file + */ +//================================================================================ + +void SMESH_File::close() +{ + 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; + } +} + +//================================================================================ +/*! + * \brief Remove the file + */ +//================================================================================ + +bool SMESH_File::remove() +{ + close(); + try { + OSD_File( TCollection_AsciiString((char*)_name.data()) ).Remove(); + } + catch ( Standard_ProgramError ) { + MESSAGE("Can't remove file: " << _name << " ; file does not exist or permission denied"); + return false; + } + return true; +} + +//================================================================================ +/*! + * \brief Return file size + */ +//================================================================================ + +int SMESH_File::size() const +{ + 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; +} + +//================================================================================ +/*! + * \brief Set cursor to the given position + */ +//================================================================================ + +void SMESH_File::setPos(const char* pos) +{ + if ( pos > (const char*)_map && pos < _end ) + _pos = (char*) pos; +} + +//================================================================================ +/*! + * \brief Skip till current line end and return the skipped string + */ +//================================================================================ + +std::string SMESH_File::getLine() +{ + std::string line; + const char* p = _pos; + while ( !eof() ) + if ( *(++_pos) == '\n' ) + break; + line.append( p, _pos ); + if ( !eof() ) _pos++; + return line; +} + +//================================================================================ +/*! + * \brief Move cursor to the file beginning + */ +//================================================================================ + +void SMESH_File::rewind() +{ + _pos = (char*) _map; +} + +//================================================================================ +/*! + * \brief Fill vector by reading out integers from file. Vector size gives number + * of integers to read + */ +//================================================================================ + +bool SMESH_File::getInts(std::vector& ints) +{ + 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() ); +} diff --git a/src/SMESH/SMESH_File.hxx b/src/SMESH/SMESH_File.hxx new file mode 100644 index 000000000..b1045e331 --- /dev/null +++ b/src/SMESH/SMESH_File.hxx @@ -0,0 +1,92 @@ +// Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE +// +// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// File : SMESH_File.hxx +// Created : Wed Mar 10 10:33:04 2010 +// Author : Edward AGAPOV (eap) + + +#ifndef __SMESH_File_HXX__ +#define __SMESH_File_HXX__ + +#include "SMESH_SMESH.hxx" + +#include +#include + +/*! + * \brief High level util for effective file reading and other file operations + */ +class SMESH_File +{ +public: + + SMESH_File(const std::string& name, bool open=true); + + ~SMESH_File(); + + std::string getName() const { return _name; } + + bool open(); + + void close(); + + bool remove(); + + int size() const; + + // ------------------------ + // Access to file contents + // ------------------------ + + operator const char*() const { return _pos; } + + bool operator++() { return ++_pos < _end; } + + void operator +=(int posDelta) { _pos+=posDelta; } + + bool eof() const { return _pos >= _end; } + + const char* getPos() const { return _pos; } + + void setPos(const char* pos); + + std::string getLine(); + + void rewind(); + + bool getInts(std::vector& ids); + +private: + + std::string _name; //!< file name + int _size; //!< file size +#ifdef WNT + HANDLE _file, _mapObj; +#else + int _file; +#endif + void* _map; + const char* _pos; //!< current position + const char* _end; //!< position after file end +}; + +#endif