WUMSLoader/source/fs/DirList.cpp

219 lines
5.7 KiB
C++
Raw Normal View History

2020-04-28 14:43:07 +02:00
/****************************************************************************
* Copyright (C) 2010
* by Dimok
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any
* damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any
* purpose, including commercial applications, and to alter it and
* redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you
* must not claim that you wrote the original software. If you use
* this software in a product, an acknowledgment in the product
* documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and
* must not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*
* DirList Class
* for WiiXplorer 2010
***************************************************************************/
2022-02-04 21:44:03 +01:00
#include <algorithm>
2020-04-28 14:43:07 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <strings.h>
#include <sys/dirent.h>
2022-02-04 21:44:03 +01:00
#include <sys/stat.h>
2020-04-28 14:43:07 +02:00
#include <fs/DirList.h>
#include <utils/StringTools.h>
DirList::DirList() {
2022-02-04 21:44:03 +01:00
Flags = 0;
2020-04-28 14:43:07 +02:00
Filter = 0;
2022-02-04 21:44:03 +01:00
Depth = 0;
2020-04-28 14:43:07 +02:00
}
2020-05-17 19:05:51 +02:00
DirList::DirList(const std::string &path, const char *filter, uint32_t flags, uint32_t maxDepth) {
2020-04-28 14:43:07 +02:00
this->LoadPath(path, filter, flags, maxDepth);
this->SortList();
}
DirList::~DirList() {
ClearList();
}
2020-05-17 19:05:51 +02:00
BOOL DirList::LoadPath(const std::string &folder, const char *filter, uint32_t flags, uint32_t maxDepth) {
if (folder.empty())
2020-04-28 14:43:07 +02:00
return false;
2022-02-04 21:44:03 +01:00
Flags = flags;
2020-04-28 14:43:07 +02:00
Filter = filter;
2022-02-04 21:44:03 +01:00
Depth = maxDepth;
2020-04-28 14:43:07 +02:00
std::string folderpath(folder);
uint32_t length = folderpath.size();
//! clear path of double slashes
StringTools::RemoveDoubleSlashs(folderpath);
//! remove last slash if exists
2020-05-17 19:05:51 +02:00
if (length > 0 && folderpath[length - 1] == '/')
folderpath.erase(length - 1);
2020-04-28 14:43:07 +02:00
//! add root slash if missing
2020-05-17 19:05:51 +02:00
if (folderpath.find('/') == std::string::npos) {
2020-04-28 14:43:07 +02:00
folderpath += '/';
}
return InternalLoadPath(folderpath);
}
BOOL DirList::InternalLoadPath(std::string &folderpath) {
2020-05-17 19:05:51 +02:00
if (folderpath.size() < 3)
2020-04-28 14:43:07 +02:00
return false;
struct dirent *dirent = NULL;
2022-02-04 21:44:03 +01:00
DIR *dir = NULL;
2020-04-28 14:43:07 +02:00
dir = opendir(folderpath.c_str());
if (dir == NULL)
return false;
while ((dirent = readdir(dir)) != 0) {
2022-02-04 21:44:03 +01:00
BOOL isDir = dirent->d_type & DT_DIR;
2020-04-28 14:43:07 +02:00
const char *filename = dirent->d_name;
2020-05-17 19:05:51 +02:00
if (isDir) {
if (strcmp(filename, ".") == 0 || strcmp(filename, "..") == 0)
2020-04-28 14:43:07 +02:00
continue;
2020-05-17 19:05:51 +02:00
if ((Flags & CheckSubfolders) && (Depth > 0)) {
2020-04-28 14:43:07 +02:00
int32_t length = folderpath.size();
2020-05-17 19:05:51 +02:00
if (length > 2 && folderpath[length - 1] != '/') {
2020-04-28 14:43:07 +02:00
folderpath += '/';
}
folderpath += filename;
Depth--;
InternalLoadPath(folderpath);
folderpath.erase(length);
Depth++;
}
2020-05-17 19:05:51 +02:00
if (!(Flags & Dirs))
2020-04-28 14:43:07 +02:00
continue;
2020-05-17 19:05:51 +02:00
} else if (!(Flags & Files)) {
2020-04-28 14:43:07 +02:00
continue;
}
2020-05-17 19:05:51 +02:00
if (Filter) {
char *fileext = strrchr(filename, '.');
if (!fileext)
2020-04-28 14:43:07 +02:00
continue;
2020-05-17 19:05:51 +02:00
if (StringTools::strtokcmp(fileext, Filter, ",") == 0)
2020-04-28 14:43:07 +02:00
AddEntrie(folderpath, filename, isDir);
} else {
AddEntrie(folderpath, filename, isDir);
}
}
closedir(dir);
return true;
}
2020-05-17 19:05:51 +02:00
void DirList::AddEntrie(const std::string &filepath, const char *filename, BOOL isDir) {
if (!filename)
2020-04-28 14:43:07 +02:00
return;
int32_t pos = FileInfo.size();
2020-05-17 19:05:51 +02:00
FileInfo.resize(pos + 1);
2020-04-28 14:43:07 +02:00
2020-05-17 19:05:51 +02:00
FileInfo[pos].FilePath = (char *) malloc(filepath.size() + strlen(filename) + 2);
if (!FileInfo[pos].FilePath) {
2020-04-28 14:43:07 +02:00
FileInfo.resize(pos);
return;
}
sprintf(FileInfo[pos].FilePath, "%s/%s", filepath.c_str(), filename);
FileInfo[pos].isDir = isDir;
}
void DirList::ClearList() {
2020-05-17 19:05:51 +02:00
for (uint32_t i = 0; i < FileInfo.size(); ++i) {
if (FileInfo[i].FilePath) {
2020-04-28 14:43:07 +02:00
free(FileInfo[i].FilePath);
FileInfo[i].FilePath = NULL;
}
}
FileInfo.clear();
std::vector<DirEntry>().swap(FileInfo);
}
2020-05-17 19:05:51 +02:00
const char *DirList::GetFilename(int32_t ind) const {
2020-04-28 14:43:07 +02:00
if (!valid(ind))
return "";
return StringTools::FullpathToFilename(FileInfo[ind].FilePath);
}
2020-05-17 19:05:51 +02:00
static BOOL SortCallback(const DirEntry &f1, const DirEntry &f2) {
if (f1.isDir && !(f2.isDir))
2020-04-28 14:43:07 +02:00
return true;
2020-05-17 19:05:51 +02:00
if (!(f1.isDir) && f2.isDir)
2020-04-28 14:43:07 +02:00
return false;
2020-05-17 19:05:51 +02:00
if (f1.FilePath && !f2.FilePath)
2020-04-28 14:43:07 +02:00
return true;
2020-05-17 19:05:51 +02:00
if (!f1.FilePath)
2020-04-28 14:43:07 +02:00
return false;
2020-05-17 19:05:51 +02:00
if (strcasecmp(f1.FilePath, f2.FilePath) > 0)
2020-04-28 14:43:07 +02:00
return false;
return true;
}
void DirList::SortList() {
2020-05-17 19:05:51 +02:00
if (FileInfo.size() > 1)
2020-04-28 14:43:07 +02:00
std::sort(FileInfo.begin(), FileInfo.end(), SortCallback);
}
void DirList::SortList(BOOL (*SortFunc)(const DirEntry &a, const DirEntry &b)) {
2020-05-17 19:05:51 +02:00
if (FileInfo.size() > 1)
2020-04-28 14:43:07 +02:00
std::sort(FileInfo.begin(), FileInfo.end(), SortFunc);
}
uint64_t DirList::GetFilesize(int32_t index) const {
struct stat st;
const char *path = GetFilepath(index);
2020-05-17 19:05:51 +02:00
if (!path || stat(path, &st) != 0)
2020-04-28 14:43:07 +02:00
return 0;
return st.st_size;
}
int32_t DirList::GetFileIndex(const char *filename) const {
2020-05-17 19:05:51 +02:00
if (!filename)
2020-04-28 14:43:07 +02:00
return -1;
for (uint32_t i = 0; i < FileInfo.size(); ++i) {
if (strcasecmp(GetFilename(i), filename) == 0)
return i;
}
return -1;
}