ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambient.c
(Generate patch)

Comparing ray/src/rt/ambient.c (file contents):
Revision 2.65 by greg, Sat Sep 15 02:47:30 2007 UTC vs.
Revision 2.66 by greg, Sat Sep 15 05:16:10 2007 UTC

# Line 140 | Line 140 | extern void
140   setambient(void)                                /* initialize calculation */
141   {
142          int     readonly = 0;
143 <        long  pos, flen;
143 >        long    flen;
144          AMBVAL  amb;
145                                                  /* make sure we're fresh */
146          ambdone();
# Line 160 | Line 160 | setambient(void)                               /* initialize calculation */
160                  readonly = (ambfp = fopen(ambfile, "r")) != NULL;
161          if (ambfp != NULL) {
162                  initambfile(0);                 /* file exists */
163 <                pos = ftell(ambfp);
163 >                lastpos = ftell(ambfp);
164                  while (readambval(&amb, ambfp))
165                          avinsert(avstore(&amb));
166                  nambshare = nambvals;           /* share loaded values */
# Line 174 | Line 174 | setambient(void)                               /* initialize calculation */
174                          return;                 /* avoid ambsync() */
175                  }
176                                                  /* align file pointer */
177 <                pos += (long)nambvals*AMBVALSIZ;
177 >                lastpos += (long)nambvals*AMBVALSIZ;
178                  flen = lseek(fileno(ambfp), (off_t)0, SEEK_END);
179 <                if (flen != pos) {
179 >                if (flen != lastpos) {
180                          sprintf(errmsg,
181                          "ignoring last %ld values in ambient file (corrupted)",
182 <                                        (flen - pos)/AMBVALSIZ);
182 >                                        (flen - lastpos)/AMBVALSIZ);
183                          error(WARNING, errmsg);
184 <                        fseek(ambfp, pos, 0);
184 >                        fseek(ambfp, lastpos, SEEK_SET);
185   #ifndef _WIN32 /* XXX we need a replacement for that one */
186 <                        ftruncate(fileno(ambfp), (off_t)pos);
186 >                        ftruncate(fileno(ambfp), (off_t)lastpos);
187   #endif
188                  }
189          } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
190                  initambfile(1);                 /* else create new file */
191 +                lastpos = ftell(ambfp);
192          } else {
193                  sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
194                  error(SYSTEM, errmsg);
# Line 863 | Line 864 | aflock(                        /* lock/unlock ambient file */
864   {
865          static struct flock  fls;       /* static so initialized to zeroes */
866  
867 +        if (typ == fls.l_type)          /* already called? */
868 +                return;
869          fls.l_type = typ;
870          if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
871                  error(SYSTEM, "cannot (un)lock ambient file");
# Line 878 | Line 881 | ambsync(void)                  /* synchronize ambient file */
881  
882          if (ambfp == NULL)      /* no ambient file? */
883                  return(0);
881        if (lastpos < 0)        /* initializing (locked in initambfile) */
882                goto syncend;
884                                  /* gain appropriate access */
885          aflock(nunflshed ? F_WRLCK : F_RDLCK);
886                                  /* see if file has grown */
# Line 891 | Line 892 | ambsync(void)                  /* synchronize ambient file */
892                          if (ambinp == NULL)
893                                  error(SYSTEM, "fdopen failed in ambsync");
894                  }
895 <                if (fseek(ambinp, lastpos, 0) < 0)
895 >                if (fseek(ambinp, lastpos, SEEK_SET) < 0)
896                          goto seekerr;
897                  while (n >= AMBVALSIZ) {        /* load contributed values */
898                          if (!readambval(&avs, ambinp)) {
# Line 918 | Line 919 | ambsync(void)                  /* synchronize ambient file */
919                  error(CONSISTENCY, errmsg);
920          }
921   #endif
921 syncend:
922          n = fflush(ambfp);                      /* calls write() at last */
923 <        if ((n == EOF) | (lastpos < 0)) {
924 <                if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
925 <                        goto seekerr;
926 <        } else
923 >        if (n != EOF)
924                  lastpos += (long)nunflshed*AMBVALSIZ;
925 +        else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
926 +                goto seekerr;
927 +                
928          aflock(F_UNLCK);                        /* release file */
929          nunflshed = 0;
930          return(n);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines