//-----------------------------------------------------------------------------
// Copyright © 2004 - 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	split_to_common
//
// purpose	Collect parts from the specified split object and construct
//		a common pointer array and string pool from the parts.
//
// argument	1 (split_p) pointer to split state
//		2 (char * * *) where to store pointer to common area
//
// returns	(int) -1 : error
//		(int)  0 : no parts found
//		(int)  N : number of parts found
//-----------------------------------------------------------------------------
int
split_to_common (
    split_p		arg_split
    ,
    char * * *		arg_common
    )
__PROTO_END__
{
    char * *	common_ptr	;
    char * *	array_ptr	;
    char *	pool_ptr	;
    size_t	str_len		;
    int		count		;

    //-- Do a dry run split to determine size needed.
    count = 1;
    str_len = 0;
    split_reset( arg_split );
    while ( split_next( arg_split ) == 0 ) {
	str_len += split_part_len( arg_split );
	str_len += 1;
	++ count;
    }

    //-- Allocate memory for the common area.
    common_ptr = malloc( count * sizeof (char *) + str_len );
    if ( ! common_ptr ) return -1;

    //-- Split again and collect parts this time.
    array_ptr = common_ptr;
    pool_ptr = (char *) ( array_ptr + count );
    split_reset( arg_split );
    while ( split_next( arg_split ) == 0 ) {
	* array_ptr ++ = pool_ptr;
	memcpy( pool_ptr, split_part_ptr( arg_split ), split_part_len( arg_split ) );
	pool_ptr += split_part_len( arg_split );
	* pool_ptr ++ = 0;
    }
    * array_ptr = NULL;

    //-- Return number of parts.
    return count;
}

