home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / x / x11r6-ch / xpm-3.4 / xpm-3 / xpm-3.4c / lib / RdFToI.c < prev    next >
C/C++ Source or Header  |  1994-06-06  |  6KB  |  204 lines

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