grub2: bring back build of aros-side grub2 tools
[AROS.git] / workbench / libs / iffparse / writechunkrecords.c
blobbe4fc559c05e529f816b7c7c913e1ddcd61bf749
1 /*
2 Copyright © 1995-2016, 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_LH4(LONG, WriteChunkRecords,
15 /* SYNOPSIS */
16 AROS_LHA(struct IFFHandle *, iff, A0),
17 AROS_LHA(APTR , buf, A1),
18 AROS_LHA(LONG , bytesPerRecord, D0),
19 AROS_LHA(LONG , numRecords, D1),
21 /* LOCATION */
22 struct Library *, IFFParseBase, 13, IFFParse)
24 /* FUNCTION
25 Write numRecods records of bytesPerRecord bytes to the current chunk.
26 Attempts to write past the end of the chunk will be truncated.
28 INPUTS
29 iff - pointer to IFFHandle struct.
30 buf - pointer to a buffer containig the data to be written.
31 bytesPerRecord - number of bytes per record.
32 numRecords - number of records to write.
34 RESULT
35 actual - (positive) the actual number of whole records written.
36 (negative) IFFERR_#? error code if not successful.
38 NOTES
40 EXAMPLE
42 BUGS
44 SEE ALSO
45 WriteChunkBytes()
47 INTERNALS
49 *****************************************************************************/
51 AROS_LIBFUNC_INIT
53 struct ContextNode *cn;
55 /* Number of bytes possible to write in the chunk */
56 LONG lefttowrite;
57 /* Number of bytes actually written */
58 LONG byteswritten;
59 /* Number of bytes to write into the chunk */
60 LONG bytestowrite;
62 DEBUG_WRITECHUNKRECORDS(dprintf("WriteChunkRecords: iff %p buf %p bytesPerRecord %ld numRecords %ld\n",
63 iff, buf, bytesPerRecord, numRecords));
65 /* Calculate number of bytes to write */
66 bytestowrite = bytesPerRecord * numRecords;
68 /* Get the top contextnode */
69 cn = TopChunk(iff);
71 /* Is the numBytes known for this chunk ? */
72 if (cn->cn_Size != IFFSIZE_UNKNOWN)
74 /* We must truncate attempts to write larger than the chunksize */
75 lefttowrite = cn->cn_Size - cn->cn_Scan;
76 if (bytestowrite > lefttowrite)
78 bytestowrite = lefttowrite;
80 /* See to it that we only write whole records */
81 bytestowrite -= (lefttowrite % bytesPerRecord);
86 /* Actually write the chunk */
87 byteswritten = WriteStream(iff, buf, bytestowrite, IPB(IFFParseBase));
89 if (byteswritten < 0)
90 /* IFFERR_#? returned by WriteStream() */
91 numRecords = byteswritten;
92 else
94 /* No error */
95 cn->cn_Scan += byteswritten;
97 numRecords = (byteswritten / bytesPerRecord);
100 DEBUG_WRITECHUNKRECORDS(dprintf("WriteChunkRecords: return %ld\n", numRecords));
101 return (numRecords);
103 AROS_LIBFUNC_EXIT
104 } /* WriteChunkRecords */