Salome HOME
Base implementation of Notebook
[modules/kernel.git] / src / Container / SALOME_FileTransfer_i.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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 /*! \class fileTransfer_i
31     \brief A class to manage file transfer in SALOME
32
33 */
34
35 //=============================================================================
36 /*! 
37  *  Default constructor,
38  */
39 //=============================================================================
40
41 fileTransfer_i::fileTransfer_i()
42 {
43   MESSAGE("fileTransfer_i::fileTransfer_i");
44   _fileKey=1;
45 }
46
47 //=============================================================================
48 /*! 
49  *  Destructor
50  */
51 //=============================================================================
52
53 fileTransfer_i::~fileTransfer_i()
54 {
55   MESSAGE("fileTransfer_i::~fileTransfer_i");
56 }
57
58
59 //=============================================================================
60 /*! \brief open the given file
61  *
62  *  CORBA method: try to open the file. If the file is readable, return
63  *  a positive integer else return 0;
64  *  \param  fileName path to the file to be transfered
65  *  \return fileId = positive integer > 0 if open OK.
66  */
67 //=============================================================================
68
69 CORBA::Long fileTransfer_i::open(const char* fileName)
70 {
71   MESSAGE(" fileTransfer_i::open " << fileName);
72   int aKey = _fileKey++;
73   _ctr=0;
74   FILE* fp;
75   if ((fp = fopen(fileName,"rb")) == NULL)
76     {
77       INFOS("file " << fileName << " is not readable");
78       return 0;
79     }
80   _fileAccess[aKey] = fp;
81   return aKey;
82 }
83
84 //=============================================================================
85 /*! \brief close a file
86  *
87  *  CORBA method: close the file associated to the fileId given at open.
88  *  \param fileId got in return from open method
89  */
90 //=============================================================================
91
92 void fileTransfer_i::close(CORBA::Long fileId)
93 {
94   MESSAGE("fileTransfer_i::close");
95   FILE* fp;
96   if (! (fp = _fileAccess[fileId]) )
97     {
98       INFOS(" no FILE structure associated to fileId " <<fileId);
99     }
100   else 
101     {
102       fclose(fp);
103       _fileAccess.erase(fileId);
104     }
105 }
106
107 #define FILEBLOCK_SIZE 256*1024
108
109 //=============================================================================
110 /*! \brief get a data block from a file
111  * 
112  *  CORBA method: get a block of data from the file associated to the fileId
113  *  given at open.
114  *  \param fileId got in return from open method
115  *  \return an octet sequence. Last one is empty.
116  */
117 //=============================================================================
118
119 Engines::fileBlock* fileTransfer_i::getBlock(CORBA::Long fileId)
120 {
121   MESSAGE("fileTransfer_i::getBlock");
122   Engines::fileBlock* aBlock = new Engines::fileBlock;
123
124   FILE* fp;
125   if (! (fp = _fileAccess[fileId]) )
126     {
127       INFOS(" no FILE structure associated to fileId " <<fileId);
128       return aBlock;
129     }
130
131   // use replace member function for sequence to avoid copy
132   // see Advanced CORBA Programming with C++ pp 187-194
133   
134   CORBA::Octet *buf;
135   buf = Engines::fileBlock::allocbuf(FILEBLOCK_SIZE);
136   int nbRed = fread(buf, sizeof(CORBA::Octet), FILEBLOCK_SIZE, fp);
137   SCRUTE(nbRed);
138   aBlock->replace(nbRed, nbRed, buf, 1); // 1 means give ownership
139   return aBlock;
140 }
141
142 /*! \brief open the given file in write mode (for copy)
143  *
144  *  CORBA method: try to open the file. If the file is writable, 
145  *  return a positive integer else return 0;
146  *  \param  fileName path to the file to be transfered
147  *  \return fileId = positive integer > 0 if open OK.
148  */
149 CORBA::Long fileTransfer_i::openW(const char* fileName)
150 {
151   MESSAGE(" fileTransfer_i::openW " << fileName);
152   int aKey = _fileKey++;
153   _ctr=0;
154   FILE* fp;
155   if ((fp = fopen(fileName,"wb")) == NULL)
156     {
157       INFOS("file " << fileName << " is not writable");
158       return 0;
159     }
160   _fileAccess[aKey] = fp;
161   return aKey;
162 }
163
164 /*! \brief put a data block for copy into a file
165  * 
166  *  CORBA method: put a block of data into the file associated to the fileId
167  *  given at openW.
168  *  \param fileId got in return from openW method
169  *  \param block an octet sequence to copy into opened file
170  */
171 void fileTransfer_i::putBlock(CORBA::Long fileId, const Engines::fileBlock& block)
172 {
173   MESSAGE("fileTransfer_i::putBlock");
174   FILE* fp;
175   if (! (fp = _fileAccess[fileId]) )
176     {
177       INFOS(" no FILE structure associated to fileId " <<fileId);
178       return ;
179     }
180   int toFollow = block.length();
181   SCRUTE(toFollow);
182   const CORBA::Octet *buf = block.get_buffer();
183   fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
184 }
185
186