/src/libtomahawk/thirdparty/quazip/quazip/qioapi.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 142 lines · 114 code · 18 blank · 10 comment · 6 complexity · d0a9e0ec7b7a71ca7f6932f80423d1ff MD5 · raw file

  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. Modified by Sergey A. Tachenov to integrate with Qt.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "zlib.h"
  11. #include "ioapi.h"
  12. #include "quazip_global.h"
  13. #include <QIODevice>
  14. /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
  15. #ifndef SEEK_CUR
  16. #define SEEK_CUR 1
  17. #endif
  18. #ifndef SEEK_END
  19. #define SEEK_END 2
  20. #endif
  21. #ifndef SEEK_SET
  22. #define SEEK_SET 0
  23. #endif
  24. voidpf ZCALLBACK qiodevice_open_file_func (
  25. voidpf opaque UNUSED,
  26. voidpf file,
  27. int mode)
  28. {
  29. QIODevice *iodevice = reinterpret_cast<QIODevice*>(file);
  30. if(iodevice->isSequential())
  31. return NULL;
  32. if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
  33. iodevice->open(QIODevice::ReadOnly);
  34. else
  35. if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
  36. iodevice->open(QIODevice::ReadWrite);
  37. else
  38. if (mode & ZLIB_FILEFUNC_MODE_CREATE)
  39. iodevice->open(QIODevice::WriteOnly);
  40. if(iodevice->isOpen())
  41. return iodevice;
  42. else
  43. return NULL;
  44. }
  45. uLong ZCALLBACK qiodevice_read_file_func (
  46. voidpf opaque UNUSED,
  47. voidpf stream,
  48. void* buf,
  49. uLong size)
  50. {
  51. uLong ret;
  52. ret = (uLong)((QIODevice*)stream)->read((char*)buf,size);
  53. return ret;
  54. }
  55. uLong ZCALLBACK qiodevice_write_file_func (
  56. voidpf opaque UNUSED,
  57. voidpf stream,
  58. const void* buf,
  59. uLong size)
  60. {
  61. uLong ret;
  62. ret = (uLong)((QIODevice*)stream)->write((char*)buf,size);
  63. return ret;
  64. }
  65. uLong ZCALLBACK qiodevice_tell_file_func (
  66. voidpf opaque UNUSED,
  67. voidpf stream)
  68. {
  69. uLong ret;
  70. ret = ((QIODevice*)stream)->pos();
  71. return ret;
  72. }
  73. int ZCALLBACK qiodevice_seek_file_func (
  74. voidpf opaque UNUSED,
  75. voidpf stream,
  76. uLong offset,
  77. int origin)
  78. {
  79. uLong qiodevice_seek_result=0;
  80. int ret;
  81. switch (origin)
  82. {
  83. case ZLIB_FILEFUNC_SEEK_CUR :
  84. qiodevice_seek_result = ((QIODevice*)stream)->pos() + offset;
  85. break;
  86. case ZLIB_FILEFUNC_SEEK_END :
  87. qiodevice_seek_result = ((QIODevice*)stream)->size() - offset;
  88. break;
  89. case ZLIB_FILEFUNC_SEEK_SET :
  90. qiodevice_seek_result = offset;
  91. break;
  92. default: return -1;
  93. }
  94. ret = !((QIODevice*)stream)->seek(qiodevice_seek_result);
  95. return ret;
  96. }
  97. int ZCALLBACK qiodevice_close_file_func (
  98. voidpf opaque UNUSED,
  99. voidpf stream)
  100. {
  101. ((QIODevice*)stream)->close();
  102. return 0;
  103. }
  104. int ZCALLBACK qiodevice_error_file_func (
  105. voidpf opaque UNUSED,
  106. voidpf stream)
  107. {
  108. return !((QIODevice*)stream)->errorString().isEmpty();
  109. }
  110. void fill_qiodevice_filefunc (
  111. zlib_filefunc_def* pzlib_filefunc_def)
  112. {
  113. pzlib_filefunc_def->zopen_file = qiodevice_open_file_func;
  114. pzlib_filefunc_def->zread_file = qiodevice_read_file_func;
  115. pzlib_filefunc_def->zwrite_file = qiodevice_write_file_func;
  116. pzlib_filefunc_def->ztell_file = qiodevice_tell_file_func;
  117. pzlib_filefunc_def->zseek_file = qiodevice_seek_file_func;
  118. pzlib_filefunc_def->zclose_file = qiodevice_close_file_func;
  119. pzlib_filefunc_def->zerror_file = qiodevice_error_file_func;
  120. pzlib_filefunc_def->opaque = NULL;
  121. }