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
|
|
|
|
|
|
|
#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
|
|
|
{
|
2010-12-18 14:20:45 +01:00
|
|
|
this->LoadPath(path, filter, flags);
|
2010-09-25 08:54:27 +02:00
|
|
|
this->SortList();
|
|
|
|
}
|
|
|
|
|
|
|
|
DirList::~DirList()
|
|
|
|
{
|
|
|
|
ClearList();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2010-12-18 14:20:45 +01:00
|
|
|
if(!folder)
|
|
|
|
return false;
|
|
|
|
|
2010-09-25 08:54:27 +02:00
|
|
|
struct stat st;
|
2011-01-30 17:22:16 +01:00
|
|
|
struct dirent *dirent = NULL;
|
|
|
|
DIR *dir = NULL;
|
2010-12-18 14:20:45 +01:00
|
|
|
std::string folderpath = folder;
|
2010-09-25 08:54:27 +02:00
|
|
|
|
2010-12-18 14:20:45 +01:00
|
|
|
if(folderpath[folderpath.size()-1] == '/')
|
2010-12-26 18:02:14 +01:00
|
|
|
folderpath.erase(folderpath.size()-1, 1);
|
2010-09-25 08:54:27 +02:00
|
|
|
|
2010-12-26 18:02:14 +01:00
|
|
|
const char * notRoot = strchr(folderpath.c_str(), '/');
|
2010-09-25 08:54:27 +02:00
|
|
|
if(!notRoot)
|
2010-12-18 14:20:45 +01:00
|
|
|
folderpath += '/';
|
2010-09-25 08:54:27 +02:00
|
|
|
|
2011-01-30 17:22:16 +01:00
|
|
|
dir = opendir(folderpath.c_str());
|
2010-09-25 08:54:27 +02:00
|
|
|
if (dir == NULL)
|
|
|
|
return false;
|
|
|
|
|
2010-12-18 14:20:45 +01:00
|
|
|
char * filename = new (std::nothrow) char[1024];
|
|
|
|
if(!filename)
|
|
|
|
{
|
2011-01-30 17:22:16 +01:00
|
|
|
closedir(dir);
|
2010-12-18 14:20:45 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-30 17:22:16 +01:00
|
|
|
while ((dirent = readdir(dir)) != 0)
|
2010-09-25 08:54:27 +02:00
|
|
|
{
|
2011-01-30 17:22:16 +01:00
|
|
|
snprintf(filename, 1024, "%s/%s", folderpath.c_str(), dirent->d_name);
|
|
|
|
|
|
|
|
if(stat(filename, &st) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
snprintf(filename, 1024, dirent->d_name);
|
|
|
|
|
2010-12-18 14:20:45 +01:00
|
|
|
if(st.st_mode & S_IFDIR)
|
2010-09-25 08:54:27 +02:00
|
|
|
{
|
2010-12-18 14:20:45 +01:00
|
|
|
if(!(flags & Dirs))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(strcmp(filename,".") == 0 || strcmp(filename,"..") == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(flags & CheckSubfolders)
|
2010-09-25 08:54:27 +02:00
|
|
|
{
|
2010-12-18 14:20:45 +01:00
|
|
|
std::string newFolder = folderpath;
|
|
|
|
if(notRoot) newFolder += '/';
|
|
|
|
newFolder += filename;
|
|
|
|
LoadPath(newFolder.c_str(), filter, flags);
|
2010-09-25 08:54:27 +02:00
|
|
|
}
|
|
|
|
}
|
2010-12-18 14:20:45 +01:00
|
|
|
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);
|
|
|
|
}
|
2010-09-25 08:54:27 +02:00
|
|
|
}
|
2011-01-30 17:22:16 +01:00
|
|
|
closedir(dir);
|
2010-12-18 14:20:45 +01:00
|
|
|
delete [] filename;
|
2010-09-25 08:54:27 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2010-12-18 14:20:45 +01:00
|
|
|
if(!folderpath || !filename)
|
|
|
|
return;
|
2010-09-25 08:54:27 +02:00
|
|
|
|
2010-12-18 14:20:45 +01:00
|
|
|
int pos = FileInfo.size();
|
2010-09-25 08:54:27 +02:00
|
|
|
|
2010-12-18 14:20:45 +01:00
|
|
|
FileInfo.resize(pos+1);
|
2010-09-25 08:54:27 +02:00
|
|
|
|
*Completely rewrote the asynchron GuiImageData class (problems from switch Carousel<->Grid layour should be fixed now)
*Fixed bug mounting a wbfs partition which was formatted from an ext partition
*Rewrote the parental control feature. Removed loading pin or settings from the Wii Settings. Parental control is now completely managed in the loader from the settings selected and the password set.
*Saving password in config file is now encrypted
*Added loop to wait for usb when reloading the cIOS before game start
The parental control feature is filtering games like following when usb loader is locked:
level 0 (everyone 0+) > shows only games with lvl 0
level 1 (childs 7+) > shows games with lvl 0, 1
level 2 (teens 12+) > shows games with lvl 0, 1, 2
level 3 (mature 16+) > shows games with lvl 0, 1, 2, 3
level 4 (adults only 18+) > shows all games (lvl 0, 1, 2, 3, 4)
level 4 is default when creating new configs
2010-12-19 19:20:33 +01:00
|
|
|
FileInfo[pos].FilePath = new (std::nothrow) char[strlen(folderpath)+strlen(filename)+2];
|
2010-12-18 14:20:45 +01:00
|
|
|
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()
|
|
|
|
{
|
2010-12-18 14:20:45 +01:00
|
|
|
for(u32 i = 0; i < FileInfo.size(); ++i)
|
2010-09-25 08:54:27 +02:00
|
|
|
{
|
|
|
|
if(FileInfo[i].FilePath)
|
2010-12-18 14:20:45 +01:00
|
|
|
delete [] FileInfo[i].FilePath;
|
2010-09-25 08:54:27 +02:00
|
|
|
}
|
|
|
|
|
2010-12-18 14:20:45 +01:00
|
|
|
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
|
|
|
{
|
2010-12-18 14:20:45 +01:00
|
|
|
if (!valid(ind))
|
2010-09-25 08:54:27 +02:00
|
|
|
return NULL;
|
|
|
|
|
2010-12-18 14:20:45 +01: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
|
|
|
|
2010-12-18 14:20:45 +01:00
|
|
|
const char * Filename1 = FullpathToFilename(f1.FilePath);
|
|
|
|
const char * Filename2 = FullpathToFilename(f2.FilePath);
|
2010-09-25 08:54:27 +02:00
|
|
|
|
2010-12-18 14:20:45 +01:00
|
|
|
if(Filename1 && !Filename2) return true;
|
|
|
|
if(!Filename1 && Filename2) return false;
|
|
|
|
|
|
|
|
if(strcasecmp(Filename1, Filename2) > 0)
|
|
|
|
return false;
|
2010-09-25 08:54:27 +02:00
|
|
|
|
2010-12-18 14:20:45 +01:00
|
|
|
return true;
|
2010-09-25 08:54:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DirList::SortList()
|
|
|
|
{
|
2010-12-18 14:20:45 +01:00
|
|
|
std::sort(FileInfo.begin(), FileInfo.end(), SortCallback);
|
2010-09-25 08:54:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int DirList::GetFileIndex(const char *filename)
|
|
|
|
{
|
2010-12-18 14:20:45 +01:00
|
|
|
if(!filename)
|
2010-09-25 08:54:27 +02:00
|
|
|
return -1;
|
|
|
|
|
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;
|
|
|
|
}
|