]> git.saurik.com Git - wxWidgets.git/blob - src/mac/xpm/RdFToI.c
merge with latest sources
[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 #ifdef __std
39 #undef __std
40 #define __std()
41 #endif
42 #include <stat.h>
43 #include <unix.h>
44 #include <unistd.h>
45 #include <fcntl.h>
46 #undef NO_ZPIPE
47 #define NO_ZPIPE
48 #endif
49
50 #include "XpmI.h"
51 #ifndef macintosh
52 #include <sys/stat.h>
53 #if !defined(NO_ZPIPE) && defined(WIN32)
54 # define popen _popen
55 # define pclose _pclose
56 # if defined(STAT_ZFILE)
57 # include <io.h>
58 # define stat _stat
59 # define fstat _fstat
60 # endif
61 #endif
62 #endif
63
64 LFUNC(OpenReadFile, int, (char *filename, xpmData *mdata));
65 LFUNC(xpmDataClose, void, (xpmData *mdata));
66
67 #ifndef CXPMPROG
68 int
69 XpmReadFileToImage(display, filename,
70 image_return, shapeimage_return, attributes)
71 Display *display;
72 char *filename;
73 XImage **image_return;
74 XImage **shapeimage_return;
75 XpmAttributes *attributes;
76 {
77 XpmImage image;
78 XpmInfo info;
79 int ErrorStatus;
80 xpmData mdata;
81
82 xpmInitXpmImage(&image);
83 xpmInitXpmInfo(&info);
84
85 /* open file to read */
86 if ((ErrorStatus = OpenReadFile(filename, &mdata)) != XpmSuccess)
87 return (ErrorStatus);
88
89 /* create the XImage from the XpmData */
90 if (attributes) {
91 xpmInitAttributes(attributes);
92 xpmSetInfoMask(&info, attributes);
93 ErrorStatus = xpmParseDataAndCreate(display, &mdata,
94 image_return, shapeimage_return,
95 &image, &info, attributes);
96 } else
97 ErrorStatus = xpmParseDataAndCreate(display, &mdata,
98 image_return, shapeimage_return,
99 &image, NULL, attributes);
100 if (attributes) {
101 if (ErrorStatus >= 0) /* no fatal error */
102 xpmSetAttributes(attributes, &image, &info);
103 XpmFreeXpmInfo(&info);
104 }
105
106 xpmDataClose(&mdata);
107 /* free the XpmImage */
108 XpmFreeXpmImage(&image);
109
110 return (ErrorStatus);
111 }
112
113 int
114 XpmReadFileToXpmImage(filename, image, info)
115 char *filename;
116 XpmImage *image;
117 XpmInfo *info;
118 {
119 xpmData mdata;
120 int ErrorStatus;
121
122 /* init returned values */
123 xpmInitXpmImage(image);
124 xpmInitXpmInfo(info);
125
126 /* open file to read */
127 if ((ErrorStatus = OpenReadFile(filename, &mdata)) != XpmSuccess)
128 return (ErrorStatus);
129
130 /* create the XpmImage from the XpmData */
131 ErrorStatus = xpmParseData(&mdata, image, info);
132
133 xpmDataClose(&mdata);
134
135 return (ErrorStatus);
136 }
137 #endif /* CXPMPROG */
138
139 /*
140 * open the given file to be read as an xpmData which is returned.
141 */
142 static int
143 OpenReadFile(filename, mdata)
144 char *filename;
145 xpmData *mdata;
146 {
147 #ifndef NO_ZPIPE
148 char *compressfile, buf[BUFSIZ];
149 # ifdef STAT_ZFILE
150 struct stat status;
151 # endif
152 #endif
153
154 if (!filename) {
155 mdata->stream.file = (stdin);
156 mdata->type = XPMFILE;
157 } else {
158 #ifndef NO_ZPIPE
159 int len = strlen(filename);
160 if ((len > 2) && !strcmp(".Z", filename + (len - 2))) {
161 mdata->type = XPMPIPE;
162 sprintf(buf, "uncompress -c \"%s\"", filename);
163 if (!(mdata->stream.file = popen(buf, "r")))
164 return (XpmOpenFailed);
165
166 } else if ((len > 3) && !strcmp(".gz", filename + (len - 3))) {
167 mdata->type = XPMPIPE;
168 sprintf(buf, "gunzip -qc \"%s\"", filename);
169 if (!(mdata->stream.file = popen(buf, "r")))
170 return (XpmOpenFailed);
171
172 } else {
173 # ifdef STAT_ZFILE
174 if (!(compressfile = (char *) XpmMalloc(len + 4)))
175 return (XpmNoMemory);
176
177 sprintf(compressfile, "%s.Z", filename);
178 if (!stat(compressfile, &status)) {
179 sprintf(buf, "uncompress -c \"%s\"", compressfile);
180 if (!(mdata->stream.file = popen(buf, "r"))) {
181 XpmFree(compressfile);
182 return (XpmOpenFailed);
183 }
184 mdata->type = XPMPIPE;
185 } else {
186 sprintf(compressfile, "%s.gz", filename);
187 if (!stat(compressfile, &status)) {
188 sprintf(buf, "gunzip -c \"%s\"", compressfile);
189 if (!(mdata->stream.file = popen(buf, "r"))) {
190 XpmFree(compressfile);
191 return (XpmOpenFailed);
192 }
193 mdata->type = XPMPIPE;
194 } else {
195 # endif
196 #endif
197 if (!(mdata->stream.file = fopen(filename, "r"))) {
198 #if !defined(NO_ZPIPE) && defined(STAT_ZFILE)
199 XpmFree(compressfile);
200 #endif
201 return (XpmOpenFailed);
202 }
203 mdata->type = XPMFILE;
204 #ifndef NO_ZPIPE
205 # ifdef STAT_ZFILE
206 }
207 }
208 XpmFree(compressfile);
209 # endif
210 }
211 #endif
212 }
213 mdata->CommentLength = 0;
214 #ifdef CXPMPROG
215 mdata->lineNum = 0;
216 mdata->charNum = 0;
217 #endif
218 return (XpmSuccess);
219 }
220
221 /*
222 * close the file related to the xpmData if any
223 */
224 static void
225 xpmDataClose(mdata)
226 xpmData *mdata;
227 {
228 switch (mdata->type) {
229 case XPMFILE:
230 if (mdata->stream.file != (stdin))
231 fclose(mdata->stream.file);
232 break;
233 #ifndef NO_ZPIPE
234 case XPMPIPE:
235 pclose(mdata->stream.file);
236 break;
237 #endif
238 }
239 }