Do not install wrong cache routine if called before SysBase is complete.
[AROS.git] / workbench / libs / iffparse / openiff.c
blobda65c566295b1e86360ff888a7675b3e195b2263
1 /*
2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #define DEBUG 0
7 #include <aros/debug.h>
8 #include "iffparse_intern.h"
10 /* NOTE: Original iffparse.library doesn't have this check here. - Piru */
11 #define USE_IFFVALIDITYCHECK 1
13 /*****************************************************************************
15 NAME */
16 #include <proto/iffparse.h>
18 AROS_LH2(LONG, OpenIFF,
20 /* SYNOPSIS */
21 AROS_LHA(struct IFFHandle *, iff, A0),
22 AROS_LHA(LONG , rwMode, D0),
24 /* LOCATION */
25 struct Library *, IFFParseBase, 6, IFFParse)
27 /* FUNCTION
28 Initializes an IFFHandle struct for a new session of reading or
29 writing. The direction of the I/O is determined by the rwMode flags
30 supplied (IFFF_READ or IFFF_WRITE).
32 INPUTS
33 iff - pointer to IFFHandle struct.
34 ewMode - IFFF_READ or IFFF_WRITE
37 RESULT
38 error - 0 if successfull, IFFERR_#? elsewise.
40 NOTES
41 This function tells the custom stream handler to initialize
42 by sending it a IFFCMD_INIT IFFStreamCmd.
44 EXAMPLE
46 BUGS
48 SEE ALSO
49 CloseIFF(), InitIFF()
51 INTERNALS
53 *****************************************************************************/
55 AROS_LIBFUNC_INIT
56 LONG err;
57 struct IFFStreamCmd cmd;
59 if (iff == NULL)
61 D(bug("openiff error: IFFERR_NOMEM\n"));
62 return (IFFERR_NOMEM);
65 /* Check that a valid StreamHandler Hook has been supplied */
66 if (!( GetIntIH(iff)->iff_StreamHandler) )
68 D(bug("openiff error: IFFERR_NOHOOK\n"));
69 return (IFFERR_NOHOOK);
72 /* Tell the custom stream to initialize itself */
74 cmd.sc_Command = IFFCMD_INIT;
75 err = CallHookPkt (GetIntIH(iff)->iff_StreamHandler, iff, &cmd);
77 if (!err)
79 #if USE_IFFVALIDITYCHECK
80 /* If we are opend in read mode we should test if we have a valid IFF-File */
81 if (rwMode == IFFF_READ && iff->iff_Stream)
83 struct ContextNode *cn;
85 D(bug("testing if it's a valid IFF file\n"));
86 /* Get header of iff-stream */
87 err = GetChunkHeader(iff, IPB(IFFParseBase));
89 /* Valid IFF header ? */
90 if (!err)
92 /* We have now entried the chunk */
93 D(bug("entered the chunk\n"));
94 GetIntIH(iff)->iff_CurrentState = IFFSTATE_COMPOSITE;
96 cn = TopChunk(iff);
98 /* We must see if we have a IFF header ("FORM", "CAT" or "LIST") */
99 if (GetIntCN(cn)->cn_Composite)
101 D(bug("an iff header\n"));
102 /* Everything went OK */
103 /* Set the acess mode, and mark the stream as opened */
104 iff->iff_Flags &= ~IFFF_RWBITS;
105 iff->iff_Flags |= (rwMode | IFFF_OPEN | IFFF_NEWFILE);
107 err = 0L;
109 else
111 err = IFFERR_NOTIFF;
112 D(bug("OpenIFF: not an IFF header, pop the context node\n"));
114 /* Pop the contextnode */
115 PopContextNode(iff, IPB(IFFParseBase));
118 else
120 if (err == IFFERR_MANGLED)
122 err = IFFERR_NOTIFF;
123 D(bug("openiff error: IFFERR_MANGLED\n"));
126 /* Fail. We should send CLEANUP to the stream */
127 cmd.sc_Command = IFFCMD_CLEANUP;
128 err = CallHookPkt
130 GetIntIH(iff)->iff_StreamHandler,
131 iff,
132 &cmd
135 } /* IFFF_READ */
136 else
137 #endif /* USE_IFFVALIDITYCHECK */
139 iff->iff_Flags &= ~IFFF_RWBITS;
140 iff->iff_Flags |= (rwMode | IFFF_OPEN | IFFF_NEWFILE);
141 err = 0L;
144 } /* if (!err) */
146 D(bug("openiff: return %d\n", err));
147 return (err);
148 AROS_LIBFUNC_EXIT
149 } /* OpenIFF */