-// Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
//
// 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.
+// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// Created : Wed Mar 10 11:23:25 2010
// Author : Edward AGAPOV (eap)
//
-#include "SMESH_File.hxx"
-#include "utilities.h"
-#include <OSD_File.hxx>
-#include <OSD_Path.hxx>
-#include <Standard_ProgramError.hxx>
-#include <Standard_ErrorHandler.hxx>
-#include <Standard_Failure.hxx>
+#include "SMESH_File.hxx"
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#endif
+#include <boost/filesystem.hpp>
+
+namespace boofs = boost::filesystem;
+
//================================================================================
/*!
* \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)
+ :_name(name), _size(-1),
+#ifdef WIN32
+ _file(INVALID_HANDLE_VALUE),
+#else
+ _file(-1),
+#endif
+ _map(0), _pos(0), _end(0)
{
if ( open ) this->open();
}
bool SMESH_File::open()
{
- int length = size();
+ long length = size();
if ( !_map && length > 0 )
{
-#ifdef WNT
+#ifdef WIN32
_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 );
+ bool ok = ( _file >= 0 );
#endif
if ( ok )
{
-#ifdef WNT
+#ifdef WIN32
_mapObj = CreateFileMapping(_file, NULL, PAGE_READONLY, 0, (DWORD)length, NULL);
_map = (void*) MapViewOfFile( _mapObj, FILE_MAP_READ, 0, 0, 0 );
#else
}
else
{
-#ifdef WNT
+#ifdef WIN32
CloseHandle(_mapObj);
CloseHandle(_file);
#else
#endif
}
}
+ else if ( _error.empty() )
+ {
+ _error = "Can't open for reading an existing file " + _name;
+ }
}
return _pos;
}
{
if ( _map != NULL )
{
-#ifdef WNT
+#ifdef WIN32
UnmapViewOfFile(_map);
CloseHandle(_mapObj);
CloseHandle(_file);
_pos = _end = 0;
_size = -1;
}
+ else if ( _file >= 0 )
+ {
+#ifdef WIN32
+ if(_file != INVALID_HANDLE_VALUE) {
+ CloseHandle(_file);
+ _file = INVALID_HANDLE_VALUE;
+ }
+#else
+ if(_file != -1) {
+ ::close(_file);
+ _file = -1;
+ }
+#endif
+ }
}
//================================================================================
bool SMESH_File::remove()
{
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;
+
+ boost::system::error_code err;
+ boofs::remove( _name, err );
+ _error = err.message();
+
+ return !err;
}
//================================================================================
*/
//================================================================================
-int SMESH_File::size() const
+long SMESH_File::size()
{
- if ( _size >= 0 ) return _size; // size of open file
+ if ( _size >= 0 ) return _size; // size of an 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;
+ boost::system::error_code err;
+ boost::uintmax_t size = boofs::file_size( _name, err );
+ _error = err.message();
+
+ return !err ? (long) size : -1;
+}
+
+//================================================================================
+/*!
+ * \brief Check existence
+ */
+//================================================================================
+
+bool SMESH_File::exists()
+{
+ boost::system::error_code err;
+ bool res = boofs::exists( _name, err );
+ _error = err.message();
+
+ return err ? false : res;
+}
+
+//================================================================================
+/*!
+ * \brief Check existence
+ */
+//================================================================================
+
+bool SMESH_File::isDirectory()
+{
+ boost::system::error_code err;
+ bool res = boofs::is_directory( _name, err );
+ _error = err.message();
+
+ return err ? false : res;
}
//================================================================================
bool SMESH_File::getInts(std::vector<int>& ints)
{
- int i = 0;
+ size_t i = 0;
while ( i < ints.size() )
{
while ( !isdigit( *_pos ) && !eof()) ++_pos;
}
return ( i == ints.size() );
}
+
+//================================================================================
+/*!
+ * \brief Open for binary writing only.
+ */
+//================================================================================
+
+bool SMESH_File::openForWriting()
+{
+#ifdef WIN32
+
+ _file = CreateFile( _name.c_str(), // name of the write
+ GENERIC_WRITE, // open for writing
+ 0, // do not share
+ NULL, // default security
+ OPEN_ALWAYS, // CREATE NEW or OPEN EXISTING
+ FILE_ATTRIBUTE_NORMAL, // normal file
+ NULL); // no attr. template
+ return ( _file != INVALID_HANDLE_VALUE );
+
+#else
+
+ _file = ::open( _name.c_str(),
+ O_WRONLY | O_CREAT,
+ S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ); // rw-r--r--
+ return _file >= 0;
+
+#endif
+}
+
+//================================================================================
+/*!
+ * \brief Write binary data
+ */
+//================================================================================
+
+bool SMESH_File::writeRaw(const void* data, size_t size)
+{
+#ifdef WIN32
+
+ DWORD nbWritten = 0;
+ BOOL err = WriteFile( _file, data, size, & nbWritten, NULL);
+
+ return (( err == FALSE ) &&
+ ( nbWritten == (DWORD) size ));
+
+#else
+
+ ssize_t nbWritten = ::write( _file, data, size );
+ return ( nbWritten == (ssize_t) size );
+
+#endif
+}