//-----------------------------------------------------------------------------
// 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/string
// homepage	http://libh.slashusr.org/
//-----------------------------------------------------------------------------
// author	Philip Howard
// email	libh at ipal dot org
// homepage	http://phil.ipal.org/
//-----------------------------------------------------------------------------
// This file is best viewed using a fixed spaced font such as Courier
// and in a display at least 120 columns wide.
//-----------------------------------------------------------------------------

#include "string_lib.h"

__FMACRO_BEGIN__
//-----------------------------------------------------------------------------
// macro	str_app_arg
//
// purpose	Append to the first string (given with maximum available space)
//		the contents of zero or more (multiple) strings.
//
//		Do append only if all source strings can be appended.
//
// arguments	1 (char *) pointer to target string
//		2 (size_t) maximum space in target string
//		3..N (const char *) source strings to append
//
// returns	(size_t) new length of string, or ~0 for error
//
// note		Contents of available space after the termination character
//		are destroyed if the append fails due to insufficient space.
//		Nothing should be kept there, anyway.
//-----------------------------------------------------------------------------
#define str_app_arg(t,l,a...) ((str_app_arg)((t),(l),a,(const char*)(NULL)))

__FMACRO_END__

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	(str_app_arg)
//
// purpose	Append to the first string (given with maximum available space)
//		the contents of zero or more (multiple) strings.
//
//		Do append only if all source strings can be appended.
//
// arguments	1 (char *) pointer to target string
//		2 (size_t) maximum space in target string
//		3..N (const char *) source strings to append
//		N+1 (const char *) NULL to indicate no more strings
//
// returns	(size_t) new length of string, or ~0 for error
//
// note		Contents of available space after the termination character
//		are destroyed if the append fails due to insufficient space.
//		Nothing should be kept there, anyway.
//-----------------------------------------------------------------------------
size_t
(str_app_arg) (
    char *		arg_target
    ,
    size_t		arg_length
    ,
    const char *	arg_source
    ,
    ...
    )
__PROTO_END__
{
    va_list		var_args	;
    char *		target_ptr	;
    char *		target_end	;
    char *		target_undo	;
    const char *	source_ptr	;

    //-- Determine end of string and check for space.
    target_ptr = arg_target;
    target_end = target_ptr + arg_length - 1;
    while ( target_ptr < target_end && * target_ptr ) ++ target_ptr;
    if ( target_ptr >= target_end ) return ~0;

    //-- Remember where to undo the append.
    target_undo = target_ptr;

    //-- Append each string until no more strings or no more space.
    if ( ( source_ptr = arg_source ) ) {
	va_start( var_args, arg_source );
	do {
	    while ( target_ptr < target_end && ( * target_ptr = * source_ptr ) ) {
		++ target_ptr;
		++ source_ptr;
	    }
	} while ( target_ptr < target_end && ( source_ptr = va_arg( var_args, const char * ) ) );
	va_end( var_args );
    }

    //-- If the strings did not fit, undo and abort.
    if ( target_ptr >= target_end ) {
	* target_undo = 0;
	return ~0;
    }

    //-- Return with new length.
    return target_ptr - arg_target;
}

