Debian Codegraph @

code/qcommon/ioapi.c

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