From a8b72d380614f958892ad94100b5775dc3ac3d89 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 11 Jun 2012 18:44:27 -0700 Subject: [PATCH] Fully support resolving absolute paths --- Atom-Linux/native_handler.cpp | 41 ++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/Atom-Linux/native_handler.cpp b/Atom-Linux/native_handler.cpp index 93c40d0d5..84a0aa564 100644 --- a/Atom-Linux/native_handler.cpp +++ b/Atom-Linux/native_handler.cpp @@ -63,12 +63,41 @@ void NativeHandler::Absolute(const CefString& name, CefRefPtr object, const CefV8ValueList& arguments, CefRefPtr& retval, CefString& exception) { string path = arguments[0]->GetStringValue().ToString(); - if (path[0] == '~') { - string resolved = getenv("HOME"); - resolved.append(path.substr(1)); - retval = CefV8Value::CreateString(resolved); - } else - retval = CefV8Value::CreateString(path); + string relativePath; + if (path[0] != '~') + relativePath.append(path); + else { + relativePath.append(getenv("HOME")); + relativePath.append(path, 1, path.length() - 1); + } + + vector < string > segments; + char allSegments[path.length() + 1]; + strcpy(allSegments, path.c_str()); + const char* segment; + for (segment = strtok(allSegments, "/"); segment; + segment = strtok(NULL, "/")) { + if (strcmp(segment, ".") == 0) + continue; + if (strcmp(segment, "..") == 0) { + if (segments.empty()) { + retval = CefV8Value::CreateString("/"); + return; + } else { + segments.pop_back(); + continue; + } + } + segments.push_back(segment); + } + + string absolutePath; + unsigned int i; + for (i = 0; i < segments.size(); i++) { + absolutePath.append("/"); + absolutePath.append(segments.at(i)); + } + retval = CefV8Value::CreateString(absolutePath); } void ListDirectory(string path, vector* paths, bool recursive) {