BITIO.C
上传用户:bjghjy
上传日期:2007-01-07
资源大小:379k
文件大小:5k
源码类别:

金融证券系统

开发平台:

Visual C++

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //#include "bitio.h"
  4. //#include "errhand.h"
  5.         
  6. typedef struct tag_bit_file
  7. {
  8. FILE *file;
  9. unsigned char mask;
  10. int rack;
  11. int pacifier_counter;
  12.    char *buffer;
  13.    unsigned int ptr;
  14. }BIT_FILE;
  15. BIT_FILE *OpenInputBitFile(char *filename);
  16. BIT_FILE *OpenOutputBitFile(char *filename);
  17. void OutputBit(BIT_FILE *bit_file,int bit);
  18. void OutputBits(BIT_FILE *bit_file,unsigned long code,int count);
  19. int InputBit(BIT_FILE *bit_file);
  20. unsigned long InputBits(BIT_FILE *bit_file,int count);
  21. void CloseInputBitFile(BIT_FILE *bit_file);
  22. void CloseOutputBitFile(BIT_FILE *bit_file);
  23. void FilePrintBinary(FILE *file,unsigned int code,int bits);
  24. void fatal_error(char *fmt);
  25.         
  26. #define PACIFIER_COUNT 2047
  27. #define    BUFFER_SIZE 1024*2
  28. void fatal_error(char *fmt)
  29. {
  30. }
  31. BIT_FILE *OpenOutputBitFile(char *name)
  32. {
  33. BIT_FILE *bit_file;
  34. bit_file=(BIT_FILE *)calloc(1,sizeof(BIT_FILE));
  35. if(bit_file ==NULL)
  36. return (bit_file);
  37. bit_file->file =fopen(name,"wb");
  38. bit_file->rack =0;
  39. bit_file->mask =0x80;
  40. bit_file->pacifier_counter =0;
  41.    bit_file->buffer = calloc(1,BUFFER_SIZE);
  42.    bit_file->ptr =0;
  43. return (bit_file);
  44. }
  45. BIT_FILE *OpenInputBitFile(char *name)
  46. {
  47. BIT_FILE *bit_file;
  48. bit_file=(BIT_FILE *)calloc(1,sizeof(BIT_FILE));
  49. if(bit_file ==NULL)
  50. return (bit_file);
  51. bit_file->file =fopen(name,"rb");
  52. bit_file->rack =0;
  53. bit_file->mask =0x80;
  54. bit_file->pacifier_counter =0;
  55. return (bit_file);
  56. }
  57. void CloseOutputBitFile(BIT_FILE *bit_file)
  58. {
  59.   if(bit_file->ptr>0)
  60.   {
  61.     if(fwrite((void *)&bit_file->buffer[0],sizeof(char),bit_file->ptr,
  62.         bit_file->file)!=bit_file->ptr)
  63.       fatal_error("Fatal error in write CloseBitFile!n");
  64.     bit_file->ptr=0;
  65.   }
  66. if(bit_file->mask !=0x80)
  67. if(putc(bit_file->rack,bit_file->file) !=bit_file->rack)
  68. fatal_error("Fatal error in CloseBitFile!n");
  69. fclose(bit_file->file);
  70.   if(bit_file->buffer!=NULL)
  71.     free(bit_file->buffer);
  72. free((char *)bit_file);
  73. }
  74. void CloseInputBitFile(BIT_FILE *bit_file)
  75. {
  76. fclose(bit_file->file);
  77. free((char *)bit_file);
  78. }
  79. void OutPutBit(BIT_FILE *bit_file,int bit)
  80. {
  81. if(bit)
  82. bit_file->rack |=bit_file->mask;
  83. bit_file->mask >>=1;
  84. if(bit_file->mask==0)
  85. {
  86.     //if(putc(bit_file->rack,bit_file->file)!=bit_file->rack)
  87.     //  fatal_error("Fatal error in OutputBit!n");
  88.     //else
  89.     //  if((bit_file->pacifier_counter++&PACIFIER_COUNT )==0)
  90.     //    putc('.',stdout);
  91.     if(bit_file->ptr<BUFFER_SIZE)
  92.     {
  93.       bit_file->buffer[bit_file->ptr]=bit_file->rack;
  94.       bit_file->ptr++;
  95.     }
  96.     if(bit_file->ptr == BUFFER_SIZE)
  97.     {
  98.       if(fwrite((void *)&bit_file->buffer[0],sizeof(char),bit_file->ptr,
  99.           bit_file->file)!=bit_file->ptr)
  100.         fatal_error("Fatal error in write CloseBitFile!n");
  101.       bit_file->ptr=0;
  102.     }
  103. bit_file->rack =0;
  104. bit_file->mask =0x80;
  105. }
  106. }
  107. void OutputBits(BIT_FILE *bit_file,unsigned long code,int count)
  108. {
  109. unsigned long mask;
  110. mask =1L << (count -1);
  111. while(mask!=0)
  112. {
  113. if( mask & code)
  114. bit_file->rack |=bit_file->mask;
  115. bit_file->mask >>=1;
  116. if(bit_file->mask ==0)
  117. {
  118.      // if(putc(bit_file->rack,bit_file->file )!=bit_file->rack)
  119.      //   fatal_error("Fatal error in OutputBits!n");
  120.      // else if ((bit_file->pacifier_counter++&PACIFIER_COUNT)==0)
  121.      //   putc('.',stdout);
  122.       if(bit_file->ptr<BUFFER_SIZE)
  123.       {
  124.         bit_file->buffer[bit_file->ptr]=bit_file->rack;
  125.         bit_file->ptr++;
  126.       }
  127.       if(bit_file->ptr ==BUFFER_SIZE)
  128.       {
  129.         if(fwrite((void *)&bit_file->buffer[0],sizeof(char),bit_file->ptr,
  130.             bit_file->file)!=bit_file->ptr)
  131.         {
  132.           fatal_error("Fatal error in write OutputBits");
  133.         }
  134.         bit_file->ptr=0;
  135.       }
  136. bit_file->rack=0;
  137. bit_file->mask=0x80;
  138. }
  139. mask >>=1;
  140. }
  141. }
  142. int InputBit(BIT_FILE *bit_file)
  143. {
  144. int value;
  145. if(bit_file->mask == 0x80)
  146. {
  147. bit_file->rack =getc(bit_file->file);
  148. if(bit_file->rack ==EOF)
  149. fatal_error("Fatal error in InputBit!n");
  150. //if((bit_file->pacifier_counter++&PACIFIER_COUNT )==0)
  151. // putc('.',stdout);
  152. }
  153. value=bit_file->rack &bit_file->mask;
  154. bit_file->mask >>=1;
  155. if(bit_file->mask==0)
  156. bit_file->mask =0x80;
  157. return (value ? 1:0);
  158. }
  159. unsigned long InputBits(BIT_FILE *bit_file,int bit_count)
  160. {
  161. unsigned long mask;
  162. unsigned long return_value;
  163. mask =1L <<(bit_count -1);
  164. return_value =0;
  165. while(mask !=0)
  166. {
  167. if(bit_file->mask ==0x80)
  168. {
  169. bit_file->rack =getc(bit_file->file);
  170. if(bit_file->rack==EOF)
  171. fatal_error("Fatal error in InputBit!n");
  172. //if((bit_file->pacifier_counter++ &PACIFIER_COUNT )==0)
  173. // putc('.',stdout);
  174. }
  175. if(bit_file->rack&bit_file->mask)
  176. return_value|=mask;
  177. mask>>=1;
  178. bit_file->mask>>=1;
  179. if(bit_file->mask ==0)
  180. bit_file->mask =0x80;
  181. }
  182. return (return_value);
  183. }
  184. void FilePrintBinary(FILE *file,unsigned int code,int bits)
  185. {
  186. unsigned int mask;
  187. mask =1<<(bits-1);
  188. while(mask!=0)
  189. {
  190. if(code&mask)
  191. fputc('1',file);
  192. else
  193. fputc('0',file);
  194. mask >>=1;
  195. }
  196. }