| 1 | greg | 1.1 | #ifndef lint | 
| 2 | schorsch | 1.2 | static const char       RCSid[] = "$Id: mfio.c,v 1.1 2003/02/22 02:07:26 greg Exp $"; | 
| 3 | greg | 1.1 | #endif | 
| 4 |  |  | /* | 
| 5 |  |  | *  Meta-file input and output routines | 
| 6 |  |  | */ | 
| 7 |  |  |  | 
| 8 |  |  | #include  "meta.h" | 
| 9 |  |  |  | 
| 10 |  |  |  | 
| 11 | schorsch | 1.2 | static PRIMITIVE  peof = {PEOF, 0200, {-1, -1, -1, -1}, NULL, NULL}; | 
| 12 | greg | 1.1 |  | 
| 13 |  |  |  | 
| 14 |  |  |  | 
| 15 |  |  | #define  INTARG(n)  (((unsigned)inbuf[n] << 7) | (unsigned)inbuf[n+1]) | 
| 16 |  |  |  | 
| 17 |  |  |  | 
| 18 | schorsch | 1.2 | int | 
| 19 |  |  | readp(          /* read in primitive from file */ | 
| 20 |  |  | PRIMITIVE  *p, | 
| 21 |  |  | FILE  *fp | 
| 22 |  |  | ) | 
| 23 | greg | 1.1 |  | 
| 24 |  |  | { | 
| 25 |  |  | char  inbuf[MAXARGS]; | 
| 26 |  |  | register int  c, nargs; | 
| 27 |  |  |  | 
| 28 |  |  | if (fp == NULL) fp = stdin; | 
| 29 |  |  |  | 
| 30 |  |  | if ((c = getc(fp)) == EOF) {           /* used to be fatal */ | 
| 31 |  |  | mcopy((char *)p, (char *)&peof, sizeof(PRIMITIVE)); | 
| 32 |  |  | return(0); | 
| 33 |  |  | } | 
| 34 |  |  | if (!(c & 0200)) | 
| 35 |  |  | error(USER, "readp not started on command"); | 
| 36 |  |  |  | 
| 37 |  |  | p->com = c & 0177; | 
| 38 |  |  | if (!iscom(p->com)) | 
| 39 |  |  | error(USER, "bad command in readp"); | 
| 40 |  |  |  | 
| 41 |  |  | for (nargs = 0; (c = getc(fp)) != EOF && !(c & 0200) && | 
| 42 |  |  | nargs < MAXARGS-1; nargs++) | 
| 43 |  |  | inbuf[nargs] = c; | 
| 44 |  |  |  | 
| 45 |  |  | if (c & 0200) | 
| 46 |  |  | ungetc(c, fp); | 
| 47 |  |  | else if (nargs >= MAXARGS-1) | 
| 48 |  |  | error(USER, "command w/ too many arguments in readp"); | 
| 49 |  |  |  | 
| 50 |  |  | c = 0; | 
| 51 |  |  |  | 
| 52 |  |  | if (nargs)  { | 
| 53 |  |  | p->arg0 = inbuf[c++]; | 
| 54 |  |  | nargs--; | 
| 55 |  |  | } | 
| 56 |  |  | else | 
| 57 |  |  | p->arg0 = 0200; | 
| 58 |  |  |  | 
| 59 |  |  | if (isprim(p->com))  { | 
| 60 |  |  | if (nargs < 8) | 
| 61 |  |  | error(USER, "missing extent in readp"); | 
| 62 |  |  | p->xy[XMN] = INTARG(c); c += 2; | 
| 63 |  |  | p->xy[YMN] = INTARG(c); c += 2; | 
| 64 |  |  | p->xy[XMX] = INTARG(c); c += 2; | 
| 65 |  |  | p->xy[YMX] = INTARG(c); c += 2; | 
| 66 |  |  | nargs -= 8; | 
| 67 |  |  | } | 
| 68 |  |  | else | 
| 69 |  |  | p->xy[XMN] = p->xy[YMN] = p->xy[XMX] = p->xy[YMX] = -1; | 
| 70 |  |  |  | 
| 71 |  |  | if (nargs)  { | 
| 72 |  |  | inbuf[c+nargs] = '\0'; | 
| 73 |  |  | p->args = savestr(inbuf+c); | 
| 74 |  |  | } | 
| 75 |  |  | else | 
| 76 |  |  | p->args = NULL; | 
| 77 |  |  |  | 
| 78 |  |  | return(p->com != PEOF); | 
| 79 |  |  | } | 
| 80 |  |  |  | 
| 81 |  |  | #undef  INTARG | 
| 82 |  |  |  | 
| 83 |  |  |  | 
| 84 |  |  |  | 
| 85 |  |  |  | 
| 86 |  |  |  | 
| 87 |  |  |  | 
| 88 |  |  |  | 
| 89 |  |  | #define  HI7(i)  ((i >> 7) & 0177) | 
| 90 |  |  |  | 
| 91 |  |  | #define  LO7(i)  (i & 0177) | 
| 92 |  |  |  | 
| 93 |  |  |  | 
| 94 | schorsch | 1.2 | void | 
| 95 |  |  | writep(                 /* write primitive to file */ | 
| 96 |  |  | register PRIMITIVE  *p, | 
| 97 |  |  | FILE  *fp | 
| 98 |  |  | ) | 
| 99 | greg | 1.1 | { | 
| 100 |  |  | if (fp == NULL) fp = stdout; | 
| 101 |  |  |  | 
| 102 |  |  | if (!iscom(p->com)) | 
| 103 |  |  | error(USER, "bad command in writep"); | 
| 104 |  |  |  | 
| 105 |  |  | putc(p->com | 0200, fp); | 
| 106 |  |  |  | 
| 107 |  |  | if (isprim(p->com))  { | 
| 108 |  |  | putc(p->arg0 & 0177, fp); | 
| 109 |  |  | putc(HI7(p->xy[XMN]), fp); putc(LO7(p->xy[XMN]), fp); | 
| 110 |  |  | putc(HI7(p->xy[YMN]), fp); putc(LO7(p->xy[YMN]), fp); | 
| 111 |  |  | putc(HI7(p->xy[XMX]), fp); putc(LO7(p->xy[XMX]), fp); | 
| 112 |  |  | putc(HI7(p->xy[YMX]), fp); putc(LO7(p->xy[YMX]), fp); | 
| 113 |  |  | } | 
| 114 |  |  | else if (!(p->arg0 & 0200)) | 
| 115 |  |  | putc(p->arg0, fp); | 
| 116 |  |  |  | 
| 117 |  |  | if (p->args != NULL) | 
| 118 |  |  | fputs(p->args, fp); | 
| 119 |  |  |  | 
| 120 |  |  | if (p->com == PDRAW || p->com == PEOF) | 
| 121 |  |  | if (fflush(fp) == -1) | 
| 122 |  |  | error(SYSTEM, "error detected writing file in writep"); | 
| 123 |  |  |  | 
| 124 |  |  | } | 
| 125 |  |  |  | 
| 126 |  |  | #undef  HI7 | 
| 127 |  |  |  | 
| 128 |  |  | #undef  LO7 | 
| 129 |  |  |  | 
| 130 |  |  |  | 
| 131 | schorsch | 1.2 | void | 
| 132 |  |  | writeof(                /* write end of file command to fp */ | 
| 133 |  |  | FILE  *fp | 
| 134 |  |  | ) | 
| 135 | greg | 1.1 | { | 
| 136 |  |  | writep(&peof, fp); | 
| 137 | schorsch | 1.2 | } | 
| 138 | greg | 1.1 |  |