mirror of
git://projects.qi-hardware.com/gmenu2x.git
synced 2024-11-25 09:05:21 +02:00
filelister.cpp: Small cleanup
This commit is contained in:
parent
a37ed02f8f
commit
2a6a1f0041
@ -30,31 +30,40 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
FileLister::FileLister(const string &startPath, bool showDirectories, bool showFiles) {
|
FileLister::FileLister(const string &startPath, bool showDirectories,
|
||||||
this->showDirectories = showDirectories;
|
bool showFiles) : showDirectories(showDirectories), showFiles(showFiles)
|
||||||
this->showFiles = showFiles;
|
{
|
||||||
setPath(startPath,false);
|
setPath(startPath, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
const string &FileLister::getPath() {
|
const string &FileLister::getPath()
|
||||||
|
{
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
void FileLister::setPath(const string &path, bool doBrowse) {
|
|
||||||
|
void FileLister::setPath(const string &path, bool doBrowse)
|
||||||
|
{
|
||||||
this->path = path;
|
this->path = path;
|
||||||
if (this->path[path.length()-1]!='/')
|
|
||||||
|
if (this->path[path.length() - 1]!='/')
|
||||||
this->path += "/";
|
this->path += "/";
|
||||||
|
|
||||||
if (doBrowse)
|
if (doBrowse)
|
||||||
browse();
|
browse();
|
||||||
}
|
}
|
||||||
|
|
||||||
const string &FileLister::getFilter() {
|
const string &FileLister::getFilter()
|
||||||
|
{
|
||||||
return filter;
|
return filter;
|
||||||
}
|
}
|
||||||
void FileLister::setFilter(const string &filter) {
|
|
||||||
|
void FileLister::setFilter(const string &filter)
|
||||||
|
{
|
||||||
this->filter = filter;
|
this->filter = filter;
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileLister::browse() {
|
void FileLister::browse()
|
||||||
|
{
|
||||||
directories.clear();
|
directories.clear();
|
||||||
files.clear();
|
files.clear();
|
||||||
|
|
||||||
@ -66,7 +75,7 @@ void FileLister::browse() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
vector<string> vfilter;
|
vector<string> vfilter;
|
||||||
split(vfilter,getFilter(),",");
|
split(vfilter, getFilter(), ",");
|
||||||
|
|
||||||
string filepath, file;
|
string filepath, file;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
@ -74,8 +83,11 @@ void FileLister::browse() {
|
|||||||
|
|
||||||
while ((dptr = readdir(dirp))) {
|
while ((dptr = readdir(dirp))) {
|
||||||
file = dptr->d_name;
|
file = dptr->d_name;
|
||||||
if (file[0]=='.' && file!="..") continue;
|
|
||||||
filepath = path+file;
|
if (file[0] == '.' && file != "..")
|
||||||
|
continue;
|
||||||
|
|
||||||
|
filepath = path + file;
|
||||||
int statRet = stat(filepath.c_str(), &st);
|
int statRet = stat(filepath.c_str(), &st);
|
||||||
if (statRet == -1) {
|
if (statRet == -1) {
|
||||||
cout << "\033[0;34mGMENU2X:\033[0;31m stat failed on '" << filepath << "' with error '" << strerror(errno) << "'\033[0m" << endl;
|
cout << "\033[0;34mGMENU2X:\033[0;31m stat failed on '" << filepath << "' with error '" << strerror(errno) << "'\033[0m" << endl;
|
||||||
@ -86,52 +98,63 @@ void FileLister::browse() {
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (S_ISDIR(st.st_mode)) {
|
if (S_ISDIR(st.st_mode)) {
|
||||||
if (!showDirectories) continue;
|
if (!showDirectories)
|
||||||
#ifdef TARGET_GP2X
|
continue;
|
||||||
// if (!(path=="/card/" && (file!="sd" && file!="ext" && file!="nand")))
|
directories.push_back(file);
|
||||||
#endif
|
|
||||||
directories.push_back(file);
|
|
||||||
} else {
|
} else {
|
||||||
if (!showFiles) continue;
|
if (!showFiles)
|
||||||
bool filterOk = false;
|
continue;
|
||||||
for (uint i = 0; i<vfilter.size() && !filterOk; i++)
|
for (vector<string>::iterator it = vfilter.begin(); it != vfilter.end(); ++it) {
|
||||||
if (vfilter[i].length()<=file.length())
|
if (it->length() <= file.length()) {
|
||||||
filterOk = file.substr(file.length()-vfilter[i].length(),vfilter[i].length())==vfilter[i];
|
if (file.compare(file.length() - it->length(), it->length(), *it) == 0) {
|
||||||
if (filterOk) files.push_back(file);
|
files.push_back(file);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
closedir(dirp);
|
closedir(dirp);
|
||||||
sort(files.begin(),files.end(),case_less());
|
sort(files.begin(), files.end(), case_less());
|
||||||
sort(directories.begin(),directories.end(),case_less());
|
sort(directories.begin(), directories.end(), case_less());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint FileLister::size() {
|
unsigned int FileLister::size()
|
||||||
return files.size()+directories.size();
|
{
|
||||||
|
return files.size() + directories.size();
|
||||||
}
|
}
|
||||||
uint FileLister::dirCount() {
|
|
||||||
|
unsigned int FileLister::dirCount()
|
||||||
|
{
|
||||||
return directories.size();
|
return directories.size();
|
||||||
}
|
}
|
||||||
uint FileLister::fileCount() {
|
|
||||||
|
unsigned int FileLister::fileCount()
|
||||||
|
{
|
||||||
return files.size();
|
return files.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
string FileLister::operator[](uint x) {
|
string FileLister::operator[](uint x)
|
||||||
|
{
|
||||||
return at(x);
|
return at(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
string FileLister::at(uint x) {
|
string FileLister::at(uint x)
|
||||||
if (x<directories.size())
|
{
|
||||||
|
if (x < directories.size())
|
||||||
return directories[x];
|
return directories[x];
|
||||||
else
|
else
|
||||||
return files[x-directories.size()];
|
return files[x-directories.size()];
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FileLister::isFile(uint x) {
|
bool FileLister::isFile(unsigned int x)
|
||||||
return x>=directories.size() && x<size();
|
{
|
||||||
|
return x >= directories.size() && x < size();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FileLister::isDirectory(uint x) {
|
bool FileLister::isDirectory(unsigned int x)
|
||||||
return x<directories.size();
|
{
|
||||||
|
return x < directories.size();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user