]> git.saurik.com Git - wxWidgets.git/blob - src/mac/xpm/RdFToI.c
changed version number
[wxWidgets.git] / src / mac / xpm / RdFToI.c
1 /*
2 * Copyright (C) 1989-95 GROUPE BULL
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 * Except as contained in this notice, the name of GROUPE BULL shall not be
22 * used in advertising or otherwise to promote the sale, use or other dealings
23 * in this Software without prior written authorization from GROUPE BULL.
24 */
25
26 /*****************************************************************************\
27 * RdFToI.c: *
28 * *
29 * XPM library *
30 * Parse an XPM file and create the image and possibly its mask *
31 * *
32 * Developed by Arnaud Le Hors *
33 \*****************************************************************************/
34
35 #include "wx/setup.h"
36
37 #ifdef macintosh
38 #include <stat.h>
39 #include <unix.h>
40 #include <unistd.h>
41 #include <fcntl.h>
42 #undef NO_ZPIPE
43 #define NO_ZPIPE
44 #endif
45
46 #include "XpmI.h"
47 #ifndef macintosh
48 #include <sys/stat.h>
49 #if !defined(NO_ZPIPE) && defined(WIN32)
50 # define popen _popen
51 # define pclose _pclose
52 # if defined(STAT_ZFILE)
53 # include <io.h>
54 # define stat _stat
55 # define fstat _fstat
56 # endif
57 #endif
58 #endif
59
60 LFUNC(OpenReadFile, int, (char *filename, xpmData *mdata));
61 LFUNC(xpmDataClose, void, (xpmData *mdata));
62
63 #ifndef CXPMPROG
64 int
65 XpmReadFileToImage(display, filename,
66 image_return, shapeimage_return, attributes)
67 Display *display;
68 char *filename;
69 XImage **image_return;
70 XImage **shapeimage_return;
71 XpmAttributes *attributes;
72 {
73 XpmImage image;
74 XpmInfo info;
75 int ErrorStatus;
76 xpmData mdata;
77
78 xpmInitXpmImage(&image);
79 xpmInitXpmInfo(&info);
80
81 /* open file to read */
82 if ((ErrorStatus = OpenReadFile(filename, &mdata)) != XpmSuccess)
83 return (ErrorStatus);
84
85 /* create the XImage from the XpmData */
86 if (attributes) {
87 xpmInitAttributes(attributes);
88 xpmSetInfoMask(&info, attributes);
89 ErrorStatus = xpmParseDataAndCreate(display, &mdata,
90 image_return, shapeimage_return,
91 &image, &info, attributes);
92 } else
93 ErrorStatus = xpmParseDataAndCreate(display, &mdata,
94 image_return, shapeimage_return,
95 &image, NULL, attributes);
96 if (attributes) {
97 if (ErrorStatus >= 0) /* no fatal error */
98 xpmSetAttributes(attributes, &image, &info);
99 XpmFreeXpmInfo(&info);
100 }
101
102 xpmDataClose(&mdata);
103 /* free the XpmImage */
104 XpmFreeXpmImage(&image);
105
106 return (ErrorStatus);
107 }
108
109 int
110 XpmReadFileToXpmImage(filename, image, info)
111 char *filename;
112 XpmImage *image;
113 XpmInfo *info;
114 {
115 xpmData mdata;
116 int ErrorStatus;
117
118 /* init returned values */
119 xpmInitXpmImage(image);
120 xpmInitXpmInfo(info);
121
122 /* open file to read */
123 if ((ErrorStatus = OpenReadFile(filename, &mdata)) != XpmSuccess)
124 return (ErrorStatus);
125
126 /* create the XpmImage from the XpmData */
127 ErrorStatus = xpmParseData(&mdata, image, info);
128
129 xpmDataClose(&mdata);
130
131 return (ErrorStatus);
132 }
133 #endif /* CXPMPROG */
134
135 /*
136 * open the given file to be read as an xpmData which is returned.
137 */
138 static int
139 OpenReadFile(filename, mdata)
140 char *filename;
141 xpmData *mdata;
142 {
143 #ifndef NO_ZPIPE
144 char *compressfile, buf[BUFSIZ];
145 # ifdef STAT_ZFILE
146 struct stat status;
147 # endif
148 #endif
149
150 if (!filename) {
151 mdata->stream.file = (stdin);
152 mdata->type = XPMFILE;
153 } else {
154 #ifndef NO_ZPIPE
155 int len = strlen(filename);
156 if ((len > 2) && !strcmp(".Z", filename + (len - 2))) {
157 mdata->type = XPMPIPE;
158 sprintf(buf, "uncompress -c \"%s\"", filename);
159 if (!(mdata->stream.file = popen(buf, "r")))
160 return (XpmOpenFailed);
161
162 } else if ((len > 3) && !strcmp(".gz", filename + (len - 3))) {
163 mdata->type = XPMPIPE;
164 sprintf(buf, "gunzip -qc \"%s\"", filename);
165 if (!(mdata->stream.file = popen(buf, "r")))
166 return (XpmOpenFailed);
167
168 } else {
169 # ifdef STAT_ZFILE
170 if (!(compressfile = (char *) XpmMalloc(len + 4)))
171 return (XpmNoMemory);
172
173 sprintf(compressfile, "%s.Z", filename);
174 if (!stat(compressfile, &status)) {
175 sprintf(buf, "uncompress -c \"%s\"", compressfile);
176 if (!(mdata->stream.file = popen(buf, "r"))) {
177 XpmFree(compressfile);
178 return (XpmOpenFailed);
179 }
180 mdata->type = XPMPIPE;
181 } else {
182 sprintf(compressfile, "%s.gz", filename);
183 if (!stat(compressfile, &status)) {
184 sprintf(buf, "gunzip -c \"%s\"", compressfile);
185 if (!(mdata->stream.file = popen(buf, "r"))) {
186 XpmFree(compressfile);
187 return (XpmOpenFailed);
188 }
189 mdata->type = XPMPIPE;
190 } else {
191 # endif
192 #endif
193 if (!(mdata->stream.file = fopen(filename, "r"))) {
194 #if !defined(NO_ZPIPE) && defined(STAT_ZFILE)
195 XpmFree(compressfile);
196 #endif
197 return (XpmOpenFailed);
198 }
199 mdata->type = XPMFILE;
200 #ifndef NO_ZPIPE
201 # ifdef STAT_ZFILE
202 }
203 }
204 XpmFree(compressfile);
205 # endif
206 }
207 #endif
208 }
209 mdata->CommentLength = 0;
210 #ifdef CXPMPROG
211 mdata->lineNum = 0;
212 mdata->charNum = 0;
213 #endif
214 return (XpmSuccess);
215 }
216
217 /*
218 * close the file related to the xpmData if any
219 */
220 static void
221 xpmDataClose(mdata)
222 xpmData *mdata;
223 {
224 switch (mdata->type) {
225 case XPMFILE:
226 if (mdata->stream.file != (stdin))
227 fclose(mdata->stream.file);
228 break;
229 #ifndef NO_ZPIPE
230 case XPMPIPE:
231 pclose(mdata->stream.file);
232 break;
233 #endif
234 }
235 }