//-----------------------------------------------------------------------------
// 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_app_part_ch
//
// purpose	Append to the target string given with maximum available space
//		the first part found in the source string separated by a given
//		character, returning a pointer to the remainder of the source
//		string.
//
//		If the string to be appended does not fit the available space,
//		it remains truncated without indication.
//
//		Unlike str_split_ch(), this function does not modify the
//		source string.
//
// arguments	1 (char *) pointer to target string
//		2 (size_t) maximum space in target string
//		3 (const char *) input string
//		4 (int) separator character
//
// returns	(char *) NULL : no item found or error
//		(char *) pointer to remainder of input string.
//-----------------------------------------------------------------------------
char *
str_app_part_ch (
    char *		arg_target
    ,
    size_t		arg_length
    ,
    const char *	arg_source
    ,
    int			arg_ch
    )
__PROTO_END__
{
    char *		target_ptr	;
    char *		target_end	;
    char *		source_ptr	;

    //-- If there is no source string, nothing to do.
    if ( ! arg_source ) return NULL;
    * ( (const char * *) & source_ptr ) = arg_source;

    //-- Determine end of target and where to begin append.
    target_end = ( target_ptr = arg_target ) + arg_length - 1;
    while ( target_ptr < target_end && * target_ptr ) ++ target_ptr;

    //-- Append characters until the separator is seen.
    while ( * source_ptr && * source_ptr != arg_ch ) {
	if ( target_ptr < target_end ) * target_ptr ++ = * source_ptr;
	++ source_ptr;
    }
    * target_ptr = 0;

    //-- Return pointer to next item in source, or end of source.
    return ( * source_ptr ) ? source_ptr + 1 : source_ptr;
}

