usbloadergx/source/homebrewboot/HomebrewFiles.cpp

134 lines
3.2 KiB
C++
Raw Normal View History

/****************************************************************************
* HomebrewFiles Class
* for USB Loader GX
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/dir.h>
#include "HomebrewFiles.h"
2010-09-24 02:48:03 +02:00
HomebrewFiles::HomebrewFiles(const char * path)
{
filecount = 0;
2010-09-24 02:48:03 +02:00
FileInfo = (FileInfos *) malloc(sizeof(FileInfos));
if (!FileInfo)
{
return;
}
2010-09-24 02:48:03 +02:00
memset(&FileInfo[filecount], 0, sizeof(FileInfos));
2010-09-24 02:48:03 +02:00
this->LoadPath(path);
this->SortList();
}
HomebrewFiles::~HomebrewFiles()
{
2010-09-24 02:48:03 +02:00
if (FileInfo)
{
2010-09-24 02:48:03 +02:00
free(FileInfo);
FileInfo = NULL;
}
}
2010-09-24 02:48:03 +02:00
bool HomebrewFiles::LoadPath(const char * folderpath)
{
struct stat st;
DIR_ITER *dir = NULL;
char filename[1024];
2010-09-24 02:48:03 +02:00
dir = diropen(folderpath);
if (dir == NULL)
{
return false;
}
2010-09-24 02:48:03 +02:00
while (dirnext(dir, filename, &st) == 0)
{
2010-09-24 02:48:03 +02:00
if ((st.st_mode & S_IFDIR) != 0)
{
2010-09-24 02:48:03 +02:00
if (strcmp(filename, ".") != 0 && strcmp(filename, "..") != 0)
{
char currentname[200];
2010-09-24 02:48:03 +02:00
snprintf(currentname, sizeof(currentname), "%s%s/", folderpath, filename);
this->LoadPath(currentname);
}
}
else
{
char temp[5];
2010-09-24 02:48:03 +02:00
for (int i = 0; i < 5; i++)
{
2010-09-24 02:48:03 +02:00
temp[i] = filename[strlen(filename) - 4 + i];
}
2010-09-24 02:48:03 +02:00
if ((strncasecmp(temp, ".dol", 4) == 0 || strncasecmp(temp, ".elf", 4) == 0) && filecount < MAXHOMEBREWS
&& filename[0] != '.')
{
2010-09-24 02:48:03 +02:00
FileInfo = (FileInfos *) realloc(FileInfo, (filecount + 1) * sizeof(FileInfos));
2010-09-24 02:48:03 +02:00
if (!FileInfo)
{
2010-09-24 02:48:03 +02:00
free(FileInfo);
FileInfo = NULL;
filecount = 0;
2010-09-24 02:48:03 +02:00
dirclose(dir);
return false;
}
2010-09-24 02:48:03 +02:00
memset(&(FileInfo[filecount]), 0, sizeof(FileInfo));
2010-09-24 02:48:03 +02:00
strlcpy(FileInfo[filecount].FilePath, folderpath, sizeof(FileInfo[filecount].FilePath));
strlcpy(FileInfo[filecount].FileName, filename, sizeof(FileInfo[filecount].FileName));
FileInfo[filecount].FileSize = st.st_size;
filecount++;
}
}
}
2010-09-24 02:48:03 +02:00
dirclose(dir);
return true;
}
2010-09-24 02:48:03 +02:00
char * HomebrewFiles::GetFilename(int ind)
{
2010-09-24 02:48:03 +02:00
if (ind > filecount)
return NULL;
2010-09-24 02:48:03 +02:00
else return FileInfo[ind].FileName;
}
2010-09-24 02:48:03 +02:00
char * HomebrewFiles::GetFilepath(int ind)
{
2010-09-24 02:48:03 +02:00
if (ind > filecount)
return NULL;
2010-09-24 02:48:03 +02:00
else return FileInfo[ind].FilePath;
}
2010-09-24 02:48:03 +02:00
unsigned int HomebrewFiles::GetFilesize(int ind)
{
2010-09-24 02:48:03 +02:00
if (ind > filecount || !filecount || !FileInfo)
return 0;
2010-09-24 02:48:03 +02:00
else return FileInfo[ind].FileSize;
}
int HomebrewFiles::GetFilecount()
{
return filecount;
}
2010-09-24 02:48:03 +02:00
static int ListCompare(const void *a, const void *b)
{
2010-09-24 02:48:03 +02:00
FileInfos *ab = (FileInfos*) a;
FileInfos *bb = (FileInfos*) b;
2010-09-24 02:48:03 +02:00
return stricmp((char *) ab->FilePath, (char *) bb->FilePath);
}
void HomebrewFiles::SortList()
{
2010-09-24 02:48:03 +02:00
qsort(FileInfo, filecount, sizeof(FileInfos), ListCompare);
}