//-----------------------------------------------------------------------------
// 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_cmp_upper
//
// purpose	Compare two strings converted to upper case, returning a result
//		that indicates the collation order of the strings.
//
// arguments	1 (const char *) string 1
//		2 (const char *) string 2
//
// returns	(int) -1 : string 1 <  string 2
//		(int)  0 : string 1 == string 2
//		(int)  1 : string 1 >  string 2
//-----------------------------------------------------------------------------
#define str_cmp_upper(a,b) str_cmp_lim_upper((a),(b),~0)

//-----------------------------------------------------------------------------
// function     str_equal_upper
//
// purpose      Compare two strings converted to upper case for equality,
//              returning true or false.
//
// arguments    1 (const char *) string 1
//              2 (const char *) string 2
//
// returns      (int) 0 : strings are not equal
//              (int) 1 : strings are equal
//-----------------------------------------------------------------------------
#define str_equal_upper(a,b) ((str_cmp_lim_upper((a),(b),~0))==0)

//-----------------------------------------------------------------------------
// function	str_cmp_lim_upper
//
// purpose	Compare two strings converted to upper case, returning a result
//		that indicates the collation order of the strings.
//
// arguments	1 (const char *) string 1
//		2 (const char *) string 2
//		3 (size_t) maximum length to compare, or ~0 to compare full
//
// returns	(int) -1 : string 1 <  string 2
//		(int)  0 : string 1 == string 2
//		(int)  1 : string 1 >  string 2
//-----------------------------------------------------------------------------
int
str_cmp_lim_upper (
    const char *	arg_string1
    ,
    const char *	arg_string2
    ,
    size_t		arg_length
    )
__PROTO_END__
{
    const char *	str1	;
    const char *	str2	;
    size_t		len	;

    str1 = arg_string1 ? arg_string1 : "";
    str2 = arg_string2 ? arg_string2 : "";

    if ( ( len = arg_length ) == ~0 ) {
	while ( * str1 && * str2 && toupper( * str1 ) == toupper( * str2 ) ) {
	    ++ str1;
	    ++ str2;
	}
    } else {
	while ( len > 0 && toupper( * str1 ) == toupper( * str2 ) ) {
	    -- len;
	    ++ str1;
	    ++ str2;
	}
    }
    return ( toupper( * str1 ) < toupper( * str2 ) ) ? -1 :
	 ( ( toupper( * str1 ) > toupper( * str2 ) ) ? 1 : 0 );
}

