1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : SMESH_File.cxx
21 // Created : Wed Mar 10 11:23:25 2010
22 // Author : Edward AGAPOV (eap)
25 #include "SMESH_File.hxx"
37 #include <boost/filesystem.hpp>
39 namespace boofs = boost::filesystem;
41 //================================================================================
43 * \brief Creator opening the file for reading by default
45 //================================================================================
47 SMESH_File::SMESH_File(const std::string& name, bool open)
48 :_name(name), _size(-1), _file(0), _map(0), _pos(0), _end(0)
50 if ( open ) this->open();
53 //================================================================================
55 * \brief Destructor closing the file
57 //================================================================================
59 SMESH_File::~SMESH_File()
64 //================================================================================
66 * \brief Open file for reading. Return true if there is something to read
68 //================================================================================
70 bool SMESH_File::open()
73 if ( !_map && length > 0 )
76 _file = CreateFile(_name.data(), GENERIC_READ, FILE_SHARE_READ,
77 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
78 bool ok = ( _file != INVALID_HANDLE_VALUE );
80 _file = ::open(_name.data(), O_RDONLY );
81 bool ok = ( _file > 0 );
86 _mapObj = CreateFileMapping(_file, NULL, PAGE_READONLY, 0, (DWORD)length, NULL);
87 _map = (void*) MapViewOfFile( _mapObj, FILE_MAP_READ, 0, 0, 0 );
89 _map = ::mmap(0,length,PROT_READ,MAP_PRIVATE,_file,0);
90 if ( _map == MAP_FAILED ) _map = NULL;
101 CloseHandle(_mapObj);
112 //================================================================================
114 * \brief Close the file
116 //================================================================================
118 void SMESH_File::close()
123 UnmapViewOfFile(_map);
124 CloseHandle(_mapObj);
127 ::munmap(_map, _size);
134 else if ( _file >= 0 )
138 _file = INVALID_HANDLE_VALUE;
146 //================================================================================
148 * \brief Remove the file
150 //================================================================================
152 bool SMESH_File::remove()
156 boost::system::error_code err;
157 boofs::remove( _name, err );
158 _error = err.message();
163 //================================================================================
165 * \brief Return file size
167 //================================================================================
169 long SMESH_File::size()
171 if ( _size >= 0 ) return _size; // size of an open file
173 boost::system::error_code err;
174 boost::uintmax_t size = boofs::file_size( _name, err );
175 _error = err.message();
177 return err ? -1 : (long) size;
180 //================================================================================
182 * \brief Check existence
184 //================================================================================
186 bool SMESH_File::exists()
188 boost::system::error_code err;
189 bool res = boofs::exists( _name, err );
190 _error = err.message();
192 return err ? false : res;
195 //================================================================================
197 * \brief Check existence
199 //================================================================================
201 bool SMESH_File::isDirectory()
203 boost::system::error_code err;
204 bool res = boofs::is_directory( _name, err );
205 _error = err.message();
207 return err ? false : res;
210 //================================================================================
212 * \brief Set cursor to the given position
214 //================================================================================
216 void SMESH_File::setPos(const char* pos)
218 if ( pos > (const char*)_map && pos < _end )
222 //================================================================================
224 * \brief Skip till current line end and return the skipped string
226 //================================================================================
228 std::string SMESH_File::getLine()
231 const char* p = _pos;
233 if ( *(++_pos) == '\n' )
235 line.append( p, _pos );
236 if ( !eof() ) _pos++;
240 //================================================================================
242 * \brief Move cursor to the file beginning
244 //================================================================================
246 void SMESH_File::rewind()
251 //================================================================================
253 * \brief Fill vector by reading out integers from file. Vector size gives number
254 * of integers to read
256 //================================================================================
258 bool SMESH_File::getInts(std::vector<int>& ints)
261 while ( i < ints.size() )
263 while ( !isdigit( *_pos ) && !eof()) ++_pos;
265 if ( _pos[-1] == '-' ) --_pos;
266 ints[ i++ ] = strtol( _pos, (char**)&_pos, 10 );
268 return ( i == ints.size() );
271 //================================================================================
273 * \brief Open for binary writing only.
275 //================================================================================
277 bool SMESH_File::openForWriting()
281 _file = CreateFile( _name.c_str(), // name of the write
282 GENERIC_WRITE, // open for writing
284 NULL, // default security
285 OPEN_ALWAYS, // CREATE NEW or OPEN EXISTING
286 FILE_ATTRIBUTE_NORMAL, // normal file
287 NULL); // no attr. template
288 return ( _file != INVALID_HANDLE_VALUE );
292 _file = ::open( _name.c_str(),
294 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ); // rw-r--r--
300 //================================================================================
302 * \brief Write binary data
304 //================================================================================
306 bool SMESH_File::writeRaw(const void* data, size_t size)
311 BOOL err = WriteFile( _file, data, size, & nbWritten, NULL);
313 return (( err == FALSE ) &&
314 ( nbWritten == (DWORD) size ));
318 ssize_t nbWritten = ::write( _file, data, size );
319 return ( nbWritten == size );