/*
 *  TEX Device Driver  ver 2.00, 2.25
 *  copyright(c) 1988, 1989 by TSG.
 *	The above is lost, 1990 made by SHIMA
 *
 *  dviread.c : ファイルから数値を読み出す
 *	very slightly modified against warnings by Oh-Yeah? 25 May 1992
 */

#include	<stdio.h>
#include	"dd.h"

/* buffer.c */
void *marea(unsigned int size);

char *read_str(FILE * fp, int len)
{
	char *pt0, *pt;

	if ((pt = pt0 = (char *)marea(len + 1)) != NULL) {
		while (len-- > 0)
			*pt++ = getc(fp);
		*pt = '\0';

	}
	return (pt0);
}

long read_n(FILE * fp, int n)
{
	long	d;

	if(n--)
		d = (unsigned char)getc(fp);
	else
		return 0;
	while (n-- > 0)
		d = (d<<8) +  (unsigned char)getc(fp);
	return d;
}

long signed_read_n(FILE * fp, int n)
{
	long   d;

	if(n--)
		d = (signed char)getc(fp);
	else
		return 0;
	while (n-- > 0)
		d = (d<<8) +  (unsigned char)getc(fp);
	return d;
}

long long_int(uchar *p)
{
	long	d;

	if( (d = p[3]) & 0x80 ) d |= ~0xffL;

	d = (d << 8) + p[2];
	return (d << 16)|((uint)(p[1]) << 8)|p[0];
}

long save_long(uchar *pt, long num)
{
	int	i;
	long tmp;

	for(tmp = num, i = 0; i < 4; ){
		pt[i++] = tmp & 0xff;
		tmp >>= 8;
	}
	return num;
}
