2010-09-25 08:54:27 +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
|
|
|
|
***************************************************************************/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2010-12-18 14:20:45 +01:00
|
|
|
#include <string>
|
|
|
|
#include <algorithm>
|
2011-01-30 17:22:16 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/dirent.h>
|
2010-09-25 08:54:27 +02:00
|
|
|
|
2011-02-14 19:52:36 +01:00
|
|
|
#define MAXPATHLEN 1024
|
|
|
|
|
2010-09-25 08:54:27 +02:00
|
|
|
#include "utils/StringTools.h"
|
|
|
|
#include "DirList.h"
|
|
|
|
|
2010-12-18 14:20:45 +01:00
|
|
|
DirList::DirList(const char * path, const char *filter, u32 flags)
|
2010-09-25 08:54:27 +02:00
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
this->LoadPath(path, filter, flags);
|
|
|
|
this->SortList();
|
2010-09-25 08:54:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DirList::~DirList()
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
ClearList();
|
2010-09-25 08:54:27 +02:00
|
|
|
}
|
|
|
|
|
2010-12-18 14:20:45 +01:00
|
|
|
bool DirList::LoadPath(const char * folder, const char *filter, u32 flags)
|
2010-09-25 08:54:27 +02:00
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if(!folder) return false;
|
2011-02-21 20:41:48 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
std::string folderpath(folder);
|
2011-02-21 20:41:48 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
return LoadPath(folderpath, filter, flags);
|
2011-02-21 20:41:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DirList::LoadPath(std::string &folderpath, const char *filter, u32 flags)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if(folderpath.size() < 3)
|
|
|
|
return false;
|
2010-12-18 14:20:45 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
struct dirent *dirent = NULL;
|
|
|
|
DIR *dir = NULL;
|
2010-09-25 08:54:27 +02:00
|
|
|
|
2010-12-18 14:20:45 +01:00
|
|
|
if(folderpath[folderpath.size()-1] == '/')
|
2011-07-26 00:28:22 +02:00
|
|
|
folderpath.erase(folderpath.size()-1);
|
2010-09-25 08:54:27 +02:00
|
|
|
|
2011-02-21 20:41:48 +01:00
|
|
|
bool isRoot = (folderpath.find('/') == std::string::npos);
|
|
|
|
if(isRoot)
|
2011-07-26 00:28:22 +02:00
|
|
|
folderpath += '/';
|
|
|
|
|
|
|
|
dir = opendir(folderpath.c_str());
|
|
|
|
if (dir == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
char * filename = new (std::nothrow) char[MAXPATHLEN];
|
|
|
|
struct stat *st = new (std::nothrow) struct stat;
|
|
|
|
if(!filename || !st)
|
|
|
|
{
|
|
|
|
delete [] filename;
|
|
|
|
delete st;
|
|
|
|
closedir(dir);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((dirent = readdir(dir)) != 0)
|
|
|
|
{
|
|
|
|
if(!dirent->d_name)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
snprintf(filename, MAXPATHLEN, "%s/%s", folderpath.c_str(), dirent->d_name);
|
|
|
|
|
|
|
|
if(stat(filename, st) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
snprintf(filename, MAXPATHLEN, dirent->d_name);
|
|
|
|
|
|
|
|
if(st->st_mode & S_IFDIR)
|
|
|
|
{
|
|
|
|
if(!(flags & Dirs))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(strcmp(filename,".") == 0 || strcmp(filename,"..") == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(flags & CheckSubfolders)
|
|
|
|
{
|
|
|
|
int length = folderpath.size();
|
|
|
|
if(!isRoot) folderpath += '/';
|
|
|
|
folderpath += filename;
|
|
|
|
LoadPath(folderpath, filter, flags);
|
|
|
|
folderpath.erase(length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(!(flags & Files))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(filter)
|
|
|
|
{
|
|
|
|
char * fileext = strrchr(filename, '.');
|
|
|
|
if(!fileext)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(strtokcmp(fileext, filter, ",") == 0)
|
|
|
|
AddEntrie(folderpath.c_str(), filename, st->st_size, (st->st_mode & S_IFDIR) ? true : false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AddEntrie(folderpath.c_str(), filename, st->st_size, (st->st_mode & S_IFDIR) ? true : false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
delete [] filename;
|
|
|
|
delete st;
|
|
|
|
|
|
|
|
return true;
|
2010-09-25 08:54:27 +02:00
|
|
|
}
|
|
|
|
|
2010-12-18 14:20:45 +01:00
|
|
|
void DirList::AddEntrie(const char * folderpath, const char * filename, u64 filesize, bool isDir)
|
2010-09-25 08:54:27 +02:00
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if(!folderpath || !filename)
|
|
|
|
return;
|
2010-09-25 08:54:27 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
int pos = FileInfo.size();
|
2010-09-25 08:54:27 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
FileInfo.resize(pos+1);
|
2010-09-25 08:54:27 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
FileInfo[pos].FilePath = new (std::nothrow) char[strlen(folderpath)+strlen(filename)+2];
|
|
|
|
if(FileInfo[pos].FilePath)
|
|
|
|
sprintf(FileInfo[pos].FilePath, "%s/%s", folderpath, filename);
|
|
|
|
FileInfo[pos].FileSize = filesize;
|
|
|
|
FileInfo[pos].isDir = isDir;
|
2010-09-25 08:54:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DirList::ClearList()
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
for(u32 i = 0; i < FileInfo.size(); ++i)
|
|
|
|
{
|
|
|
|
if(FileInfo[i].FilePath)
|
|
|
|
delete [] FileInfo[i].FilePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileInfo.clear();
|
|
|
|
std::vector<FileInfos>().swap(FileInfo);
|
2010-09-25 08:54:27 +02:00
|
|
|
}
|
|
|
|
|
2010-12-18 14:20:45 +01:00
|
|
|
const char * DirList::GetFilename(int ind)
|
2010-09-25 08:54:27 +02:00
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if (!valid(ind))
|
|
|
|
return NULL;
|
2010-09-25 08:54:27 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
return FullpathToFilename(FileInfo[ind].FilePath);
|
2010-09-25 08:54:27 +02:00
|
|
|
}
|
|
|
|
|
2010-12-18 14:20:45 +01:00
|
|
|
static bool SortCallback(const FileInfos & f1, const FileInfos & f2)
|
2010-09-25 08:54:27 +02:00
|
|
|
{
|
2010-12-18 14:20:45 +01:00
|
|
|
if(f1.isDir && !(f2.isDir)) return true;
|
|
|
|
if(!(f1.isDir) && f2.isDir) return false;
|
2010-09-25 08:54:27 +02:00
|
|
|
|
2011-02-21 20:41:48 +01:00
|
|
|
if(f1.FilePath && !f2.FilePath) return true;
|
|
|
|
if(!f1.FilePath) return false;
|
2010-12-18 14:20:45 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if(strcasecmp(f1.FilePath, f2.FilePath) > 0)
|
|
|
|
return false;
|
2010-09-25 08:54:27 +02:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
return true;
|
2010-09-25 08:54:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DirList::SortList()
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if(FileInfo.size() > 1)
|
|
|
|
std::sort(FileInfo.begin(), FileInfo.end(), SortCallback);
|
2011-02-21 20:41:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DirList::SortList(bool (*SortFunc)(const FileInfos &a, const FileInfos &b))
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if(FileInfo.size() > 1)
|
|
|
|
std::sort(FileInfo.begin(), FileInfo.end(), SortFunc);
|
2010-09-25 08:54:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int DirList::GetFileIndex(const char *filename)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if(!filename)
|
|
|
|
return -1;
|
2010-09-25 08:54:27 +02:00
|
|
|
|
2010-12-18 14:20:45 +01:00
|
|
|
for (u32 i = 0; i < FileInfo.size(); ++i)
|
2010-09-25 08:54:27 +02:00
|
|
|
{
|
2010-12-18 14:20:45 +01:00
|
|
|
if (strcasecmp(GetFilename(i), filename) == 0)
|
2010-09-25 08:54:27 +02:00
|
|
|
return i;
|
|
|
|
}
|
2010-12-18 14:20:45 +01:00
|
|
|
|
2010-09-25 08:54:27 +02:00
|
|
|
return -1;
|
|
|
|
}
|