Back to index

salome-smesh  6.5.0
SMESH_File.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 // File      : SMESH_File.cxx
00021 // Created   : Wed Mar 10 11:23:25 2010
00022 // Author    : Edward AGAPOV (eap)
00023 //
00024 #include "SMESH_File.hxx"
00025 #include "utilities.h"
00026 
00027 #include <OSD_File.hxx>
00028 #include <OSD_Path.hxx>
00029 #include <Standard_ProgramError.hxx>
00030 #include <Standard_ErrorHandler.hxx>
00031 #include <Standard_Failure.hxx>
00032 
00033 #include <fcntl.h>
00034 #include <sys/stat.h>
00035 
00036 #ifdef WIN32
00037 #include <io.h>
00038 #else
00039 #include <sys/mman.h>
00040 #endif
00041 
00042 //================================================================================
00046 //================================================================================
00047 
00048 SMESH_File::SMESH_File(const std::string& name, bool open)
00049   :_name(name), _size(-1), _file(0), _map(0), _pos(0), _end(0)
00050 {
00051   if ( open ) this->open();
00052 }
00053 
00054 //================================================================================
00058 //================================================================================
00059 
00060 SMESH_File::~SMESH_File()
00061 {
00062   close();
00063 }
00064 
00065 //================================================================================
00069 //================================================================================
00070 
00071 bool SMESH_File::open()
00072 {
00073   int length = size();
00074   if ( !_map && length > 0 )
00075   {
00076 #ifdef WNT
00077     _file = CreateFile(_name.data(), GENERIC_READ, FILE_SHARE_READ,
00078                        NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
00079     bool ok = ( _file != INVALID_HANDLE_VALUE );
00080 #else
00081     _file = ::open(_name.data(), O_RDONLY );
00082     bool ok = ( _file > 0 );
00083 #endif
00084     if ( ok )
00085     {
00086 #ifdef WNT
00087       _mapObj = CreateFileMapping(_file, NULL, PAGE_READONLY, 0, (DWORD)length, NULL);
00088       _map = (void*) MapViewOfFile( _mapObj, FILE_MAP_READ, 0, 0, 0 );
00089 #else
00090       _map = ::mmap(0,length,PROT_READ,MAP_PRIVATE,_file,0);
00091       if ( _map == MAP_FAILED ) _map = NULL;
00092 #endif
00093       if ( _map != NULL )
00094       {
00095         _size = length;
00096         _pos = (char*) _map;
00097         _end = _pos + _size;
00098       }
00099       else
00100       {
00101 #ifdef WNT
00102         CloseHandle(_mapObj);
00103         CloseHandle(_file);
00104 #else
00105         ::close(_file);
00106 #endif
00107       }
00108     }
00109   }
00110   return _pos;
00111 }
00112 
00113 //================================================================================
00117 //================================================================================
00118 
00119 void SMESH_File::close()
00120 {
00121   if ( _map != NULL )
00122   {
00123 #ifdef WNT
00124     UnmapViewOfFile(_map);
00125     CloseHandle(_mapObj);
00126     CloseHandle(_file);
00127 #else
00128     ::munmap(_map, _size);
00129     ::close(_file);
00130 #endif
00131     _map = NULL;
00132     _pos = _end = 0;
00133     _size = -1;
00134   }
00135 }
00136 
00137 //================================================================================
00141 //================================================================================
00142 
00143 bool SMESH_File::remove()
00144 {
00145   close();
00146   try {
00147     OSD_Path filePath(TCollection_AsciiString((char*)_name.data()));
00148     OSD_File(filePath).Remove();
00149   }
00150   catch ( Standard_ProgramError ) {
00151     MESSAGE("Can't remove file: " << _name << " ; file does not exist or permission denied");
00152     return false;
00153   }
00154   return true;
00155 }
00156 
00157 //================================================================================
00161 //================================================================================
00162 
00163 int SMESH_File::size() const
00164 {
00165   if ( _size >= 0 ) return _size; // size of open file
00166 
00167   int size = -1;
00168   int file = ::open( _name.data(), O_RDONLY );
00169   if ( file > 0 )
00170   {
00171     struct stat status;
00172     int err = fstat( file, &status);
00173     if ( !err )
00174       size = status.st_size;
00175     ::close( file );
00176   }
00177   return size;
00178 }
00179 
00180 //================================================================================
00184 //================================================================================
00185 
00186 void SMESH_File::setPos(const char* pos)
00187 {
00188   if ( pos > (const char*)_map && pos < _end )
00189     _pos = (char*) pos;
00190 }
00191 
00192 //================================================================================
00196 //================================================================================
00197 
00198 std::string SMESH_File::getLine()
00199 {
00200   std::string line;
00201   const char* p = _pos;
00202   while ( !eof() )
00203     if ( *(++_pos) == '\n' )
00204       break;
00205   line.append( p, _pos );
00206   if ( !eof() ) _pos++;
00207   return line;
00208 }
00209 
00210 //================================================================================
00214 //================================================================================
00215 
00216 void SMESH_File::rewind()
00217 {
00218   _pos = (char*) _map;
00219 }
00220 
00221 //================================================================================
00226 //================================================================================
00227 
00228 bool SMESH_File::getInts(std::vector<int>& ints)
00229 {
00230   int i = 0;
00231   while ( i < ints.size() )
00232   {
00233     while ( !isdigit( *_pos ) && !eof()) ++_pos;
00234     if ( eof() ) break;
00235     if ( _pos[-1] == '-' ) --_pos;
00236     ints[ i++ ] = strtol( _pos, (char**)&_pos, 10 );
00237   }
00238   return ( i == ints.size() );
00239 }