Back to index

radiance  4R0+20100331
hfio.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char RCSid[] = "$Id: hfio.c,v 1.4 2003/11/15 02:13:37 schorsch Exp $";
00003 #endif
00004 /*
00005  *    Human-readable file I/O
00006  */
00007 
00008 
00009 #include  "meta.h"
00010 
00011 
00012 
00013 static PRIMITIVE  peof = {PEOF, 0200, {-1, -1, -1, -1}, NULL, NULL};
00014 
00015 
00016 int
00017 readp(        /* get human-readable primitive */
00018        PRIMITIVE  *p,
00019        FILE  *fp
00020 )
00021 {
00022  char  inbuf[MAXARGS];
00023  register int  c, nargs;
00024  int  tmp;
00025 
00026  if (fp == NULL) fp = stdin;
00027 
00028  restart:
00029  
00030  if ((c = getc(fp)) == EOF) {             /* used to be fatal */
00031     mcopy((char *)p, (char *)&peof, sizeof(PRIMITIVE));
00032     return(0);
00033  }
00034 
00035  if (c == CDELIM) {                /* skip user comment */
00036     fgets(inbuf, MAXARGS, fp);
00037     goto restart;
00038  } else if (c == '\n')                    /* skip empty line */
00039     goto restart;
00040     
00041  if (!iscom(c))
00042     error(USER, "bad command in readp");
00043 
00044  p->com = c;
00045 
00046  fscanf(fp, "%o", &tmp);
00047  p->arg0 = tmp & 0377;
00048 
00049  if (isglob(c))
00050     p->xy[XMN] = p->xy[YMN] = p->xy[XMX] = p->xy[YMX] = -1;
00051  else if (fscanf(fp, "%d %d %d %d", &p->xy[XMN], &p->xy[YMN],
00052                             &p->xy[XMX], &p->xy[YMX]) != 4)
00053     error(USER, "missing extent in readp");
00054 
00055  while ((c = getc(fp)) != EOF && c != '\n' && c != ADELIM);
00056 
00057  nargs = 0;
00058 
00059  if (c == ADELIM)
00060     while ((c = getc(fp)) != EOF && c != '\n' && nargs < MAXARGS-1)
00061        inbuf[nargs++] = c;
00062 
00063  if (nargs >= MAXARGS)
00064      error(USER, "too many arguments in readp");
00065 
00066  if (nargs)  {
00067     inbuf[nargs] = '\0';
00068     p->args = savestr(inbuf);
00069     }
00070  else
00071     p->args = NULL;
00072 
00073  return(p->com != PEOF);
00074  }
00075 
00076 
00077 
00078 
00079 void
00080 writep(              /* print primitive in human-readable form */
00081        register PRIMITIVE  *p,
00082        FILE  *fp
00083 )
00084 
00085 {
00086 
00087  if (fp == NULL) fp = stdout;
00088 
00089  if (!iscom(p->com))
00090      error(USER, "bad command in writep");
00091 
00092  fprintf(fp, "%c ", p->com);
00093  if (isprim(p->com)) {
00094     fprintf(fp, "%3o ", p->arg0 & 0177);
00095     fprintf(fp, "%5d %5d %5d %5d ", p->xy[XMN], p->xy[YMN], p->xy[XMX], p->xy[YMX]);
00096  } else
00097     fprintf(fp, "%3o ", p->arg0);
00098 
00099  if (p->args != NULL)  {
00100     putc(ADELIM, fp);
00101     fputs(p->args, fp);
00102     }
00103 
00104  putc('\n', fp);
00105 
00106  if (p->com == PDRAW || p->com == PEOF)
00107     if (fflush(fp) == -1)
00108        error(SYSTEM, "error detected writing file in writep");
00109 
00110  }
00111 
00112 
00113 
00114 void
00115 writeof(             /* write end of file command to fp */
00116        FILE  *fp
00117 )
00118 
00119 {
00120 
00121  writep(&peof, fp);
00122 
00123 }
00124