home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / sound / sndplaydoublebuffer / _source / readresource.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  5.9 KB  |  190 lines

  1. /*
  2.     File:        ReadResource.c
  3.  
  4.     Contains:    Routines demonstrating how to read resource files without using
  5.                 the Resource Manager.
  6.  
  7.     Written by: Mark Cookson    
  8.  
  9.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 8/31/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  21.                 
  22. */
  23.  
  24. #include "ReadResource.h"
  25.  
  26. /*    Purpose:        This finds the number of the first resource of chosen
  27.                     type and returns that number, along with any error.
  28.     Side Effects:    None.
  29. */
  30. /*-----------------------------------------------------------------------*/
  31.         OSErr    MyGetFirstResource        (short refNum,
  32.                                         OSType targetType,
  33.                                         short *targetID)
  34. /*-----------------------------------------------------------------------*/
  35. {
  36.     ResReference    theRes;
  37.     long            dataOffset        = 0,
  38.                     typeOffset        = 0,
  39.                     ResRefSize        = 0,
  40.                     oldPos            = 0;
  41.     short            numResources    = 0;
  42.     OSErr            theErr            = noErr;
  43.  
  44.     theErr = GetFPos (refNum, &oldPos);        /* Be nice and don't move the file pointer without puting it back */
  45.     if (theErr == noErr) {
  46.         /* Find the location of the first resource of the type we want */
  47.         theErr = MyGetTypesPosition (refNum, targetType, &numResources, &dataOffset, &typeOffset);
  48.     }
  49.     if (theErr == noErr) {
  50.         /* Position the file to the start of the list of resources */
  51.         theErr = SetFPos (refNum, fsFromStart, typeOffset);
  52.     }
  53.     if (theErr == noErr) {
  54.         ResRefSize = sizeof (theRes);
  55.         if (numResources != 0) {
  56.             theErr = FSRead (refNum, &ResRefSize, &theRes);
  57.             *targetID = theRes.ID;
  58.             if (theErr == noErr) {
  59.                 theErr = SetFPos (refNum, fsFromStart, oldPos);
  60.             }
  61.         }
  62.         else {
  63.             theErr = resNotFound;
  64.         }
  65.     }
  66.  
  67.     return theErr;
  68. }
  69.  
  70. /*    Purpose:        This finds the requested resource and positions the file
  71.                     pointer to point to the first byte of that resource.  It
  72.                     returns the position, inside the resource fork, of that
  73.                     resource, or if unsuccessful the error.
  74.     Side Effects:    None.
  75. */
  76. /*-----------------------------------------------------------------------*/
  77.         OSErr    MyGetResourcePosition    (short refNum,
  78.                                         OSType targetType,
  79.                                         short targetID,
  80.                                         long *firstByte)
  81. /*-----------------------------------------------------------------------*/
  82. {
  83.     ResReference    theRes;
  84.     long            position        = 0,
  85.                     dataOffset        = 0,
  86.                     typeOffset        = 0,
  87.                     ResRefSize        = 0;
  88.     short            numResources    = 0;
  89.     OSErr            theErr            = noErr;
  90.     Boolean            found            = false;
  91.  
  92.     /* Find the location of the first resource of the type we want */
  93.     theErr = MyGetTypesPosition (refNum, targetType, &numResources, &dataOffset, &typeOffset);
  94.     if (theErr == noErr) {
  95.         /* Position the file to the start of the list of resources */
  96.         theErr = SetFPos (refNum, fsFromStart, typeOffset);
  97.     }
  98.     if (theErr == noErr) {
  99.         ResRefSize = sizeof (theRes);
  100.     }
  101.     while (theErr == noErr && numResources-- && found == false) {
  102.         theErr = FSRead (refNum, &ResRefSize, &theRes);
  103.         if (theErr == noErr && theRes.ID == targetID) {
  104.             position = (theRes.dataOffset & kDataOffset) + dataOffset;
  105.             *firstByte = position + sizeof (long);
  106.             found = true;
  107.         }
  108.     }
  109.     if (theErr == noErr) {
  110.         theErr = SetFPos (refNum, fsFromStart, *firstByte);
  111.     }
  112.  
  113.     if (found == false) {
  114.         theErr = resNotFound;
  115.     }
  116.  
  117.     return theErr;
  118. }
  119.  
  120. /*    Purpose:        This finds the beginning of the list of requested
  121.                     resource types (i.e. all 'snd ' resources).  It returns
  122.                     the offset to these resources, or if unsuccessful the
  123.                     error.
  124.     Side Effects:    None.
  125. */
  126. /*-----------------------------------------------------------------------*/
  127.         OSErr    MyGetTypesPosition    (short refNum,
  128.                                     OSType targetType,
  129.                                     short *numResources,
  130.                                     long *dataOffset,
  131.                                     long *firstByteOfTypeList)
  132. /*-----------------------------------------------------------------------*/
  133. {
  134.     ResourceHeader            ResForkHeader;
  135.     ResourceMap                ResMap;
  136.     ResourceTypeListEntry    typeListEntry;
  137.     long                    numTypes        = 0,
  138.                             numBytes        = 0,
  139.                             numBytesToRead    = 0,
  140.                             position        = 0,
  141.                             oldPos            = 0;
  142.     OSErr                    theErr            = noErr;
  143.     Boolean                    found            = false;
  144.  
  145.     theErr = GetFPos (refNum, &oldPos);        /* Be nice and don't move the file pointer without puting it back */
  146.     if (theErr == noErr) {
  147.         theErr = SetFPos (refNum, fsFromStart, 0);
  148.     }
  149.     if (theErr == noErr) {
  150.         /* Read the resource file header */
  151.         numBytesToRead = sizeof (ResForkHeader);
  152.         theErr = FSRead (refNum, &numBytesToRead, &ResForkHeader);
  153.     }
  154.     if (theErr == noErr) {
  155.         /* Read the resource map */
  156.         theErr = SetFPos (refNum, fsFromStart, ResForkHeader.resMapOffset);
  157.     }
  158.     if (theErr == noErr) {
  159.         numBytesToRead = sizeof (ResMap);
  160.         theErr = FSRead (refNum, &numBytesToRead, &ResMap);
  161.     }
  162.     if (theErr == noErr) {
  163.         /* Find the type list */
  164.         position = ResMap.typesListOffset + ResForkHeader.resMapOffset + 2;
  165.         theErr = SetFPos (refNum, fsFromStart, position);
  166.     }
  167.     if (theErr == noErr) {
  168.         numTypes = ResMap.numTypesInMap + 1;
  169.         position += sizeof (numTypes);
  170.         numBytes = sizeof (typeListEntry);
  171.     }
  172.     while (theErr == noErr && numTypes-- && found == false) {
  173.         theErr = FSRead (refNum, &numBytes, &typeListEntry);
  174.         if (theErr == noErr && typeListEntry.resType == targetType) {
  175.             *firstByteOfTypeList = ResForkHeader.resMapOffset + ResMap.typesListOffset + typeListEntry.referenceOffset;
  176.             *numResources = typeListEntry.numEntries + 1;
  177.             *dataOffset = ResForkHeader.resDataOffset;
  178.             found = true;
  179.         }
  180.     }
  181.  
  182.     theErr = SetFPos (refNum, fsFromStart, oldPos);
  183.  
  184.     if (found == false) {
  185.         theErr = resNotFound;
  186.     }
  187.  
  188.     return theErr;
  189. }
  190.