//	bfe2 - miscellaneous functions
//	Copyright (c) 1999-2003 Brand Huntsman and Lee Salzman
//


#include "common.h"
#include "functions.h"


//////////////////////////////////////////////////////////////////////////

// global

// local

//////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////


void clip_whitespace( char *string ){
	char *end;

	end = string + strlen(string);
	if(end == string) return;
	end--;

	while(end > string){
		if(isspace(*end)) *end = '\0';
		else return;
		end--;
	}
}


uint string_compare( char *s1, char *s2 ){
	// case-insensitive string comparison
	// return 1 if match, else 0
	char c1, c2;

	while(*s1 != '\0' && *s2 != '\0'){
		c1 = *s1;
		c2 = *s2;
		if(c1 >= 'A' && c1 <= 'Z') c1 |= 32;
		if(c2 >= 'A' && c2 <= 'Z') c2 |= 32;
		if(c1 != c2) return(0);
		s1++;
		s2++;
	}
	if(*s1 != *s2) return(0);
	return(1);
}


uint get_word( char *buffer, uint *i, char **word ){
	// returns length
	uint start;

	// skip whitespace
	for(; buffer[*i] != '\0' && isspace(buffer[*i]); (*i)++);

	*word = &buffer[*i];
	start = *i;

	// find end of word
	for(; buffer[*i] != '\0' && !isspace(buffer[*i]); (*i)++);

	if(buffer[*i] != '\0'){
		buffer[*i] = '\0';
		(*i)++;
		return(*i - start - 1);
	}
	return(*i - start);
}


void goto_next_word( char *buffer, uint *i ){
	// find end of word
	for(; buffer[*i] != '\0' && !isspace(buffer[*i]); (*i)++);
	// skip whitespace
	for(; buffer[*i] != '\0' && isspace(buffer[*i]); (*i)++);
}
