From dda686a04556a4cbbed0d8acf68c7d017b613024 Mon Sep 17 00:00:00 2001 From: "jsc@umbraculum.org" Date: Fri, 26 Apr 2013 13:15:57 +0200 Subject: Added cleaned path. --- plugingui/directory.cc | 99 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 72 insertions(+), 27 deletions(-) (limited to 'plugingui/directory.cc') diff --git a/plugingui/directory.cc b/plugingui/directory.cc index 7e7d7e1..be5fad9 100644 --- a/plugingui/directory.cc +++ b/plugingui/directory.cc @@ -48,13 +48,17 @@ size_t Directory::count() { } void Directory::refresh() { - _files = listFiles(this->_path); + _files = listFiles(_path); } bool Directory::cd(std::string dir) { + //TODO: Should this return true or false? + if(dir.empty() || dir == ".") return true; + DEBUG(directory, "Changing to '%s'\n", dir.c_str()); - if(exists(path() + "/" + dir)) { - _path += "/" + dir; + if(exists(_path + "/" + dir)) { + std::string path = _path + "/" + dir; + setPath(path); refresh(); return true; } @@ -66,6 +70,7 @@ bool Directory::cdUp() { } std::string Directory::path() { + setPath(cleanPath(_path)); return _path; } @@ -82,34 +87,27 @@ std::string Directory::cwd() { else return ""; } + std::string Directory::cleanPath(std::string path) { -/* - size_t c = 0; - std::string current_char; - std::string prev_char; + WARN(directory, "Cleaning path '%s'\n", path.c_str()); - for(; c < path.size(); c++) { - current_char = path.at(c); - prev_char = current_char; - } -*/ -/* + Directory::Path pathlst = parsePath(path); - size_t current_pos; - size_t prev_pos = 1; - DEBUG(directory, "Looking at path '%s'\n", path.c_str()); - while( (current_pos = path.find("/", prev_pos + 1)) != std::string::npos) { - DEBUG(directory, "%d - %d", prev_pos, current_pos); - std::string dir = path.substr(prev_pos, current_pos - prev_pos + 1); - DEBUG(directory, "Dir '%s'\n", dir.c_str()); - prev_pos = current_pos; + std::string cleaned_path; + DEBUG(directory, "Number of directories in path %d\n", pathlst.size()); + + for(Directory::Path::iterator it = pathlst.begin(); + it != pathlst.end(); it++) { + std::string dir = *it; + DEBUG(directory, "\tDir '%s'\n", dir.c_str()); + cleaned_path += "/" + dir; } - std::string dir = path.substr(prev_pos, current_pos - prev_pos + 1); - DEBUG(directory, "Dir '%s'\n", dir.c_str()); -*/ + DEBUG(directory, "Cleaned path '%s'\n", cleaned_path.c_str()); - return path; + if(cleaned_path.empty()) cleaned_path = "/"; + + return cleaned_path; } Directory::EntryList Directory::listFiles(std::string path) { @@ -124,6 +122,9 @@ Directory::EntryList Directory::listFiles(std::string path) { struct dirent *entry; while((entry = readdir(dir)) != NULL) { + std::string name = entry->d_name; + if(name == ".") continue; + if(Directory::isRoot(path) && name == "..") continue; entries.push_back(entry->d_name); } @@ -161,11 +162,11 @@ Directory::DriveList Directory::drives() { bool Directory::isDir() { - return isDir(path()); + return isDir(_path); } bool Directory::fileExists(std::string filename) { - return !isDir(path() + "/" + filename); + return !isDir(_path + "/" + filename); } bool Directory::exists(std::string path) { @@ -189,6 +190,50 @@ bool Directory::isDir(std::string path) { return false; } +Directory::Path Directory::parsePath(std::string path_str) { + //TODO: Handle "." input and propably other special cases + + DEBUG(directory, "Parsing path '%s'", path_str.c_str()); + Directory::Path path; + + std::string current_char; + std::string prev_char; + std::string dir; + for(size_t c = 0; c < path_str.size(); c++) { + current_char = path_str.at(c); + + if(current_char == "/") { + if(prev_char == "/") { + dir.clear(); + prev_char = current_char; + continue; + } + else if(prev_char == ".") { + prev_char = current_char; + continue; + } + + if(!dir.empty()) path.push_back(dir); + dir.clear(); + continue; + } + else if(current_char == ".") { + if(prev_char == ".") { + dir.clear(); + if(!path.empty()) path.pop_back(); + continue; + } + } + + dir += current_char; + prev_char = current_char; + } + + if(!dir.empty()) path.push_back(dir); + + return path; +} + #ifdef TEST_DIRECTORY //Additional dependency files //deps: -- cgit v1.2.3