From 7455f1a259ce28ee56866b5cde06e79be4cfaf97 Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Sun, 26 Jan 2014 12:19:47 -0500 Subject: Check file existance and clean up memory issues thanks to KK and Johan (bzr r12979) --- src/document.cpp | 10 +++++----- src/document.h | 2 +- src/uri-references.cpp | 4 +++- src/uri.cpp | 17 ++++++++++++++--- 4 files changed, 23 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/document.cpp b/src/document.cpp index e456f2b81..634462001 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -478,22 +478,22 @@ SPDocument *SPDocument::createDoc(Inkscape::XML::Document *rdoc, /** * Fetches a document and attaches it to the current document as a child href */ -SPDocument *SPDocument::createChildDoc(std::string const uri) +SPDocument *SPDocument::createChildDoc(std::string const &uri) { SPDocument *parent = this; SPDocument *document = NULL; while(parent != NULL && document == NULL) { // Check myself and any parents int he chain - if(uri.compare(parent->getURI())==0) { + if(uri == parent->getURI()) { document = parent; break; } // Then check children of those. boost::ptr_list::iterator iter; for (iter = parent->_child_documents.begin(); - iter != parent->_child_documents.end(); ++iter) { - if(uri.compare(iter->getURI())==0) { + iter != parent->_child_documents.end(); ++iter) { + if(uri == iter->getURI()) { document = &*iter; break; } @@ -503,7 +503,7 @@ SPDocument *SPDocument::createChildDoc(std::string const uri) // Load a fresh document from the svg source. if(!document) { - const char *path = g_strdup(uri.c_str()); + const char *path = uri.c_str(); document = createNewDoc(path, false, false, this); } return document; diff --git a/src/document.h b/src/document.h index 110db11c6..e5567d3b6 100644 --- a/src/document.h +++ b/src/document.h @@ -225,7 +225,7 @@ public: static SPDocument *createNewDoc(const gchar *uri, unsigned int keepalive, bool make_new = false, SPDocument *parent=NULL ); static SPDocument *createNewDocFromMem(const gchar *buffer, gint length, unsigned int keepalive); - SPDocument *createChildDoc(std::string const uri); + SPDocument *createChildDoc(std::string const &uri); /** * Returns the bottommost item from the list which is at the point, or NULL if none. diff --git a/src/uri-references.cpp b/src/uri-references.cpp index dc0101024..abe16ec9d 100644 --- a/src/uri-references.cpp +++ b/src/uri-references.cpp @@ -58,10 +58,12 @@ void URIReference::attach(const URI &uri) throw(BadURIException) // The path contains references to seperate document files to load. if(document && uri.getPath()) { - std::string base = std::string(g_strdup(document->getBase())); + std::string base = std::string(document->getBase()); std::string path = uri.getFullPath(base); if(!path.empty()) document = document->createChildDoc(path); + else + document = NULL; } if(!document) { g_warning("Can't get document for referenced URI: %s", uri.toString()); diff --git a/src/uri.cpp b/src/uri.cpp index 8d0e49139..89f6f33e4 100644 --- a/src/uri.cpp +++ b/src/uri.cpp @@ -135,14 +135,25 @@ gchar *URI::to_native_filename(gchar const* uri) throw(BadURIException) filename = tmp.toNativeFilename(); return filename; } - +/* + * Returns the absolute path to an existing file referenced in this URI, + * if the uri is data, the path is empty or the file doesn't exist, then + * an empty string is returned. + * + * Does not check if the returned path is the local document's path (local) + * and thus redundent. Caller is expected to check against the document's path. + */ const std::string URI::getFullPath(std::string const base) const { std::string path = std::string(_impl->getPath()); // Calculate the absolute path from an available base - if(!path.empty() && !base.empty() && path.compare(0, 1, "/") != 0) { + if(!base.empty() && !path.empty() && path[0] != '/') { path = Glib::build_filename(base, path); } - // TODO: Check existance of file here + // Check the existance of the file + if(! g_file_test(path.c_str(), G_FILE_TEST_EXISTS) + || g_file_test(path.c_str(), G_FILE_TEST_IS_DIR) ) { + path.clear(); + } return path; } -- cgit v1.2.3