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