//-----------------------------------------------------------------------------
// 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	mem_dup_len
//
// purpose	Duplicate into allocated space the contents of one or more
//		strings of bytes specified with pointer and length pairs,
//		and store the total length of the result where specified.
//
// arguments	1 (size_t *) where to store total length
//		2 (const char *) source string to duplicate
//		3 (size_t) length of string to duplicate
//		... repeat last 2
//
// returns	(char *) new allocated duplicate string, caller must free
//-----------------------------------------------------------------------------
#define mem_dup_len(l,p,x...) ((mem_dup_len)((l),(p),x,(const char*)(NULL)))

//-----------------------------------------------------------------------------
// function	mem_dup
//
// purpose	Duplicate into allocated space the contents of one or more
//		strings of bytes specified with pointer and length pairs.
//
// arguments	1 (const char *) source string to duplicate
//		2 (size_t) length of string to duplicate
//		... repeat last 2
//
// returns	(char *) new allocated duplicate string, caller must free
//-----------------------------------------------------------------------------
#define mem_dup(p,x...) ((mem_dup_len)((size_t*)(NULL),(p),x,(const char*)(NULL)))

__FMACRO_END__

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	mem_dup_len
//
// purpose	Duplicate into allocated space the contents of one or more
//		strings of bytes specified with pointer and length pairs,
//		and store the total length of the result where specified.
//
// arguments	1 (size_t *) where to store total length
//		2 (const char *) source string to duplicate
//		3 (size_t) length of string to duplicate
//		... repeat last 2
//		N (const char *) NULL to end variable arguments
//
// returns	(char *) new allocated duplicate string, caller must free
//-----------------------------------------------------------------------------
char *
(mem_dup_len) (
    size_t *		arg_return_len
    ,
    const char *	arg_source_ptr
    ,
    size_t		arg_source_len
    ,
    ...
    )
__PROTO_END__
{
    va_list		var_args	;
    char *		string_ptr	;
    char *		target_ptr	;
    const char *	source_ptr	;
    size_t		source_len	;

    //-- Add up space required.
    source_len = arg_source_len;
    va_start( var_args, arg_source_len );
    while ( va_arg( var_args, const char * ) ) {
	source_len += va_arg( var_args, size_t );
    }
    va_end( var_args );

    //-- Store back the length.
    if ( arg_return_len ) * arg_return_len = source_len;

    //-- Allocate new space from given length.
    string_ptr = (char *) malloc( source_len );
    if ( ! string_ptr ) return NULL;
    target_ptr = string_ptr;

    //-- Copy contents to new string.
    source_ptr = arg_source_ptr;
    source_len = arg_source_len;
    va_start( var_args, arg_source_len );
    for (;;) {
	memcpy( target_ptr, source_ptr, source_len );
	target_ptr += source_len;
	source_ptr = va_arg( var_args, const char * );
	if ( ! source_ptr ) break;
	source_len = va_arg( var_args, size_t );
    }
    va_end( var_args );

    //-- Return beginning of new space.
    return string_ptr;
}

