disable debug
[AROS.git] / workbench / libs / iffparse / closeiff.c
bloba072c4095e190fe03d5707a549a97cebd25d66c9
1 /*
2 Copyright © 1995-2017, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include "iffparse_intern.h"
8 /*****************************************************************************
10 NAME */
11 #include <proto/iffparse.h>
13 AROS_LH1(void, CloseIFF,
15 /* SYNOPSIS */
16 AROS_LHA(struct IFFHandle *, iff, A0),
18 /* LOCATION */
19 struct Library *, IFFParseBase, 8, IFFParse)
21 /* FUNCTION
22 Completes a read or write session by closing the IFF handle.
23 The IFFHandle struct is ready for reuse in another session,
24 it's just to open it again with OpenIFF(). This function
25 also automatically cleans up if a read or write fails halfway through.
27 INPUTS
28 iff - Pointer to an IFFhandle struct previously opened with OpenIFF()
30 RESULT
32 NOTES
33 This function tells the custom stream handler to clean up
34 by sending it a IFFCMD_CLEANUP IFFStreamCmd.
36 EXAMPLE
38 BUGS
39 Errors during writing of any of the remaining chunks are just
40 ignored and both the faulty and all following chunks are not written.
42 SEE ALSO
43 OpenIFF(), InitIFF()
45 INTERNALS
46 This function checks that buffers for buffered streams
47 have been freed. This is not very elegant and should have been
48 done at an earlier stadium. It is not a real bug though.
50 *****************************************************************************/
52 AROS_LIBFUNC_INIT
54 struct IFFStreamCmd cmd;
55 LONG error=0;
57 if (iff != NULL)
59 /* clear the IFFF_OPEN bit to mark IFF stream closed */
60 if (!(iff->iff_Flags & IFFF_OPEN) )
62 return;
65 iff->iff_Flags &= ~IFFF_OPEN;
67 /* Pop of all contextnodes so that only the default one is remaining */
70 for (count = iff->iff_Depth; count; count -- )
72 PopContextNode(iff, IPB(IFFParseBase));
75 while (iff->iff_Depth && !error)
77 error = PopChunk(iff);
79 if(error)
81 /* FIXME: handle error */
82 bug("[CloseIFF] unhandled error code: %d\n", error);
85 /* This is for safety:
86 It might be in PopChunk that seeking or writing to streams failed.
87 In that case the memory for eventual buffered setreams
88 were not freed, so we should free it here.
90 (Yes, it is is a kludge !)
92 if ( GetIntIH(iff)->iff_BufferStartDepth)
94 FreeBuffer((struct BufferList*)iff->iff_Stream, IPB(IFFParseBase));
96 GetIntIH(iff)->iff_BufferStartDepth = 0;
99 /* Tell the custom stream to cleanup */
100 cmd.sc_Command = IFFCMD_CLEANUP;
101 CallHookPkt
103 GetIntIH(iff)->iff_StreamHandler,
104 iff,
105 &cmd
109 AROS_LIBFUNC_EXIT
110 } /* CloseIFF */