util.c 3.19 KB

#include <stdio.h>

getline(s,lim,fp)
char s[];
FILE *fp;
int lim;
{
	int c,i;

	for (i=0; i<lim-1 && (c=getc(fp)) != EOF && c != '\n'; ++i)
		{
		s[i] = c;
		}
	
	if (c == '\n')
		{
		s[i] = '\0';
		++i;
		}
	return(i);
}

dastodi(buf)
char	buf[];
{
	int i;
	long n;
	
	n = 0;
	for (i=0; buf[i] != '\0'; ++i)
		n = n * 10 + buf[i] - '0';
		return(n);

}

bastodi(buf)
char	buf[];
{
	int i;
	long n;
	
	n = 0;
	for (i=0; buf[i] != '\0'; ++i)
		n = n * 2 + buf[i] - '0';
		return(n);

}


hastodi(buf) /* hex char to decimal integer */
char	buf[];
{
	int i;
	long n;
	
	n = 0;
	for (i=0; buf[i] != '\0'; ++i)
	{
		if (buf[i] >='0' && buf[i] <='9')
			n = n * 16 + buf[i] - '0';
		else
			if (buf[i] >='A' && buf[i] <='F')
			n = n * 16 + buf[i] - 55;
		else
			n = n * 16 + buf[i] - 87;
	}
		return(n);

}

itochst(value,buf)
unsigned long value;
char buf[];
{
	int i,n;

	for (i = 0;i<=7;++i)
		buf[i] = '0';
	buf[8] = '\0';
	i = 7;
	do
		{
		n = (value % 16)&0xF;
		if (n<=9)
			buf[i] = n + '0';
		else
			buf[i] = n - 10 + 'A';
		i-=1;
		} while ((value /= 16) >0);
}

itobichst(value,buf,len)
unsigned long value;
char buf[];
{
	int i,n;

	for (i = 0;i<len;++i)
		buf[i] = '0';
	buf[len] = '\0';
	i = len-1;
	do
		{
		n = (value % 2)&0x1;
		buf[i] = n + '0';
		i-=1;
		} while ((value /= 2) >0);
}


getnum(numbuf_in,err)
short *err;
char numbuf_in[];
{
	char buf[50];
	char numbuf[50];
	int i,k;
	long n;
	*err = 0;
	if (numbuf_in[0] == '\0')
		{
		*err = 2;
		return(0);
		}
	else
	{
        for (i=0,k=0; numbuf_in[i] != '\0'; ++i)
	    if (numbuf_in[i]!='_') numbuf[k++] = numbuf_in[i]; 
	numbuf[k] = '\0';
	/* hex number */
	if (numbuf[0] == '0' && (numbuf[1] == 'x' || numbuf[1] == 'X'))
		{
		for (i=2,k=0;(numbuf[i] >= '0' && numbuf[i] <= '9') || 					 (numbuf[i] >= 'a' && numbuf[i] <= 'f') ||					 (numbuf[i] >= 'A' && numbuf[i] <= 'F');++i,++k)
			buf[k]=numbuf[i];

			buf[k] = '\0';
		if (numbuf[i] != '\0' || k>8)
			*err = 1;
		else 
			return(hastodi(&buf[0]));
		}

	else	
	/* bin number */
	if (numbuf[0] == '0' && (numbuf[1] == 'b' || numbuf[1] == 'B'))
		{
		for (i=2,k=0;(numbuf[i] == '0' || numbuf[i] == '1');++i,++k)
			buf[k]=numbuf[i];

			buf[k] = '\0';
		if (numbuf[i] != '\0' || k>32)
			*err = 1;
		else 
			return(bastodi(&buf[0]));
		}

	else	
	/* dec number */
		{
		for (i=0,k=0;numbuf[i] >= '0' && numbuf[i] <= '9';++i,++k)
			buf[k]=numbuf[i];

			buf[k] = '\0';
		if (numbuf[i] != '\0' || k>10)
			*err = 1;
		else 
			return(dastodi(&buf[0]));
		}
	 }


}

err_print(lnum,ertype)
int lnum;
short ertype;
{
	static char *errtab[] = {
			    "",					/*0*/
			    "invalid operation",		/*1*/
			    "count value incorrect",		/*2*/
			    "direction field is missing",	/*3*/
			    "direction value is incorrect",	/*4*/
			    "size value is missing ",		/*5*/
			    "size value is incorrect",		/*6*/
			    "address is missing",		/*7*/
			    "address value is incorrect",	/*8*/
			    "data0,1 are missing",		/*9*/
			    "data0,1 are incorrect",		/*10*/
			    "Wcount/command value incorrect",   /*11*/
			    "Interrupt level is missing/wrong", /*12*/
			    "RMI_Mem/Boot_mode Directive missing/wrong", /*13*/
			    };



	printf("error line: %4d ----- %s   error no: %2d\n",
			   lnum,      errtab[ertype], ertype);

}