home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume26 / pico / part01 / file.c < prev    next >
C/C++ Source or Header  |  1991-12-14  |  2KB  |  86 lines

  1. #include <string.h>
  2. #include <ctype.h>
  3. #include "pico.h"
  4.  
  5. int nfiles = 2;
  6.  
  7. Buf buf[NFILES];
  8.  
  9. extern int findfile(char *name) {
  10.     int i;
  11.     if (strcmp(name, "old") == 0)
  12.         return 1;
  13.     for (i = 2; i < nfiles; i++)
  14.         if (strcmp(buf[i].name, name) == 0)
  15.             return i;
  16.     return -1;
  17. }
  18.  
  19. extern void showfiles() {
  20.     int i;
  21.     if (nfiles == 2) {
  22.         fprintf(stderr, "no files open\n");
  23.     } else {
  24.         for (i = 2; i < nfiles; i++)
  25.             printf("$%d = %s\n", i - 1, buf[i].name);
  26.     }
  27. }
  28.  
  29. extern void writefile(char *name) {
  30.     int f = findfile(name);
  31.     FILE *fp = fopen(name, "w");
  32.     if (fp == NULL) {
  33.         perror(name);
  34.         return;
  35.     }
  36.     fprintf(fp, "P5 %d %d 255\n", DEF_X, DEF_Y);
  37.     fwrite(buf[0].data, 1, DEF_X * DEF_Y, fp);
  38.     fclose(fp);
  39.     if (f < 0) {
  40.         if (nfiles >= NFILES)
  41.             return;
  42.         f = nfiles++;
  43.         buf[f].name = strdup(name);
  44.         buf[f].data = ealloc(DEF_X * DEF_Y);
  45.     }
  46.     memcpy(buf[f].data, buf[0].data, DEF_X * DEF_Y);
  47. }
  48.  
  49. #define failif(x,y) if (x) {y; f = -1; goto fail;}
  50. #define next fgetc(fp)
  51.  
  52. extern int readfile(char *name) {
  53.     FILE *fp = fopen(name, "r");
  54.     int c,i,f = findfile(name);
  55.     failif(f > 0 && nfiles >= NFILES, fprintf(stderr, "too many open files\n"))
  56.     failif(fp == NULL, perror(name))
  57.     failif(next != 'P' || next != '5', fprintf(stderr, "%s: bad magic number\n", name))
  58.     while (isspace(c = next))
  59.         ;
  60.     failif(!isdigit(c), fprintf(stderr, "%s: expected a decimal number, saw: %c\n", name, c))
  61.     for (i = c - '0'; isdigit(c = next); i = i*10 + c-'0')
  62.         ;
  63.     failif(i != DEF_X, fprintf(stderr, "%s: expected an x-coordinate of %d\n", name, DEF_X))
  64.     while (isspace(c))
  65.         c = next;
  66.     for (i = c - '0'; isdigit(c = next); i = i*10 + c-'0')
  67.         ;
  68.     failif(i != DEF_Y, fprintf(stderr, "%s: expected a y-coordinate of %d\n", name, DEF_Y))
  69.     while (isspace(c))
  70.         c = next;
  71.     for (i = c - '0'; isdigit(c = next); i = i*10 + c-'0')
  72.         ;
  73.     failif(i != 255, fprintf(stderr, "%s: expected a grey scale of 255, saw\n", name, i))
  74.     if (!isspace(c))
  75.         ungetc(c,fp);
  76.     if (f < 0) {
  77.         f = nfiles++;
  78.         buf[f].name = strdup(name);
  79.         buf[f].data = ealloc(DEF_X * DEF_Y);
  80.     }
  81.     fread(buf[f].data, 1, DEF_X * DEF_Y, fp);
  82. fail:
  83.     fclose(fp);
  84.     return f;
  85. }
  86.