1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: hfio.c,v 1.3 2003/08/01 14:14:24 schorsch Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Human-readable file I/O |
6 |
*/ |
7 |
|
8 |
|
9 |
#include "meta.h" |
10 |
|
11 |
|
12 |
|
13 |
static PRIMITIVE peof = {PEOF, 0200, {-1, -1, -1, -1}, NULL, NULL}; |
14 |
|
15 |
|
16 |
int |
17 |
readp( /* get human-readable primitive */ |
18 |
PRIMITIVE *p, |
19 |
FILE *fp |
20 |
) |
21 |
{ |
22 |
char inbuf[MAXARGS]; |
23 |
register int c, nargs; |
24 |
int tmp; |
25 |
|
26 |
if (fp == NULL) fp = stdin; |
27 |
|
28 |
restart: |
29 |
|
30 |
if ((c = getc(fp)) == EOF) { /* used to be fatal */ |
31 |
mcopy((char *)p, (char *)&peof, sizeof(PRIMITIVE)); |
32 |
return(0); |
33 |
} |
34 |
|
35 |
if (c == CDELIM) { /* skip user comment */ |
36 |
fgets(inbuf, MAXARGS, fp); |
37 |
goto restart; |
38 |
} else if (c == '\n') /* skip empty line */ |
39 |
goto restart; |
40 |
|
41 |
if (!iscom(c)) |
42 |
error(USER, "bad command in readp"); |
43 |
|
44 |
p->com = c; |
45 |
|
46 |
fscanf(fp, "%o", &tmp); |
47 |
p->arg0 = tmp & 0377; |
48 |
|
49 |
if (isglob(c)) |
50 |
p->xy[XMN] = p->xy[YMN] = p->xy[XMX] = p->xy[YMX] = -1; |
51 |
else if (fscanf(fp, "%d %d %d %d", &p->xy[XMN], &p->xy[YMN], |
52 |
&p->xy[XMX], &p->xy[YMX]) != 4) |
53 |
error(USER, "missing extent in readp"); |
54 |
|
55 |
while ((c = getc(fp)) != EOF && c != '\n' && c != ADELIM); |
56 |
|
57 |
nargs = 0; |
58 |
|
59 |
if (c == ADELIM) |
60 |
while ((c = getc(fp)) != EOF && c != '\n' && nargs < MAXARGS-1) |
61 |
inbuf[nargs++] = c; |
62 |
|
63 |
if (nargs >= MAXARGS) |
64 |
error(USER, "too many arguments in readp"); |
65 |
|
66 |
if (nargs) { |
67 |
inbuf[nargs] = '\0'; |
68 |
p->args = savestr(inbuf); |
69 |
} |
70 |
else |
71 |
p->args = NULL; |
72 |
|
73 |
return(p->com != PEOF); |
74 |
} |
75 |
|
76 |
|
77 |
|
78 |
|
79 |
void |
80 |
writep( /* print primitive in human-readable form */ |
81 |
register PRIMITIVE *p, |
82 |
FILE *fp |
83 |
) |
84 |
|
85 |
{ |
86 |
|
87 |
if (fp == NULL) fp = stdout; |
88 |
|
89 |
if (!iscom(p->com)) |
90 |
error(USER, "bad command in writep"); |
91 |
|
92 |
fprintf(fp, "%c ", p->com); |
93 |
if (isprim(p->com)) { |
94 |
fprintf(fp, "%3o ", p->arg0 & 0177); |
95 |
fprintf(fp, "%5d %5d %5d %5d ", p->xy[XMN], p->xy[YMN], p->xy[XMX], p->xy[YMX]); |
96 |
} else |
97 |
fprintf(fp, "%3o ", p->arg0); |
98 |
|
99 |
if (p->args != NULL) { |
100 |
putc(ADELIM, fp); |
101 |
fputs(p->args, fp); |
102 |
} |
103 |
|
104 |
putc('\n', fp); |
105 |
|
106 |
if (p->com == PDRAW || p->com == PEOF) |
107 |
if (fflush(fp) == -1) |
108 |
error(SYSTEM, "error detected writing file in writep"); |
109 |
|
110 |
} |
111 |
|
112 |
|
113 |
|
114 |
void |
115 |
writeof( /* write end of file command to fp */ |
116 |
FILE *fp |
117 |
) |
118 |
|
119 |
{ |
120 |
|
121 |
writep(&peof, fp); |
122 |
|
123 |
} |
124 |
|