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

__FMACRO_BEGIN__
//-----------------------------------------------------------------------------
// macro	while_split
//
// purpose	Iterate over the parts of a split object.
//
// arguments	1 (split_p) pointer to split object
//
// usage	split_while( split_obj ) {
//			/* statements */
//		}
//-----------------------------------------------------------------------------
#define while_split(o) while ( split_next( (o) ) == 0 )

__FMACRO_END__

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	split_next
//
// purpose	Advance the specified split object to the next (first if just
//		initialized) part of the string being split.  Return a status
//		of whether or not a part is found.
//
// argument	1 (split_p) pointer to split state
//
// returns	(int) -1 : error
//		(int)  0 : next part ready
//		(int)  1 : end of string, no next part
//
// note		Use the macros
//			split_part_ptr()
//			split_part_end()
//			split_part_len()
//		to access the actual part.
//-----------------------------------------------------------------------------
int
split_next (
    split_p		arg_split
    )
__PROTO_END__
{
    char *	ptr		;


    //-- If no more parts, return now.
    ptr = arg_split->next_ptr;
    if ( ! ptr ) return 1;

    //-- If spaces are to be trimmed, do the leading ones now.
    if ( arg_split->flags & SPLIT_SEP_TRIMSPACES ) {
	char *	end	;

	//-- Trim leading space and tab characters from the part.
	end = arg_split->data_end;
	while ( ptr < end && ( * ptr == ' ' || * ptr == '\t' ) ) ++ ptr;

	//-- If the end was reached, there is no part here.
	if ( ptr == end ) {
	    arg_split->next_ptr = NULL;
	    return 1;
	}

	//-- Advance the separator end with the trimming.
	arg_split->next_ptr = ptr;
    }

    //-- This is now the beginning of the part.
    arg_split->part_ptr = ptr;

    //-- Find the next separator according to the specified separator type.
    ( * ( arg_split->call ) )( arg_split );

    //-- If spaces are to be trimmed, do the trailing ones now.
    if ( arg_split->flags & SPLIT_SEP_TRIMSPACES ) {
	char *	end	;

	//-- Trim trailing space and tab characters from the part.
	end = arg_split->part_end - 1;
	while ( end >= ptr && ( * end == ' ' || * end == '\t' ) ) -- end;
	arg_split->part_end = end + 1;
    }

    //-- Return indicating a part was split off.
    return 0;
}

