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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "pdefs.h"
#include "pcvt.h"
#include "precision.h"
/*
* Output a string to a file.
*
* Returns:
* the number of characters written
* or EOF if error
*/
static int fouts(stream, chp)
FILE *stream;
register char *chp;
{
register int count = 0, res = 0;
if (chp != (char *) 0 && *chp != '\0') do {
count++;
res = putc(*chp, stream);
} while (*++chp != '\0' && res != EOF);
if (res != EOF) res = count;
return res;
}
/*
* output the value of a precision to a file (no cr or whitespace)
*
* Returns:
* The number of characters output or EOF if error
*/
int fputp(stream, p)
FILE *stream;
precision p;
{
int res;
char *chp = ptoa(pparm(p));
res = fouts(stream, chp);
deallocate(chp);
pdestroy(p);
return res;
}
/*
* Output a precision to stdout with a newline (useful from debugger)
*/
int putp(p)
precision p;
{
int res;
char *chp = ptoa(pparm(p));
res = fouts(stdout, chp);
res = putc('\n', stdout);
deallocate(chp);
pdestroy(p);
return res;
}
/*
* Output a justified precision
*
* Returns: The number of characters in the precision, or EOF if error
*/
int fprintp(stream, p, minWidth)
FILE *stream;
precision p;
register int minWidth;
{
int res;
char *chp = ptoa(pparm(p));
int len;
len = strlen(chp);
if (minWidth < 0) { /* left-justified */
res = fouts(stream, chp);
while (minWidth++ < -len) {
putc(' ', stream);
}
} else {
while (minWidth-- > len) { /* right-justified */
putc(' ', stream);
}
res = fouts(stream, chp);
}
deallocate(chp);
pdestroy(p);
return res;
}
/*
* Read in a precision type - same as atop but with io
*
* leading whitespace skipped
* an optional leading '-' or '+' followed by digits '0'..'9'
* leading 0's Ok
* stops at first unrecognized character
*
* Returns: pUndef if EOF or invalid argument (NULL or nondigit as 1st digit)
*/
precision fgetp(stream)
FILE *stream;
{
precision res = pUndef;
precision clump = pUndef;
int sign = 0;
register int ch;
register accumulator temp, x;
register int j;
ch = getc(stream);
if (ch != EOF) {
while (isspace(ch)) ch = getc(stream); /* skip whitespace */
if (ch == '-') {
sign = 1;
ch = getc(stream);
} else if (ch == '+') {
ch = getc(stream);
}
if (isdigit(ch)) {
pset(&res, pzero);
pset(&clump, utop(aDigit));
do {
j = aDigitLog-1;
temp = ch - '0';
do {
if (!isdigit(ch = getc(stream))) goto atoplast;
temp = temp * aBase + (ch - '0');
} while (--j > 0);
pset(&res, padd(pmul(res, clump), utop(temp)));
} while (isdigit(ch = getc(stream)));
goto atopdone;
atoplast:
x = aBase;
while (j++ < aDigitLog-1) {
x *= aBase;
}
pset(&res, padd(pmul(res, utop(x)), utop(temp)));
atopdone:
if (ch != EOF) ungetc(ch, stream);
if (sign) {
pset(&res, pneg(res));
}
} else {
if (ch == EOF) {
res = pUndef;
} else {
ungetc(ch, stream);
}
}
} else {
res = pUndef;
}
pdestroy(clump);
if (res == pUndef) return res;
return presult(res);
}
|