//-----------------------------------------------------------------------------
// Copyright © 2003 - Philip Howard - All rights reserved
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//-----------------------------------------------------------------------------
// package	libh/angif
// homepage	http://libh.slashusr.org/
//-----------------------------------------------------------------------------
// author	Philip Howard
// email	libh at ipal dot org
// homepage	http://phil.ipal.org/
//-----------------------------------------------------------------------------
// This library is "patent free" in that it contains no code to implement
// the LZW compression, which is covered by patent number 4,558,302 owned
// by Unisys.
//
// GIF is a service mark, property of CompuServe, Inc.
//
// The GIF standard is obsolete and is being deprecated in favor of the
// PNG (Portable Network Graphics) standard.
//-----------------------------------------------------------------------------

#include "angif_lib.h"

#include <unistd.h>

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	angif_write
//
// purpose	Write data to a target stream object.
//
// arguments	1 (angif_stream *) pointer to stream object
//		2 (const unsigned char *) address of data to write
//		3 (size_t) length of data to write
//
// returns	(int) 0 = successful
//		(int) -1 = failure
//-----------------------------------------------------------------------------
int
angif_write (
    angif_stream *		stream
    ,
    const unsigned char *	data
    ,
    size_t			len
    )
__PROTO_END__
{
    size_t		rem	;
    size_t		size	;

    int			rc	;

    //-- Check arguments.
    if ( ! stream ) return ANGIF_ERROR_ARG_STREAM;
    if ( len && ! data ) return ANGIF_ERROR_ARG_DATA;
    if ( len < 0 ) return ANGIF_ERROR_ARG_LEN;

    //-- If a function call is requested, call the function first.
    if ( stream->call_fun ) {
	rc = ( *(stream->call_fun) )( stream->call_arg, data, len );
    }

    //-- If file output requested, let it do the buffering.
    if ( stream->out_file ) {
	//-- If the length is zero, that is special for flush.
	if ( len == 0 ) {
	    fflush( stream->out_file );
	} else {
	    if ( fwrite( data, 1, len, stream->out_file ) < len ) {
		return ANGIF_ERROR_FWRITE;
	    }
	}
    }

    //-- If fd is requested, buffer the data and do write().
    if ( stream->out_fd_ok ) {
	//-- If the length is zero, that is special for flush.
	if ( len == 0 ) {
	    write( stream->out_fd,
		   stream->buffer_begin,
		   (size_t) ( stream->buffer_pos - stream->buffer_begin )
	    );
	} else {
	    //-- Keep copying until there is no more to copy.
	    while ( len ) {
		rem = stream->buffer_end - stream->buffer_pos;
		if ( rem > len ) rem = len;
		memcpy( stream->buffer_pos, data, rem );
		stream->buffer_pos += rem;
		data += rem;
		len -= rem;
		if ( stream->buffer_pos == stream->buffer_end ) {
		    write( stream->out_fd,
			   stream->buffer_begin,
			   (size_t) ( stream->buffer_pos - stream->buffer_begin )
		    );
		}
	    }
	}
    }

    //-- If memory is requested, expand if needed, and copy the data.
    if ( len && stream->memory_begin ) {
	rem = stream->memory_end - stream->memory_pos;
	if ( rem < len ) {
	    size = stream->memory_end - stream->memory_begin;
	}
    }

    //-- Return with success.
    return 0;
}

