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