ioapi.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* ioapi.c -- IO base function header for compress/uncompress .zip
  2. files using zlib + zip or unzip API
  3. Version 1.01e, February 12th, 2005
  4. Copyright (C) 1998-2005 Gilles Vollant
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. # ifdef ASSIMP_BUILD_NO_OWN_ZLIB
  10. # include <zlib.h>
  11. # else
  12. # include "../zlib/zlib.h"
  13. # endif
  14. #include "ioapi.h"
  15. /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
  16. #ifndef SEEK_CUR
  17. #define SEEK_CUR 1
  18. #endif
  19. #ifndef SEEK_END
  20. #define SEEK_END 2
  21. #endif
  22. #ifndef SEEK_SET
  23. #define SEEK_SET 0
  24. #endif
  25. voidpf ZCALLBACK fopen_file_func (
  26. voidpf opaque,
  27. const char* filename,
  28. int mode);
  29. uLong ZCALLBACK fread_file_func (
  30. voidpf opaque,
  31. voidpf stream,
  32. void* buf,
  33. uLong size);
  34. uLong ZCALLBACK fwrite_file_func (
  35. voidpf opaque,
  36. voidpf stream,
  37. const void* buf,
  38. uLong size);
  39. long ZCALLBACK ftell_file_func (
  40. voidpf opaque,
  41. voidpf stream);
  42. long ZCALLBACK fseek_file_func (
  43. voidpf opaque,
  44. voidpf stream,
  45. uLong offset,
  46. int origin);
  47. int ZCALLBACK fclose_file_func (
  48. voidpf opaque,
  49. voidpf stream);
  50. int ZCALLBACK ferror_file_func (
  51. voidpf opaque,
  52. voidpf stream);
  53. voidpf ZCALLBACK fopen_file_func (opaque, filename, mode)
  54. voidpf opaque;
  55. const char* filename;
  56. int mode;
  57. {
  58. FILE* file = NULL;
  59. const char* mode_fopen = NULL;
  60. if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
  61. mode_fopen = "rb";
  62. else
  63. if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
  64. mode_fopen = "r+b";
  65. else
  66. if (mode & ZLIB_FILEFUNC_MODE_CREATE)
  67. mode_fopen = "wb";
  68. if ((filename!=NULL) && (mode_fopen != NULL))
  69. file = fopen(filename, mode_fopen);
  70. return file;
  71. }
  72. uLong ZCALLBACK fread_file_func (opaque, stream, buf, size)
  73. voidpf opaque;
  74. voidpf stream;
  75. void* buf;
  76. uLong size;
  77. {
  78. uLong ret;
  79. ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream);
  80. return ret;
  81. }
  82. uLong ZCALLBACK fwrite_file_func (opaque, stream, buf, size)
  83. voidpf opaque;
  84. voidpf stream;
  85. const void* buf;
  86. uLong size;
  87. {
  88. uLong ret;
  89. ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream);
  90. return ret;
  91. }
  92. long ZCALLBACK ftell_file_func (opaque, stream)
  93. voidpf opaque;
  94. voidpf stream;
  95. {
  96. long ret;
  97. ret = ftell((FILE *)stream);
  98. return ret;
  99. }
  100. long ZCALLBACK fseek_file_func (opaque, stream, offset, origin)
  101. voidpf opaque;
  102. voidpf stream;
  103. uLong offset;
  104. int origin;
  105. {
  106. int fseek_origin=0;
  107. long ret;
  108. switch (origin)
  109. {
  110. case ZLIB_FILEFUNC_SEEK_CUR :
  111. fseek_origin = SEEK_CUR;
  112. break;
  113. case ZLIB_FILEFUNC_SEEK_END :
  114. fseek_origin = SEEK_END;
  115. break;
  116. case ZLIB_FILEFUNC_SEEK_SET :
  117. fseek_origin = SEEK_SET;
  118. break;
  119. default: return -1;
  120. }
  121. ret = 0;
  122. fseek((FILE *)stream, offset, fseek_origin);
  123. return ret;
  124. }
  125. int ZCALLBACK fclose_file_func (opaque, stream)
  126. voidpf opaque;
  127. voidpf stream;
  128. {
  129. int ret;
  130. ret = fclose((FILE *)stream);
  131. return ret;
  132. }
  133. int ZCALLBACK ferror_file_func (opaque, stream)
  134. voidpf opaque;
  135. voidpf stream;
  136. {
  137. int ret;
  138. ret = ferror((FILE *)stream);
  139. return ret;
  140. }
  141. void fill_fopen_filefunc (pzlib_filefunc_def)
  142. zlib_filefunc_def* pzlib_filefunc_def;
  143. {
  144. pzlib_filefunc_def->zopen_file = fopen_file_func;
  145. pzlib_filefunc_def->zread_file = fread_file_func;
  146. pzlib_filefunc_def->zwrite_file = fwrite_file_func;
  147. pzlib_filefunc_def->ztell_file = ftell_file_func;
  148. pzlib_filefunc_def->zseek_file = fseek_file_func;
  149. pzlib_filefunc_def->zclose_file = fclose_file_func;
  150. pzlib_filefunc_def->zerror_file = ferror_file_func;
  151. pzlib_filefunc_def->opaque = NULL;
  152. }