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