Salome HOME
Merge branch 'rnv/unicode_path'
[modules/kernel.git] / src / Container / SALOME_FileTransfer_i.cxx
1 // Copyright (C) 2007-2019  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, or (at your option) any later version.
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
23 //  File   : SALOME_FileTransfer_i.cxx
24 //  Author : Paul RASCLE, EDF
25 //  Module : SALOME
26 //  $Header$
27 //
28 #include "SALOME_FileTransfer_i.hxx"
29 #include "utilities.h"
30
31 /*! \class fileTransfer_i
32     \brief A class to manage file transfer in SALOME
33
34 */
35
36 //=============================================================================
37 /*! 
38  *  Default constructor,
39  */
40 //=============================================================================
41
42 fileTransfer_i::fileTransfer_i()
43 {
44   MESSAGE("fileTransfer_i::fileTransfer_i");
45   _fileKey=1;
46 }
47
48 //=============================================================================
49 /*! 
50  *  Destructor
51  */
52 //=============================================================================
53
54 fileTransfer_i::~fileTransfer_i()
55 {
56   MESSAGE("fileTransfer_i::~fileTransfer_i");
57 }
58
59
60 //=============================================================================
61 /*! \brief open the given file
62  *
63  *  CORBA method: try to open the file. If the file is readable, return
64  *  a positive integer else return 0;
65  *  \param  fileName path to the file to be transferred
66  *  \return fileId = positive integer > 0 if open OK.
67  */
68 //=============================================================================
69
70 CORBA::Long fileTransfer_i::open(const char* fileName)
71 {
72   MESSAGE(" fileTransfer_i::open " << fileName);
73   int aKey = _fileKey++;
74   _ctr=0;
75   FILE* fp;
76   if ((fp = fopen(fileName,"rb")) == NULL)
77     {
78       INFOS("file " << fileName << " is not readable");
79       return 0;
80     }
81   _fileAccess[aKey] = fp;
82   return aKey;
83 }
84
85 //=============================================================================
86 /*! \brief close a file
87  *
88  *  CORBA method: close the file associated to the fileId given at open.
89  *  \param fileId got in return from open method
90  */
91 //=============================================================================
92
93 void fileTransfer_i::close(CORBA::Long fileId)
94 {
95   MESSAGE("fileTransfer_i::close");
96   FILE* fp;
97   if (! (fp = _fileAccess[fileId]) )
98     {
99       INFOS(" no FILE structure associated to fileId " <<fileId);
100     }
101   else 
102     {
103       fclose(fp);
104       _fileAccess.erase(fileId);
105     }
106 }
107
108 #define FILEBLOCK_SIZE 256*1024
109
110 //=============================================================================
111 /*! \brief get a data block from a file
112  * 
113  *  CORBA method: get a block of data from the file associated to the fileId
114  *  given at open.
115  *  \param fileId got in return from open method
116  *  \return an octet sequence. Last one is empty.
117  */
118 //=============================================================================
119
120 Engines::fileBlock* fileTransfer_i::getBlock(CORBA::Long fileId)
121 {
122   //MESSAGE("fileTransfer_i::getBlock");
123   Engines::fileBlock* aBlock = new Engines::fileBlock;
124
125   FILE* fp;
126   if (! (fp = _fileAccess[fileId]) )
127     {
128       INFOS(" no FILE structure associated to fileId " <<fileId);
129       return aBlock;
130     }
131
132   // use replace member function for sequence to avoid copy
133   // see Advanced CORBA Programming with C++ pp 187-194
134   
135   CORBA::Octet *buf;
136   buf = Engines::fileBlock::allocbuf(FILEBLOCK_SIZE);
137   int nbRed = fread(buf, sizeof(CORBA::Octet), FILEBLOCK_SIZE, fp);
138   //SCRUTE(nbRed);
139   aBlock->replace(nbRed, nbRed, buf, 1); // 1 means give ownership
140   return aBlock;
141 }
142
143 /*! \brief open the given file in write mode (for copy)
144  *
145  *  CORBA method: try to open the file. If the file is writable, 
146  *  return a positive integer else return 0;
147  *  \param  fileName path to the file to be transferred
148  *  \return fileId = positive integer > 0 if open OK.
149  */
150 CORBA::Long fileTransfer_i::openW(const char* fileName)
151 {
152   MESSAGE(" fileTransfer_i::openW " << fileName);
153   int aKey = _fileKey++;
154   _ctr=0;
155   FILE* fp;
156   if ((fp = fopen(fileName,"wb")) == NULL)
157     {
158       INFOS("file " << fileName << " is not writable");
159       return 0;
160     }
161   _fileAccess[aKey] = fp;
162   return aKey;
163 }
164
165 /*! \brief put a data block for copy into a file
166  * 
167  *  CORBA method: put a block of data into the file associated to the fileId
168  *  given at openW.
169  *  \param fileId got in return from openW method
170  *  \param block an octet sequence to copy into opened file
171  */
172 void fileTransfer_i::putBlock(CORBA::Long fileId, const Engines::fileBlock& block)
173 {
174   MESSAGE("fileTransfer_i::putBlock");
175   FILE* fp;
176   if (! (fp = _fileAccess[fileId]) )
177     {
178       INFOS(" no FILE structure associated to fileId " <<fileId);
179       return ;
180     }
181   int toFollow = block.length();
182   SCRUTE(toFollow);
183   const CORBA::Octet *buf = block.get_buffer();
184   fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
185 }
186
187