Salome HOME
Implementation of "setExtension" method and working with stream (by Daniel remarks)
authormpv <mpv@opencascade.com>
Fri, 23 Nov 2012 13:27:40 +0000 (13:27 +0000)
committermpv <mpv@opencascade.com>
Fri, 23 Nov 2012 13:27:40 +0000 (13:27 +0000)
src/Container/SALOME_DataContainerPy.py
src/Container/SALOME_DataContainer_i.cxx
src/Container/SALOME_DataContainer_i.hxx

index ea2808c2650011116b13aaee8cc6a077e601a299..65b7d02e08211195ccfc7c83c1d8e03bc3a761eb 100755 (executable)
@@ -49,25 +49,32 @@ class SALOME_DataContainerPy_i (Engines__POA.DataContainer):
 
     #-------------------------------------------------------------------------
 
-    def __init__(self, url, name, identifier, removeAfterGet):
-        self._url = url
+    def __init__(self, urlorstream, name, identifier, removeAfterGet, isStream = False):
+        self._urlorstream = urlorstream
         self._name = name
         self._identifier = identifier
         self._removeAfterGet = removeAfterGet
-        self._ext = url[url.rfind(".") + 1 : ]
+        self._isStream = isStream
+        if isStream:
+          self._ext = ""
+        else:
+          self._ext = urlorstream[urlorstream.rfind(".") + 1 : ]
 
     #-------------------------------------------------------------------------
 
     def get(self):
-      f = open(self._url, 'r')
+      if self._isStream:
+        return self._urlorstream
+
+      f = open(self._urlorstream, 'r')
       stream = f.read()
       f.close()
       if self._removeAfterGet:
-        os.remove(self._url)
+        os.remove(self._urlorstream)
         try: # try to remove directory if it is empty
-          index = max(self._url.rfind("\\"), self._url.rfind("/"))
+          index = max(self._urlorstream.rfind("\\"), self._url.rfind("/"))
           if index > 0:
-            os.rmdir(self._url[:index])
+            os.rmdir(self._urlorstream[:index])
         except:
           pass
       return stream
@@ -84,3 +91,6 @@ class SALOME_DataContainerPy_i (Engines__POA.DataContainer):
 
     def extension(self):
        return self._ext
+
+    def setExtension(self, ext):
+       self._ext = ext
index 2085bd2c17385aac23b1c2796a6f2c7f5c847dcc..965b4047c7b0ffb71d622678662c0eb2db1059e5 100644 (file)
@@ -40,7 +40,8 @@ Engines_DataContainer_i::Engines_DataContainer_i()
 
 Engines_DataContainer_i::Engines_DataContainer_i(const char* url,
   const char* name, const char* identifier, const bool removeAfterGet)
- : myName(name), myIdentifier(identifier), myURL(url), myRemoveAfterGet(removeAfterGet)
+ : myName(name), myIdentifier(identifier), myURL(url), myRemoveAfterGet(removeAfterGet), 
+   myStream(0)
 {
   std::string anExtension(url);
   if (anExtension.rfind(".") != std::string::npos) { // keep only extension
@@ -48,42 +49,60 @@ Engines_DataContainer_i::Engines_DataContainer_i(const char* url,
   } else myExt = "";
 }
 
+Engines_DataContainer_i::Engines_DataContainer_i(char* stream,
+  const int streamSize, const char* name, const char* identifier, const bool removeAfterGet)
+ : myName(name), myIdentifier(identifier), myRemoveAfterGet(removeAfterGet), 
+   myStream(stream), myStreamSize(streamSize), myExt("")
+{
+}
+
 Engines_DataContainer_i::~Engines_DataContainer_i()
 {
 }
 
 Engines::TMPFile* Engines_DataContainer_i::get()
 {
-  // open file to make stream from its content
+  char* aBuffer = NULL;
+  int aFileSize = 0;
+  if (myStream) { // send from stream
+    aBuffer = myStream;
+    aFileSize = myStreamSize;
+  } else { // send from file
+    // open file to make stream from its content
 #ifdef WIN32
-  ifstream aFile(myURL.c_str(), std::ios::binary);
+    ifstream aFile(myURL.c_str(), std::ios::binary);
 #else
-  ifstream aFile(myURL.c_str());
+    ifstream aFile(myURL.c_str());
 #endif
-  if (!aFile.good()) {
-    std::cerr<<"File "<<myURL.c_str()<<" can not be opened for reading"<<std::endl;
-  }
-  aFile.seekg(0, std::ios::end);
-  int aFileSize = aFile.tellg();
-  char* aBuffer = new char[aFileSize];
+    if (!aFile.good()) {
+      std::cerr<<"File "<<myURL.c_str()<<" can not be opened for reading"<<std::endl;
+    } else {
+      aFile.seekg(0, std::ios::end);
+      aFileSize = aFile.tellg();
+      aBuffer = new char[aFileSize];
                                                            
-  aFile.seekg(0, std::ios::beg);
-  aFile.read(aBuffer, aFileSize);
-  aFile.close();
+      aFile.seekg(0, std::ios::beg);
+      aFile.read(aBuffer, aFileSize);
+      aFile.close();
 
-  // remove file after it converted to a stream
-  if (myRemoveAfterGet) {
-    #ifdef WIN32
-      DeleteFile(myURL.c_str());
-    #else
-      unlink(myURL.c_str());
-    #endif
+      // remove file after it converted to a stream
+      if (myRemoveAfterGet) {
+#ifdef WIN32
+        DeleteFile(myURL.c_str());
+#else
+        unlink(myURL.c_str());
+#endif
+      }
+    }
   }
-                                                                                                                                                    
+  
   // make CORBA TMP file from the buffer
   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
   Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aFileSize, aFileSize, anOctetBuf, 1);
   
+  if (myStream && myRemoveAfterGet)
+    delete [] myStream;
+  
   return aStreamFile._retn();
 }
 
@@ -101,3 +120,8 @@ char* Engines_DataContainer_i::extension()
 {
   return CORBA::string_dup(myExt.c_str());
 }
+
+void Engines_DataContainer_i::setExtension(const char* theExt)
+{
+  myExt = theExt;
+}
index 0b4f1b7b5a458a561618cf5a6fea1276eea9260a..66f59d30803cfd6a6b7993ab2e00b3408c487792 100644 (file)
@@ -45,6 +45,12 @@ public:
                           const char* name,
                           const char* identifier,
                           const bool removeAfterGet);
+
+  Engines_DataContainer_i(char* stream,
+                          const int   streamSize,
+                          const char* name,
+                          const char* identifier,
+                          const bool removeAfterGet);
   virtual ~Engines_DataContainer_i();
 
   // --- CORBA methods
@@ -52,6 +58,7 @@ public:
   virtual char* name();
   virtual char* identifier();
   virtual char* extension();
+  virtual void  setExtension(const char* theExt);
             
 protected:
 
@@ -60,6 +67,8 @@ protected:
   std::string myIdentifier; ///< module identifier of the document corresponding to this data
   std::string myURL;        ///< path to the locally located file
   bool        myRemoveAfterGet; ///< if this flag is true, file must be removed after the first "get" method call
+  char*       myStream;     ///< if it is not NULL, data must be get from this stream, not from the file
+  int         myStreamSize; ///< size (in bytes) if the stream in myStream
 };
 
 #endif