Salome HOME
First stable version after merging with V3_2_2
[modules/kernel.git] / src / Container / SALOME_FileTransfer_i.cxx
1
2 // Copyright (C) 2006  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // 
4 //  This library is free software; you can redistribute it and/or 
5 //  modify it under the terms of the GNU Lesser General Public 
6 //  License as published by the Free Software Foundation; either 
7 //  version 2.1 of the License. 
8 // 
9 //  This library is distributed in the hope that it will be useful, 
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 //  Lesser General Public License for more details. 
13 // 
14 //  You should have received a copy of the GNU Lesser General Public 
15 //  License along with this library; if not, write to the Free Software 
16 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
17 // 
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 //
21 //
22 //  File   : SALOME_FileTransfer_i.cxx
23 //  Author : Paul RASCLE, EDF
24 //  Module : SALOME
25 //  $Header$
26
27 #include "SALOME_FileTransfer_i.hxx"
28 #include "utilities.h"
29
30 //=============================================================================
31 /*! 
32  *  Default constructor,
33  */
34 //=============================================================================
35
36 fileTransfer_i::fileTransfer_i()
37 {
38   MESSAGE("fileTransfer_i::fileTransfer_i");
39   _fileKey=1;
40 }
41
42 //=============================================================================
43 /*! 
44  *  Destructor
45  */
46 //=============================================================================
47
48 fileTransfer_i::~fileTransfer_i()
49 {
50   MESSAGE("fileTransfer_i::~fileTransfer_i");
51 }
52
53
54 //=============================================================================
55 /*! 
56  *  CORBA method: try to open the file given. If the file is readable, return
57  *  a positive integer else return 0;
58  *  \param  fileName path to the file to be transfered
59  *  \return fileId = positive integer > 0 if open OK.
60  */
61 //=============================================================================
62
63 CORBA::Long fileTransfer_i::open(const char* fileName)
64 {
65   MESSAGE(" fileTransfer_i::open " << fileName);
66   int aKey = _fileKey++;
67   _ctr=0;
68   FILE* fp;
69   if ((fp = fopen(fileName,"rb")) == NULL)
70     {
71       INFOS("file " << fileName << " is not readable");
72       return 0;
73     }
74   _fileAccess[aKey] = fp;
75   return aKey;
76 }
77
78 //=============================================================================
79 /*! 
80  *  CORBA method: close the file associated to the fileId given at open.
81  *  \param fileId got in return from open method
82  */
83 //=============================================================================
84
85 void fileTransfer_i::close(CORBA::Long fileId)
86 {
87   MESSAGE("fileTransfer_i::close");
88   FILE* fp;
89   if (! (fp = _fileAccess[fileId]) )
90     {
91       INFOS(" no FILE structure associated to fileId " <<fileId);
92     }
93   else fclose(fp);
94 }
95
96 //=============================================================================
97 /*! 
98  *  CORBA method: get a block of data from the file associated to the fileId
99  *  given at open.
100  *  \param fileId got in return from open method
101  *  \return an octet sequence. Last one is empty.
102  */
103 //=============================================================================
104
105 #define FILEBLOCK_SIZE 256*1024
106
107 Engines::fileBlock* fileTransfer_i::getBlock(CORBA::Long fileId)
108 {
109   MESSAGE("fileTransfer_i::getBlock");
110   Engines::fileBlock* aBlock = new Engines::fileBlock;
111
112   FILE* fp;
113   if (! (fp = _fileAccess[fileId]) )
114     {
115       INFOS(" no FILE structure associated to fileId " <<fileId);
116       return aBlock;
117     }
118
119   // use replace member function for sequence to avoid copy
120   // see Advanced CORBA Programming with C++ pp 187-194
121   
122   CORBA::Octet *buf;
123   buf = Engines::fileBlock::allocbuf(FILEBLOCK_SIZE);
124   int nbRed = fread(buf, sizeof(CORBA::Octet), FILEBLOCK_SIZE, fp);
125   SCRUTE(nbRed);
126   aBlock->replace(nbRed, nbRed, buf, 1); // 1 means give ownership
127   return aBlock;
128 }
129