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-17 11:05:21 +03:00
|
|
|
bool 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;
|
2014-08-17 11:22:09 +03:00
|
|
|
if (!path.empty() && path[path.length() - 1] != '/') {
|
2014-08-13 08:08:04 +03:00
|
|
|
slashedPath.push_back('/');
|
2014-08-17 11:22:09 +03:00
|
|
|
}
|
2014-08-13 08:08:04 +03:00
|
|
|
|
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-17 11:05:21 +03:00
|
|
|
return false;
|
2014-08-13 08:08:04 +03:00
|
|
|
}
|
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] == '.') {
|
2014-08-17 11:15:15 +03:00
|
|
|
if (!(dptr->d_name[1] == '.' && showUpdir && slashedPath != "/")) {
|
2014-08-14 05:45:26 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2014-08-13 08:08:04 +03:00
|
|
|
|
2014-08-20 14:09:49 +03:00
|
|
|
bool isDir, isFile;
|
2014-08-14 07:10:55 +03:00
|
|
|
#ifdef _DIRENT_HAVE_D_TYPE
|
2014-08-20 14:07:11 +03:00
|
|
|
if (dptr->d_type != DT_UNKNOWN && dptr->d_type != DT_LNK) {
|
2014-08-14 06:25:26 +03:00
|
|
|
isDir = dptr->d_type == DT_DIR;
|
2014-08-20 14:09:49 +03:00
|
|
|
isFile = dptr->d_type == DT_REG;
|
2014-08-14 06:25:26 +03:00
|
|
|
} 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-20 14:09:49 +03:00
|
|
|
isFile = S_ISREG(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-20 14:09:49 +03:00
|
|
|
} else if (isFile) {
|
2014-08-13 08:08:04 +03:00
|
|
|
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 07:43:37 +03:00
|
|
|
// Determine file extension.
|
|
|
|
const char *ext = strrchr(dptr->d_name, '.');
|
|
|
|
if (ext) ext++; else ext = "";
|
|
|
|
|
|
|
|
for (auto& filterExt : filter) {
|
|
|
|
// Note: strcasecmp can't compare multi-byte UTF-8 characters,
|
|
|
|
// but the filtered file extensions don't contain any of
|
|
|
|
// those.
|
|
|
|
if (strcasecmp(ext, filterExt.c_str()) == 0) {
|
|
|
|
fileSet.insert(string(dptr->d_name));
|
2014-08-13 08:08:04 +03:00
|
|
|
break;
|
2013-07-08 09:21:42 +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);
|
|
|
|
}
|
2014-08-17 11:05:21 +03:00
|
|
|
|
|
|
|
return true;
|
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
|
|
|
}
|