1 // Copyright (C) 2007-2021 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 #include <Basics_Utils.hxx>
41 namespace boofs = boost::filesystem;
43 //================================================================================
45 * \brief Creator opening the file for reading by default
47 //================================================================================
49 SMESH_File::SMESH_File(const std::string& name, bool open)
50 :_name(name), _size(-1),
52 _file(INVALID_HANDLE_VALUE),
56 _map(0), _pos(0), _end(0)
58 if ( open ) this->open();
61 //================================================================================
63 * \brief Destructor closing the file
65 //================================================================================
67 SMESH_File::~SMESH_File()
72 //================================================================================
74 * \brief Open file for reading. Return true if there is something to read
76 //================================================================================
78 bool SMESH_File::open()
81 if ( !_map && length > 0 )
85 std::wstring aName = Kernel_Utils::utf8_decode_s(_name);
86 const wchar_t* name = aName.c_str();
88 char* name = _name.data();
90 _file = CreateFile(name, GENERIC_READ, FILE_SHARE_READ,
91 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
92 bool ok = ( _file != INVALID_HANDLE_VALUE );
94 _file = ::open(_name.data(), O_RDONLY );
95 bool ok = ( _file >= 0 );
100 _mapObj = CreateFileMapping(_file, NULL, PAGE_READONLY, 0, (DWORD)length, NULL);
101 _map = (void*) MapViewOfFile( _mapObj, FILE_MAP_READ, 0, 0, 0 );
103 _map = ::mmap(0,length,PROT_READ,MAP_PRIVATE,_file,0);
104 if ( _map == MAP_FAILED ) _map = NULL;
115 CloseHandle(_mapObj);
122 else if ( _error.empty() )
124 _error = "Can't open for reading an existing file " + _name;
130 //================================================================================
132 * \brief Close the file
134 //================================================================================
136 void SMESH_File::close()
141 UnmapViewOfFile(_map);
142 CloseHandle(_mapObj);
145 ::munmap(_map, _size);
152 else if ( _file >= 0 )
155 if(_file != INVALID_HANDLE_VALUE) {
157 _file = INVALID_HANDLE_VALUE;
168 //================================================================================
170 * \brief Remove the file
172 //================================================================================
174 bool SMESH_File::remove()
178 boost::system::error_code err;
179 #if defined(WIN32) && defined(UNICODE)
180 std::wstring name = Kernel_Utils::utf8_decode_s(_name);
181 boofs::remove(name.c_str(), err);
183 boofs::remove( _name, err );
185 _error = err.message();
190 //================================================================================
192 * \brief Return file size
194 //================================================================================
196 long SMESH_File::size()
198 if ( _size >= 0 ) return _size; // size of an open file
200 boost::system::error_code err;
201 #if defined(WIN32) && defined(UNICODE)
202 std::wstring name = Kernel_Utils::utf8_decode_s(_name);
203 boost::uintmax_t size = boofs::file_size(name.c_str(), err);
205 boost::uintmax_t size = boofs::file_size( _name, err );
207 _error = err.message();
209 return !err ? (long) size : -1;
212 //================================================================================
214 * \brief Check existence
216 //================================================================================
218 bool SMESH_File::exists()
220 boost::system::error_code err;
221 #if defined(WIN32) && defined(UNICODE)
222 std::wstring name = Kernel_Utils::utf8_decode_s(_name);
223 bool res = boofs::exists(name.c_str(), err);
225 bool res = boofs::exists(_name, err);
228 _error = err.message();
230 return err ? false : res;
233 //================================================================================
235 * \brief Check existence
237 //================================================================================
239 bool SMESH_File::isDirectory()
241 boost::system::error_code err;
242 #if defined(WIN32) && defined(UNICODE)
243 std::wstring name = Kernel_Utils::utf8_decode_s(_name);
244 bool res = boofs::is_directory(name.c_str(), err);
246 bool res = boofs::is_directory( _name, err );
248 _error = err.message();
250 return err ? false : res;
253 //================================================================================
255 * \brief Set cursor to the given position
257 //================================================================================
259 void SMESH_File::setPos(const char* pos)
261 if ( pos > (const char*)_map && pos < _end )
265 //================================================================================
267 * \brief Skip till current line end and return the skipped string
269 //================================================================================
271 std::string SMESH_File::getLine()
274 const char* p = _pos;
276 if ( *(++_pos) == '\n' )
278 line.append( p, _pos );
279 if ( !eof() ) _pos++;
283 //================================================================================
285 * \brief Move cursor to the file beginning
287 //================================================================================
289 void SMESH_File::rewind()
294 //================================================================================
296 * \brief Fill vector by reading out integers from file. Vector size gives number
297 * of integers to read
299 //================================================================================
301 bool SMESH_File::getInts(std::vector<int>& ints)
304 while ( i < ints.size() )
306 while ( !isdigit( *_pos ) && !eof()) ++_pos;
308 if ( _pos[-1] == '-' ) --_pos;
309 ints[ i++ ] = strtol( _pos, (char**)&_pos, 10 );
311 return ( i == ints.size() );
314 //================================================================================
316 * \brief Open for binary writing only.
318 //================================================================================
320 bool SMESH_File::openForWriting()
326 std::wstring aName = Kernel_Utils::utf8_decode_s(_name);
327 const wchar_t* name = aName.c_str();
329 char* name = _name.data();
331 _file = CreateFile( name, // name of the write
332 GENERIC_WRITE, // open for writing
334 NULL, // default security
335 OPEN_ALWAYS, // CREATE NEW or OPEN EXISTING
336 FILE_ATTRIBUTE_NORMAL, // normal file
337 NULL); // no attr. template
338 return ( _file != INVALID_HANDLE_VALUE );
341 _file = ::open( _name.c_str(),
343 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ); // rw-r--r--
349 //================================================================================
351 * \brief Write binary data
353 //================================================================================
355 bool SMESH_File::writeRaw(const void* data, size_t size)
360 BOOL err = WriteFile( _file, data, size, & nbWritten, NULL);
362 return (( err == FALSE ) &&
363 ( nbWritten == (DWORD) size ));
367 ssize_t nbWritten = ::write( _file, data, size );
368 return ( nbWritten == (ssize_t) size );