//-----------------------------------------------------------------------------
// 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_sep_find_string
//
// purpose	Find the start and end of the next separator and update the
//		split state for the new position.
//
//		The separator is a specified character.
//
// argument	1 (split_p) pointer to split state
//
// returns	(int) -1 : error
//		(int)  0 : separator found
//		(int)  1 : end of string, not found
//-----------------------------------------------------------------------------
int
split_sep_find_string (
    split_p		arg_split
    )
__PROTO_END__
{
    char *	str_ptr		;
    char *	str_stop	;
    char *	sep_ptr		;
    size_t	sep_len		;


    str_ptr = arg_split->next_ptr;
    sep_ptr = arg_split->sep.data.ptr;
    sep_len = arg_split->sep.data.end - sep_ptr;
    str_stop = arg_split->data_end - sep_len;

    //-- Find start of separator.
    if ( arg_split->flags & SPLIT_SEP_NOCASE ) {
	while ( str_ptr <= str_stop &&
		mem_cmp_lower( sep_ptr, sep_len, str_ptr, sep_len ) != 0 ) {
	    ++ str_ptr;
	}
    } else {
	while ( str_ptr <= str_stop &&
		memcmp( sep_ptr, str_ptr, sep_len ) != 0 ) {
	    ++ str_ptr;
	}
    }

    //-- If end of string, return not found
    if ( str_ptr > str_stop ) {
	//-- The end of string is the end of this part.
	arg_split->part_end = arg_split->data_end;
	arg_split->next_ptr = NULL;
	return 1;
    }

    //-- The separator is the end of this part.
    arg_split->part_end = str_ptr;

    //-- Find end of separator.
    str_ptr += sep_len;
    arg_split->next_ptr = str_ptr;

    //-- Return success.
    return 0;
}

