
/*	Various helper functions used in the compiler project
	by Jason Plumb
	Note: Most of these functions exist in one form or another in some
	C library or another....but apparently I will reach total consciousness
	if I implement them myself. */


int ccstrncmp(char *pStr1, char *pStr2, int iLen){
	int i = 0;
	for(i=0; i<iLen; i++){
		if(pStr1[i] > pStr2[i])
			return 1;					//Greater than
		else if(pStr1[i] < pStr2[i])
			return -1;					//Less than
	}//for
	return 0;	//strings are same for this length
}//ccstrncmp function

int ccstrcmp(char *pStr1, char *pStr2){
	int i = 0;
	
	while(1==1){
		if(pStr1[i] > pStr2[i])
			return 1;					//Greater than
		else if(pStr1[i] < pStr2[i])
			return -1;					//Less than
		else if((pStr1[i] == '\0') && (pStr2[i] == '\0'))
			return 0;
		i++;
	}//while
	return 0;
}//ccstrcmp function

void ccstrcpy(char *pDest, char *pSrc){
	int i=0;
	do{
		pDest[i] = pSrc[i];
		i++;
	}while(pSrc[i] != '\0');
	pDest[i] = pSrc[i];
}//ccstrcpy

void ccstrncpy(char *pDest, char *pSrc, int iLen){
	int i=0;
	do{
		pDest[i] = pSrc[i];
		i++;
	}while((pSrc[i] != '\0') && (i <= iLen));

	if(pDest[iLen] != '\0')
		pDest[iLen] = '\0';
}//ccstrncpy

int ccstrlen(char *pStr){
	int i=0;
	while(pStr[i] != '\0')
		i++;
	return i;
}//ccstrlen function

//Convert ascii number to integer representation.
//Note: This could probably use some more error checking...
int ccatoi(char *pStr){
	int iLen = ccstrlen(pStr);
	int iDigit, i;
	int ans = 0;
	iDigit = 1;
	for(i = iLen-1; i >= 0; i--){
		ans += iDigit*(pStr[i]-0x30);
		iDigit *= 10;
	}//for
	return ans;	
}//ccatoi function

//Remove all comments from a buffer by converting them to whitespace.
//CR+LF will remain intact.
void ccRemoveComments(char *pStr){
	
	int iLen = ccstrlen(pStr);
	int i = 0;
	bool bInComments = false;
	while(i < iLen){
		
		if(bInComments){
			if(ccstrncmp(&pStr[i], "*/", 2)==0){
				pStr[i] = ' ';
				pStr[i+1] = ' ';
				bInComments = false;
			}//if
			else if((pStr[i] != '\n') && (pStr[i] != '\r')){
				pStr[i] = ' ';
			}//else
		}//if
		else{
			if(ccstrncmp(&pStr[i], "/*", 2)==0){
				bInComments = true;
				pStr[i] = ' ';
			}//if
		}//else
		i++;
	}//while
}//ccRemoveComments

//Determines if a char is an alpha char
bool isAlpha(char c){
	if(((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')))
		return true;

	if(c == '_')				//We will allow underscore to be an alpha char
		return true;

	return false;
}//isAlpha function

//Determines if a char is whitespace
bool isWS(char c){
	if((c == ' ') || (c == '\t') || (c == '\r') || (c == '\n'))	//Valid whitespace chars
		return true;
	return false;
}//isWS function

bool isOp(char *pStr, int iLen){
	char *ops[] =				{	"+", "-", "*", "/", "<", ">", "=", ">=", "<=", "<>", ",",
								";", "**", ":=", "(", ")", "[", "]", "/*", "*//", ":", "..", ""};
 
	if(iLen > 2)
		return false;			//We don't have any operators > 2 chars long
	int i = 0;
 
	char b[3];
	ccstrncpy(b, pStr, iLen);

	while(ccstrlen(ops[i]) != 0){

		if(ccstrcmp(b, ops[i])==0){			//We found an op
			return true;
		}//if

		i++;
	}//while

	return false;

}//isOp function