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,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is 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
18 * IN 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 used
22 * in advertising or otherwise to promote the sale, use or other dealings in
23 * 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 "xpmP.h"
36 #include <sys/stat.h>
37
38 LFUNC(OpenReadFile, int, (char *filename, xpmData * mdata));
39 LFUNC(xpmDataClose, void, (xpmData * mdata));
40
41 int
XpmReadFileToImage(display,filename,image_return,shapeimage_return,attributes)42 XpmReadFileToImage(display, filename,
43 image_return, shapeimage_return, attributes)
44 Display *display;
45 char *filename;
46 XImage **image_return;
47 XImage **shapeimage_return;
48 XpmAttributes *attributes;
49 {
50 XpmImage image;
51 XpmInfo info;
52 int ErrorStatus;
53
54 /* create an XpmImage from the file */
55 if (attributes)
56 {
57 xpmInitAttributes(attributes);
58 xpmSetInfoMask(&info, attributes);
59 ErrorStatus = XpmReadFileToXpmImage(filename, &image, &info);
60 } else
61 ErrorStatus = XpmReadFileToXpmImage(filename, &image, NULL);
62
63 if (ErrorStatus != XpmSuccess)
64 return (ErrorStatus);
65
66 /* create the related ximages */
67 ErrorStatus = XpmCreateImageFromXpmImage(display, &image,
68 image_return, shapeimage_return,
69 attributes);
70 if (attributes)
71 {
72 if (ErrorStatus >= 0) /* no fatal error */
73 xpmSetAttributes(attributes, &image, &info);
74 XpmFreeXpmInfo(&info);
75 }
76 /* free the XpmImage */
77 XpmFreeXpmImage(&image);
78
79 return (ErrorStatus);
80 }
81
82 int
XpmReadFileToXpmImage(filename,image,info)83 XpmReadFileToXpmImage(filename, image, info)
84 char *filename;
85 XpmImage *image;
86 XpmInfo *info;
87 {
88 xpmData mdata;
89 int ErrorStatus;
90
91 /* init returned values */
92 xpmInitXpmImage(image);
93 xpmInitXpmInfo(info);
94
95 /* open file to read */
96 if ((ErrorStatus = OpenReadFile(filename, &mdata)) != XpmSuccess)
97 return (ErrorStatus);
98
99 /* create the XpmImage from the XpmData */
100 ErrorStatus = xpmParseData(&mdata, image, info);
101
102 xpmDataClose(&mdata);
103
104 return (ErrorStatus);
105 }
106
107 /*
108 * open the given file to be read as an xpmData which is returned.
109 */
110 static int
OpenReadFile(filename,mdata)111 OpenReadFile(filename, mdata)
112 char *filename;
113 xpmData *mdata;
114 {
115 #ifdef ZPIPE
116 char *compressfile, buf[BUFSIZ];
117 struct stat status;
118
119 #endif
120
121 if (!filename)
122 {
123 mdata->stream.file = (stdin);
124 mdata->type = XPMFILE;
125 } else
126 {
127 #ifdef ZPIPE
128 if (((int) strlen(filename) > 2) &&
129 !strcmp(".Z", filename + (strlen(filename) - 2)))
130 {
131 mdata->type = XPMPIPE;
132 sprintf(buf, "uncompress -c \"%s\"", filename);
133 if (!(mdata->stream.file = popen(buf, "r")))
134 return (XpmOpenFailed);
135
136 } else
137 if (((int) strlen(filename) > 3) &&
138 !strcmp(".gz", filename + (strlen(filename) - 3)))
139 {
140 mdata->type = XPMPIPE;
141 sprintf(buf, "gunzip -qc \"%s\"", filename);
142 if (!(mdata->stream.file = popen(buf, "r")))
143 return (XpmOpenFailed);
144
145 } else
146 {
147 if (!(compressfile = (char *) XpmMalloc(strlen(filename) + 4)))
148 return (XpmNoMemory);
149
150 strcpy(compressfile, filename);
151 strcat(compressfile, ".Z");
152 if (!stat(compressfile, &status))
153 {
154 sprintf(buf, "uncompress -c \"%s\"", compressfile);
155 if (!(mdata->stream.file = popen(buf, "r")))
156 {
157 XpmFree(compressfile);
158 return (XpmOpenFailed);
159 }
160 mdata->type = XPMPIPE;
161 } else
162 {
163 strcpy(compressfile, filename);
164 strcat(compressfile, ".gz");
165 if (!stat(compressfile, &status))
166 {
167 sprintf(buf, "gunzip -c \"%s\"", compressfile);
168 if (!(mdata->stream.file = popen(buf, "r")))
169 {
170 XpmFree(compressfile);
171 return (XpmOpenFailed);
172 }
173 mdata->type = XPMPIPE;
174 } else
175 {
176 #endif
177 if (!(mdata->stream.file = fopen(filename, "r")))
178 {
179 #ifdef ZPIPE
180 XpmFree(compressfile);
181 #endif
182 return (XpmOpenFailed);
183 }
184 mdata->type = XPMFILE;
185 #ifdef ZPIPE
186 }
187 }
188 XpmFree(compressfile);
189 }
190 #endif
191 }
192 mdata->CommentLength = 0;
193 return (XpmSuccess);
194 }
195
196 /*
197 * close the file related to the xpmData if any
198 */
199 static void
xpmDataClose(mdata)200 xpmDataClose(mdata)
201 xpmData *mdata;
202 {
203 switch (mdata->type)
204 {
205 case XPMFILE:
206 if (mdata->stream.file != (stdin))
207 fclose(mdata->stream.file);
208 break;
209 #ifdef ZPIPE
210 case XPMPIPE:
211 pclose(mdata->stream.file);
212 break;
213 #endif
214 }
215 }
216