Back to index

radiance  4R0+20100331
mfio.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: mfio.c,v 1.2 2003/11/15 02:13:37 schorsch Exp $";
00003 #endif
00004 /*
00005  *  Meta-file input and output routines
00006  */
00007 
00008 #include  "meta.h"
00009 
00010 
00011 static PRIMITIVE  peof = {PEOF, 0200, {-1, -1, -1, -1}, NULL, NULL};
00012 
00013 
00014 
00015 #define  INTARG(n)  (((unsigned)inbuf[n] << 7) | (unsigned)inbuf[n+1])
00016 
00017 
00018 int
00019 readp(        /* read in primitive from file */
00020        PRIMITIVE  *p,
00021        FILE  *fp
00022 )
00023 
00024 {
00025  char  inbuf[MAXARGS];
00026  register int  c, nargs;
00027 
00028  if (fp == NULL) fp = stdin;
00029 
00030  if ((c = getc(fp)) == EOF) {             /* used to be fatal */
00031     mcopy((char *)p, (char *)&peof, sizeof(PRIMITIVE));
00032     return(0);
00033  }
00034  if (!(c & 0200))
00035     error(USER, "readp not started on command");
00036 
00037  p->com = c & 0177;
00038  if (!iscom(p->com))
00039     error(USER, "bad command in readp");
00040 
00041  for (nargs = 0; (c = getc(fp)) != EOF && !(c & 0200) &&
00042                nargs < MAXARGS-1; nargs++)
00043     inbuf[nargs] = c;
00044 
00045  if (c & 0200)
00046     ungetc(c, fp);
00047  else if (nargs >= MAXARGS-1)
00048     error(USER, "command w/ too many arguments in readp");
00049 
00050  c = 0;
00051 
00052  if (nargs)  {
00053     p->arg0 = inbuf[c++];
00054     nargs--;
00055     }
00056  else
00057     p->arg0 = 0200;
00058 
00059  if (isprim(p->com))  {
00060     if (nargs < 8)
00061        error(USER, "missing extent in readp");
00062     p->xy[XMN] = INTARG(c); c += 2;
00063     p->xy[YMN] = INTARG(c); c += 2;
00064     p->xy[XMX] = INTARG(c); c += 2;
00065     p->xy[YMX] = INTARG(c); c += 2;
00066     nargs -= 8;
00067     }
00068  else
00069     p->xy[XMN] = p->xy[YMN] = p->xy[XMX] = p->xy[YMX] = -1;
00070 
00071  if (nargs)  {
00072     inbuf[c+nargs] = '\0';
00073     p->args = savestr(inbuf+c);
00074     }
00075  else
00076     p->args = NULL;
00077 
00078  return(p->com != PEOF);
00079  }
00080 
00081 #undef  INTARG
00082 
00083 
00084 
00085 
00086 
00087 
00088 
00089 #define  HI7(i)  ((i >> 7) & 0177)
00090 
00091 #define  LO7(i)  (i & 0177)
00092 
00093 
00094 void
00095 writep(                     /* write primitive to file */
00096        register PRIMITIVE  *p,
00097        FILE  *fp
00098 )
00099 {
00100  if (fp == NULL) fp = stdout;
00101 
00102  if (!iscom(p->com))
00103     error(USER, "bad command in writep");
00104 
00105  putc(p->com | 0200, fp);
00106 
00107  if (isprim(p->com))  {
00108     putc(p->arg0 & 0177, fp);
00109     putc(HI7(p->xy[XMN]), fp); putc(LO7(p->xy[XMN]), fp);
00110     putc(HI7(p->xy[YMN]), fp); putc(LO7(p->xy[YMN]), fp);
00111     putc(HI7(p->xy[XMX]), fp); putc(LO7(p->xy[XMX]), fp);
00112     putc(HI7(p->xy[YMX]), fp); putc(LO7(p->xy[YMX]), fp);
00113     }
00114  else if (!(p->arg0 & 0200))
00115     putc(p->arg0, fp);
00116 
00117  if (p->args != NULL)
00118     fputs(p->args, fp);
00119 
00120  if (p->com == PDRAW || p->com == PEOF)
00121     if (fflush(fp) == -1)
00122        error(SYSTEM, "error detected writing file in writep");
00123 
00124  }
00125 
00126 #undef  HI7
00127 
00128 #undef  LO7
00129 
00130 
00131 void
00132 writeof(             /* write end of file command to fp */
00133        FILE  *fp
00134 )
00135 {
00136  writep(&peof, fp);
00137 }
00138