Back to index

plt-scheme  4.2.1
RdFToI.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 1989-95 GROUPE BULL
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to
00006  * deal in the Software without restriction, including without limitation the
00007  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
00008  * sell copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00017  * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00018  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00019  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00020  *
00021  * Except as contained in this notice, the name of GROUPE BULL shall not be
00022  * used in advertising or otherwise to promote the sale, use or other dealings
00023  * in this Software without prior written authorization from GROUPE BULL.
00024  */
00025 
00026 /*****************************************************************************\
00027 *  RdFToI.c:                                                                  *
00028 *                                                                             *
00029 *  XPM library                                                                *
00030 *  Parse an XPM file and create the image and possibly its mask               *
00031 *                                                                             *
00032 *  Developed by Arnaud Le Hors                                                *
00033 \*****************************************************************************/
00034 
00035 #include "xpmP.h"
00036 #include <sys/stat.h>
00037 
00038 LFUNC(OpenReadFile, int, (char *filename, xpmData *mdata));
00039 LFUNC(xpmDataClose, void, (xpmData *mdata));
00040 
00041 int
00042 XpmReadFileToImage(display, filename,
00043                  image_return, shapeimage_return, attributes)
00044     Display *display;
00045     char *filename;
00046     XImage **image_return;
00047     XImage **shapeimage_return;
00048     XpmAttributes *attributes;
00049 {
00050     XpmImage image;
00051     XpmInfo info;
00052     int ErrorStatus;
00053 
00054     /* create an XpmImage from the file */
00055     if (attributes) {
00056        xpmInitAttributes(attributes);
00057        xpmSetInfoMask(&info, attributes);
00058        ErrorStatus = XpmReadFileToXpmImage(filename, &image, &info);
00059     } else
00060        ErrorStatus = XpmReadFileToXpmImage(filename, &image, NULL);
00061 
00062     if (ErrorStatus != XpmSuccess)
00063        return (ErrorStatus);
00064 
00065     /* create the related ximages */
00066     ErrorStatus = XpmCreateImageFromXpmImage(display, &image,
00067                                         image_return, shapeimage_return,
00068                                         attributes);
00069     if (attributes) {
00070        if (ErrorStatus >= 0)              /* no fatal error */
00071            xpmSetAttributes(attributes, &image, &info);
00072        XpmFreeXpmInfo(&info);
00073     }
00074     /* free the XpmImage */
00075     XpmFreeXpmImage(&image);
00076 
00077     return (ErrorStatus);
00078 }
00079 
00080 int
00081 XpmReadFileToXpmImage(filename, image, info)
00082     char *filename;
00083     XpmImage *image;
00084     XpmInfo *info;
00085 {
00086     xpmData mdata;
00087     int ErrorStatus;
00088 
00089     /* init returned values */
00090     xpmInitXpmImage(image);
00091     xpmInitXpmInfo(info);
00092 
00093     /* open file to read */
00094     if ((ErrorStatus = OpenReadFile(filename, &mdata)) != XpmSuccess)
00095        return (ErrorStatus);
00096 
00097     /* create the XpmImage from the XpmData */
00098     ErrorStatus = xpmParseData(&mdata, image, info);
00099 
00100     xpmDataClose(&mdata);
00101 
00102     return (ErrorStatus);
00103 }
00104 
00105 /*
00106  * open the given file to be read as an xpmData which is returned.
00107  */
00108 static int
00109 OpenReadFile(filename, mdata)
00110     char *filename;
00111     xpmData *mdata;
00112 {
00113 #ifdef ZPIPE
00114     char *compressfile, buf[BUFSIZ];
00115     struct stat status;
00116 
00117 #endif
00118 
00119     if (!filename) {
00120        mdata->stream.file = (stdin);
00121        mdata->type = XPMFILE;
00122     } else {
00123 #ifdef ZPIPE
00124        if (((int) strlen(filename) > 2) &&
00125            !strcmp(".Z", filename + (strlen(filename) - 2))) {
00126            mdata->type = XPMPIPE;
00127            sprintf(buf, "uncompress -c \"%s\"", filename);
00128            if (!(mdata->stream.file = popen(buf, "r")))
00129               return (XpmOpenFailed);
00130 
00131        } else if (((int) strlen(filename) > 3) &&
00132                  !strcmp(".gz", filename + (strlen(filename) - 3))) {
00133            mdata->type = XPMPIPE;
00134            sprintf(buf, "gunzip -qc \"%s\"", filename);
00135            if (!(mdata->stream.file = popen(buf, "r")))
00136               return (XpmOpenFailed);
00137 
00138        } else {
00139            if (!(compressfile = (char *) XpmMalloc(strlen(filename) + 4)))
00140               return (XpmNoMemory);
00141 
00142            strcpy(compressfile, filename);
00143            strcat(compressfile, ".Z");
00144            if (!stat(compressfile, &status)) {
00145               sprintf(buf, "uncompress -c \"%s\"", compressfile);
00146               if (!(mdata->stream.file = popen(buf, "r"))) {
00147                   XpmFree(compressfile);
00148                   return (XpmOpenFailed);
00149               }
00150               mdata->type = XPMPIPE;
00151            } else {
00152               strcpy(compressfile, filename);
00153               strcat(compressfile, ".gz");
00154               if (!stat(compressfile, &status)) {
00155                   sprintf(buf, "gunzip -c \"%s\"", compressfile);
00156                   if (!(mdata->stream.file = popen(buf, "r"))) {
00157                      XpmFree(compressfile);
00158                      return (XpmOpenFailed);
00159                   }
00160                   mdata->type = XPMPIPE;
00161               } else {
00162 #endif
00163                   if (!(mdata->stream.file = fopen(filename, "r"))) {
00164 #ifdef ZPIPE
00165                      XpmFree(compressfile);
00166 #endif
00167                      return (XpmOpenFailed);
00168                   }
00169                   mdata->type = XPMFILE;
00170 #ifdef ZPIPE
00171               }
00172            }
00173            XpmFree(compressfile);
00174        }
00175 #endif
00176     }
00177     mdata->CommentLength = 0;
00178     return (XpmSuccess);
00179 }
00180 
00181 /*
00182  * close the file related to the xpmData if any
00183  */
00184 static void
00185 xpmDataClose(mdata)
00186     xpmData *mdata;
00187 {
00188     switch (mdata->type) {
00189     case XPMFILE:
00190        if (mdata->stream.file != (stdin))
00191            fclose(mdata->stream.file);
00192        break;
00193 #ifdef ZPIPE
00194     case XPMPIPE:
00195        pclose(mdata->stream.file);
00196        break;
00197 #endif
00198     }
00199 }