X作成講座 on TC(5) リソースを扱う
さて、現在のスタックにインストールされているサウンドのリストを返すXです。
リソースのリストを得る場合の基本的な流れは
まずそのリソースの総数を得る
1番目から順にループ
n番目のリソースを読む
そのリソースの名前を取り出す
ハイパカに返す文字列にその名前を繋げる
ループ終わり
ハイパカに返す
となります。
リソースはIDや名前で指定する他に「○番目」というアクセスが出来るようになっているので、それを利用するわけです。
#include "HyperXCmd.h" #include "SetUpA4.h" /* Prototypes */ void myRoutin( XCmdPtr paramPtr ); void getSoundList( XCmdPtr paramPtr ); void pStrCat( Str255 str1, Str255 str2 ); /* Main */ pascal void main( XCmdPtr paramPtr ){ RememberA0( ); SetUpA4( ); getSoundList( paramPtr ); RestoreA4( ); } /* my routin */ void getSoundList( XCmdPtr paramPtr ){ Str255 theStr, resName, resultStr; Handle resHand; short theID, itemNum, i; ResType theType; resultStr[0] = 0; itemNum = Count1Resources('snd '); SetResLoad( false ); for ( i = 1; i <= itemNum; i++ ){ resHand = Get1IndResource( 'snd ', i ); GetResInfo( resHand, &theID, &theType, resName ); pStrCat( resultStr, resName ); pStrCat( resultStr, "\p\r" ); } SetResLoad( true ); paramPtr->returnValue = PasToZero( paramPtr, resultStr ); } /* connect str2 to str1 */ void pStrCat( Str255 str1, Str255 str2 ){ short lgth; lgth = str2[0]; if ( str1[0] + lgth > 255 ) lgth = 255 - str1[0]; BlockMove( str2 + 1, str1 + 1 + str1[0], lgth ); str1[0] += lgth; }
#include "HyperXCmd.h" #include "SetUpA4.h" /* Prototypes */ void myRoutin( XCmdPtr paramPtr ); void getAllSoundList( XCmdPtr paramPtr ); Boolean putStrAfterC( Handle resultHand, long *resultOfs, Str255 theStr ); /* Main */ pascal void main( XCmdPtr paramPtr ){ RememberA0( ); SetUpA4( ); getAllSoundList( paramPtr ); RestoreA4( ); } /* my routin */ void getAllSoundList( XCmdPtr paramPtr ){ Str255 theStr, resName, resultStr; Handle resultHand, resHand; long resultOfs; short theID, itemNum, i; ResType theType; resultOfs = 0; resultHand = NewHandle( 0 ); itemNum = CountResources('snd '); SetResLoad( false ); for ( i = 1; i <= itemNum; i++ ){ resHand = GetIndResource( 'snd ', i ); GetResInfo( resHand, &theID, &theType, resName ); if ( resName[0] != 0 ){ if ( putStrAfterC( resultHand, &resultOfs, resName ) == false ){ SetResLoad( true ); DisosHandle( resultHand ); paramPtr->returnValue = PasToZero( paramPtr, "\pMemErr" ); return; } if ( putStrAfterC( resultHand, &resultOfs, "\p\r" ) == false ){ SetResLoad( true ); DisosHandle( resultHand ); paramPtr->returnValue = PasToZero( paramPtr, "\pMemErr" ); return; } } } SetResLoad( true ); ZeroTermHandle( paramPtr, resultHand ); paramPtr->returnValue = resultHand; } /* -- put P-string after C string Handle -- */ Boolean putStrAfterC( Handle resultHand, long *resultOfs, Str255 theStr ){ SetHandleSize( resultHand, *resultOfs + theStr[0] ); if ( MemError() ) return false; BlockMove( theStr +1, *resultHand + *resultOfs, theStr[0] ); *resultOfs += theStr[0]; return true; }