home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / snip1292.zip / BITFILES.C < prev    next >
C/C++ Source or Header  |  1992-04-11  |  3KB  |  139 lines

  1. /*
  2. **  BITFILES.C - reading/writing bit files
  3. **
  4. **  Public domain by Aare Tali
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9.  
  10. typedef struct  {
  11.       FILE *  file;       /* for stream I/O */
  12.       char    rbuf;       /* read bit buffer */
  13.       char    rcnt;       /* read bit count */
  14.       char    wbuf;       /* write bit buffer */
  15.       char    wcnt;       /* write bit count */
  16. } bfile;
  17.  
  18. bfile *bfopen(char *name, char *mode)
  19. {
  20.       bfile * bf;
  21.  
  22.       bf = malloc(sizeof(bfile));
  23.       if (NULL == bf)
  24.             return NULL;
  25.       bf->file = fopen(name, mode);
  26.       if (NULL == bf->file)
  27.       {
  28.             free(bf);
  29.             return NULL;
  30.       }
  31.       bf->rcnt = 0;
  32.       bf->wcnt = 0;
  33.       return bf;
  34. }
  35.  
  36. int bfread(bfile *bf)
  37. {
  38.       if (0 == bf->rcnt)          /* read new byte */
  39.       {
  40.             bf->rbuf = (char)fgetc(bf->file);
  41.             bf->rcnt = 8;
  42.       }
  43.       bf->rcnt--;
  44.       return (bf->rbuf & (1 << bf->rcnt)) != 0;
  45. }
  46.  
  47. void bfwrite(int bit, bfile *bf)
  48. {
  49.       if (8 == bf->wcnt)          /* write full byte */
  50.       {
  51.             fputc(bf->wbuf, bf->file);
  52.             bf->wcnt = 0;
  53.       }
  54.       bf->wcnt++;
  55.       bf->wbuf <<= 1;
  56.       bf->wbuf |= bit & 1;
  57. }
  58.  
  59. void bfclose(bfile *bf)
  60. {
  61.       fclose(bf->file);
  62.       free(bf);
  63. }
  64.  
  65. void test1(void)
  66. {
  67.       bfile *out;
  68.       bfile *in;
  69.       FILE  *in1;
  70.       FILE  *in2;
  71.  
  72.       in = bfopen("bitfiles.c", "rb");
  73.       out = bfopen("bitfiles.cc", "wb");
  74.       if ((NULL == in) || (NULL == out))
  75.       {
  76.             printf("Can't open/create test files\n");
  77.             exit(1);
  78.       }
  79.       while (!feof(in->file))
  80.             bfwrite(bfread(in), out);
  81.       bfclose(in);
  82.       bfclose(out);
  83.       in1 = fopen("bitfiles.c", "rb");
  84.       in2 = fopen("bitfiles.cc", "rb");
  85.       if ((NULL == in1) || (NULL == in2))
  86.       {
  87.             printf("Can't open test files for verifying\n");
  88.             exit(1);
  89.       }
  90.       while (!feof(in1) && !feof(in2))
  91.       {
  92.             if (fgetc(in1) != fgetc(in2))
  93.             {
  94.                   printf("Files not identical, copy failed!\n");
  95.                   exit(1);
  96.             }
  97.       }
  98.       if (!feof(in1) || !feof(in2))
  99.       {
  100.             printf("Not same size, copy failed!\n");
  101.             exit(1);
  102.       }
  103.       fclose(in1);
  104.       fclose(in2);
  105. }
  106.  
  107. void test2(void)
  108. {
  109.       FILE  *in1;
  110.       bfile *in2;
  111.       int    ch;
  112.  
  113.       in1 = fopen("bitfiles.c", "rb");
  114.       in2 = bfopen("bitfiles.cc", "rb");
  115.       if ((NULL == in1) || (NULL == in2))
  116.       {
  117.             printf("Can't open test files\n");
  118.             exit(1);
  119.       }
  120.       while (!feof(in1) && !feof(in2->file))
  121.       {
  122.             ch = fgetc(in1);
  123.             if (ch < ' ')
  124.                   ch = '.';
  125.             printf(" '%c' ", ch);
  126.             for (ch = 0; ch < 8; ch++)
  127.                   printf("%c", "01"[bfread(in2)]);
  128.             printf("   ");
  129.       }
  130.       fclose(in1);
  131.       bfclose(in2);
  132. }
  133.  
  134. void main(void)
  135. {
  136.       test1();
  137.       test2();
  138. }
  139.