home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / misc / emu / AROSdev.lha / AROS / workbench / libs / iffparse / prophooks.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-03  |  3.0 KB  |  177 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: prophooks.c,v 1.2 1997/02/03 18:38:02 digulla Exp $
  4.  
  5.     Desc: Hook funtions needed for PropChunk().
  6.     Lang: English.
  7. */
  8.  
  9. #include "iffparse_intern.h"
  10.  
  11.  
  12.  
  13. /****************************/
  14. /* PropLCI Purge func  */
  15. /****************************/
  16.  
  17. #undef SysBase
  18. #define SysBase     IPB(hook->h_Data)->sysbase
  19. #define IFFParseBase IPB(hook->h_Data)
  20.  
  21. IPTR PropPurgeFunc
  22. (
  23.     struct Hook         * hook,
  24.     struct LocalContextItem * lci,
  25.     ULONG              p
  26. )
  27. {
  28.     struct StoredProperty *sp;
  29.  
  30.     /* Get the stored property structure */
  31.     sp = (struct StoredProperty*)LocalItemData(lci);
  32.  
  33.     /* Free the chunk buffer */
  34.     FreeMem(sp->sp_Data, sp->sp_Size);
  35.  
  36.     /* Free the local item itself */
  37.     FreeLocalItem(lci);
  38.  
  39.     return (NULL);
  40. }
  41.  
  42. /****************************/
  43. /* PropChunk entry-handler  */
  44. /****************************/
  45.  
  46. struct PF_ResourceInfo
  47. {
  48.     struct LocalContextItem *LCI;
  49.     APTR            Buffer;
  50.     LONG            BufferSize;
  51. };
  52.  
  53. #undef SysBase
  54. #undef IFFParseBase
  55. #define SysBase     IFFParseBase->sysbase
  56.  
  57. VOID PF_FreeResources(struct PF_ResourceInfo *ri,
  58.     struct IFFParseBase_intern * IFFParseBase)
  59. {
  60.     if (ri->LCI)     FreeLocalItem(ri->LCI);
  61.     if (ri->Buffer)  FreeMem(ri->Buffer, ri->BufferSize);
  62.  
  63.     return;
  64. }
  65.  
  66.  
  67. #undef SysBase
  68. #define SysBase     IPB(hook->h_Data)->sysbase
  69. #define IFFParseBase IPB(hook->h_Data)
  70.  
  71. LONG PropFunc
  72. (
  73.     struct Hook     * hook,
  74.     struct IFFHandle    * iff,
  75.     APTR          p
  76. )
  77. {
  78.     struct LocalContextItem *lci;
  79.  
  80.  
  81.     struct StoredProperty    *sp;
  82.     struct ContextNode        *cn;
  83.  
  84.     struct PF_ResourceInfo resinfo = {0}; /* = {0} is important */
  85.  
  86.  
  87.     LONG   type,
  88.       id,
  89.       size;
  90.  
  91.     LONG  bytesread,
  92.       err;
  93.  
  94.     APTR  buf;
  95.  
  96.     /* The Chunk that caused us to be invoked is always the top chunk */
  97.     cn = TopChunk(iff);
  98.  
  99.     type   = cn->cn_Type;
  100.     id      = cn->cn_ID;
  101.  
  102.     /* Allocate new LCI for containig the property */
  103.  
  104.     lci = AllocLocalItem
  105.     (
  106.     type,
  107.     id,
  108.     IFFLCI_PROP,
  109.     sizeof (struct StoredProperty)
  110.     );
  111.     if (!lci) return (IFFERR_NOMEM);
  112.  
  113.     resinfo.LCI = lci;
  114.  
  115.  
  116.     /* Get userdata (storedproperty) */
  117.     sp = (struct StoredProperty*)LocalItemData(lci);
  118.  
  119.  
  120.     /* Allocate buffer to read chunk into */
  121.     size = cn->cn_Size;
  122.  
  123.  
  124.     buf = AllocMem
  125.     (
  126.     size,
  127.     MEMF_ANY
  128.     );
  129.  
  130.     if (!buf)
  131.     {
  132.     PF_FreeResources(&resinfo, IFFParseBase);
  133.     return (IFFERR_NOMEM);
  134.     }
  135.  
  136.     resinfo.Buffer = buf;
  137.     resinfo.BufferSize = size;
  138.  
  139.     sp->sp_Data = buf;
  140.     sp->sp_Size = size;
  141.  
  142.     /* Read chunk into the buffer */
  143.  
  144.     bytesread = ReadChunkBytes
  145.     (
  146.     iff,
  147.     buf,
  148.     size
  149.     );
  150.  
  151.     /* Sucess ? */
  152.     if (bytesread != size)
  153.     {
  154.     PF_FreeResources(&resinfo, IFFParseBase);
  155.  
  156.     /* IFFERR_.. ? */
  157.     if (bytesread >= 0)
  158.         err = IFFERR_MANGLED;
  159.     }
  160.  
  161.  
  162.     /* Store the new item IN ROOT, so it may be found with FindProp() */
  163.     err = StoreLocalItem(iff,lci,IFFSLI_ROOT);
  164.  
  165.     if (err)
  166.     {
  167.     PF_FreeResources(&resinfo, IFFParseBase);
  168.     return (err);
  169.     }
  170.  
  171.  
  172.     SetLocalItemPurge(lci, &IFFParseBase->proppurgehook);
  173.  
  174.     return (NULL);
  175. }
  176.  
  177.