/*
 * Copyright (C) 1995-1997 Christopher D. Granz
 *
 * This header may not be removed.
 *
 * Refer to the file "License" included in this package for further
 * information and before using any of the following.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "sapphire.h"
/*
 * Functions
 */
/*
 * Return TRUE if argument is completely numeric.
 */
bool is_number( char *pArg )
{
    if ( *pArg == '\0' )
        return ( FALSE );
    if ( *pArg == '+' || *pArg == '-' )
        pArg++;
    for ( ; *pArg != '\0'; pArg++ )
    {
        if ( isdigit( *pArg ) == 0 )
            return ( FALSE );
    }
    return ( TRUE );
}
/*
 * Compare strings, case-insensitive.  Returns FALSE if strings
 * are different.
 */
bool str_compare( char *pStrA, char *pStrB )
{
    for ( ; *pStrA || *pStrB; pStrA++, pStrB++ )
    {
        if ( LOWER( *pStrA ) != LOWER( *pStrB ) )
            return ( FALSE );
    }
    return ( TRUE );
}
/*
 * Compares pStrA to list of words (pStrB) seperated by spaces.
 */
bool multi_compare( char *pStrA, char *pStrB )
{
    char cBuf[MAX_INPUT];
    while ( *pStrB != '\0' )
    {
        pStrB = one_arg( pStrB, cBuf );
        if ( str_compare( pStrA, cBuf ) == TRUE )
            return ( TRUE );
    }
    return ( FALSE );
}
/*
 * Compares pStrA to an array of strings (pStrs).
 */
bool multi_compare_2( char *pStrA, string *pStrs )
{
    int i;
    for ( i = 0; pStrs[i] != NULL; i++ )
    {
        if ( str_compare( pStrA, pStrs[i] ) == TRUE )
            return ( TRUE );
    }
    return ( FALSE );
}
/*
 * Compare strings, case insensitive, for prefix matching.
 * Returns FALSE if pStrA is not a prefix of pStrB.
 */
bool str_prefix( char *pStrA, char *pStrB )
{
    for ( ; *pStrA; pStrA++, pStrB++ )
    {
        if ( LOWER( *pStrA ) != LOWER( *pStrB ) )
            return ( FALSE );
    }
    return ( TRUE );
}
/*
 * Compare strings, case insensitive, for match anywhere.
 * Returns FALSE if pStrA is not a part of pStrB.
 */
bool str_infix( char *pStrA, char *pStrB )
{
    int iSStr1;
    int iSStr2;
    int iChar;
    char c;
    if ( ( c = LOWER( pStrA[0] ) ) == '\0' )
        return ( TRUE );
    iSStr1 = strlen( pStrA );
    iSStr2 = strlen( pStrB );
    for ( iChar = 0; iChar <= iSStr2 - iSStr1; iChar++ )
    {
        if ( c == LOWER( pStrB[iChar] )
        && str_prefix( pStrA, pStrB + iChar ) == TRUE )
            return ( TRUE );
    }
    return ( FALSE );
}
/*
 * Compare strings, case insensitive, for suffix matching.
 * Returns FALSE if pStrA not a suffix of pStrB.
 */
bool str_suffix( char *pStrA, char *pStrB )
{
    int iSStr1;
    int iSStr2;
    iSStr1 = strlen( pStrA );
    iSStr2 = strlen( pStrB );
    if ( iSStr1 <= iSStr2
    && str_compare( pStrA, pStrB + iSStr2 - iSStr1 ) == TRUE )
        return ( TRUE );
    else
        return ( FALSE );
}
/*
 * End of compare.c
 */