ioapi.c

Go to the documentation of this file.
00001 /* ioapi.c -- IO base function header for compress/uncompress .zip
00002    files using zlib + zip or unzip API
00003 
00004    Version 1.01e, February 12th, 2005
00005 
00006    Copyright (C) 1998-2005 Gilles Vollant
00007 */
00008 
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include <string.h>
00012 
00013 #include <zlib.h>
00014 #include "ioapi.h"
00015 
00016 
00017 
00018 /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
00019 
00020 #ifndef SEEK_CUR
00021 #define SEEK_CUR    1
00022 #endif
00023 
00024 #ifndef SEEK_END
00025 #define SEEK_END    2
00026 #endif
00027 
00028 #ifndef SEEK_SET
00029 #define SEEK_SET    0
00030 #endif
00031 
00032 voidpf ZCALLBACK fopen_file_func OF((
00033    voidpf opaque,
00034    const char* filename,
00035    int mode));
00036 
00037 uLong ZCALLBACK fread_file_func OF((
00038    voidpf opaque,
00039    voidpf stream,
00040    void* buf,
00041    uLong size));
00042 
00043 uLong ZCALLBACK fwrite_file_func OF((
00044    voidpf opaque,
00045    voidpf stream,
00046    const void* buf,
00047    uLong size));
00048 
00049 long ZCALLBACK ftell_file_func OF((
00050    voidpf opaque,
00051    voidpf stream));
00052 
00053 long ZCALLBACK fseek_file_func OF((
00054    voidpf opaque,
00055    voidpf stream,
00056    uLong offset,
00057    int origin));
00058 
00059 int ZCALLBACK fclose_file_func OF((
00060    voidpf opaque,
00061    voidpf stream));
00062 
00063 int ZCALLBACK ferror_file_func OF((
00064    voidpf opaque,
00065    voidpf stream));
00066 
00067 
00068 voidpf ZCALLBACK fopen_file_func (opaque, filename, mode)
00069    voidpf opaque;
00070    const char* filename;
00071    int mode;
00072 {
00073     FILE* file = NULL;
00074     const char* mode_fopen = NULL;
00075     if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
00076         mode_fopen = "rb";
00077     else
00078     if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
00079         mode_fopen = "r+b";
00080     else
00081     if (mode & ZLIB_FILEFUNC_MODE_CREATE)
00082         mode_fopen = "wb";
00083 
00084     if ((filename!=NULL) && (mode_fopen != NULL))
00085         file = fopen(filename, mode_fopen);
00086     return file;
00087 }
00088 
00089 
00090 uLong ZCALLBACK fread_file_func (opaque, stream, buf, size)
00091    voidpf opaque;
00092    voidpf stream;
00093    void* buf;
00094    uLong size;
00095 {
00096     uLong ret;
00097     ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream);
00098     return ret;
00099 }
00100 
00101 
00102 uLong ZCALLBACK fwrite_file_func (opaque, stream, buf, size)
00103    voidpf opaque;
00104    voidpf stream;
00105    const void* buf;
00106    uLong size;
00107 {
00108     uLong ret;
00109     ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream);
00110     return ret;
00111 }
00112 
00113 long ZCALLBACK ftell_file_func (opaque, stream)
00114    voidpf opaque;
00115    voidpf stream;
00116 {
00117     long ret;
00118     ret = ftell((FILE *)stream);
00119     return ret;
00120 }
00121 
00122 long ZCALLBACK fseek_file_func (opaque, stream, offset, origin)
00123    voidpf opaque;
00124    voidpf stream;
00125    uLong offset;
00126    int origin;
00127 {
00128     int fseek_origin=0;
00129     long ret;
00130     switch (origin)
00131     {
00132     case ZLIB_FILEFUNC_SEEK_CUR :
00133         fseek_origin = SEEK_CUR;
00134         break;
00135     case ZLIB_FILEFUNC_SEEK_END :
00136         fseek_origin = SEEK_END;
00137         break;
00138     case ZLIB_FILEFUNC_SEEK_SET :
00139         fseek_origin = SEEK_SET;
00140         break;
00141     default: return -1;
00142     }
00143     ret = 0;
00144     fseek((FILE *)stream, offset, fseek_origin);
00145     return ret;
00146 }
00147 
00148 int ZCALLBACK fclose_file_func (opaque, stream)
00149    voidpf opaque;
00150    voidpf stream;
00151 {
00152     int ret;
00153     ret = fclose((FILE *)stream);
00154     return ret;
00155 }
00156 
00157 int ZCALLBACK ferror_file_func (opaque, stream)
00158    voidpf opaque;
00159    voidpf stream;
00160 {
00161     int ret;
00162     ret = ferror((FILE *)stream);
00163     return ret;
00164 }
00165 
00166 void fill_fopen_filefunc (pzlib_filefunc_def)
00167   zlib_filefunc_def* pzlib_filefunc_def;
00168 {
00169     pzlib_filefunc_def->zopen_file = fopen_file_func;
00170     pzlib_filefunc_def->zread_file = fread_file_func;
00171     pzlib_filefunc_def->zwrite_file = fwrite_file_func;
00172     pzlib_filefunc_def->ztell_file = ftell_file_func;
00173     pzlib_filefunc_def->zseek_file = fseek_file_func;
00174     pzlib_filefunc_def->zclose_file = fclose_file_func;
00175     pzlib_filefunc_def->zerror_file = ferror_file_func;
00176     pzlib_filefunc_def->opaque = NULL;
00177 }

Generated by  doxygen 1.6.2