2010-02-04 13:33:47 +02:00
|
|
|
/***************************************************************************
|
2011-10-23 17:43:56 +03:00
|
|
|
* Copyright (C) 2006 by Massimiliano Torromeo *
|
|
|
|
* massimiliano.torromeo@gmail.com *
|
2010-02-04 13:33:47 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
2011-10-23 17:43:56 +03:00
|
|
|
#include "filelister.h"
|
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
#include "utilities.h"
|
|
|
|
|
2010-02-04 13:33:47 +02:00
|
|
|
//for browsing the filesystem
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <iostream>
|
2010-06-18 01:45:16 +03:00
|
|
|
#include <algorithm>
|
2010-06-19 05:44:03 +03:00
|
|
|
#include <cstring>
|
2014-08-13 04:54:12 +03:00
|
|
|
#include <set>
|
2010-02-04 13:33:47 +02:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2014-08-13 03:24:18 +03:00
|
|
|
FileLister::FileLister()
|
|
|
|
: showDirectories(true)
|
2014-08-14 05:45:26 +03:00
|
|
|
, showUpdir(true)
|
2014-08-13 03:24:18 +03:00
|
|
|
, showFiles(true)
|
2010-05-03 22:14:11 +03:00
|
|
|
{
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2014-08-13 03:24:18 +03:00
|
|
|
void FileLister::setFilter(const string &filter)
|
2010-05-03 22:14:11 +03:00
|
|
|
{
|
2014-08-13 04:07:17 +03:00
|
|
|
if (filter.empty() || filter == "*") {
|
|
|
|
this->filter.clear();
|
|
|
|
} else {
|
|
|
|
split(this->filter, filter, ",");
|
|
|
|
}
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2014-08-13 07:46:08 +03:00
|
|
|
static void moveNames(set<string, case_less>&& from, vector<string>& to)
|
|
|
|
{
|
|
|
|
to.reserve(from.size());
|
|
|
|
for (string const& name : from) {
|
|
|
|
// Casting away the const is necessary to make the move work.
|
|
|
|
// It will leave the set in an invalid state where it contains multiple
|
|
|
|
// empty strings, but since the set is an rvalue we don't care.
|
|
|
|
to.emplace_back(move(const_cast<string&>(name)));
|
|
|
|
}
|
|
|
|
to.shrink_to_fit();
|
|
|
|
}
|
|
|
|
|
2014-08-13 03:24:18 +03:00
|
|
|
void FileLister::browse(const string& path, bool clean)
|
2010-05-03 22:14:11 +03:00
|
|
|
{
|
2014-08-13 08:35:29 +03:00
|
|
|
if (clean) {
|
|
|
|
directories.clear();
|
|
|
|
files.clear();
|
2011-04-01 19:17:33 +03:00
|
|
|
}
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2014-08-13 08:08:04 +03:00
|
|
|
string slashedPath = path;
|
|
|
|
if (path[path.length() - 1] != '/')
|
|
|
|
slashedPath.push_back('/');
|
|
|
|
|
2014-08-13 08:14:30 +03:00
|
|
|
DIR *dirp;
|
2014-08-13 08:08:04 +03:00
|
|
|
if ((dirp = opendir(slashedPath.c_str())) == NULL) {
|
2014-08-13 23:56:47 +03:00
|
|
|
if (errno != ENOENT) {
|
|
|
|
ERROR("Unable to open directory: %s\n", slashedPath.c_str());
|
|
|
|
}
|
2014-08-13 08:08:04 +03:00
|
|
|
return;
|
|
|
|
}
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2014-08-13 08:35:29 +03:00
|
|
|
set<string, case_less> directorySet;
|
|
|
|
set<string, case_less> fileSet;
|
|
|
|
|
2014-08-13 08:14:30 +03:00
|
|
|
while (struct dirent *dptr = readdir(dirp)) {
|
2014-08-14 05:45:26 +03:00
|
|
|
// Ignore hidden files and optionally "..".
|
|
|
|
if (dptr->d_name[0] == '.') {
|
|
|
|
if (!(dptr->d_name[1] == '.' && showUpdir)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2014-08-13 08:08:04 +03:00
|
|
|
|
2014-08-14 06:25:26 +03:00
|
|
|
bool isDir;
|
|
|
|
#ifdef DT_DIR
|
|
|
|
if (dptr->d_type != DT_UNKNOWN) {
|
|
|
|
isDir = dptr->d_type == DT_DIR;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2014-08-14 06:30:31 +03:00
|
|
|
string filepath = slashedPath + dptr->d_name;
|
2014-08-14 06:25:26 +03:00
|
|
|
struct stat st;
|
|
|
|
int statRet = stat(filepath.c_str(), &st);
|
|
|
|
if (statRet == -1) {
|
|
|
|
ERROR("Stat failed on '%s' with error '%s'\n", filepath.c_str(), strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
isDir = S_ISDIR(st.st_mode);
|
2014-08-13 08:08:04 +03:00
|
|
|
}
|
2014-08-14 06:25:26 +03:00
|
|
|
|
|
|
|
if (isDir) {
|
2014-08-13 08:08:04 +03:00
|
|
|
if (!showDirectories)
|
2010-02-04 13:33:47 +02:00
|
|
|
continue;
|
2014-08-13 08:08:04 +03:00
|
|
|
|
2014-08-14 06:30:31 +03:00
|
|
|
directorySet.insert(string(dptr->d_name));
|
2014-08-13 08:08:04 +03:00
|
|
|
} else {
|
|
|
|
if (!showFiles)
|
2010-02-04 13:33:47 +02:00
|
|
|
continue;
|
|
|
|
|
2014-08-13 08:08:04 +03:00
|
|
|
if (filter.empty()) {
|
2014-08-14 06:30:31 +03:00
|
|
|
fileSet.insert(string(dptr->d_name));
|
2014-08-13 08:08:04 +03:00
|
|
|
continue;
|
|
|
|
}
|
2011-04-14 12:16:16 +03:00
|
|
|
|
2014-08-14 06:30:31 +03:00
|
|
|
string file = dptr->d_name;
|
2014-08-13 08:08:04 +03:00
|
|
|
for (vector<string>::iterator it = filter.begin(); it != filter.end(); ++it) {
|
|
|
|
if (file.find('.') == string::npos) {
|
|
|
|
if (!it->empty())
|
|
|
|
continue;
|
2011-04-14 12:16:16 +03:00
|
|
|
|
2014-08-13 04:54:12 +03:00
|
|
|
fileSet.insert(file);
|
2014-08-13 08:08:04 +03:00
|
|
|
break;
|
2013-07-08 09:21:42 +03:00
|
|
|
}
|
|
|
|
|
2014-08-13 08:08:04 +03:00
|
|
|
if (it->length() < file.length()) {
|
|
|
|
if (file[file.length() - it->length() - 1] != '.')
|
|
|
|
continue;
|
2013-07-05 21:42:55 +03:00
|
|
|
|
2014-08-13 08:08:04 +03:00
|
|
|
string file_lowercase =
|
|
|
|
file.substr(file.length() - it->length());
|
2013-07-05 21:00:46 +03:00
|
|
|
|
2014-08-13 08:08:04 +03:00
|
|
|
/* XXX: This won't accept UTF-8 codes.
|
|
|
|
* Thanksfully file extensions shouldn't contain any. */
|
|
|
|
transform(file_lowercase.begin(), file_lowercase.end(),
|
|
|
|
file_lowercase.begin(), ::tolower);
|
2013-07-05 21:00:46 +03:00
|
|
|
|
2014-08-13 08:08:04 +03:00
|
|
|
if (file_lowercase.compare(0, it->length(), *it) == 0) {
|
|
|
|
fileSet.insert(file);
|
|
|
|
break;
|
2010-05-03 22:14:11 +03:00
|
|
|
}
|
|
|
|
}
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-13 04:54:12 +03:00
|
|
|
|
2014-08-13 08:08:04 +03:00
|
|
|
closedir(dirp);
|
|
|
|
|
2014-08-13 08:35:29 +03:00
|
|
|
if (!directorySet.empty()) {
|
|
|
|
for (string& dir : directories) {
|
|
|
|
directorySet.emplace(move(dir));
|
|
|
|
}
|
|
|
|
directories.clear();
|
|
|
|
moveNames(move(directorySet), directories);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fileSet.empty()) {
|
|
|
|
for (string& file : files) {
|
|
|
|
fileSet.emplace(move(file));
|
|
|
|
}
|
|
|
|
files.clear();
|
|
|
|
moveNames(move(fileSet), files);
|
|
|
|
}
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2010-05-03 22:14:11 +03:00
|
|
|
string FileLister::operator[](uint x)
|
|
|
|
{
|
2014-08-14 06:02:24 +03:00
|
|
|
const auto dirCount = directories.size();
|
|
|
|
return x < dirCount ? directories[x] : files[x - dirCount];
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|