/*

The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/

Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.

The Original Code is a portion of the Path Development API developed
by CygnaCom Solutions, Inc.

Portions created by CygnaCom Solutions, Inc. are Copyright (C) CygnaCom 
Solutions, Inc. All Rights Reserved.

Contributor(s): ______________________________________.

http://www.cygnacom.com

*/
/*------------------------------------------------------------------------*\
|  Project Name  :   Case Insensitive string in string (like strstr)
|  File Name     :   stristr.cpp
|  Creation Date :   April 12, 1999
|  Author        :   Matt Cooper
|  Last Mode Date:   April 12, 1999
|  Modification History:
|--------------------------------------------------------------------------
|
|     Known Bugs:   
|                   
|     Dependent on these files:   
|                                 
|--------------------------------------------------------------------------
| Cryptographic devices and technical data regarding them are subject to
| Federal Government export controls as specified in Title 22, Code of
| Federal Regulations, Parts 121 through 128.  Cryptographic devices
| implementing the Data Encryption Standard (DES) and technical data
| regarding them must comply with these Federal regulations
|--------------------------------------------------------------------------
| Copyright (c) 1999, CygnaCom Solutions, Inc.
| All Rights Reserved.
\*------------------------------------------------------------------------*/

#ifdef VX /* Added by Masanori Satake. */
#include "vxWorks.h"
#include "vxdbg.h"
#endif

#include "stristr.h"
#include <string.h>

/*-------------------------------------------------------------------------*\
|  Function Name :   stristr(const char* str, const char* substr)
|  Creation Date :   April 12, 1999
|  Author        :   Matt Cooper
|  Last Mode Date:   April 12, 1999
|---------------------------------------------------------------------------
|	        
|  Purpose:	Returns a pointer to the first occurrence of substr in str, 
|	        or NULL if substr does not appear in str.  If substr points 
|	        to a string of zero length, the function returns str.
|
|---------------------------------------------------------------------------
|  Inputs Params:   str		Null-terminated string to search
|					substr	Null-terminated string to search for within str
|	                
|  Pre-Conditions:  N/a
|  Post-Conditions: N/a
|  Return values:   Pointer to the first occurrence of substr in str || NULL      
\*-------------------------------------------------------------------------*/
char* stristr(const char* str, const char* substr)
{
	if( NULL == str || NULL == substr)
		return NULL;

	// check for empty input strs
	if( !*str )
		return NULL;

	if( !*substr )
		return (char*)str;

	// create tmp bufs
	char *src = new char[strlen(str)+1];
	char *sub = new char[strlen(substr)+1];
	char *tmp = src, *src2 = src;
	int pos = 0; // tracks cur pos in src

	// cpy data
	strcpy(src,str);
	strcpy(sub,substr);

     
	// change [A-Z] to [a-z] in src
	char c = *tmp; //tmpval to prevent two mem lookups
	do 
	{
		if ( (c >= 'A') && (c <= 'Z') )
			*tmp += ('a' - 'A');
	} 
	while (  (c = (char)(*(++tmp))) != '\0' );

	// change [A-Z] to [a-z] in sub
	tmp = sub;
	c = *tmp; //tmpval to prevent two mem lookups
	do 
	{
		if ( (c >= 'A') && (c <= 'Z') )
			*tmp += ('a' - 'A');
	} 
	while (  (c = (char)(*(++tmp))) != '\0' );

/*	c = *tmp;
	do 
	{
		if ( ((c = (char)(*tmp)) >= 'A') && (c <= 'Z') )
			*tmp += ('a' - 'A');
	} 
	while (*(++tmp) != NULL);
*/
	// search src for sub
	while (*str)
	{
		tmp = sub;
		src2 = &(src[pos]);
		
		while ( *src2 && *tmp && !(*src2-*tmp) )
			src2++, tmp++;
		
		if (!(*tmp))
		{
			// found it.
			delete []src;
			delete []sub;
			return (char*)str;
		}
		
		pos++;
		str++; // inc str to keep track of return value
	}

	// Not found
	delete []src;
	delete []sub;
	return NULL;
}


#if !defined(_WIN32)

// returns 0 if same, 1 if str > str2, -1 if str < str2
int stricmp(const char *str, const char *str2)
{

	if( NULL == str && NULL == str2)
		return 0;

	if( !*str && !*str2 )
		return 0;

	if( !*str && *str2 )
		return -1;

	if( *str && !str2 )
		return 1;

	char c1, c2;

	do
	{
		c1 = (char)(*(++str));
		if( (c1 >= 'A') && (c1 <= 'Z') )
		{
			c1 += ('a' - 'A');
		}

		c2 = (char)(*(++str2));
		if( (c2 >= 'A') && (c2 <= 'Z') )
		{
			c2 += ('a' - 'A');
		}

	}
	while( c1 == c2 && c1 != '\0' && c2 != '\0' );


	if( c1 > c2 )
		return 1;

	if( c2 > c1 )
		return -1;

	return 0;
}

#ifdef _DEBUG_VERBOSE
#undef _DEBUG_VERBOSE
#endif


#endif
