Salome HOME
Copyright update 2022
[modules/smesh.git] / src / SMESHUtils / SMESH_File.cxx
index b2549e76750cf76e2fa6b46f7395c043eb9d649e..9e3d7526314af709832941d0e16a57fcb838943e 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2022  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
 // 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>
+
+#include <Basics_Utils.hxx>
+
+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();
 }
@@ -71,16 +77,22 @@ SMESH_File::~SMESH_File()
 
 bool SMESH_File::open()
 {
-  int length = size();
+  long length = size();
   if ( !_map && length > 0 )
   {
 #ifdef WIN32
-    _file = CreateFile(_name.data(), GENERIC_READ, FILE_SHARE_READ,
+#  ifdef UNICODE
+    std::wstring  aName = Kernel_Utils::utf8_decode_s(_name);
+    const wchar_t* name = aName.c_str();
+#  else
+    char* name = _name.data();
+#  endif
+    _file = CreateFile(name, 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 )
     {
@@ -107,6 +119,10 @@ bool SMESH_File::open()
 #endif
       }
     }
+    else if ( _error.empty() )
+    {
+      _error = "Can't open for reading an existing file " + _name;
+    }
   }
   return _pos;
 }
@@ -133,6 +149,20 @@ void SMESH_File::close()
     _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
+  }
 }
 
 //================================================================================
@@ -144,15 +174,17 @@ void SMESH_File::close()
 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;
+#if defined(WIN32) && defined(UNICODE)
+  std::wstring name = Kernel_Utils::utf8_decode_s(_name);
+  boofs::remove(name.c_str(), err);
+#else
+  boofs::remove( _name, err );
+#endif
+  _error = err.message();
+
+  return !err;
 }
 
 //================================================================================
@@ -161,21 +193,61 @@ bool SMESH_File::remove()
  */
 //================================================================================
 
-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;
+#if defined(WIN32) && defined(UNICODE)
+  std::wstring name = Kernel_Utils::utf8_decode_s(_name);
+  boost::uintmax_t size = boofs::file_size(name.c_str(), err);
+#else
+  boost::uintmax_t size = boofs::file_size( _name, err );
+#endif
+  _error = err.message();
+
+  return !err ? (long) size : -1;
+}
+
+//================================================================================
+/*!
+ * \brief Check existence
+ */
+//================================================================================
+
+bool SMESH_File::exists()
+{
+  boost::system::error_code err;
+#if defined(WIN32) && defined(UNICODE)
+  std::wstring name = Kernel_Utils::utf8_decode_s(_name);
+  bool res = boofs::exists(name.c_str(), err);
+#else
+  bool res = boofs::exists(_name, err);
+#endif
+
+  _error = err.message();
+
+  return err ? false : res;
+}
+
+//================================================================================
+/*!
+ * \brief Check existence
+ */
+//================================================================================
+
+bool SMESH_File::isDirectory()
+{
+  boost::system::error_code err;
+#if defined(WIN32) && defined(UNICODE)
+  std::wstring name = Kernel_Utils::utf8_decode_s(_name);
+  bool res = boofs::is_directory(name.c_str(), err);
+#else
+  bool res = boofs::is_directory( _name, err );
+#endif
+  _error = err.message();
+
+  return err ? false : res;
 }
 
 //================================================================================
@@ -228,7 +300,7 @@ void SMESH_File::rewind()
 
 bool SMESH_File::getInts(std::vector<int>& ints)
 {
-  int i = 0;
+  size_t i = 0;
   while ( i < ints.size() )
   {
     while ( !isdigit( *_pos ) && !eof()) ++_pos;
@@ -238,3 +310,62 @@ bool SMESH_File::getInts(std::vector<int>& ints)
   }
   return ( i == ints.size() );
 }
+
+//================================================================================
+/*!
+ * \brief Open for binary writing only.
+ */
+//================================================================================
+
+bool SMESH_File::openForWriting()
+{
+  close();
+
+#ifdef WIN32
+#ifdef UNICODE
+  std::wstring  aName = Kernel_Utils::utf8_decode_s(_name);
+  const wchar_t* name = aName.c_str();
+#else
+  char* name = _name.data();
+#endif
+  _file = CreateFile( name,                   // 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
+}