/*
    (C) 1995-96 AROS - The Amiga Replacement OS
    $Id: writechunkbytes.c,v 1.1 1997/02/03 16:44:29 digulla Exp $

    Desc:
    Lang: english
*/
#include "iffparse_intern.h"

/*****************************************************************************

    NAME */
#include <proto/iffparse.h>

	AROS_LH3(LONG, WriteChunkBytes,

/*  SYNOPSIS */
	AROS_LHA(struct IFFHandle *, iff, A0),
	AROS_LHA(APTR              , buf, A1),
	AROS_LHA(LONG              , numBytes, D0),

/*  LOCATION */
	struct Library *, IFFParseBase, 11, IFFParse)

/*  FUNCTION
	Writes given number of bytes in the supplied buffer into the
	current chunk. Attempts to write past the endo of the chunk will
	be truncated.

    INPUTS
	iff	   - pointer to IFFHandle struct.
	buf	   -  buffer with data to write.
	numBytes  - number of bytes to write.

    RESULT
	actual	  -  (positive) number of bytes actually written.
		    (negative) IFFERR_#? indicating unsuccesfull write.
    NOTES

    EXAMPLE

    BUGS

    SEE ALSO
	PushChunk(), PopChunk(), WriteChunkRecords()

    INTERNALS

    HISTORY
  27-11-96    digulla automatically created from
	  iffparse_lib.fd and clib/iffparse_protos.h

*****************************************************************************/
{
    AROS_LIBFUNC_INIT
    AROS_LIBBASE_EXT_DECL(struct Library *,IFFParseBase)

    struct ContextNode *cn;

    LONG placeleft;
    LONG byteswritten;


    /* Get the top contextnode */
    cn = TopChunk(iff);

    /* Is the numBytes known for this chunk ? */
    if (cn->cn_Size != IFFSIZE_UNKNOWN)
    {
	/* We must truncate attempts to write larger than the chunksize */
	placeleft = cn->cn_Size - cn->cn_Scan;
	if  (numBytes > placeleft)

	numBytes = placeleft;
    }


    /* Actually write the chunk */
    byteswritten = WriteStream(iff, buf, numBytes, IPB(IFFParseBase));

    if (byteswritten > 0)
	/* No error */
	cn->cn_Scan += byteswritten;

    return (byteswritten);

    AROS_LIBFUNC_EXIT
} /* WriteChunkBytes */
