//-----------------------------------------------------------------------------
// 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"

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	str_dup_var
//
// purpose	Duplicate into allocated space the contents of zero or more
//		(multiple) strings provided as a variable argument list passed
//		in one argument.
//
// arguments	1 (va_list) source strings to duplicate
//
// returns	(char *) new allocated duplicate string, caller must free
//		(char *) NULL : error allocating string or other error
//
// warning	If other threads modify any source strings during duplication,
//		then results are unpredictable.
//-----------------------------------------------------------------------------
char *
str_dup_var (
    va_list		arg_list
    )
__PROTO_END__
{
    const char * *		array_base	;
    const char * *		array_end	;
    const char * *		array_ptr	;
    size_t			count		;

    //---------------------------------------------------------------
    // Since a passed va_list cannot be iterated through more than
    // once, this function will iterate just once to create a dynamic
    // array of string pointers and let str_dup_arr() do the work.
    //---------------------------------------------------------------
    array_base = NULL;
    array_end = NULL;
    array_ptr = NULL;
    count = 2;

    //-- Iterate variable argument list building pointer array.
    do {
	//-- If the array has no space, make a bigger array.
	if ( array_ptr == array_end ) {
	    size_t	array_off	;

	    count = count * 2 + 2; // 6 14 30 62 ...
	    array_off = array_ptr - array_base;
	    array_base = realloc( array_base, ( sizeof (const char *) * count ) );
	    if ( ! array_base ) return NULL;
	    array_end = array_base + count;
	    array_ptr = array_base + array_off;
	}
    } while ( ( * array_ptr ++ = va_arg( arg_list, const char * ) ) );

    //-- Call str_dup_arr() to do the work, then clean up and return.
    {
	char *		ptr_string	;

	ptr_string = str_dup_arr( array_base );
	free( array_base );
	return ptr_string;
    }
}

