Salome HOME
create_python_service_instance(): meaningful implementation in
[modules/kernel.git] / src / Container / SALOME_DataContainer_i.cxx
1 // Copyright (C) 2007-2015  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 //  SALOME DataContainer : implementation of data container
24 //  File   : SALOME_DataContainer_i.cxx
25 //  Author : Mikhail PONIKAROV
26 //  Module : SALOME
27 //  $Header$
28 //
29
30 #include "SALOME_DataContainer_i.hxx"
31
32 #include <fstream>
33 #include <iostream>
34 #ifndef WIN32
35 #include <unistd.h>
36 #endif
37 using namespace std;
38
39 Engines_DataContainer_i::Engines_DataContainer_i()
40 {
41 }
42
43 Engines_DataContainer_i::Engines_DataContainer_i(const char* url,
44   const char* name, const char* identifier, const bool removeAfterGet)
45  : myName(name), myIdentifier(identifier), myURL(url), myRemoveAfterGet(removeAfterGet), 
46    myStream(0)
47 {
48   std::string anExtension(url);
49   if (anExtension.rfind(".") != std::string::npos) { // keep only extension
50     myExt = anExtension.substr(anExtension.rfind(".") + 1);
51   } else myExt = "";
52 }
53
54 Engines_DataContainer_i::Engines_DataContainer_i(char* stream,
55   const int streamSize, const char* name, const char* identifier, const bool removeAfterGet)
56  : myName(name), myIdentifier(identifier), myRemoveAfterGet(removeAfterGet), 
57    myStream(stream), myStreamSize(streamSize), myExt("")
58 {
59 }
60
61 Engines_DataContainer_i::~Engines_DataContainer_i()
62 {
63 }
64
65 Engines::TMPFile* Engines_DataContainer_i::get()
66 {
67   char* aBuffer = NULL;
68   int aFileSize = 0;
69   if (myStream) { // send from stream
70     aBuffer = myStream;
71     aFileSize = myStreamSize;
72   } else { // send from file
73     // open file to make stream from its content
74 #ifdef WIN32
75     ifstream aFile(myURL.c_str(), std::ios::binary);
76 #else
77     ifstream aFile(myURL.c_str());
78 #endif
79     if (!aFile.good()) {
80       std::cerr<<"File "<<myURL.c_str()<<" can not be opened for reading"<<std::endl;
81     } else {
82       aFile.seekg(0, std::ios::end);
83       aFileSize = aFile.tellg();
84       aBuffer = new char[aFileSize];
85                                                            
86       aFile.seekg(0, std::ios::beg);
87       aFile.read(aBuffer, aFileSize);
88       aFile.close();
89
90       // remove file after it converted to a stream
91       // also remove directory of the file if it is empty
92       if (myRemoveAfterGet) {
93         string aDirName = myURL.substr(0, myURL.find_last_of("/\\"));
94 #ifdef WIN32
95         DeleteFile(myURL.c_str());
96         RemoveDirectory(aDirName.c_str());
97 #else
98         unlink(myURL.c_str());
99         rmdir(aDirName.c_str());
100 #endif
101       }
102     }
103   }
104   
105   // make CORBA TMP file from the buffer
106   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
107   Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aFileSize, aFileSize, anOctetBuf, 1);
108   
109   if (myStream && myRemoveAfterGet)
110     delete [] myStream;
111   
112   return aStreamFile._retn();
113 }
114
115 char* Engines_DataContainer_i::name()
116 {
117   return CORBA::string_dup(myName.c_str());
118 }
119
120 char* Engines_DataContainer_i::identifier()
121 {
122   return CORBA::string_dup(myIdentifier.c_str());
123 }
124
125 char* Engines_DataContainer_i::extension()
126 {
127   return CORBA::string_dup(myExt.c_str());
128 }
129
130 void Engines_DataContainer_i::setExtension(const char* theExt)
131 {
132   myExt = theExt;
133 }