home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK1.toast / Development Kits (Disc 1) / ColorSync SDK / Sample Code / CSDemo 2.1 / ShellSources / resourceUtils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-13  |  3.6 KB  |  126 lines  |  [TEXT/CWIE]

  1. // This file contains useful routines for loading
  2. // common resource data types.
  3. // 
  4. // 9/16/94    david    first cut
  5. // 9/20/95    david    improved comments
  6.  
  7.  
  8. #include <Resources.h>
  9. #include <TextUtils.h>
  10. #include "resourceUtils.h"
  11.  
  12.  
  13. /**\
  14. |**| ==============================================================================
  15. |**| PRIVATE TYPEDEFS
  16. |**| ==============================================================================
  17. \**/
  18. typedef struct OSTypeList
  19. {
  20.     short    count ;            // 1-based count of structs to follow 
  21.     struct
  22.     {
  23.         OSType    type ;
  24.         Str27    string ;
  25.     } entry[];
  26. } OSTypeList, *OSTypeListPtr, **OSTypeListHdl ;
  27.  
  28.  
  29.  
  30. /**\
  31. |**| ==============================================================================
  32. |**| PUBLIC FUNCTIONS
  33. |**| ==============================================================================
  34. \**/
  35.  
  36.  
  37. /*------------------------------------------------------------------------------*\
  38.     GetRect
  39.  *------------------------------------------------------------------------------*
  40.         This routine loads a 'RECT' resource.
  41.         If the resource id can't be found then it return the rest {0,0,0,0}.
  42. \*------------------------------------------------------------------------------*/
  43. Rect GetRect ( short id )
  44. {
  45.     Handle        rectHndl ;
  46.     Rect        rect = {0,0,0,0} ;
  47.     
  48.     rectHndl = GetResource( 'RECT', id ) ;
  49.     if (rectHndl != nil )
  50.     {
  51.         rect = **((Rect**)(rectHndl)) ;
  52.         ReleaseResource( rectHndl ) ;
  53.     }
  54.     return rect ;
  55. }
  56.  
  57.  
  58. /*------------------------------------------------------------------------------*\
  59.     GetStringPtr
  60.  *------------------------------------------------------------------------------*
  61.         This routine loads a 'STR ' resource.
  62.         It is similar to the GetString routine except that it returns a StringPtr
  63.         instead of a StringHandle.
  64. \*------------------------------------------------------------------------------*/
  65. OSErr GetStringPtr ( short id, StringPtr dest )
  66. {
  67.     StringHandle    strHndl ;
  68.     
  69.     strHndl = GetString( id ) ;
  70.     if (!strHndl) return ResError() ;
  71.  
  72.     HLock( (Handle)strHndl ) ;
  73.     BlockMove( *strHndl, dest, (*strHndl)[0]+1) ;
  74.     ReleaseResource( (Handle)strHndl ) ;
  75.     return noErr ;
  76. }
  77.  
  78.  
  79. /*------------------------------------------------------------------------------*\
  80.     GetErrStringPtr
  81.  *------------------------------------------------------------------------------*
  82.         This routine loads a 'Estr' resource.
  83.         It is similar to the GetString routine except that it returns a StringPtr
  84.         instead of a StringHandle.
  85. \*------------------------------------------------------------------------------*/
  86. OSErr GetErrStringPtr ( short id, StringPtr dest )
  87. {
  88.     Handle    strHndl ;
  89.     
  90.     strHndl = GetResource( 'Estr', id ) ;
  91.     if (!strHndl) return ResError() ;
  92.     
  93.     HLock( strHndl ) ;
  94.     BlockMove( *strHndl, dest, (*strHndl)[0]+1) ;
  95.     ReleaseResource( strHndl ) ;
  96.     return noErr ;
  97. }
  98.  
  99.  
  100. /*------------------------------------------------------------------------------*\
  101.     GetOSTypeListStringPtr
  102.  *------------------------------------------------------------------------------*
  103.         This routine loads a 'OST#' resource and loops through it looking
  104.         for the string that coresponds to the type parameter.
  105. \*------------------------------------------------------------------------------*/
  106. OSErr GetOSTypeListStringPtr ( short id, OSType type, StringPtr dest )
  107. {
  108.     short            i, count ;
  109.     OSTypeListHdl    list ;
  110.     StringPtr        src    ;
  111.     
  112.     list = (OSTypeListHdl) GetResource('OST#', id) ;
  113.     if (list==nil) return ResError() ;
  114.     
  115.     count = (**list).count ;
  116.     for (i=0; i<count; i++ )
  117.         if ((**list).entry[i].type == type )
  118.         {
  119.             src = (StringPtr)&((**list).entry[i].string);
  120.             HLock( (Handle)list ) ;
  121.             BlockMove(src, dest, src[0]+1) ;
  122.         }
  123.     ReleaseResource( (Handle)list ) ;
  124.     return noErr ;
  125. }
  126.