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