Salome HOME
[EDF30399] : Steer directory hosting replay files
[modules/kernel.git] / src / Container / SALOME_FileTransfer_i.cxx
index d487a8c0195d54dec04a0d231f13f10d886d9df5..f83d9de95c00105dd6badcba08444fd09d1cd1c2 100644 (file)
@@ -1,32 +1,38 @@
-
-// Copyright (C) 2006  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
+// Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
+//
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
+// 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_FileTransfer_i.cxx
 //  Author : Paul RASCLE, EDF
 //  Module : SALOME
 //  $Header$
-
+//
 #include "SALOME_FileTransfer_i.hxx"
 #include "utilities.h"
 
+/*! \class fileTransfer_i
+    \brief A class to manage file transfer in SALOME
+
+*/
+
 //=============================================================================
 /*! 
  *  Default constructor,
@@ -35,7 +41,6 @@
 
 fileTransfer_i::fileTransfer_i()
 {
-  MESSAGE("fileTransfer_i::fileTransfer_i");
   _fileKey=1;
 }
 
@@ -47,22 +52,21 @@ fileTransfer_i::fileTransfer_i()
 
 fileTransfer_i::~fileTransfer_i()
 {
-  MESSAGE("fileTransfer_i::~fileTransfer_i");
 }
 
 
 //=============================================================================
-/*! 
- *  CORBA method: try to open the file given. If the file is readable, return
+/*! \brief open the given file
+ *
+ *  CORBA method: try to open the file. If the file is readable, return
  *  a positive integer else return 0;
- *  \param  fileName path to the file to be transfered
+ *  \param  fileName path to the file to be transferred
  *  \return fileId = positive integer > 0 if open OK.
  */
 //=============================================================================
 
 CORBA::Long fileTransfer_i::open(const char* fileName)
 {
-  MESSAGE(" fileTransfer_i::open " << fileName);
   int aKey = _fileKey++;
   _ctr=0;
   FILE* fp;
@@ -76,7 +80,8 @@ CORBA::Long fileTransfer_i::open(const char* fileName)
 }
 
 //=============================================================================
-/*! 
+/*! \brief close a file
+ *
  *  CORBA method: close the file associated to the fileId given at open.
  *  \param fileId got in return from open method
  */
@@ -84,17 +89,23 @@ CORBA::Long fileTransfer_i::open(const char* fileName)
 
 void fileTransfer_i::close(CORBA::Long fileId)
 {
-  MESSAGE("fileTransfer_i::close");
   FILE* fp;
   if (! (fp = _fileAccess[fileId]) )
     {
       INFOS(" no FILE structure associated to fileId " <<fileId);
     }
-  else fclose(fp);
+  else 
+    {
+      fclose(fp);
+      _fileAccess.erase(fileId);
+    }
 }
 
+#define FILEBLOCK_SIZE 256*1024
+
 //=============================================================================
-/*! 
+/*! \brief get a data block from a file
+ * 
  *  CORBA method: get a block of data from the file associated to the fileId
  *  given at open.
  *  \param fileId got in return from open method
@@ -102,11 +113,8 @@ void fileTransfer_i::close(CORBA::Long fileId)
  */
 //=============================================================================
 
-#define FILEBLOCK_SIZE 256*1024
-
 Engines::fileBlock* fileTransfer_i::getBlock(CORBA::Long fileId)
 {
-  MESSAGE("fileTransfer_i::getBlock");
   Engines::fileBlock* aBlock = new Engines::fileBlock;
 
   FILE* fp;
@@ -121,9 +129,52 @@ Engines::fileBlock* fileTransfer_i::getBlock(CORBA::Long fileId)
   
   CORBA::Octet *buf;
   buf = Engines::fileBlock::allocbuf(FILEBLOCK_SIZE);
-  int nbRed = fread(buf, sizeof(CORBA::Octet), FILEBLOCK_SIZE, fp);
-  SCRUTE(nbRed);
-  aBlock->replace(nbRed, nbRed, buf, 1); // 1 means give ownership
+  size_t nbRed = fread(buf, sizeof(CORBA::Octet), FILEBLOCK_SIZE, fp);
+  //SCRUTE(nbRed);
+  aBlock->replace((CORBA::ULong)nbRed, (CORBA::ULong)nbRed, buf, 1); // 1 means give ownership
   return aBlock;
 }
 
+/*! \brief open the given file in write mode (for copy)
+ *
+ *  CORBA method: try to open the file. If the file is writable, 
+ *  return a positive integer else return 0;
+ *  \param  fileName path to the file to be transferred
+ *  \return fileId = positive integer > 0 if open OK.
+ */
+CORBA::Long fileTransfer_i::openW(const char* fileName)
+{
+  int aKey = _fileKey++;
+  _ctr=0;
+  FILE* fp;
+  if ((fp = fopen(fileName,"wb")) == NULL)
+    {
+      INFOS("file " << fileName << " is not writable");
+      return 0;
+    }
+  _fileAccess[aKey] = fp;
+  return aKey;
+}
+
+/*! \brief put a data block for copy into a file
+ * 
+ *  CORBA method: put a block of data into the file associated to the fileId
+ *  given at openW.
+ *  \param fileId got in return from openW method
+ *  \param block an octet sequence to copy into opened file
+ */
+void fileTransfer_i::putBlock(CORBA::Long fileId, const Engines::fileBlock& block)
+{
+  FILE* fp;
+  if (! (fp = _fileAccess[fileId]) )
+    {
+      INFOS(" no FILE structure associated to fileId " <<fileId);
+      return ;
+    }
+  int toFollow = block.length();
+  SCRUTE(toFollow);
+  const CORBA::Octet *buf = block.get_buffer();
+  fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
+}
+
+