duck_io.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /***********************************************\
  2. ??? duck_io.c
  3. \***********************************************/
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <io.h>
  7. #include <fcntl.h>
  8. #include "duck_io.h"
  9. #include "duck_hfb.h"
  10. #include <assert.h>
  11. int read_count;
  12. int duck_readFinished(int han, int flag)
  13. {
  14. return 1;
  15. }
  16. int duck_open(const char *name, unsigned long userData)
  17. {
  18. int f;
  19. FILE *fp;
  20. unsigned long x;
  21. read_count = 0;
  22. fp = fopen(name, "rb");
  23. if (!fp)
  24. assert(0);
  25. x = (unsigned long ) fp;
  26. /* high bit is set, the cast is a bad idea ! */
  27. if (x & 0x90000000)
  28. assert(0);
  29. f = (int ) x;
  30. return f;
  31. }
  32. void duck_close(int handle)
  33. {
  34. fclose((FILE *) handle);
  35. }
  36. static long totalRead = 0;
  37. static long tellNo = 0;
  38. long duck_read(int handle,unsigned char *buffer,long bytes)
  39. {
  40. int x;
  41. if (buffer == NULL){
  42. duck_seek(handle,bytes,SEEK_CUR);
  43. return bytes;
  44. }
  45. tellNo = ftell((FILE *) handle);
  46. if (bytes == 0)
  47. return 0;
  48. x = fread(buffer,sizeof(char) ,bytes, (FILE *) handle);
  49. if (feof((FILE *) handle) && (x != (int ) bytes))
  50. return -1;
  51. totalRead += x;
  52. if (x == -1L)
  53. assert(0);
  54. return x ;
  55. }
  56. long duck_seek(int handle,long offset,int origin)
  57. {
  58. long x = fseek((FILE *) handle,offset,origin);
  59. tellNo = ftell((FILE *) handle);
  60. return tellNo ;
  61. }