1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-25 11:25:18 +02:00

filelister.cpp: Small cleanup

This commit is contained in:
Lars-Peter Clausen 2010-05-03 21:14:11 +02:00
parent a37ed02f8f
commit 2a6a1f0041

View File

@ -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();
@ -74,7 +83,10 @@ void FileLister::browse() {
while ((dptr = readdir(dirp))) { while ((dptr = readdir(dirp))) {
file = dptr->d_name; file = dptr->d_name;
if (file[0]=='.' && file!="..") continue;
if (file[0] == '.' && file != "..")
continue;
filepath = path + file; filepath = path + file;
int statRet = stat(filepath.c_str(), &st); int statRet = stat(filepath.c_str(), &st);
if (statRet == -1) { if (statRet == -1) {
@ -86,18 +98,20 @@ 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")))
#endif
directories.push_back(file); 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;
}
}
}
} }
} }
@ -107,31 +121,40 @@ void FileLister::browse() {
} }
} }
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();
} }