Salome HOME
updated copyright message
[modules/kernel.git] / src / Container / Salome_file_i.cxx
index a098c06f95707253605a0f90d4e6810891ec9f07..68606a4518a21b65b6f2c1a590521e548a350650 100644 (file)
@@ -1,34 +1,46 @@
-// Copyright (C) 2007  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
-// 
-//  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. 
-// 
-//  This library is distributed in the hope that it will be useful, 
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of 
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
-//  Lesser General Public License for more details. 
-// 
-//  You should have received a copy of the GNU Lesser General Public 
-//  License along with this library; if not, write to the Free Software 
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
-// 
-// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+// Copyright (C) 2007-2023  CEA, EDF, 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, 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
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
 //
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 //  File   : Salome_file_i.cxx
 //  Author : AndrĂ© RIBES, EDF
 //  Module : SALOME
 //  $Header: 
-
+//
 #include "Salome_file_i.hxx"
 #include "utilities.h"
 #include <stdlib.h>
-#include <unistd.h>
 #include "HDFOI.hxx"
-#include <stdlib.h>
+#ifndef WIN32
+# include <unistd.h>
+# define _getcwd getcwd
+# define _open   open
+#else
+# include <direct.h>
+# include <io.h>
+# include <windows.h>
+#endif
+
+/*! \class Salome_file_i
+ *  \brief A class to manage file transfer in %SALOME
+ *
+ */
+
 
 //=============================================================================
 /*! 
 Salome_file_i::Salome_file_i()
 {
   _fileId = 0;
+#ifndef WIN32
   _path_max = 1 + pathconf("/", _PC_PATH_MAX);
+#else
+  _path_max = 32768;
+  //from MSDN:
+  //Note The C Runtime supports path lengths up to 32768 characters in length, but it is up to the operating system, specifically the file system, to support these longer paths. The sum of the fields should not exceed _MAX_PATH for full backwards compatibility with Windows 98 FAT32 file systems. Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003, and Windows Server 2003 NTFS file system supports paths up to 32768 characters in length, but only when using the Unicode APIs. When using long path names, prefix the path with the characters \\?\ and use the Unicode versions of the C Runtime functions.
+  //currently #define _MAX_PATH   260
+#endif
   _state.name = CORBA::string_dup("");
   _state.hdf5_file_name = CORBA::string_dup("");
   _state.number_of_files = 0;
@@ -72,7 +91,7 @@ Salome_file_i::load(const char* hdf5_file) {
     HDFfile *hdf_file;
     HDFgroup *hdf_group;
     HDFdataset *hdf_dataset;
-    int size;
+    size_t size;
     int fd;
     char * value;
     char * buffer;
@@ -154,68 +173,66 @@ Salome_file_i::load(const char* hdf5_file) {
 
       if (mode == "all") {
 
-       // Changing path, is now current directory
-       char CurrentPath[_path_max];
-       getcwd(CurrentPath, _path_max);
-       path = CurrentPath;
-
-       std::string group_name("GROUP");
-       group_name += file_name;
-       hdf_group = new HDFgroup(group_name.c_str(),hdf_file); 
-       hdf_group->OpenOnDisk();
-       hdf_dataset = new HDFdataset("FILE DATASET",hdf_group);
-       hdf_dataset->OpenOnDisk();
-       size = hdf_dataset->GetSize();
-       buffer = new char[size];
+        // Changing path, is now current directory
+        path = getcwd(NULL, _path_max);
+  
+        std::string group_name("GROUP");
+        group_name += file_name;
+        hdf_group = new HDFgroup(group_name.c_str(),hdf_file); 
+        hdf_group->OpenOnDisk();
+        hdf_dataset = new HDFdataset("FILE DATASET",hdf_group);
+        hdf_dataset->OpenOnDisk();
+        size = hdf_dataset->GetSize();
+        buffer = new char[size];
       
-       if ( (fd = ::open(file_name.c_str(),O_RDWR|O_CREAT,00666)) <0) { 
-         SALOME::ExceptionStruct es;
-         es.type = SALOME::INTERNAL_ERROR;
-         std::string text = "open failed";
-         es.text = CORBA::string_dup(text.c_str());
-         throw SALOME::SALOME_Exception(es);
-       };
-       hdf_dataset->ReadFromDisk(buffer);
-       if ( write(fd,buffer,size) <0) { 
-         SALOME::ExceptionStruct es;
-         es.type = SALOME::INTERNAL_ERROR;
-         std::string text = "write failed";
-         es.text = CORBA::string_dup(text.c_str());
-         throw SALOME::SALOME_Exception(es);
-       };
-       // Close the target file
-       ::close(fd);
-
-       Engines::file infos;
-       infos.file_name = CORBA::string_dup(file_name.c_str());
-       infos.path = CORBA::string_dup(path.c_str());
-       infos.type = CORBA::string_dup(type.c_str());
-       infos.source_file_name = CORBA::string_dup(source_file_name.c_str());
-       infos.status = CORBA::string_dup(status.c_str());
-
-       _fileManaged[file_name] = infos;
-
-       // Update Salome_file state
-       _state.number_of_files++;
-       _state.files_ok = true;
+        if ( (fd = ::open(file_name.c_str(),O_RDWR|O_CREAT,00666)) <0) { 
+          SALOME::ExceptionStruct es;
+          es.type = SALOME::INTERNAL_ERROR;
+          std::string text = "open failed";
+          es.text = CORBA::string_dup(text.c_str());
+          throw SALOME::SALOME_Exception(es);
+        }
+        hdf_dataset->ReadFromDisk(buffer);
+        if ( write(fd,buffer,(unsigned int)size) <0) {
+          SALOME::ExceptionStruct es;
+          es.type = SALOME::INTERNAL_ERROR;
+          std::string text = "write failed";
+          es.text = CORBA::string_dup(text.c_str());
+          throw SALOME::SALOME_Exception(es);
+        }
+        // Close the target file
+        ::close(fd);
+
+        Engines::file infos;
+        infos.file_name = CORBA::string_dup(file_name.c_str());
+        infos.path = CORBA::string_dup(path.c_str());
+        infos.type = CORBA::string_dup(type.c_str());
+        infos.source_file_name = CORBA::string_dup(source_file_name.c_str());
+        infos.status = CORBA::string_dup(status.c_str());
+
+        _fileManaged[file_name] = infos;
+
+        // Update Salome_file state
+        _state.number_of_files++;
+        _state.files_ok = true;
       }
       else {
-       Engines::file infos;
-       infos.file_name = CORBA::string_dup(file_name.c_str());
-       infos.path = CORBA::string_dup(path.c_str());
-       infos.type = CORBA::string_dup(type.c_str());
-       infos.source_file_name = CORBA::string_dup(source_file_name.c_str());
-       infos.status = CORBA::string_dup(status.c_str());
-       // Infos for parallel extensions...
-       infos.node = 0;
-       infos.container = Engines::Container::_duplicate(_container);
-
-       _fileManaged[file_name] = infos;
-
-       // Update Salome_file state
-       _state.number_of_files++;
-       if (status != "ok")
-         _state.files_ok = false;
+        Engines::file infos;
+        infos.file_name = CORBA::string_dup(file_name.c_str());
+        infos.path = CORBA::string_dup(path.c_str());
+        infos.type = CORBA::string_dup(type.c_str());
+        infos.source_file_name = CORBA::string_dup(source_file_name.c_str());
+        infos.status = CORBA::string_dup(status.c_str());
+        // Infos for parallel extensions...
+        infos.node = 0;
+        infos.container = Engines::Container::_duplicate(_container);
+
+        _fileManaged[file_name] = infos;
+
+        // Update Salome_file state
+        _state.number_of_files++;
+        if (status != "ok")
+          _state.files_ok = false;
       }
     }
   }
@@ -356,7 +373,7 @@ Salome_file_i::save_all(const char* hdf5_file) {
   }
 
   // For each file we create two groups
-  // First group contains file's informations
+  // First group contains file's information
   // Second group contains the file
   // At the end we create a group and a dataset containing the names
   // of all the files.
@@ -484,17 +501,15 @@ Salome_file_i::setLocalFile(const char* comp_file_name)
 
   std::string cp_file_name(comp_file_name);
   std::size_t index = cp_file_name.rfind("/");
-  if (index != -1)
+  if (index != std::string::npos)
   {
     file_name = cp_file_name.substr(index+1);
     path =  cp_file_name.substr(0,index+1);
   }
   else
   {
-    file_name = comp_file_name;
-    char CurrentPath[_path_max];
-    getcwd(CurrentPath, _path_max);
-    path = CurrentPath;
+    file_name = comp_file_name;    
+    path = getcwd(NULL, _path_max);;
   }
 
   // Test if this file is already added
@@ -512,7 +527,7 @@ Salome_file_i::setLocalFile(const char* comp_file_name)
   if(fopen(comp_file_name,"rb") != NULL)
     status = "ok";
 
-  // Adding file with is informations
+  // Adding file with its information
   Engines::file infos;
   infos.file_name = CORBA::string_dup(file_name.c_str());
   infos.path = CORBA::string_dup(path.c_str());
@@ -548,7 +563,7 @@ Salome_file_i::setDistributedFile(const char* comp_file_name)
 
   std::string cp_file_name(comp_file_name);
   std::size_t index = cp_file_name.rfind("/");
-  if (index != -1)
+  if (index != std::string::npos)
   {
     file_name = cp_file_name.substr(index+1);
     path =  cp_file_name.substr(0,index+1);
@@ -556,9 +571,7 @@ Salome_file_i::setDistributedFile(const char* comp_file_name)
   else
   {
     file_name = comp_file_name;
-    char CurrentPath[_path_max];
-    getcwd(CurrentPath, _path_max);
-    path = CurrentPath;
+    path = getcwd(NULL, _path_max);;
   }
 
   // Test if this file is already added
@@ -572,7 +585,7 @@ Salome_file_i::setDistributedFile(const char* comp_file_name)
     throw SALOME::SALOME_Exception(es);
   }
 
-  // Adding file with his informations
+  // Adding file with its information
   Engines::file infos;
   infos.file_name = CORBA::string_dup(file_name.c_str());
   infos.path = CORBA::string_dup(path.c_str());
@@ -616,7 +629,7 @@ Salome_file_i::connect(Engines::Salome_file_ptr source_Salome_file)
       _t_fileDistributedSource::iterator it = _fileDistributedSource.find(file_name);
       if (it == _fileDistributedSource.end()) 
       {
-       _fileDistributedSource[file_name] = Engines::Salome_file::_duplicate(source_Salome_file);
+        _fileDistributedSource[file_name] = Engines::Salome_file::_duplicate(source_Salome_file);
       }
     }
   }
@@ -656,7 +669,7 @@ Salome_file_i::connect(Engines::Salome_file_ptr source_Salome_file)
 //=============================================================================
 void
 Salome_file_i::connectDistributedFile(const char * file_name,
-                                     Engines::Salome_file_ptr source_Salome_file) 
+                                      Engines::Salome_file_ptr source_Salome_file) 
 {
   // Test if this file is added
   _t_fileManaged::iterator it = _fileManaged.find(file_name);
@@ -682,7 +695,7 @@ Salome_file_i::connectDistributedFile(const char * file_name,
 //=============================================================================
 void 
 Salome_file_i::setDistributedSourceFile(const char* file_name,
-                                       const char * source_file_name)
+                                        const char * source_file_name)
 {
   std::string fname(file_name);
 
@@ -723,12 +736,12 @@ Salome_file_i::recvFiles() {
     if (std::string(file_infos.type.in()) == "local")
     {
       if (std::string(file_infos.status.in()) == "not_ok")
-       result = checkLocalFile(file_infos.file_name.in());
+        result = checkLocalFile(file_infos.file_name.in());
     }
     else
     {
       if (std::string(file_infos.status.in()) == "not_ok")
-       result = getDistributedFile(file_infos.file_name.in());
+        result = getDistributedFile(file_infos.file_name.in());
     }
     // if the result is false
     // we add this file to files_not_ok
@@ -741,12 +754,10 @@ Salome_file_i::recvFiles() {
 
   if (files_not_ok != "")
   {
-    std::cerr << "tutu" << std::endl;
     SALOME::ExceptionStruct es;
     es.type = SALOME::INTERNAL_ERROR;
     std::string text = "files not ready : " + files_not_ok;
     es.text = CORBA::string_dup(text.c_str());
-    std::cerr << "titi" << std::endl;
     throw SALOME::SALOME_Exception(es);
   }
   else
@@ -840,8 +851,12 @@ Salome_file_i::getDistributedFile(std::string file_name)
       aBlock = _fileDistributedSource[file_name]->getBlock(fileId);
       toFollow = aBlock->length();
       CORBA::Octet *buf = aBlock->get_buffer();
+#if defined(_DEBUG_) || defined(_DEBUG)
       int nbWri = fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
       ASSERT(nbWri == toFollow);
+#else
+      fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
+#endif
       delete aBlock;
     }
     fclose(fp);
@@ -868,7 +883,7 @@ Salome_file_i::getDistributedFile(std::string file_name)
  */
 //=============================================================================
 void 
-Salome_file_i::removeFile(const char* file_name
+Salome_file_i::removeFile(const char* /*file_name*/
 {
   MESSAGE("Salome_file_i::removeFile : NOT YET IMPLEMENTED");
 }
@@ -894,7 +909,7 @@ Engines::files*
 Salome_file_i::getFilesInfos() {
 
   Engines::files * infos = new Engines::files();
-  infos->length(_fileManaged.size());
+  infos->length((CORBA::ULong)_fileManaged.size());
 
   _t_fileManaged::iterator begin = _fileManaged.begin();
   _t_fileManaged::iterator end = _fileManaged.end();
@@ -947,7 +962,7 @@ Salome_file_i::getSalome_fileState()
 /*! 
  *  CORBA method: try to open the file given. If the file is readable, return
  *  a positive integer else return 0;
- *  \param  fileName file name to be transfered
+ *  \param  fileName file name to be transferred
  *  \return fileId = positive integer > 0 if open OK.
  */
 //=============================================================================
@@ -1043,8 +1058,8 @@ Salome_file_i::getBlock(CORBA::Long fileId)
   // see Advanced CORBA Programming with C++ pp 187-194
   CORBA::Octet *buf;
   buf = Engines::fileBlock::allocbuf(FILEBLOCK_SIZE);
-  int nbRed = fread(buf, sizeof(CORBA::Octet), FILEBLOCK_SIZE, fp);
-  aBlock->replace(nbRed, nbRed, buf, 1); // 1 means give ownership
+  size_t nbRed = fread(buf, sizeof(CORBA::Octet), FILEBLOCK_SIZE, fp);
+  aBlock->replace((CORBA::ULong)nbRed, (CORBA::ULong)nbRed, buf, 1); // 1 means give ownership //!< TODO: conversion from size_t to CORBA::ULong
   return aBlock;
 }