workaround a problem with the harmattan gcc
[drnoksnes] / ioapi.c
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
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <zlib.h>
13
14 #include "ioapi.h"
15
16 voidpf ZCALLBACK fopen_file_func OF((
17    voidpf opaque,
18    const char* filename,
19    int mode));
20
21 uLong ZCALLBACK fread_file_func OF((
22    voidpf opaque,
23    voidpf stream,
24    void* buf,
25    uLong size));
26
27 uLong ZCALLBACK fwrite_file_func OF((
28    voidpf opaque,
29    voidpf stream,
30    const void* buf,
31    uLong size));
32
33 long ZCALLBACK ftell_file_func OF((
34    voidpf opaque,
35    voidpf stream));
36
37 long ZCALLBACK fseek_file_func OF((
38    voidpf opaque,
39    voidpf stream,
40    uLong offset,
41    int origin));
42
43 int ZCALLBACK fclose_file_func OF((
44    voidpf opaque,
45    voidpf stream));
46
47 int ZCALLBACK ferror_file_func OF((
48    voidpf opaque,
49    voidpf stream));
50
51
52 voidpf ZCALLBACK fopen_file_func (opaque, filename, mode)
53    voidpf opaque;
54    const char* filename;
55    int mode;
56 {
57     FILE* file = NULL;
58     const char* mode_fopen = NULL;
59     if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
60         mode_fopen = "rb";
61     else
62     if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
63         mode_fopen = "r+b";
64     else
65     if (mode & ZLIB_FILEFUNC_MODE_CREATE)
66         mode_fopen = "wb";
67
68     if ((filename!=NULL) && (mode_fopen != NULL))
69         file = fopen(filename, mode_fopen);
70     return file;
71 }
72
73
74 uLong ZCALLBACK fread_file_func (opaque, stream, buf, size)
75    voidpf opaque;
76    voidpf stream;
77    void* buf;
78    uLong size;
79 {
80     uLong ret;
81     ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream);
82     return ret;
83 }
84
85
86 uLong ZCALLBACK fwrite_file_func (opaque, stream, buf, size)
87    voidpf opaque;
88    voidpf stream;
89    const void* buf;
90    uLong size;
91 {
92     uLong ret;
93     ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream);
94     return ret;
95 }
96
97 long ZCALLBACK ftell_file_func (opaque, stream)
98    voidpf opaque;
99    voidpf stream;
100 {
101     long ret;
102     ret = ftell((FILE *)stream);
103     return ret;
104 }
105
106 long ZCALLBACK fseek_file_func (opaque, stream, offset, origin)
107    voidpf opaque;
108    voidpf stream;
109    uLong offset;
110    int origin;
111 {
112     int fseek_origin=0;
113     long ret;
114     switch (origin)
115     {
116     case ZLIB_FILEFUNC_SEEK_CUR :
117         fseek_origin = SEEK_CUR;
118         break;
119     case ZLIB_FILEFUNC_SEEK_END :
120         fseek_origin = SEEK_END;
121         break;
122     case ZLIB_FILEFUNC_SEEK_SET :
123         fseek_origin = SEEK_SET;
124         break;
125     default: return -1;
126     }
127     ret = 0;
128     fseek((FILE *)stream, offset, fseek_origin);
129     return ret;
130 }
131
132 int ZCALLBACK fclose_file_func (opaque, stream)
133    voidpf opaque;
134    voidpf stream;
135 {
136     int ret;
137     ret = fclose((FILE *)stream);
138     return ret;
139 }
140
141 int ZCALLBACK ferror_file_func (opaque, stream)
142    voidpf opaque;
143    voidpf stream;
144 {
145     int ret;
146     ret = ferror((FILE *)stream);
147     return ret;
148 }
149
150 void fill_fopen_filefunc (pzlib_filefunc_def)
151   zlib_filefunc_def* pzlib_filefunc_def;
152 {
153     pzlib_filefunc_def->zopen_file = fopen_file_func;
154     pzlib_filefunc_def->zread_file = fread_file_func;
155     pzlib_filefunc_def->zwrite_file = fwrite_file_func;
156     pzlib_filefunc_def->ztell_file = ftell_file_func;
157     pzlib_filefunc_def->zseek_file = fseek_file_func;
158     pzlib_filefunc_def->zclose_file = fclose_file_func;
159     pzlib_filefunc_def->zerror_file = ferror_file_func;
160     pzlib_filefunc_def->opaque = NULL;
161 }