From 81079295b8be14128b7e532d504b32280360532e Mon Sep 17 00:00:00 2001 From: Stefan Radomski Date: Wed, 6 Mar 2013 21:34:05 +0100 Subject: Fixed issue with temporary files --- src/uscxml/URL.cpp | 29 ++++++++++++++++------ src/uscxml/URL.h | 11 ++++++++ .../graphics/openscenegraph/OSGConverter.cpp | 26 +++++++++++++------ test/src/test-url.cpp | 7 ++++++ 4 files changed, 58 insertions(+), 15 deletions(-) diff --git a/src/uscxml/URL.cpp b/src/uscxml/URL.cpp index 1f180e4..4a4440d 100644 --- a/src/uscxml/URL.cpp +++ b/src/uscxml/URL.cpp @@ -26,10 +26,14 @@ namespace uscxml { URLImpl::URLImpl(const std::string& url) : _handle(NULL), _uri(url), _isDownloaded(false), _hasFailed(false) { - _handle = curl_easy_init(); - if (_handle == NULL) { - LOG(ERROR) << "curl_easy_init returned NULL, this is bad!"; + std::stringstream ss(_uri.path()); + std::string item; + while(std::getline(ss, item, '/')) { + if (item.length() == 0) + continue; + _pathComponents.push_back(item); } + } URLImpl::~URLImpl() { @@ -37,6 +41,15 @@ URLImpl::~URLImpl() { curl_easy_cleanup(_handle); } +CURL* URLImpl::getCurlHandle() { + if (_handle == NULL) { + _handle = curl_easy_init(); + if (_handle == NULL) + LOG(ERROR) << "curl_easy_init returned NULL, this is bad!"; + } + return _handle; +} + size_t URLImpl::writeHandler(void *ptr, size_t size, size_t nmemb, void *userdata) { URLImpl* url = (URLImpl*)userdata; url->_inContent.write((char*)ptr, size * nmemb); @@ -304,13 +317,13 @@ void URLFetcher::fetchURL(URL& url) { URLFetcher* instance = getInstance(); tthread::lock_guard lock(instance->_mutex); - assert(url._impl->_handle != NULL); - if (url._impl->_handle == NULL) + CURL* handle = url._impl->getCurlHandle(); + assert(handle != NULL); + if (handle == NULL) return; - if (instance->_handlesToURLs.find(url._impl->_handle) == instance->_handlesToURLs.end()) { + if (instance->_handlesToURLs.find(handle) == instance->_handlesToURLs.end()) { CURLcode curlError; - CURL* handle = url._impl->_handle; (curlError = curl_easy_setopt(handle, CURLOPT_URL, url.asString().c_str())) == CURLE_OK || LOG(ERROR) << "Cannot set url to " << url.asString() << ": " << curl_easy_strerror(curlError); @@ -372,7 +385,7 @@ void URLFetcher::fetchURL(URL& url) { void URLFetcher::breakURL(URL& url) { URLFetcher* instance = getInstance(); - CURL* handle = url._impl->_handle; + CURL* handle = url._impl->getCurlHandle(); tthread::lock_guard lock(instance->_mutex); if (instance->_handlesToURLs.find(handle) != instance->_handlesToURLs.end()) { diff --git a/src/uscxml/URL.h b/src/uscxml/URL.h index df93e60..0e47607 100644 --- a/src/uscxml/URL.h +++ b/src/uscxml/URL.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -59,6 +60,9 @@ public: const std::string path() const { return _uri.path(); } + const std::vector pathComponents() const { + return _pathComponents; + } const std::string asString() const { return _uri.as_string(); } @@ -87,6 +91,8 @@ protected: URLImpl() : _handle(NULL), _isDownloaded(false), _hasFailed(false) {} std::string getLocalFilename(const std::string& suffix); + CURL* getCurlHandle(); + std::string _outContent; std::map _outHeader; std::string _requestType; @@ -96,6 +102,8 @@ protected: std::stringstream _inHeader; Arabica::io::URI _uri; + std::vector _pathComponents; + bool _isDownloaded; bool _hasFailed; @@ -193,6 +201,9 @@ public: const std::string path() const { return _impl->path(); } + const std::vector pathComponents() const { + return _impl->pathComponents(); + } const std::string asString() const { return _impl->asString(); } diff --git a/src/uscxml/plugins/invoker/graphics/openscenegraph/OSGConverter.cpp b/src/uscxml/plugins/invoker/graphics/openscenegraph/OSGConverter.cpp index fbc244a..09cf663 100644 --- a/src/uscxml/plugins/invoker/graphics/openscenegraph/OSGConverter.cpp +++ b/src/uscxml/plugins/invoker/graphics/openscenegraph/OSGConverter.cpp @@ -503,17 +503,29 @@ void OSGConverter::dumpMatrix(const osg::Matrix& m) { void OSGConverter::NameRespectingWriteToFile::operator()(const osg::Image& image, const unsigned int context_id) { - bool success = osgDB::writeImageFile(image, _filename + ".tmp"); - if (success) { - int err = rename(std::string(_filename + ".tmp").c_str(), _filename.c_str()); +// URL fileURL(_filename); +// fileURL.path() + + std::string tmpName = _filename; + size_t pathSep = _filename.find_last_of(PATH_SEPERATOR); + if (pathSep != std::string::npos) { + tmpName = _filename.substr(0, pathSep) + PATH_SEPERATOR + ".tmp" + _filename.substr(pathSep + 1, _filename.length() - pathSep - 1); + } + + bool success = osgDB::writeImageFile(image, tmpName); // <- no plugin to write to .tmp format + if (!success) { + _converter->reportFailure(_req); + return; + } + + if (pathSep != std::string::npos) { + int err = rename(tmpName.c_str(), _filename.c_str()); if (err) { _converter->reportFailure(_req); - } else { - _converter->reportSuccess(_req); } - } else { - _converter->reportFailure(_req); } + + _converter->reportSuccess(_req); } } \ No newline at end of file diff --git a/test/src/test-url.cpp b/test/src/test-url.cpp index d4ae9bb..344df9f 100644 --- a/test/src/test-url.cpp +++ b/test/src/test-url.cpp @@ -65,4 +65,11 @@ int main(int argc, char** argv) { assert(url.isAbsolute()); assert(iequals(url.scheme(), "file")); } + { + URL url("C:\\Document\\Some Spaces\\index.txt"); + assert(iequals(url.pathComponents()[0], "C:")); + assert(iequals(url.pathComponents()[1], "Document")); + assert(iequals(url.pathComponents()[2], "Some Spaces")); + assert(iequals(url.pathComponents()[3], "index.txt")); + } } \ No newline at end of file -- cgit v0.12