//////////////////////////////////////////////////////////////////////
// UCSB - Electrical and Computer Engineering Dept.
//
// Input/Output Library Manager
//
// Version  When      Who               Why
// -------  ----      ---               ---
// 1.0      02/01/00  Daryl Fortney     Initial Design
//
// Copyright 2000 UCSB.  All rights reserved.
//////////////////////////////////////////////////////////////////////
// Input/Output Library Manager
//////////////////////////////////////////////////////////////////////
#include <iol/iol.hpp>

BOOL Range(long* n,long min,long max){
	if(*n<min){
		*n=min;
		return FALSE;
	}
	if(*n>max){
		*n=max;
		return FALSE;
	}

	return TRUE;
}

void TextTrim(TEXT text){
	WORD	i;
	WORD	pos=0;

	for(i=0;;i++){
		
		// Insert Character
		text[pos]=text[i];
		
		// Strip Various Characters
		if(text[i]!=IOL_ESC)
			pos++;

		if(text[i]==IOL_NUL)
			break;
	}
}

//////////////////////////////////////////////////////////////////////
// DisplayGoto() - Puts Text Cursor at given position.
//////////////////////////////////////////////////////////////////////
void DisplayGoto(int x, int y) {
	printf("\33[%d;%dH", y, x);
}

//////////////////////////////////////////////////////////////////////
// DisplayClear2End() - Clear Text Screen from given position to end.
//////////////////////////////////////////////////////////////////////
void DisplayClear2End(int x, int y) {
	long	i;
	
	// Goto Given Position
	DisplayGoto(x, y);

	// Clear To End
	fputs("\33[K", stdout);
	for(i=y;i<25;i++) {
		DisplayGoto(1,i);
		fputs("\33[K", stdout);
	}
	
	// Return To Given Position
	DisplayGoto(x, y);
}

//////////////////////////////////////////////////////////////////////
// DisplayTextBox() - Draws a text box at given position of given
// size out of DISPLAY_BOX_CHAR characters.
//////////////////////////////////////////////////////////////////////
#define	DISPLAY_BOX_CHAR	'*'
void DisplayTextBox(int x, int y, int dx, int dy) {
	int i, j;

	// Draw Top Line
	DisplayGoto(x, y);							   
	for (i = 0; i < dx; i++) 
		putchar(DISPLAY_BOX_CHAR); 
	
	// Draw Side Lines
	for (i = 2; i < dy; i++) {
		DisplayGoto(x, y + i - 1);
		putchar(DISPLAY_BOX_CHAR);
		for (j = 2; j < dx; j++) 
			puts(" ");
		putchar(DISPLAY_BOX_CHAR);
	}

	// Draw Bottom Line
	DisplayGoto(x, y + dy - 1);
	for (i = 0; i < dx; i++) 
		putchar(DISPLAY_BOX_CHAR);
}

//////////////////////////////////////////////////////////////////////
// Delay() - This function delays for given milliseconds.
//////////////////////////////////////////////////////////////////////
void Delay(long ms){

	#ifdef IOL_DOS
	delay(ms);
	#else
	long wait=(ms*CLOCKS_PER_SEC)/1000;
	long ts=clock();
	while(clock()-ts<wait);
	#endif
}

//////////////////////////////////////////////////////////////////////
// Indent() - This function just prints DISPLAY_INDENT_CHAR the given
// number of times.
//////////////////////////////////////////////////////////////////////
#define	DISPLAY_INDENT_CHAR	"   "
void Indent(long v){
	for(long i=0;i<v;i++) 
		cerr<<DISPLAY_INDENT_CHAR;
}

void* Address(void* ad){
	unsigned long	rv=(unsigned long)ad;
	return (void*)(((rv&0xffff0000)>>16)*16+(rv&0xffff));
}

long StrSame(char* a,char* b){

	long i;

	for(i=0;i<=StrLength(a);i++)
		if(toupper(a[i])!=toupper(b[i])) return 0;

	return 1;
}

long StrLength(char* a){

	long i;

	for(i=0;a[i];i++){}

	return i;
}

