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