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

金融证券系统

开发平台:

Visual C++

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