Back to index

salome-kernel  6.5.0
SALOME_FileTransfer_i.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  File   : SALOME_FileTransfer_i.cxx
00024 //  Author : Paul RASCLE, EDF
00025 //  Module : SALOME
00026 //  $Header: /home/server/cvs/KERNEL/KERNEL_SRC/src/Container/SALOME_FileTransfer_i.cxx,v 1.5.2.2.20.2.12.1 2012-04-12 14:05:03 vsr Exp $
00027 //
00028 #include "SALOME_FileTransfer_i.hxx"
00029 #include "utilities.h"
00030 
00036 //=============================================================================
00040 //=============================================================================
00041 
00042 fileTransfer_i::fileTransfer_i()
00043 {
00044   MESSAGE("fileTransfer_i::fileTransfer_i");
00045   _fileKey=1;
00046 }
00047 
00048 //=============================================================================
00052 //=============================================================================
00053 
00054 fileTransfer_i::~fileTransfer_i()
00055 {
00056   MESSAGE("fileTransfer_i::~fileTransfer_i");
00057 }
00058 
00059 
00060 //=============================================================================
00068 //=============================================================================
00069 
00070 CORBA::Long fileTransfer_i::open(const char* fileName)
00071 {
00072   MESSAGE(" fileTransfer_i::open " << fileName);
00073   int aKey = _fileKey++;
00074   _ctr=0;
00075   FILE* fp;
00076   if ((fp = fopen(fileName,"rb")) == NULL)
00077     {
00078       INFOS("file " << fileName << " is not readable");
00079       return 0;
00080     }
00081   _fileAccess[aKey] = fp;
00082   return aKey;
00083 }
00084 
00085 //=============================================================================
00091 //=============================================================================
00092 
00093 void fileTransfer_i::close(CORBA::Long fileId)
00094 {
00095   MESSAGE("fileTransfer_i::close");
00096   FILE* fp;
00097   if (! (fp = _fileAccess[fileId]) )
00098     {
00099       INFOS(" no FILE structure associated to fileId " <<fileId);
00100     }
00101   else 
00102     {
00103       fclose(fp);
00104       _fileAccess.erase(fileId);
00105     }
00106 }
00107 
00108 #define FILEBLOCK_SIZE 256*1024
00109 
00110 //=============================================================================
00118 //=============================================================================
00119 
00120 Engines::fileBlock* fileTransfer_i::getBlock(CORBA::Long fileId)
00121 {
00122   MESSAGE("fileTransfer_i::getBlock");
00123   Engines::fileBlock* aBlock = new Engines::fileBlock;
00124 
00125   FILE* fp;
00126   if (! (fp = _fileAccess[fileId]) )
00127     {
00128       INFOS(" no FILE structure associated to fileId " <<fileId);
00129       return aBlock;
00130     }
00131 
00132   // use replace member function for sequence to avoid copy
00133   // see Advanced CORBA Programming with C++ pp 187-194
00134   
00135   CORBA::Octet *buf;
00136   buf = Engines::fileBlock::allocbuf(FILEBLOCK_SIZE);
00137   int nbRed = fread(buf, sizeof(CORBA::Octet), FILEBLOCK_SIZE, fp);
00138   SCRUTE(nbRed);
00139   aBlock->replace(nbRed, nbRed, buf, 1); // 1 means give ownership
00140   return aBlock;
00141 }
00142 
00150 CORBA::Long fileTransfer_i::openW(const char* fileName)
00151 {
00152   MESSAGE(" fileTransfer_i::openW " << fileName);
00153   int aKey = _fileKey++;
00154   _ctr=0;
00155   FILE* fp;
00156   if ((fp = fopen(fileName,"wb")) == NULL)
00157     {
00158       INFOS("file " << fileName << " is not writable");
00159       return 0;
00160     }
00161   _fileAccess[aKey] = fp;
00162   return aKey;
00163 }
00164 
00172 void fileTransfer_i::putBlock(CORBA::Long fileId, const Engines::fileBlock& block)
00173 {
00174   MESSAGE("fileTransfer_i::putBlock");
00175   FILE* fp;
00176   if (! (fp = _fileAccess[fileId]) )
00177     {
00178       INFOS(" no FILE structure associated to fileId " <<fileId);
00179       return ;
00180     }
00181   int toFollow = block.length();
00182   SCRUTE(toFollow);
00183   const CORBA::Octet *buf = block.get_buffer();
00184   fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
00185 }
00186 
00187