usbloadergx/source/homebrewboot/HomebrewFiles.cpp
giantpune 30535c6f5d *code beautification*
formatted the code to make it easier to read.  no functional changes at all.

i didn't put anything from the libwiigui folder or banner folder in the beautifier.

my automated .bat seems to have done a good job.  the only places i see it fucked up was on (GXColor){blablabla}.  it treated the brackets in the color like all the other brackets and put the color on a new line and indented it.  i think i fixed most of them.  not sure if it messed up anywhere else.  also not sure about how it handled different linebreaks.  it looks fine on windows.  if it looks messed up on linux, it can be reverted.

the code still compiles and runs fine.
2009-07-30 05:41:12 +00:00

117 lines
3.2 KiB
C++

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