| 1 |
#ifndef lint |
| 2 |
static const char RCSid[] = "$Id: readobj2.c,v 2.10 2004/09/28 23:55:02 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 |
char *fgetline(); |
| 31 |
FILE *infp; |
| 32 |
char buf[2048]; |
| 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 |
readobj2(buf, callback); |
| 56 |
} else { /* object */ |
| 57 |
ungetc(c, infp); |
| 58 |
getobject2(input, infp, callback); |
| 59 |
} |
| 60 |
} |
| 61 |
if (input[0] == '!') |
| 62 |
pclose(infp); |
| 63 |
else |
| 64 |
fclose(infp); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
static void |
| 69 |
getobject2( /* read the next object */ |
| 70 |
char *name, |
| 71 |
FILE *fp, |
| 72 |
ro_cbfunc f |
| 73 |
) |
| 74 |
{ |
| 75 |
char sbuf[MAXSTR]; |
| 76 |
OBJREC thisobj; |
| 77 |
/* get modifier */ |
| 78 |
fgetword(sbuf, MAXSTR, fp); |
| 79 |
thisobj.omod = OVOID; |
| 80 |
/* get type */ |
| 81 |
fgetword(sbuf, MAXSTR, fp); |
| 82 |
if ((thisobj.otype = otype(sbuf)) < 0) { |
| 83 |
sprintf(errmsg, "(%s): unknown type \"%s\"", name, sbuf); |
| 84 |
error(USER, errmsg); |
| 85 |
} |
| 86 |
/* get identifier */ |
| 87 |
fgetword(sbuf, MAXSTR, fp); |
| 88 |
thisobj.oname = sbuf; |
| 89 |
/* get arguments */ |
| 90 |
if (thisobj.otype == MOD_ALIAS) { |
| 91 |
fscanf(fp, "%*s"); |
| 92 |
return; |
| 93 |
} |
| 94 |
if (readfargs(&thisobj.oargs, fp) <= 0) { |
| 95 |
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 |
free_os(&thisobj); |
| 104 |
} |