Back to index

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