Blame lib/Xm/XpmRdFToI.c

Packit b099d7
/* $TOG: XpmRdFToI.c /main/8 1998/07/22 15:43:27 mgreess $ */
Packit b099d7
/*
Packit b099d7
 * Copyright (C) 1989-95 GROUPE BULL
Packit b099d7
 *
Packit b099d7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit b099d7
 * of this software and associated documentation files (the "Software"), to
Packit b099d7
 * deal in the Software without restriction, including without limitation the
Packit b099d7
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
Packit b099d7
 * sell copies of the Software, and to permit persons to whom the Software is
Packit b099d7
 * furnished to do so, subject to the following conditions:
Packit b099d7
 *
Packit b099d7
 * The above copyright notice and this permission notice shall be included in
Packit b099d7
 * all copies or substantial portions of the Software.
Packit b099d7
 *
Packit b099d7
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit b099d7
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit b099d7
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
Packit b099d7
 * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
Packit b099d7
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
Packit b099d7
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Packit b099d7
 *
Packit b099d7
 * Except as contained in this notice, the name of GROUPE BULL shall not be
Packit b099d7
 * used in advertising or otherwise to promote the sale, use or other dealings
Packit b099d7
 * in this Software without prior written authorization from GROUPE BULL.
Packit b099d7
 */
Packit b099d7
Packit b099d7
/*****************************************************************************\
Packit b099d7
*  RdFToI.c:                                                                  *
Packit b099d7
*                                                                             *
Packit b099d7
*  XPM library                                                                *
Packit b099d7
*  Parse an XPM file and create the image and possibly its mask               *
Packit b099d7
*                                                                             *
Packit b099d7
*  Developed by Arnaud Le Hors                                                *
Packit b099d7
\*****************************************************************************/
Packit b099d7
Packit b099d7
#ifdef HAVE_CONFIG_H
Packit b099d7
#include <config.h>
Packit b099d7
#endif
Packit b099d7
Packit b099d7
Packit b099d7
/* October 2004, source code review by Thomas Biege <thomas@suse.de> */
Packit b099d7
Packit b099d7
#include "XpmI.h"
Packit b099d7
#include <sys/stat.h>
Packit b099d7
#include <sys/param.h>
Packit b099d7
#if !defined(NO_ZPIPE) && defined(WIN32)
Packit b099d7
# define popen _popen
Packit b099d7
# define pclose _pclose
Packit b099d7
# if defined(STAT_ZFILE)
Packit b099d7
#  include <io.h>
Packit b099d7
#  define stat _stat
Packit b099d7
#  define fstat _fstat
Packit b099d7
# endif
Packit b099d7
#endif
Packit b099d7
Packit b099d7
LFUNC(OpenReadFile, int, (char *filename, xpmData *mdata));
Packit b099d7
LFUNC(xpmDataClose, void, (xpmData *mdata));
Packit b099d7
Packit b099d7
int
Packit b099d7
XpmReadFileToImage(display, filename,
Packit b099d7
		   image_return, shapeimage_return, attributes)
Packit b099d7
    Display *display;
Packit b099d7
    char *filename;
Packit b099d7
    XImage **image_return;
Packit b099d7
    XImage **shapeimage_return;
Packit b099d7
    XpmAttributes *attributes;
Packit b099d7
{
Packit b099d7
    XpmImage image;
Packit b099d7
    XpmInfo info;
Packit b099d7
    int ErrorStatus;
Packit b099d7
    xpmData mdata;
Packit b099d7
Packit b099d7
    xpmInitXpmImage(&image);
Packit b099d7
    xpmInitXpmInfo(&info;;
Packit b099d7
Packit b099d7
    /* open file to read */
Packit b099d7
    if ((ErrorStatus = OpenReadFile(filename, &mdata)) != XpmSuccess)
Packit b099d7
	return (ErrorStatus);
Packit b099d7
Packit b099d7
    /* create the XImage from the XpmData */
Packit b099d7
    if (attributes) {
Packit b099d7
	xpmInitAttributes(attributes);
Packit b099d7
	xpmSetInfoMask(&info, attributes);
Packit b099d7
	ErrorStatus = xpmParseDataAndCreate(display, &mdata,
Packit b099d7
					    image_return, shapeimage_return,
Packit b099d7
					    &image, &info, attributes);
Packit b099d7
    } else
Packit b099d7
	ErrorStatus = xpmParseDataAndCreate(display, &mdata,
Packit b099d7
					    image_return, shapeimage_return,
Packit b099d7
					    &image, NULL, attributes);
Packit b099d7
    if (attributes) {
Packit b099d7
	if (ErrorStatus >= 0)		/* no fatal error */
Packit b099d7
	    xpmSetAttributes(attributes, &image, &info;;
Packit b099d7
	XpmFreeXpmInfo(&info;;
Packit b099d7
    }
Packit b099d7
Packit b099d7
    xpmDataClose(&mdata);
Packit b099d7
    /* free the XpmImage */
Packit b099d7
    XpmFreeXpmImage(&image);
Packit b099d7
Packit b099d7
    return (ErrorStatus);
Packit b099d7
}
Packit b099d7
Packit b099d7
int
Packit b099d7
XpmReadFileToXpmImage(filename, image, info)
Packit b099d7
    char *filename;
Packit b099d7
    XpmImage *image;
Packit b099d7
    XpmInfo *info;
Packit b099d7
{
Packit b099d7
    xpmData mdata;
Packit b099d7
    int ErrorStatus;
Packit b099d7
Packit b099d7
    /* init returned values */
Packit b099d7
    xpmInitXpmImage(image);
Packit b099d7
    xpmInitXpmInfo(info);
Packit b099d7
Packit b099d7
    /* open file to read */
Packit b099d7
    if ((ErrorStatus = OpenReadFile(filename, &mdata)) != XpmSuccess)
Packit b099d7
	return (ErrorStatus);
Packit b099d7
Packit b099d7
    /* create the XpmImage from the XpmData */
Packit b099d7
    ErrorStatus = xpmParseData(&mdata, image, info);
Packit b099d7
Packit b099d7
    xpmDataClose(&mdata);
Packit b099d7
Packit b099d7
    return (ErrorStatus);
Packit b099d7
}
Packit b099d7
Packit b099d7
/*
Packit b099d7
 * open the given file to be read as an xpmData which is returned.
Packit b099d7
 */
Packit b099d7
#ifndef NO_ZPIPE
Packit b099d7
	FILE *Xpms_popen(char *cmd, const char *type);
Packit b099d7
#else
Packit b099d7
#	define Xpms_popen popen
Packit b099d7
#endif
Packit b099d7
Packit b099d7
static int
Packit b099d7
OpenReadFile(filename, mdata)
Packit b099d7
    char *filename;
Packit b099d7
    xpmData *mdata;
Packit b099d7
{
Packit b099d7
#ifndef NO_ZPIPE
Packit b099d7
    char *compressfile, buf[(2*MAXPATHLEN) + 1];
Packit b099d7
# ifdef STAT_ZFILE
Packit b099d7
    struct stat status;
Packit b099d7
# endif
Packit b099d7
#endif
Packit b099d7
Packit b099d7
    if (!filename) {
Packit b099d7
	mdata->stream.file = (stdin);
Packit b099d7
	mdata->type = XPMFILE;
Packit b099d7
    } else {
Packit b099d7
#ifndef NO_ZPIPE
Packit b099d7
	size_t len = strlen(filename);
Packit b099d7
Packit b099d7
	if(len == 0                        ||
Packit b099d7
	   filename[len-1] == '/')
Packit b099d7
		return(XpmOpenFailed);
Packit b099d7
	if ((len > 2) && !strcmp(".Z", filename + (len - 2))) {
Packit b099d7
	    mdata->type = XPMPIPE;
Packit b099d7
	    snprintf(buf, sizeof(buf), "uncompress -c \"%s\"", filename);
Packit b099d7
	    if (!(mdata->stream.file = Xpms_popen(buf, "r")))
Packit b099d7
		return (XpmOpenFailed);
Packit b099d7
Packit b099d7
	} else if ((len > 3) && !strcmp(".gz", filename + (len - 3))) {
Packit b099d7
	    mdata->type = XPMPIPE;
Packit b099d7
	    snprintf(buf, sizeof(buf), "gunzip -qc \"%s\"", filename);
Packit b099d7
	    if (!(mdata->stream.file = Xpms_popen(buf, "r")))
Packit b099d7
		return (XpmOpenFailed);
Packit b099d7
Packit b099d7
	} else {
Packit b099d7
# ifdef STAT_ZFILE
Packit b099d7
	    if (!(compressfile = (char *) XpmMalloc(len + 4)))
Packit b099d7
		return (XpmNoMemory);
Packit b099d7
Packit b099d7
	    snprintf(compressfile, len+4, "%s.Z", filename);
Packit b099d7
	    if (!stat(compressfile, &status)) {
Packit b099d7
		snprintf(buf, sizeof(buf), "uncompress -c \"%s\"", compressfile);
Packit b099d7
		if (!(mdata->stream.file = Xpms_popen(buf, "r"))) {
Packit b099d7
		    XpmFree(compressfile);
Packit b099d7
		    return (XpmOpenFailed);
Packit b099d7
		}
Packit b099d7
		mdata->type = XPMPIPE;
Packit b099d7
	    } else {
Packit b099d7
		snprintf(compressfile, len+4, "%s.gz", filename);
Packit b099d7
		if (!stat(compressfile, &status)) {
Packit b099d7
		    snprintf(buf, sizeof(buf), "gunzip -c \"%s\"", compressfile);
Packit b099d7
		    if (!(mdata->stream.file = Xpms_popen(buf, "r"))) {
Packit b099d7
			XpmFree(compressfile);
Packit b099d7
			return (XpmOpenFailed);
Packit b099d7
		    }
Packit b099d7
		    mdata->type = XPMPIPE;
Packit b099d7
		} else {
Packit b099d7
# endif
Packit b099d7
#endif
Packit b099d7
		    if (!(mdata->stream.file = fopen(filename, "r"))) {
Packit b099d7
#if !defined(NO_ZPIPE) && defined(STAT_ZFILE)
Packit b099d7
			XpmFree(compressfile);
Packit b099d7
#endif
Packit b099d7
			return (XpmOpenFailed);
Packit b099d7
		    }
Packit b099d7
		    mdata->type = XPMFILE;
Packit b099d7
#ifndef NO_ZPIPE
Packit b099d7
# ifdef STAT_ZFILE
Packit b099d7
		}
Packit b099d7
	    }
Packit b099d7
	    XpmFree(compressfile);
Packit b099d7
# endif
Packit b099d7
	}
Packit b099d7
#endif
Packit b099d7
    }
Packit b099d7
    mdata->CommentLength = 0;
Packit b099d7
    return (XpmSuccess);
Packit b099d7
}
Packit b099d7
Packit b099d7
/*
Packit b099d7
 * close the file related to the xpmData if any
Packit b099d7
 */
Packit b099d7
static void
Packit b099d7
xpmDataClose(mdata)
Packit b099d7
    xpmData *mdata;
Packit b099d7
{
Packit b099d7
    switch (mdata->type) {
Packit b099d7
    case XPMFILE:
Packit b099d7
	if (mdata->stream.file != (stdin))
Packit b099d7
	    fclose(mdata->stream.file);
Packit b099d7
	break;
Packit b099d7
#ifndef NO_ZPIPE
Packit b099d7
    case XPMPIPE:
Packit b099d7
	fclose(mdata->stream.file);
Packit b099d7
	break;
Packit b099d7
#endif
Packit b099d7
    }
Packit b099d7
}