home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume5 / xldimage / part02 / zio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-13  |  1.3 KB  |  78 lines

  1. /* zio.c:
  2.  *
  3.  * this properly opens and reads from an image file, compressed or otherwise.
  4.  *
  5.  * jim frost 10.03.89
  6.  *
  7.  * Copyright 1989 Jim Frost.  See included file "copyright.h" for complete
  8.  * copyright information.
  9.  */
  10.  
  11. #include "copyright.h"
  12. #include "image.h"
  13.  
  14. ZFILE *zopen(name)
  15.      char *name;
  16. { ZFILE *zf;
  17.   char   buf[BUFSIZ];
  18.  
  19.   zf= (ZFILE *)lmalloc(sizeof(ZFILE));
  20.   if ((strlen(name) > 2) && !strcmp(".Z", name + (strlen(name) - 2))) {
  21.     zf->type= ZPIPE;
  22.     sprintf(buf, "uncompress -c %s", name);
  23.     if (! (zf->stream= popen(buf, "r"))) {
  24.       lfree(zf);
  25.       return(NULL);
  26.     }
  27.     return(zf);
  28.   }
  29.   zf->type= ZSTANDARD;
  30.   if (! (zf->stream= fopen(name, "r"))) {
  31.     lfree(zf);
  32.     return(NULL);
  33.   }
  34.   return(zf);
  35. }
  36.  
  37. int zread(zf, buf, len)
  38.      ZFILE        *zf;
  39.      byte         *buf;
  40.      unsigned int  len;
  41. { int r;
  42.  
  43.   if ((r= fread(buf, len, 1, zf->stream)) != 1)
  44.     return(r);
  45.   return(len);
  46. }
  47.  
  48. int zgetc(zf)
  49.      ZFILE *zf;
  50. {
  51.   return(fgetc(zf->stream));
  52. }
  53.  
  54. char *zgets(buf, size, zf)
  55.      char         *buf;
  56.      unsigned int  size;
  57.      ZFILE        *zf;
  58. {
  59.   return(fgets(buf, size, zf->stream));
  60. }
  61.  
  62. void zclose(zf)
  63.      ZFILE *zf;
  64. {
  65.   switch(zf->type) {
  66.   case ZSTANDARD:
  67.     fclose(zf->stream);
  68.     break;
  69.   case ZPIPE:
  70.     pclose(zf->stream);
  71.     break;
  72.   default:
  73.     printf("zclose: bad ZFILE structure\n");
  74.     exit(1);
  75.   }
  76.   lfree(zf);
  77. }
  78.