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