* use more c++

* use less memory
This commit is contained in:
giantpune 2010-09-19 13:03:16 +00:00
parent dd4704cce3
commit bb234a9a51
4 changed files with 59 additions and 85 deletions

View File

@ -112,7 +112,7 @@ bool TitleSelector( char output[] )
char id[ 5 ]; char id[ 5 ];
titles.AsciiTID( tid, ( char* )&id ); titles.AsciiTID( tid, ( char* )&id );
char* name = titles.NameOf( tid ); const char* name = titles.NameOf( tid );
//gprintf("%016llx: %s: %s\n%p\t%p\n", tid, id, name, &id, name ); //gprintf("%016llx: %s: %s\n%p\t%p\n", tid, id, name, &id, name );
options4.SetName( i, "%s", id ); options4.SetName( i, "%s", id );
@ -309,7 +309,7 @@ int TitleBrowser()
gprintf( "shit happened3\n" ); gprintf( "shit happened3\n" );
break; break;
} }
//gprintf("[ %u] tid: %016llx\t%s\n", i, tid, titles.NameOf( tid ) ); gprintf("[ %u ] tid: %016llx\t%s\n", i, tid, titles.NameOf( tid ) );
if ( !titles.Exists( tid ) ) if ( !titles.Exists( tid ) )
{ {
@ -319,7 +319,7 @@ int TitleBrowser()
char id[ 5 ]; char id[ 5 ];
titles.AsciiTID( tid, ( char* )&id ); titles.AsciiTID( tid, ( char* )&id );
char* name = titles.NameOf( tid ); const char* name = titles.NameOf( tid );
options3.SetName( i, "%s", id ); options3.SetName( i, "%s", id );
options3.SetValue( i, "%s", name ? titles.NameOf( tid ) : tr( "Unknown" ) ); options3.SetValue( i, "%s", name ? titles.NameOf( tid ) : tr( "Unknown" ) );
@ -345,7 +345,7 @@ int TitleBrowser()
char id[ 5 ]; char id[ 5 ];
titles.AsciiTID( tid, ( char* )&id ); titles.AsciiTID( tid, ( char* )&id );
char* name = titles.NameOf( tid ); const char* name = titles.NameOf( tid );
options3.SetName( i, "%s", id ); options3.SetName( i, "%s", id );
options3.SetValue( i, "%s", name ? titles.NameOf( tid ) : tr( "Unknown" ) ); options3.SetValue( i, "%s", name ? titles.NameOf( tid ) : tr( "Unknown" ) );

View File

@ -1101,7 +1101,7 @@ int MenuSettings()
int haveTitle = titles.FindU32( Settings.returnTo ); int haveTitle = titles.FindU32( Settings.returnTo );
if( haveTitle >= 0 ) if( haveTitle >= 0 )
{ {
name = titles.NameFromIndex( haveTitle ); name = (char*)titles.NameFromIndex( haveTitle );
if( !strlen( name ) ) if( !strlen( name ) )
name = NULL; name = NULL;
} }

View File

@ -27,43 +27,24 @@ static u64 atoi_hex( const char *s ) {
NandTitle::NandTitle() NandTitle::NandTitle()
{ {
numTitles = 0;
currentIndex = 0; currentIndex = 0;
currentType = 0; currentType = 0;
list = NULL;
nameList = NULL;
} }
NandTitle::~NandTitle() NandTitle::~NandTitle()
{ {
if ( list ) titleIds.clear();
{ NameList.clear();
free( list );
list = NULL;
}
if ( nameList )
{
free( nameList );
nameList = NULL;
}
} }
s32 NandTitle::Get() s32 NandTitle::Get()
{ {
s32 ret; s32 ret;
u64 *list = NULL;
u32 numTitles = 0;
if ( list ) titleIds.clear();
{ NameList.clear();
free( list );
list = NULL;
}
if ( nameList )
{
free( nameList );
nameList = NULL;
}
ret = ES_GetNumTitles( &numTitles ); ret = ES_GetNumTitles( &numTitles );
if ( ret < 0 ) if ( ret < 0 )
@ -82,13 +63,13 @@ s32 NandTitle::Get()
return WII_EINTERNAL; return WII_EINTERNAL;
} }
nameList = ( char* )malloc( IMET_MAX_NAME_LEN * numTitles ); for( u32 i = 0; i < numTitles; i++ )
if ( !nameList )
{ {
free( nameList ); titleIds.push_back( list[ i ] );
return -2;
} }
memset( nameList, 0, IMET_MAX_NAME_LEN * numTitles );
free( list );
MagicPatches( 1 ); MagicPatches( 1 );
int language = CONF_GetLanguage(); int language = CONF_GetLanguage();
@ -96,15 +77,13 @@ s32 NandTitle::Get()
wchar_t name[ IMET_MAX_NAME_LEN ]; wchar_t name[ IMET_MAX_NAME_LEN ];
for ( u32 i = 0; i < numTitles; i++ ) for ( u32 i = 0; i < titleIds.size(); i++ )
{ {
bool r = GetName( list[ i ], language, name ); bool r = GetName( titleIds.at( i ), language, name );
if ( r ) if ( r )
{ {
wString *wsname = new wString( name ); wString wsname( name );
memcpy( nameList + ( IMET_MAX_NAME_LEN * i ), ( wsname->toUTF8() ).c_str(), strlen( ( wsname->toUTF8() ).c_str() ) ); NameList[ titleIds.at( i ) ] = wsname.toUTF8();
//gprintf("adding: %s\n", (wsname->toUTF8()).c_str() );
delete wsname;
} }
} }
@ -270,64 +249,57 @@ bool NandTitle::Exists( u64 tid )
bool NandTitle::ExistsFromIndex( u32 i ) bool NandTitle::ExistsFromIndex( u32 i )
{ {
if ( i > numTitles || i < 0 ) if ( i > titleIds.size() || i < 0 )
return false; return false;
return Exists( list[ i ] ); return Exists( titleIds.at( i ) );
} }
u64 NandTitle::At( u32 i ) u64 NandTitle::At( u32 i )
{ {
if ( i > numTitles || i < 0 ) if ( i > titleIds.size() || i < 0 )
return 0; return 0;
return list[ i ]; return titleIds.at( i );
} }
int NandTitle::IndexOf( u64 tid ) int NandTitle::IndexOf( u64 tid )
{ {
for ( u32 i = 0; i < numTitles; i++ ) for ( u32 i = 0; i < titleIds.size(); i++ )
{ {
if ( list[ i ] == tid ) if ( titleIds.at( i ) == tid )
return i; return i;
} }
return WII_EINSTALL; return WII_EINSTALL;
} }
char* NandTitle::NameOf( u64 tid ) const char* NandTitle::NameOf( u64 tid )
{ {
for ( u32 i = 0; i < numTitles; i++ ) map<u64, string>::iterator itr = NameList.find( tid );
{ if( itr != NameList.end() )
if ( list[ i ] == tid ) return itr->second.c_str();
{
if ( !nameList[ IMET_MAX_NAME_LEN * i ] )
return NULL;
return nameList + ( IMET_MAX_NAME_LEN * i );
}
}
return NULL; return NULL;
} }
char* NandTitle::NameFromIndex( u32 i ) const char* NandTitle::NameFromIndex( u32 i )
{ {
if ( i > numTitles || i < 0 ) if ( i > titleIds.size() || i < 0 )
return NULL; return NULL;
if ( !nameList[ IMET_MAX_NAME_LEN * i ] ) map<u64, string>::iterator itr = NameList.find( titleIds.at( i ) );
return NULL; if( itr != NameList.end() )
return itr->second.c_str();
return nameList + ( IMET_MAX_NAME_LEN * i ); return NULL;
} }
u16 NandTitle::VersionOf( u64 tid ) u16 NandTitle::VersionOf( u64 tid )
{ {
for ( u32 i = 0; i < numTitles; i++ ) for ( u32 i = 0; i < titleIds.size(); i++ )
{ {
if ( list[ i ] == tid ) if ( titleIds.at( i ) == tid )
{ {
tmd* Tmd = GetTMD( tid ); tmd* Tmd = GetTMD( tid );
if ( !Tmd ) if ( !Tmd )
@ -342,10 +314,10 @@ u16 NandTitle::VersionOf( u64 tid )
u16 NandTitle::VersionFromIndex( u32 i ) u16 NandTitle::VersionFromIndex( u32 i )
{ {
if ( i > numTitles || i < 0 ) if ( i > titleIds.size() || i < 0 )
return 0; return 0;
tmd* Tmd = GetTMD( list[ i ] ); tmd* Tmd = GetTMD( titleIds.at( i ) );
if ( !Tmd ) if ( !Tmd )
return 0; return 0;
@ -355,9 +327,9 @@ u16 NandTitle::VersionFromIndex( u32 i )
u32 NandTitle::CountType( u32 type ) u32 NandTitle::CountType( u32 type )
{ {
u32 ret = 0; u32 ret = 0;
for ( u32 i = 0; i < numTitles; i++ ) for ( u32 i = 0; i < titleIds.size(); i++ )
{ {
if ( TITLE_UPPER( list[ i ] ) == type ) if ( TITLE_UPPER( titleIds.at( i ) ) == type )
{ {
ret++; ret++;
} }
@ -378,19 +350,19 @@ u64 NandTitle::Next()
u64 ret = 0; u64 ret = 0;
//gprintf("Next( %08x, %u )\n", currentType, currentIndex ); //gprintf("Next( %08x, %u )\n", currentType, currentIndex );
u32 i; u32 i;
for ( i = currentIndex; i < numTitles; i++ ) for ( i = currentIndex; i < titleIds.size(); i++ )
{ {
if ( currentType ) if ( currentType )
{ {
if ( currentType == TITLE_UPPER( list[ i ] ) ) if ( currentType == TITLE_UPPER( titleIds.at( i ) ) )
{ {
ret = list[ i ]; ret = titleIds.at( i );
break; break;
} }
} }
else else
{ {
ret = list[ i ]; ret = titleIds.at( i );
break; break;
} }
} }
@ -417,13 +389,13 @@ void NandTitle::AsciiTID( u64 tid, char* out )
void NandTitle::AsciiFromIndex( u32 i, char* out ) void NandTitle::AsciiFromIndex( u32 i, char* out )
{ {
if ( i > numTitles || i < 0 ) if ( i > titleIds.size() || i < 0 )
{ {
out[ 0 ] = 0; out[ 0 ] = 0;
return; return;
} }
AsciiTID( list[ i ], out ); AsciiTID( titleIds.at( i ), out );
} }
s32 NandTitle::GetTicketViews( u64 tid, tikview **outbuf, u32 *outlen ) s32 NandTitle::GetTicketViews( u64 tid, tikview **outbuf, u32 *outlen )
@ -471,9 +443,9 @@ int NandTitle::FindU64( const char *s )
int NandTitle::FindU32( const char *s ) int NandTitle::FindU32( const char *s )
{ {
u64 tid = atoi_hex( s ); u64 tid = atoi_hex( s );
for ( u32 i = 0; i < numTitles; i++ ) for ( u32 i = 0; i < titleIds.size(); i++ )
{ {
if ( TITLE_LOWER( list[ i ] ) == TITLE_LOWER( tid ) ) if ( TITLE_LOWER( titleIds.at( i ) ) == TITLE_LOWER( tid ) )
return i; return i;
} }
return WII_EINSTALL; return WII_EINSTALL;

View File

@ -7,8 +7,11 @@
#include <stdio.h> #include <stdio.h>
#include <malloc.h> #include <malloc.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <vector>
#include <map>
#include "wstring.hpp" #include "wstring.hpp"
using namespace std;
#define TITLE_ID(x,y) (((u64)(x) << 32) | (y)) #define TITLE_ID(x,y) (((u64)(x) << 32) | (y))
#define TITLE_UPPER(x) ((u32)((x) >> 32)) #define TITLE_UPPER(x) ((u32)((x) >> 32))
@ -65,10 +68,10 @@ class NandTitle
s32 Get(); s32 Get();
u64 At( u32 i ); u64 At( u32 i );
int IndexOf( u64 tid ); int IndexOf( u64 tid );
u32 Count() { return numTitles; } u32 Count() { return titleIds.size(); }
char* NameOf( u64 tid ); const char* NameOf( u64 tid );
char* NameFromIndex( u32 i ); const char* NameFromIndex( u32 i );
u16 VersionOf( u64 tid ); u16 VersionOf( u64 tid );
u16 VersionFromIndex( u32 i ); u16 VersionFromIndex( u32 i );
@ -94,10 +97,9 @@ class NandTitle
u64 operator[]( u32 i ) { return At( i ); } u64 operator[]( u32 i ) { return At( i ); }
private: private:
u64* list; std::vector<u64> titleIds;
char* nameList; std::map<u64, string> NameList;
u32 numTitles; bool GetName( u64 tid, int language, wchar_t* name );
bool GetName( u64 tid, int language, wchar_t* name );
tmd* GetTMD( u64 tid ); tmd* GetTMD( u64 tid );