1 |
greg |
1.1 |
#ifndef lint |
2 |
schorsch |
2.9 |
static const char RCSid[] = "$Id: readobj2.c,v 2.8 2003/10/27 10:29:29 schorsch Exp $"; |
3 |
greg |
1.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* readobj2.c - routines for reading in object descriptions. |
6 |
|
|
*/ |
7 |
|
|
|
8 |
schorsch |
2.6 |
#include <ctype.h> |
9 |
schorsch |
2.8 |
#include <stdio.h> |
10 |
schorsch |
2.6 |
|
11 |
greg |
2.7 |
#include "platform.h" |
12 |
schorsch |
2.8 |
#include "rtprocess.h" |
13 |
|
|
#include "rtmath.h" |
14 |
|
|
#include "rtio.h" |
15 |
|
|
#include "rterror.h" |
16 |
greg |
1.1 |
#include "object.h" |
17 |
|
|
#include "otypes.h" |
18 |
schorsch |
2.9 |
#include "oconv.h" |
19 |
greg |
1.1 |
|
20 |
|
|
|
21 |
schorsch |
2.9 |
static void getobject2(char *name, FILE *fp, ro_cbfunc f); |
22 |
greg |
1.5 |
|
23 |
schorsch |
2.9 |
|
24 |
|
|
void |
25 |
|
|
readobj2( /* read in an object file or stream */ |
26 |
|
|
char *input, |
27 |
|
|
ro_cbfunc callback |
28 |
|
|
) |
29 |
greg |
1.1 |
{ |
30 |
greg |
2.3 |
char *fgetline(); |
31 |
greg |
1.1 |
FILE *infp; |
32 |
|
|
char buf[512]; |
33 |
|
|
register int c; |
34 |
|
|
|
35 |
|
|
if (input == NULL) { |
36 |
|
|
infp = stdin; |
37 |
|
|
input = "standard input"; |
38 |
|
|
} else if (input[0] == '!') { |
39 |
|
|
if ((infp = popen(input+1, "r")) == NULL) { |
40 |
|
|
sprintf(errmsg, "cannot execute \"%s\"", input); |
41 |
|
|
error(SYSTEM, errmsg); |
42 |
|
|
} |
43 |
|
|
} else if ((infp = fopen(input, "r")) == NULL) { |
44 |
|
|
sprintf(errmsg, "cannot open scene file \"%s\"", input); |
45 |
|
|
error(SYSTEM, errmsg); |
46 |
|
|
} |
47 |
|
|
while ((c = getc(infp)) != EOF) { |
48 |
|
|
if (isspace(c)) |
49 |
|
|
continue; |
50 |
|
|
if (c == '#') { /* comment */ |
51 |
|
|
fgets(buf, sizeof(buf), infp); |
52 |
|
|
} else if (c == '!') { /* command */ |
53 |
|
|
ungetc(c, infp); |
54 |
|
|
fgetline(buf, sizeof(buf), infp); |
55 |
greg |
2.4 |
readobj2(buf, callback); |
56 |
greg |
1.1 |
} else { /* object */ |
57 |
|
|
ungetc(c, infp); |
58 |
greg |
2.4 |
getobject2(input, infp, callback); |
59 |
greg |
1.1 |
} |
60 |
|
|
} |
61 |
|
|
if (input[0] == '!') |
62 |
|
|
pclose(infp); |
63 |
|
|
else |
64 |
|
|
fclose(infp); |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
|
68 |
schorsch |
2.8 |
static void |
69 |
|
|
getobject2( /* read the next object */ |
70 |
schorsch |
2.9 |
char *name, |
71 |
|
|
FILE *fp, |
72 |
|
|
ro_cbfunc f |
73 |
schorsch |
2.8 |
) |
74 |
greg |
1.1 |
{ |
75 |
|
|
char sbuf[MAXSTR]; |
76 |
|
|
OBJREC thisobj; |
77 |
|
|
/* get modifier */ |
78 |
greg |
1.5 |
fgetword(sbuf, MAXSTR, fp); |
79 |
greg |
1.2 |
thisobj.omod = OVOID; |
80 |
greg |
1.1 |
/* get type */ |
81 |
greg |
1.5 |
fgetword(sbuf, MAXSTR, fp); |
82 |
greg |
2.5 |
if ((thisobj.otype = otype(sbuf)) < 0) { |
83 |
greg |
1.1 |
sprintf(errmsg, "(%s): unknown type \"%s\"", name, sbuf); |
84 |
|
|
error(USER, errmsg); |
85 |
|
|
} |
86 |
|
|
/* get identifier */ |
87 |
greg |
1.5 |
fgetword(sbuf, MAXSTR, fp); |
88 |
greg |
1.1 |
thisobj.oname = sbuf; |
89 |
|
|
/* get arguments */ |
90 |
greg |
2.5 |
if (thisobj.otype == MOD_ALIAS) { |
91 |
greg |
1.1 |
fscanf(fp, "%*s"); |
92 |
|
|
return; |
93 |
|
|
} |
94 |
greg |
1.4 |
if (readfargs(&thisobj.oargs, fp) <= 0) { |
95 |
greg |
1.1 |
sprintf(errmsg, "(%s): bad arguments", name); |
96 |
|
|
objerror(&thisobj, USER, errmsg); |
97 |
|
|
} |
98 |
|
|
thisobj.os = NULL; |
99 |
|
|
/* call function */ |
100 |
|
|
(*f)(&thisobj); |
101 |
|
|
/* free memory */ |
102 |
|
|
freefargs(&thisobj.oargs); |
103 |
greg |
2.4 |
free_os(&thisobj); |
104 |
greg |
1.1 |
} |