Prefs/ScreenMode: change the way depth is selected
[AROS.git] / workbench / libs / iffparse / prophooks.c
blobaaa9cc631e46bf202c20ad6800af7e9856841c78
1 /*
2 Copyright © 1995-2004, The AROS Development Team. All rights reserved.
3 $Id$
5 Hook funtions needed for PropChunk().
6 */
8 #include "iffparse_intern.h"
12 /****************************/
13 /* PropLCI Purge func */
14 /****************************/
16 #define IFFParseBase IPB(hook->h_Data)
18 IPTR PropPurgeFunc
20 struct Hook * hook,
21 struct LocalContextItem * lci,
22 ULONG p
25 struct StoredProperty *sp;
27 DEBUG_PROPHOOKS(dprintf("PropPurgeFunc: hook %p lci %p p 0x%lx", hook, lci, p));
29 /* Get the stored property structure */
30 sp = (struct StoredProperty*)LocalItemData(lci);
32 /* Free the chunk buffer */
33 if (sp->sp_Data) FreeMem(sp->sp_Data, sp->sp_Size);
35 /* Free the local item itself */
36 FreeLocalItem(lci);
38 DEBUG_PROPHOOKS(dprintf("PropPurgeFunc: return NULL\n"));
40 return 0;
43 /****************************/
44 /* PropChunk entry-handler */
45 /****************************/
47 struct PF_ResourceInfo
49 struct LocalContextItem *LCI;
50 APTR Buffer;
51 LONG BufferSize;
54 #undef IFFParseBase
56 VOID PF_FreeResources(struct PF_ResourceInfo *ri,
57 struct IFFParseBase_intern * IFFParseBase)
59 if (ri->LCI) FreeLocalItem(ri->LCI);
60 if (ri->Buffer) FreeMem(ri->Buffer, ri->BufferSize);
62 return;
66 #define IFFParseBase IPB(hook->h_Data)
68 LONG PropFunc
70 struct Hook * hook,
71 struct IFFHandle * iff,
72 APTR p
75 struct LocalContextItem *lci;
78 struct StoredProperty *sp;
79 struct ContextNode *cn;
81 struct PF_ResourceInfo resinfo = {0}; /* = {0} is important */
84 LONG type,
85 id,
86 size;
88 LONG bytesread,
89 err;
91 APTR buf;
93 DEBUG_PROPHOOKS(dprintf("PropFunc: hook %p iff %p p %p\n", hook, iff, p));
95 /* The Chunk that caused us to be invoked is always the top chunk */
96 cn = TopChunk(iff);
98 type = cn->cn_Type;
99 id = cn->cn_ID;
101 /* Allocate new LCI for containig the property */
103 lci = AllocLocalItem
105 type,
107 IFFLCI_PROP,
108 sizeof (struct StoredProperty)
110 if (!lci)
112 DEBUG_PROPHOOKS(dprintf("PropFunc: return IFFERR_NOMEM #1\n"));
114 return IFFERR_NOMEM;
117 resinfo.LCI = lci;
120 /* Get userdata (storedproperty) */
121 sp = (struct StoredProperty*)LocalItemData(lci);
124 /* Allocate buffer to read chunk into */
125 if ((size = cn->cn_Size))
127 buf = AllocMem(size, MEMF_ANY);
128 if (!buf)
130 DEBUG_PROPHOOKS(dprintf("PropFunc: return IFFERR_NOMEM #2\n"));
132 PF_FreeResources(&resinfo, IFFParseBase);
134 return (IFFERR_NOMEM);
136 } else buf = NULL;
138 resinfo.Buffer = buf;
139 resinfo.BufferSize = size;
141 sp->sp_Data = buf;
142 sp->sp_Size = size;
144 DEBUG_PROPHOOKS(dprintf("PropFunc: ReadChunkBytes(iff %p, buf %p, size %ld)\n", iff, buf, size));
146 if (buf)
148 /* Read chunk into the buffer */
149 bytesread = ReadChunkBytes(iff, buf, size);
151 DEBUG_PROPHOOKS(dprintf("PropFunc: ReadChunkBytes returned %lu\n", bytesread));
153 /* Success ? */
154 if (bytesread != size)
156 DEBUG_PROPHOOKS(dprintf("PropFunc: incomplete read! (%ld != %ld)\n", bytesread, size));
157 PF_FreeResources(&resinfo, IFFParseBase);
159 /* IFFERR_.. ? */
160 if (bytesread >= 0)
162 DEBUG_PROPHOOKS(dprintf("PropFunc: err = IFFERR_MANGLED\n"));
163 err = IFFERR_MANGLED;
164 /* FIXME: should return err here? */
170 /* Store the new item IN PROP, so it may be found with FindProp() */
171 err = StoreLocalItem(iff,lci,IFFSLI_PROP/*IFFSLI_ROOT*/);
173 if (err)
175 DEBUG_PROPHOOKS(dprintf("PropFunc: return %ld\n", err));
177 PF_FreeResources(&resinfo, IFFParseBase);
179 return err;
183 SetLocalItemPurge(lci, &IFFParseBase->proppurgehook);
185 DEBUG_PROPHOOKS(dprintf("PropFunc: return 0\n"));
186 return 0;