revert between 56095 -> 55830 in arch
[AROS.git] / workbench / c / iprefs / misc.c
blob2e5e109eb3c937e8027350c9874af681d58d2525
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: English
7 */
9 /*********************************************************************************************/
11 #include "global.h"
13 #include <prefs/prefhdr.h>
14 #include <aros/macros.h>
16 #define DEBUG 0
17 #include <aros/debug.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
23 #define CHECK_PRHD_VERSION 1
24 #define CHECK_PRHD_SIZE 1
26 /*********************************************************************************************/
28 struct FilePrefHeader
30 UBYTE ph_Version;
31 UBYTE ph_Type;
32 UBYTE ph_Flags[4];
35 /*********************************************************************************************/
37 struct IFFHandle *CreateIFF(STRPTR filename, LONG *stopchunks, LONG numstopchunks)
39 struct IFFHandle *iff;
41 D(bug("CreateIFF: filename = \"%s\"\n", filename));
43 if ((iff = AllocIFF()))
45 D(bug("CreateIFF: AllocIFF okay.\n"));
46 if ((iff->iff_Stream = (IPTR)Open(filename, MODE_OLDFILE)))
48 D(bug("CreateIFF: Open() okay.\n"));
49 InitIFFasDOS(iff);
51 if (OpenIFF(iff, IFFF_READ) == 0)
53 BOOL ok = FALSE;
55 D(bug("CreateIFF: OpenIFF okay.\n"));
57 if ((StopChunk(iff, ID_PREF, ID_PRHD) == 0) &&
58 (StopChunks(iff, stopchunks, numstopchunks) == 0))
60 D(bug("CreateIFF: StopChunk(PRHD) okay.\n"));
62 if (ParseIFF(iff, IFFPARSE_SCAN) == 0)
64 struct ContextNode *cn;
66 cn = CurrentChunk(iff);
68 D(bug("CreateIFF: ParseIFF okay. Chunk Type = %c%c%c%c ChunkID = %c%c%c%c\n",
69 cn->cn_Type >> 24,
70 cn->cn_Type >> 16,
71 cn->cn_Type >> 8,
72 cn->cn_Type,
73 cn->cn_ID >> 24,
74 cn->cn_ID >> 16,
75 cn->cn_ID >> 8,
76 cn->cn_ID));
78 if ((cn->cn_ID == ID_PRHD)
79 #if CHECK_PRHD_SIZE
80 && (cn->cn_Size == sizeof(struct FilePrefHeader))
81 #endif
84 struct FilePrefHeader h;
86 D(bug("CreateIFF: PRHD chunk okay.\n"));
88 if (ReadChunkBytes(iff, &h, sizeof(h)) == sizeof(h))
90 D(bug("CreateIFF: Reading PRHD chunk okay.\n"));
92 #if CHECK_PRHD_VERSION
93 if (h.ph_Version == PHV_CURRENT)
95 D(bug("CreateIFF: PrefHeader version is correct.\n"));
96 ok = TRUE;
98 #else
99 ok = TRUE;
100 #endif
106 } /* if (ParseIFF(iff, IFFPARSE_SCAN) == 0) */
108 } /* if ((StopChunk(iff, ID_PREF, ID_PRHD) == 0) && (StopChunks(... */
110 if (!ok)
112 CloseIFF(iff);
113 Close((BPTR)iff->iff_Stream);
114 FreeIFF(iff);
115 iff = NULL;
118 } /* if (OpenIFF(iff, IFFF_READ) == 0) */
119 else
121 Close((BPTR)iff->iff_Stream);
122 FreeIFF(iff);
123 iff = NULL;
126 } /* if ((iff->iff_Stream = (IPTR)Open(filename, MODE_OLDFILE))) */
127 else
129 FreeIFF(iff);
130 iff = NULL;
133 } /* if ((iff = AllocIFF())) */
135 return iff;
139 /*********************************************************************************************/
141 void KillIFF(struct IFFHandle *iff)
143 if (iff)
145 CloseIFF(iff);
146 Close((BPTR)iff->iff_Stream);
147 FreeIFF(iff);
151 /*********************************************************************************************/
153 APTR LoadChunk(struct IFFHandle *iff, LONG size, ULONG memtype)
155 APTR data;
156 struct ContextNode *cn = CurrentChunk(iff);
158 D(bug("LoadChunk: Chunk size is %d, requested %d\n", cn->cn_Size, size));
159 if (cn->cn_Size >= size) {
160 data = AllocVec(cn->cn_Size, memtype);
161 if (data) {
162 D(bug("[LoadChunk] Allocated buffer\n"));
163 if (ReadChunkBytes(iff, data, cn->cn_Size) == cn->cn_Size) {
164 D(bug("LoadChunk: Reading chunk successful.\n"));
166 return data;
168 FreeVec(data);
171 return NULL;