Salome HOME
Fix a problem with the zero file descriptor.
[modules/smesh.git] / src / SMESHUtils / SMESH_File.cxx
index d75cd108adc87461108262cbca7bdc22a3cc4c99..d4311bb71ef29464b7b926b689e2f1bbbc5dfa61 100644 (file)
@@ -45,7 +45,13 @@ namespace boofs = boost::filesystem;
 //================================================================================
 
 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();
 }
@@ -78,7 +84,7 @@ bool SMESH_File::open()
     bool ok = ( _file != INVALID_HANDLE_VALUE );
 #else
     _file = ::open(_name.data(), O_RDONLY );
-    bool ok = ( _file > 0 );
+    bool ok = ( _file >= 0 );
 #endif
     if ( ok )
     {
@@ -105,6 +111,10 @@ bool SMESH_File::open()
 #endif
       }
     }
+    else if ( _error.empty() )
+    {
+      _error = "Can't open for reading an existing file " + _name;
+    }
   }
   return _pos;
 }
@@ -134,11 +144,15 @@ void SMESH_File::close()
   else if ( _file >= 0 )
   {
 #ifdef WIN32
-    CloseHandle(_file);
-    _file = INVALID_HANDLE_VALUE;
+    if(_file != INVALID_HANDLE_VALUE) {
+      CloseHandle(_file);
+      _file = INVALID_HANDLE_VALUE;
+    }
 #else
-    ::close(_file);
-    _file = -1;
+    if(_file != -1) {
+      ::close(_file);
+      _file = -1;
+    }
 #endif
   }
 }