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 +++++++++++++++++++++++++++++++++++------------- plugingui/directory.h | 3 ++ plugingui/filebrowser.cc | 4 +- 3 files changed, 77 insertions(+), 29 deletions(-) (limited to 'plugingui') 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: diff --git a/plugingui/directory.h b/plugingui/directory.h index 48b0dd4..a85d0bb 100644 --- a/plugingui/directory.h +++ b/plugingui/directory.h @@ -76,6 +76,9 @@ class Directory { std::string _path; EntryList _files; DriveList _drives; + + typedef std::list Path; + static Path parsePath(std::string path); }; #endif/*__DRUMGIZMO_DIRECTORY_H__*/ diff --git a/plugingui/filebrowser.cc b/plugingui/filebrowser.cc index d9b3250..39cb565 100644 --- a/plugingui/filebrowser.cc +++ b/plugingui/filebrowser.cc @@ -73,7 +73,7 @@ static void changeDir(void *ptr) { std::string value = lb->selectedValue(); Directory* dir = prv->dir; - INFO(filebrowser, "Changing dir to '%s'\n", (dir->path() + "/" + value).c_str()); + INFO(filebrowser, "Changing path to '%s'\n", (dir->path() + "/" + value).c_str()); if(!value.empty() && dir->fileExists(value)) { std::string file = dir->path() + "/" + value; @@ -91,7 +91,7 @@ static void changeDir(void *ptr) { //TODO: If root and windows show drives instead of files DEBUG(filebrowser, "Setting path of lineedit to %s\n", dir->path().c_str()); - le->setText(Directory::cleanPath(dir->path())); + le->setText(dir->path()); lb->clear(); std::vector items; -- cgit v1.2.3