2010-02-04 13:33:47 +02:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2006 by Massimiliano Torromeo *
|
|
|
|
* massimiliano.torromeo@gmail.com *
|
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
//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>
|
2010-02-04 13:33:47 +02:00
|
|
|
|
|
|
|
#include "filelister.h"
|
|
|
|
#include "utilities.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2010-05-03 22:14:11 +03:00
|
|
|
FileLister::FileLister(const string &startPath, bool showDirectories,
|
|
|
|
bool showFiles) : showDirectories(showDirectories), showFiles(showFiles)
|
|
|
|
{
|
|
|
|
setPath(startPath, false);
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2010-05-03 22:14:11 +03:00
|
|
|
const string &FileLister::getPath()
|
|
|
|
{
|
2010-02-04 13:33:47 +02:00
|
|
|
return path;
|
|
|
|
}
|
2010-05-03 22:14:11 +03:00
|
|
|
|
|
|
|
void FileLister::setPath(const string &path, bool doBrowse)
|
|
|
|
{
|
2010-02-04 13:33:47 +02:00
|
|
|
this->path = path;
|
2010-05-03 22:14:11 +03:00
|
|
|
|
|
|
|
if (this->path[path.length() - 1]!='/')
|
2010-05-02 15:29:09 +03:00
|
|
|
this->path += "/";
|
2010-05-03 22:14:11 +03:00
|
|
|
|
2010-02-04 13:33:47 +02:00
|
|
|
if (doBrowse)
|
|
|
|
browse();
|
|
|
|
}
|
|
|
|
|
2010-05-03 22:14:11 +03:00
|
|
|
const string &FileLister::getFilter()
|
|
|
|
{
|
2010-02-04 13:33:47 +02:00
|
|
|
return filter;
|
|
|
|
}
|
2010-05-03 22:14:11 +03:00
|
|
|
|
|
|
|
void FileLister::setFilter(const string &filter)
|
|
|
|
{
|
2010-02-04 13:33:47 +02:00
|
|
|
this->filter = filter;
|
|
|
|
}
|
|
|
|
|
2010-05-03 22:14:11 +03:00
|
|
|
void FileLister::browse()
|
|
|
|
{
|
2010-02-04 13:33:47 +02:00
|
|
|
directories.clear();
|
|
|
|
files.clear();
|
|
|
|
|
|
|
|
if (showDirectories || showFiles) {
|
|
|
|
DIR *dirp;
|
|
|
|
if ((dirp = opendir(path.c_str())) == NULL) {
|
|
|
|
cout << "Error: opendir(" << path << ")" << endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> vfilter;
|
2010-05-03 22:14:11 +03:00
|
|
|
split(vfilter, getFilter(), ",");
|
2010-02-04 13:33:47 +02:00
|
|
|
|
|
|
|
string filepath, file;
|
|
|
|
struct stat st;
|
|
|
|
struct dirent *dptr;
|
|
|
|
|
|
|
|
while ((dptr = readdir(dirp))) {
|
|
|
|
file = dptr->d_name;
|
2010-05-03 22:14:11 +03:00
|
|
|
|
|
|
|
if (file[0] == '.' && file != "..")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
filepath = path + file;
|
2010-02-04 13:33:47 +02:00
|
|
|
int statRet = stat(filepath.c_str(), &st);
|
|
|
|
if (statRet == -1) {
|
|
|
|
cout << "\033[0;34mGMENU2X:\033[0;31m stat failed on '" << filepath << "' with error '" << strerror(errno) << "'\033[0m" << endl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (find(exclude.begin(), exclude.end(), file) != exclude.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
2010-05-03 22:14:11 +03:00
|
|
|
if (!showDirectories)
|
|
|
|
continue;
|
|
|
|
directories.push_back(file);
|
2010-02-04 13:33:47 +02:00
|
|
|
} else {
|
2010-05-03 22:14:11 +03:00
|
|
|
if (!showFiles)
|
|
|
|
continue;
|
|
|
|
for (vector<string>::iterator it = vfilter.begin(); it != vfilter.end(); ++it) {
|
|
|
|
if (it->length() <= file.length()) {
|
|
|
|
if (file.compare(file.length() - it->length(), it->length(), *it) == 0) {
|
|
|
|
files.push_back(file);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dirp);
|
2010-05-03 22:14:11 +03:00
|
|
|
sort(files.begin(), files.end(), case_less());
|
|
|
|
sort(directories.begin(), directories.end(), case_less());
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-03 22:14:11 +03:00
|
|
|
unsigned int FileLister::size()
|
|
|
|
{
|
|
|
|
return files.size() + directories.size();
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
2010-05-03 22:14:11 +03:00
|
|
|
|
|
|
|
unsigned int FileLister::dirCount()
|
|
|
|
{
|
2010-02-04 13:33:47 +02:00
|
|
|
return directories.size();
|
|
|
|
}
|
2010-05-03 22:14:11 +03:00
|
|
|
|
|
|
|
unsigned int FileLister::fileCount()
|
|
|
|
{
|
2010-02-04 13:33:47 +02:00
|
|
|
return files.size();
|
|
|
|
}
|
|
|
|
|
2010-05-03 22:14:11 +03:00
|
|
|
string FileLister::operator[](uint x)
|
|
|
|
{
|
2010-02-04 13:33:47 +02:00
|
|
|
return at(x);
|
|
|
|
}
|
|
|
|
|
2010-05-03 22:14:11 +03:00
|
|
|
string FileLister::at(uint x)
|
|
|
|
{
|
|
|
|
if (x < directories.size())
|
2010-02-04 13:33:47 +02:00
|
|
|
return directories[x];
|
|
|
|
else
|
|
|
|
return files[x-directories.size()];
|
|
|
|
}
|
|
|
|
|
2010-05-03 22:14:11 +03:00
|
|
|
bool FileLister::isFile(unsigned int x)
|
|
|
|
{
|
|
|
|
return x >= directories.size() && x < size();
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2010-05-03 22:14:11 +03:00
|
|
|
bool FileLister::isDirectory(unsigned int x)
|
|
|
|
{
|
|
|
|
return x < directories.size();
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|