41 |
|
static AMBTREE atrunk; /* our ambient trunk node */ |
42 |
|
|
43 |
|
static FILE *ambfp = NULL; /* ambient file pointer */ |
44 |
< |
static int ambheadlen; /* length of ambient file header */ |
44 |
> |
static char *afname; /* ambient file name */ |
45 |
> |
static long ambheadlen; /* length of ambient file header */ |
46 |
|
|
47 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
48 |
|
|
50 |
|
|
51 |
|
#define newambtree() (AMBTREE *)calloc(8, sizeof(AMBTREE)) |
52 |
|
|
53 |
+ |
extern long ftell(), lseek(); |
54 |
|
|
55 |
+ |
|
56 |
|
setambres(ar) /* set ambient resolution */ |
57 |
|
int ar; |
58 |
|
{ |
74 |
|
setambient(afile) /* initialize calculation */ |
75 |
|
char *afile; |
76 |
|
{ |
74 |
– |
extern long ftell(); |
77 |
|
AMBVAL amb; |
78 |
|
/* init ambient limits */ |
79 |
|
setambres(ambres); |
80 |
|
/* open ambient file */ |
81 |
< |
if (afile != NULL) |
81 |
> |
if ((afname = afile) != NULL) |
82 |
|
if ((ambfp = fopen(afile, "r+")) != NULL) { |
83 |
|
initambfile(0); |
84 |
|
while (readambval(&amb, ambfp)) |
99 |
|
initambfile(creat) /* initialize ambient file */ |
100 |
|
int creat; |
101 |
|
{ |
102 |
< |
extern char *progname, *octname; |
102 |
> |
extern char *progname, *octname, VersionID[]; |
103 |
|
|
104 |
|
setbuf(ambfp, bmalloc(BUFSIZ)); |
105 |
|
if (creat) { /* new file */ |
110 |
|
fprintf(ambfp, "-ad %d -as %d -ar %d %s\n", |
111 |
|
ambdiv, ambssamp, ambres, |
112 |
|
octname==NULL ? "" : octname); |
113 |
+ |
fprintf(ambfp, "SOFTWARE= %s\n", VersionID); |
114 |
|
fputformat(AMBFMT, ambfp); |
115 |
|
putc('\n', ambfp); |
116 |
|
putambmagic(ambfp); |
117 |
|
fflush(ambfp); |
118 |
< |
} else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp)) |
119 |
< |
error(USER, "ambient file format error"); |
118 |
> |
} else if (checkheader(ambfp, AMBFMT, NULL) < 0 |
119 |
> |
|| !hasambmagic(ambfp)) { |
120 |
> |
sprintf(errmsg, "\"%s\" is not an ambient file", afname); |
121 |
> |
error(USER, errmsg); |
122 |
> |
} |
123 |
|
ambheadlen = ftell(ambfp); |
124 |
|
} |
125 |
|
|
300 |
|
VCOPY(amb.gpos, gp); |
301 |
|
VCOPY(amb.gdir, gd); |
302 |
|
/* insert into tree */ |
303 |
< |
avinsert(&amb, &atrunk, thescene.cuorg, thescene.cusize); |
298 |
< |
avsave(&amb); /* write to file */ |
303 |
> |
avsave(&amb); /* and save to file */ |
304 |
|
return(amb.rad); |
305 |
|
} |
306 |
|
|
332 |
|
|
333 |
|
|
334 |
|
static |
335 |
< |
avsave(av) /* save an ambient value */ |
335 |
> |
avsave(av) /* insert and save an ambient value */ |
336 |
|
AMBVAL *av; |
337 |
|
{ |
338 |
|
static int nunflshed = 0; |
339 |
|
|
340 |
+ |
avinsert(av, &atrunk, thescene.cuorg, thescene.cusize); |
341 |
|
if (ambfp == NULL) |
342 |
|
return; |
343 |
|
if (writambval(av, ambfp) < 0) |
344 |
|
goto writerr; |
345 |
|
if (++nunflshed >= AMBFLUSH) { |
346 |
< |
if (fflush(ambfp) == EOF) |
346 |
> |
if (ambsync() == EOF) |
347 |
|
goto writerr; |
348 |
|
nunflshed = 0; |
349 |
|
} |
387 |
|
return; |
388 |
|
memerr: |
389 |
|
error(SYSTEM, "out of memory in avinsert"); |
390 |
+ |
} |
391 |
+ |
|
392 |
+ |
|
393 |
+ |
#include <fcntl.h> |
394 |
+ |
|
395 |
+ |
|
396 |
+ |
static |
397 |
+ |
ambsync() /* synchronize ambient file */ |
398 |
+ |
{ |
399 |
+ |
static FILE *ambinp = NULL; |
400 |
+ |
struct flock fls; |
401 |
+ |
AMBVAL avs; |
402 |
+ |
long lastpos, flen; |
403 |
+ |
register int n; |
404 |
+ |
/* gain exclusive access */ |
405 |
+ |
fls.l_type = F_WRLCK; |
406 |
+ |
fls.l_whence = 0; |
407 |
+ |
fls.l_start = 0L; |
408 |
+ |
fls.l_len = 0L; |
409 |
+ |
if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0) |
410 |
+ |
error(SYSTEM, "cannot lock ambient file"); |
411 |
+ |
/* see if file has grown */ |
412 |
+ |
lastpos = lseek(fileno(ambfp), 0L, 2); /* may move pointer */ |
413 |
+ |
flen = lseek(fileno(ambfp), 0L, 1); /* new(?) file length */ |
414 |
+ |
if (n = (flen - lastpos)%AMBVALSIZ) /* assure alignment */ |
415 |
+ |
lseek(fileno(ambfp), flen -= n, 0); |
416 |
+ |
if (n = (flen - lastpos)/AMBVALSIZ) { /* file has grown */ |
417 |
+ |
if (ambinp == NULL && (ambinp = fopen(afname, "r")) == NULL) |
418 |
+ |
error(SYSTEM, "cannot reopen ambient file"); |
419 |
+ |
fseek(ambinp, lastpos, 0); /* go to previous position */ |
420 |
+ |
while (n--) { /* load contributed values */ |
421 |
+ |
readambval(&avs, ambinp); |
422 |
+ |
avinsert(&avs,&atrunk,thescene.cuorg,thescene.cusize); |
423 |
+ |
} |
424 |
+ |
} |
425 |
+ |
n = fflush(ambfp); /* calls write() at last */ |
426 |
+ |
fls.l_type = F_UNLCK; /* release file */ |
427 |
+ |
fcntl(fileno(ambfp), F_SETLKW, &fls); |
428 |
+ |
return(n); |
429 |
|
} |