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

Comparing ray/src/cv/mgflib/parser.c (file contents):
Revision 1.20 by greg, Wed Nov 8 09:40:22 1995 UTC vs.
Revision 1.26 by gregl, Mon Dec 15 09:41:37 1997 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1995 Regents of the University of California */
1 > /* Copyright (c) 1996 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 10 | Line 10 | static char SCCSid[] = "$SunId$ LBL";
10  
11   #include <stdio.h>
12   #include <math.h>
13 + #include <stdlib.h>
14   #include <ctype.h>
15   #include <string.h>
16   #include "parser.h"
# Line 63 | Line 64 | int    mg_nqcdivs = MG_NQCD;   /* number of divisions per q
64  
65   static int      e_any_toss(),           /* discard unneeded entity */
66                  e_ies(),                /* IES luminaire file */
66                e_include(),            /* include file */
67                e_sph(),                /* sphere */
67                  e_cct(),                /* color temperature */
68                  e_cmix(),               /* color mixtures */
69 <                e_cspec(),              /* color spectra */
71 <                e_cyl(),                /* cylinder */
72 <                e_cone(),               /* cone */
73 <                e_prism(),              /* prism */
74 <                e_ring(),               /* ring */
75 <                e_torus();              /* torus */
69 >                e_cspec();              /* color spectra */
70  
71                                  /* alternate handler support functions */
72  
# Line 123 | Line 117 | mg_init()                      /* initialize alternate entity handlers */
117                  ineed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX;
118          } else
119                  uneed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX|1L<<MG_E_XF;
120 +        if (mg_ehand[MG_E_FACE] == NULL)
121 +                mg_ehand[MG_E_FACE] = mg_ehand[MG_E_FACEH];
122 +        else if (mg_ehand[MG_E_FACEH] == NULL)
123 +                mg_ehand[MG_E_FACEH] = e_faceh;
124          if (mg_ehand[MG_E_COLOR] != NULL) {
125                  if (mg_ehand[MG_E_CMIX] == NULL) {
126                          mg_ehand[MG_E_CMIX] = e_cmix;
# Line 260 | Line 258 | mg_close()                     /* close input file */
258          register MG_FCTXT       *ctx = mg_file;
259  
260          mg_file = ctx->prev;            /* restore enclosing context */
261 <        if (ctx->fp == stdin)
262 <                return;                 /* don't close standard input */
265 <        fclose(ctx->fp);
261 >        if (ctx->fp != stdin)           /* close file if it's a file */
262 >                fclose(ctx->fp);
263   }
264  
265  
# Line 270 | Line 267 | void
267   mg_fgetpos(pos)                 /* get current position in input file */
268   register MG_FPOS        *pos;
269   {
273        extern long     ftell();
274
270          pos->fid = mg_file->fid;
271          pos->lineno = mg_file->lineno;
272          pos->offset = ftell(mg_file->fp);
# Line 304 | Line 299 | mg_read()                      /* read next line from file */
299                  if (fgets(mg_file->inpline+len,
300                                  MG_MAXLINE-len, mg_file->fp) == NULL)
301                          return(len);
307                mg_file->lineno++;
302                  len += strlen(mg_file->inpline+len);
303 <                if (len > 1 && mg_file->inpline[len-2] == '\\')
304 <                        mg_file->inpline[--len-1] = ' ';
305 <        } while (mg_file->inpline[len]);
303 >                if (len >= MG_MAXLINE-1)
304 >                        return(len);
305 >                mg_file->lineno++;
306 >        } while (len > 1 && mg_file->inpline[len-2] == '\\');
307  
308          return(len);
309   }
# Line 319 | Line 314 | mg_parse()                     /* parse current input line */
314   {
315          char    abuf[MG_MAXLINE];
316          char    *argv[MG_MAXARGC];
317 <        int     en;
318 <        register char   *cp, **ap;
319 <
320 <        strcpy(cp=abuf, mg_file->inpline);
321 <        ap = argv;                      /* break into words */
317 >        register char   *cp, *cp2, **ap;
318 >                                        /* copy line, removing escape chars */
319 >        cp = abuf; cp2 = mg_file->inpline;
320 >        while ((*cp++ = *cp2++))
321 >                if (cp2[0] == '\n' && cp2[-1] == '\\')
322 >                        cp--;
323 >        cp = abuf; ap = argv;           /* break into words */
324          for ( ; ; ) {
325                  while (isspace(*cp))
326                          *cp++ = '\0';
# Line 348 | Line 345 | mg_load(fn)                    /* load an MGF file */
345   char    *fn;
346   {
347          MG_FCTXT        cntxt;
348 <        register int    rval;
348 >        int     rval;
349 >        register int    nbr;
350  
351          if ((rval = mg_open(&cntxt, fn)) != MG_OK) {
352                  fprintf(stderr, "%s: %s\n", fn, mg_err[rval]);
353                  return(rval);
354          }
355 <        while ((rval = mg_read()) > 0) {        /* parse each line */
356 <                if (rval >= MG_MAXLINE-1 && cntxt.inpline[rval-1] != '\n') {
355 >        while ((nbr = mg_read()) > 0) { /* parse each line */
356 >                if (nbr >= MG_MAXLINE-1) {
357                          fprintf(stderr, "%s: %d: %s\n", cntxt.fname,
358                                          cntxt.lineno, mg_err[rval=MG_ELINE]);
359                          break;
# Line 388 | Line 386 | void
386   mg_clear()                      /* clear parser history */
387   {
388          c_clearall();                   /* clear context tables */
389 <        mg_file = NULL;                 /* reset our context */
389 >        while (mg_file != NULL)         /* reset our file context */
390 >                mg_close();
391   }
392  
393  
# Line 406 | Line 405 | char   **av;
405   }
406  
407  
408 < static int
408 > int
409   e_include(ac, av)               /* include file */
410   int     ac;
411   char    **av;
# Line 427 | Line 426 | char   **av;
426                  for (i = 1; i < ac-1; i++)
427                          xfarg[i] = av[i+1];
428                  xfarg[ac-1] = NULL;
429 <                if ((rv = mg_handle(MG_E_XF, ac-1, xfarg)) != MG_OK)
429 >                if ((rv = mg_handle(MG_E_XF, ac-1, xfarg)) != MG_OK) {
430 >                        mg_close();
431                          return(rv);
432 +                }
433          }
434          do {
435                  while ((rv = mg_read()) > 0) {
436 <                        if (rv >= MG_MAXLINE-1 && ictx.inpline[rv-1] != '\n') {
436 >                        if (rv >= MG_MAXLINE-1) {
437                                  fprintf(stderr, "%s: %d: %s\n", ictx.fname,
438                                                  ictx.lineno, mg_err[MG_ELINE]);
439                                  mg_close();
# Line 447 | Line 448 | char   **av;
448                          }
449                  }
450                  if (ac > 2)
451 <                        if ((rv = mg_handle(MG_E_XF, 1, xfarg)) != MG_OK)
451 >                        if ((rv = mg_handle(MG_E_XF, 1, xfarg)) != MG_OK) {
452 >                                mg_close();
453                                  return(rv);
454 +                        }
455          } while (xf_context != xf_orig);
456          mg_close();
457          return(MG_OK);
458   }
459  
460  
461 + int
462 + e_faceh(ac, av)                 /* replace face+holes with single contour */
463 + int     ac;
464 + char    **av;
465 + {
466 +        char    *newav[MG_MAXARGC];
467 +        int     lastp = 0;
468 +        register int    i, j;
469 +
470 +        newav[0] = mg_ename[MG_E_FACE];
471 +        for (i = 1; i < ac; i++)
472 +                if (av[i][0] == '-') {
473 +                        if (i < 4)
474 +                                return(MG_EARGC);
475 +                        if (i >= ac-1)
476 +                                break;
477 +                        if (!lastp)
478 +                                lastp = i-1;
479 +                        for (j = i+1; j < ac-1 && av[j+1][0] != '-'; j++)
480 +                                ;
481 +                        if (j - i < 3)
482 +                                return(MG_EARGC);
483 +                        newav[i] = av[j];       /* connect hole loop */
484 +                } else
485 +                        newav[i] = av[i];       /* hole or perimeter vertex */
486 +        if (lastp)
487 +                newav[i++] = av[lastp];         /* finish seam to outside */
488 +        newav[i] = NULL;
489 +        return(mg_handle(MG_E_FACE, i, newav));
490 + }
491 +
492 +
493   static void
494   make_axes(u, v, w)              /* compute u and v given w (normalized) */
495   FVECT   u, v, w;
# Line 472 | Line 507 | FVECT  u, v, w;
507   }
508  
509  
510 < static int
510 > int
511   e_sph(ac, av)                   /* expand a sphere into cones */
512   int     ac;
513   char    **av;
# Line 524 | Line 559 | char   **av;
559   }
560  
561  
562 < static int
562 > int
563   e_torus(ac, av)                 /* expand a torus into cones */
564   int     ac;
565   char    **av;
# Line 616 | Line 651 | char   **av;
651   }
652  
653  
654 < static int
654 > int
655   e_cyl(ac, av)                   /* replace a cylinder with equivalent cone */
656   int     ac;
657   char    **av;
# Line 633 | Line 668 | char   **av;
668   }
669  
670  
671 < static int
671 > int
672   e_ring(ac, av)                  /* turn a ring into polygons */
673   int     ac;
674   char    **av;
# Line 731 | Line 766 | char   **av;
766   }
767  
768  
769 < static int
769 > int
770   e_cone(ac, av)                  /* turn a cone into polygons */
771   int     ac;
772   char    **av;
# Line 773 | Line 808 | char   **av;
808                  if (rad2 == 0.)
809                          return(MG_EILL);
810          } else if (rad2 != 0.) {
811 <                if (rad1 < 0. ^ rad2 < 0.)
811 >                if ((rad1 < 0.) ^ (rad2 < 0.))
812                          return(MG_EILL);
813          } else {                        /* swap */
814                  C_VERTEX        *cv;
# Line 901 | Line 936 | char   **av;
936   }
937  
938  
939 < static int
939 > int
940   e_prism(ac, av)                 /* turn a prism into polygons */
941   int     ac;
942   char    **av;
# Line 1013 | Line 1048 | put_cxy()                      /* put out current xy chromaticities */
1048   {
1049          static char     xbuf[24], ybuf[24];
1050          static char     *ccom[4] = {mg_ename[MG_E_CXY], xbuf, ybuf};
1016        int     rv;
1051  
1052          sprintf(xbuf, "%.4f", c_ccolor->cx);
1053          sprintf(ybuf, "%.4f", c_ccolor->cy);
1054 <        if ((rv = mg_handle(MG_E_CXY, 3, ccom)) != MG_OK)
1021 <                return(rv);
1022 <        return(MG_OK);
1054 >        return(mg_handle(MG_E_CXY, 3, ccom));
1055   }
1056  
1057  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines