misc/quazip/qioapi.cpp
changeset 8058 bcebfc477459
parent 8054 39f8ea1a441f
parent 8057 93e16240f178
child 8059 07661fb20586
equal deleted inserted replaced
8054:39f8ea1a441f 8058:bcebfc477459
     1 /* ioapi.c -- IO base function header for compress/uncompress .zip
       
     2    files using zlib + zip or unzip API
       
     3 
       
     4    Version 1.01e, February 12th, 2005
       
     5 
       
     6    Copyright (C) 1998-2005 Gilles Vollant
       
     7 
       
     8    Modified by Sergey A. Tachenov to integrate with Qt.
       
     9 */
       
    10 
       
    11 #include <stdio.h>
       
    12 #include <stdlib.h>
       
    13 #include <string.h>
       
    14 
       
    15 #include "zlib.h"
       
    16 #include "ioapi.h"
       
    17 #include "quazip_global.h"
       
    18 #include <QIODevice>
       
    19 
       
    20 
       
    21 /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
       
    22 
       
    23 #ifndef SEEK_CUR
       
    24 #define SEEK_CUR    1
       
    25 #endif
       
    26 
       
    27 #ifndef SEEK_END
       
    28 #define SEEK_END    2
       
    29 #endif
       
    30 
       
    31 #ifndef SEEK_SET
       
    32 #define SEEK_SET    0
       
    33 #endif
       
    34 
       
    35 voidpf ZCALLBACK qiodevice_open_file_func (
       
    36    voidpf opaque UNUSED,
       
    37    voidpf file,
       
    38    int mode)
       
    39 {
       
    40     QIODevice *iodevice = reinterpret_cast<QIODevice*>(file);
       
    41     if(iodevice->isSequential())
       
    42         return NULL;
       
    43     if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
       
    44         iodevice->open(QIODevice::ReadOnly);
       
    45     else
       
    46     if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
       
    47         iodevice->open(QIODevice::ReadWrite);
       
    48     else
       
    49     if (mode & ZLIB_FILEFUNC_MODE_CREATE)
       
    50         iodevice->open(QIODevice::WriteOnly);
       
    51 
       
    52     if(iodevice->isOpen())
       
    53         return iodevice;
       
    54     else
       
    55         return NULL;
       
    56 }
       
    57 
       
    58 
       
    59 uLong ZCALLBACK qiodevice_read_file_func (
       
    60    voidpf opaque UNUSED,
       
    61    voidpf stream,
       
    62    void* buf,
       
    63    uLong size)
       
    64 {
       
    65     uLong ret;
       
    66     ret = (uLong)((QIODevice*)stream)->read((char*)buf,size);
       
    67     return ret;
       
    68 }
       
    69 
       
    70 
       
    71 uLong ZCALLBACK qiodevice_write_file_func (
       
    72    voidpf opaque UNUSED,
       
    73    voidpf stream,
       
    74    const void* buf,
       
    75    uLong size)
       
    76 {
       
    77     uLong ret;
       
    78     ret = (uLong)((QIODevice*)stream)->write((char*)buf,size);
       
    79     return ret;
       
    80 }
       
    81 
       
    82 uLong ZCALLBACK qiodevice_tell_file_func (
       
    83    voidpf opaque UNUSED,
       
    84    voidpf stream)
       
    85 {
       
    86     uLong ret;
       
    87     ret = ((QIODevice*)stream)->pos();
       
    88     return ret;
       
    89 }
       
    90 
       
    91 int ZCALLBACK qiodevice_seek_file_func (
       
    92    voidpf opaque UNUSED,
       
    93    voidpf stream,
       
    94    uLong offset,
       
    95    int origin)
       
    96 {
       
    97     uLong qiodevice_seek_result=0;
       
    98     int ret;
       
    99     switch (origin)
       
   100     {
       
   101     case ZLIB_FILEFUNC_SEEK_CUR :
       
   102         qiodevice_seek_result = ((QIODevice*)stream)->pos() + offset;
       
   103         break;
       
   104     case ZLIB_FILEFUNC_SEEK_END :
       
   105         qiodevice_seek_result = ((QIODevice*)stream)->size() - offset;
       
   106         break;
       
   107     case ZLIB_FILEFUNC_SEEK_SET :
       
   108         qiodevice_seek_result = offset;
       
   109         break;
       
   110     default: return -1;
       
   111     }
       
   112     ret = !((QIODevice*)stream)->seek(qiodevice_seek_result);
       
   113     return ret;
       
   114 }
       
   115 
       
   116 int ZCALLBACK qiodevice_close_file_func (
       
   117    voidpf opaque UNUSED,
       
   118    voidpf stream)
       
   119 {
       
   120     ((QIODevice*)stream)->close();
       
   121     return 0;
       
   122 }
       
   123 
       
   124 int ZCALLBACK qiodevice_error_file_func (
       
   125    voidpf opaque UNUSED,
       
   126    voidpf stream)
       
   127 {
       
   128     return !((QIODevice*)stream)->errorString().isEmpty();
       
   129 }
       
   130 
       
   131 void fill_qiodevice_filefunc (
       
   132   zlib_filefunc_def* pzlib_filefunc_def)
       
   133 {
       
   134     pzlib_filefunc_def->zopen_file = qiodevice_open_file_func;
       
   135     pzlib_filefunc_def->zread_file = qiodevice_read_file_func;
       
   136     pzlib_filefunc_def->zwrite_file = qiodevice_write_file_func;
       
   137     pzlib_filefunc_def->ztell_file = qiodevice_tell_file_func;
       
   138     pzlib_filefunc_def->zseek_file = qiodevice_seek_file_func;
       
   139     pzlib_filefunc_def->zclose_file = qiodevice_close_file_func;
       
   140     pzlib_filefunc_def->zerror_file = qiodevice_error_file_func;
       
   141     pzlib_filefunc_def->opaque = NULL;
       
   142 }