#-------------------------------------------------------------------------
- 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
def extension(self):
return self._ext
+
+ def setExtension(self, ext):
+ self._ext = ext
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
} 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();
}
{
return CORBA::string_dup(myExt.c_str());
}
+
+void Engines_DataContainer_i::setExtension(const char* theExt)
+{
+ myExt = theExt;
+}
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
virtual char* name();
virtual char* identifier();
virtual char* extension();
+ virtual void setExtension(const char* theExt);
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