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