File: util.c

package info (click to toggle)
dvi2ps 3.2j-9
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,344 kB
  • ctags: 1,896
  • sloc: ansic: 12,767; sh: 938; makefile: 269
file content (147 lines) | stat: -rw-r--r-- 2,424 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include	"defs.h"
#include	"global.h"

char *
alloc_check(p, e)
char *p, *e;
{
    if (p == NULL)
	Fatal("can't malloc space for %s", e);
    return p;
}

makeuint(s, n)		/* return n byte quantity from string */
byte *s;
int n;
{
    int x;	/* number being constructed */

    x = 0;
    while (n--)  {
	x <<= 8;
	x |= *s++;
    }
    return x;
}

makeint(s, n)		/* return n byte quantity from string */
byte *s;
int n;
{
    int n1;		/* number of bytes	    */
    int x;	/* number being constructed */

    x = *s++;		/* get first (high-order) byte */
    n1 = n--;
    while (n--)  {
	x <<= 8;
	x |= *s++;
    }

    /* NOTE: This code assumes that the right-shift is an arithmetic, rather
    than logical, shift which will propagate the sign bit right.   According
    to Kernighan and Ritchie, this is compiler dependent! */

    x<<=32-8*n1;
    x>>=32-8*n1;  /* sign extend */

    return x;
}

htoi(s, se)		/* hex string to int */
char *s, **se;
{
    int x;

    for (x = 0; ; s++) {
	if ('0' <= *s && *s <= '9')
	    x = x*16+*s-'0';
	else if ('a' <= *s && *s <= 'f')
	    x = x*16+*s-'a'+10;
	else if ('A' <= *s && *s <= 'F')
	    x = x*16+*s-'A'+10;
	else {
	    *se = s;
	    return x;
	}
    }
}

numstr(s)
char *s;
{
    for (; *s != '\0'; s++)
	if (!isdigit(*s))
	    return FALSE;
    return TRUE;
}

char *
strsave(s)
char *s;
{
    char *t;
    int len;

    if ((t = malloc((unsigned)(len = strlen(s)+1))) == NULL)
	Fatal("cannot save string %s", s);
    bcopy(s, t, len);
    return t;
}

/* getstrtok:
 *   find the first c in d, put '\0' there, and let *e point the next position.
 */
getstrtok(d, c, e)
char *d;
char c;
char **e;
{
    for (; *d != c && *d != '\0'; d++)
	;
    if (*d == c) {
	*d = '\0';
	*e = d+1;
	return TRUE;
    }
    return FALSE;
}

skipstrblank(d, e)
char *d;
char **e;
{
    for (; *d == ' ' || *d == '\t'; d++)
	;
    *e = d;
}


#define	LOWP	16
#define	FIXP	20
#define	LOWMASK	((1<<LOWP)-1)
#define	D	(1<<(FIXP-LOWP))
#define	H	(1<<(2*LOWP-FIXP))
#define	TPT(x, y)	((x)<<(y))	/* x * 2^y */
#define	DPT(x, y)	((x)>>(y))	/* x / 2^y */

scale_exact(s, d)
int s, d;
{
    int sign;
    unsigned int s1, s0, d1, d0;

    if (s < 0) {
	sign = -1;
	s *= -1;
    } else
	sign = 1;
    s0 = s & LOWMASK;
    d0 = d & LOWMASK;
    s1 = s >> LOWP;
    d1 = d >> LOWP;
    return (sign *
	    (TPT(s1*d1, 2*LOWP-FIXP) +
	     DPT(s1*d0+s0*d1 + DPT(s0*d0, LOWP), FIXP-LOWP))
	    );
}