cxpm.c revision a966c04f
1a966c04fSmrg/* 2a966c04fSmrg * Copyright (C) 1998 Arnaud LE HORS 3a966c04fSmrg * 4a966c04fSmrg * Permission is hereby granted, free of charge, to any person obtaining a copy 5a966c04fSmrg * of this software and associated documentation files (the "Software"), to 6a966c04fSmrg * deal in the Software without restriction, including without limitation the 7a966c04fSmrg * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8a966c04fSmrg * sell copies of the Software, and to permit persons to whom the Software is 9a966c04fSmrg * furnished to do so, subject to the following conditions: 10a966c04fSmrg * 11a966c04fSmrg * The above copyright notice and this permission notice shall be included in 12a966c04fSmrg * all copies or substantial portions of the Software. 13a966c04fSmrg * 14a966c04fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15a966c04fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16a966c04fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17a966c04fSmrg * Arnaud LE HORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18a966c04fSmrg * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19a966c04fSmrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20a966c04fSmrg * 21a966c04fSmrg * Except as contained in this notice, the name of Arnaud LE HORS shall not be 22a966c04fSmrg * used in advertising or otherwise to promote the sale, use or other dealings 23a966c04fSmrg * in this Software without prior written authorization from Arnaud LE HORS. 24a966c04fSmrg */ 25a966c04fSmrg/* $XFree86: xc/extras/Xpm/cxpm/cxpm.c,v 1.2 2001/08/01 00:44:34 tsi Exp $ */ 26a966c04fSmrg 27a966c04fSmrg/*****************************************************************************\ 28a966c04fSmrg* cxpm.c: * 29a966c04fSmrg* * 30a966c04fSmrg* Check XPM File program * 31a966c04fSmrg* * 32a966c04fSmrg* Developed by Arnaud Le Hors * 33a966c04fSmrg\*****************************************************************************/ 34a966c04fSmrg 35a966c04fSmrg#define CXPMPROG 36a966c04fSmrg 37a966c04fSmrg#ifdef HAVE_CONFIG_H 38a966c04fSmrg#include <config.h> 39a966c04fSmrg#endif 40a966c04fSmrg#include "XpmI.h" 41a966c04fSmrg#ifdef USE_GETTEXT 42a966c04fSmrg#include <locale.h> 43a966c04fSmrg#include <libintl.h> 44a966c04fSmrg#else 45a966c04fSmrg#define gettext(a) (a) 46a966c04fSmrg#endif 47a966c04fSmrg 48a966c04fSmrg#undef xpmGetC 49a966c04fSmrg#define xpmGetC(data) sGetc(data, data->stream.file) 50a966c04fSmrg#define Getc sGetc 51a966c04fSmrg#define Ungetc sUngetc 52a966c04fSmrg 53a966c04fSmrg 54a966c04fSmrg/* 55a966c04fSmrg * special getc and ungetc counting read lines and characters 56a966c04fSmrg * note that 's' could stand both for "special" and "slow" ;-) 57a966c04fSmrg */ 58a966c04fSmrgstatic int 59a966c04fSmrgsGetc(data, file) 60a966c04fSmrg xpmData *data; 61a966c04fSmrg FILE *file; 62a966c04fSmrg{ 63a966c04fSmrg int c = getc(data->stream.file); 64a966c04fSmrg if (c == '\n') { 65a966c04fSmrg data->lineNum++; 66a966c04fSmrg data->charNum = 0; 67a966c04fSmrg } else { 68a966c04fSmrg data->charNum++; 69a966c04fSmrg } 70a966c04fSmrg return c; 71a966c04fSmrg} 72a966c04fSmrg 73a966c04fSmrgstatic void 74a966c04fSmrgsUngetc(data, c, file) 75a966c04fSmrg xpmData *data; 76a966c04fSmrg int c; 77a966c04fSmrg FILE *file; 78a966c04fSmrg{ 79a966c04fSmrg ungetc(c, data->stream.file); 80a966c04fSmrg if (c == '\n') { 81a966c04fSmrg data->lineNum--; 82a966c04fSmrg data->charNum = 0; 83a966c04fSmrg } else { 84a966c04fSmrg data->charNum--; 85a966c04fSmrg } 86a966c04fSmrg} 87a966c04fSmrg 88a966c04fSmrg/* include all the code we need (yeah, I know, quite ugly...) */ 89a966c04fSmrg#include "data.c" 90a966c04fSmrg#include "parse.c" 91a966c04fSmrg#include "RdFToI.c" /* only for OpenReadFile and xpmDataClose */ 92a966c04fSmrg#include "hashtab.c" 93a966c04fSmrg#include "misc.c" 94a966c04fSmrg#include "Attrib.c" 95a966c04fSmrg#include "Image.c" 96a966c04fSmrg 97a966c04fSmrgvoid 98a966c04fSmrgErrorMessage( 99a966c04fSmrg int ErrorStatus, 100a966c04fSmrg xpmData *data) 101a966c04fSmrg 102a966c04fSmrg{ 103a966c04fSmrg char *error = NULL; 104a966c04fSmrg 105a966c04fSmrg switch (ErrorStatus) { 106a966c04fSmrg case XpmSuccess: 107a966c04fSmrg return; 108a966c04fSmrg case XpmOpenFailed: 109a966c04fSmrg /* L10N_Comments : Error produced when filename does not exist 110a966c04fSmrg or insufficient permissions to open (i.e. cxpm /no/such/file ) */ 111a966c04fSmrg error = gettext("Cannot open file"); 112a966c04fSmrg break; 113a966c04fSmrg case XpmFileInvalid: 114a966c04fSmrg /* L10N_Comments : Error produced when filename can be read, but 115a966c04fSmrg is not an XPM file (i.e. cxpm /dev/null ) */ 116a966c04fSmrg error = gettext("Invalid XPM file"); 117a966c04fSmrg break; 118a966c04fSmrg case XpmNoMemory: 119a966c04fSmrg /* L10N_Comments : Error produced when filename can be read, but 120a966c04fSmrg is too big for memory 121a966c04fSmrg (i.e. limit datasize 32 ; cxpm /usr/dt/backdrops/Crochet.pm ) */ 122a966c04fSmrg error = gettext("Not enough memory"); 123a966c04fSmrg break; 124a966c04fSmrg case XpmColorFailed: 125a966c04fSmrg /* L10N_Comments : Error produced when filename can be read, but 126a966c04fSmrg contains an invalid color specification (need to create test case)*/ 127a966c04fSmrg error = gettext("Failed to parse color"); 128a966c04fSmrg break; 129a966c04fSmrg } 130a966c04fSmrg 131a966c04fSmrg if (error) { 132a966c04fSmrg /* L10N_Comments : Wrapper around above Xpm errors - %s is 133a966c04fSmrg replaced with the contents of the error message retrieved above */ 134a966c04fSmrg fprintf(stderr, gettext("Xpm Error: %s.\n"), error); 135a966c04fSmrg if (ErrorStatus == XpmFileInvalid && data) 136a966c04fSmrg /* L10N_Comments : Error produced when filename can be read, but 137a966c04fSmrg is not an XPM file (i.e. cxpm /dev/null ) */ 138a966c04fSmrg fprintf(stderr, gettext("Error found line %d near character %d\n"), 139a966c04fSmrg data->lineNum + 1, 140a966c04fSmrg data->charNum + 1); 141a966c04fSmrg exit(1); 142a966c04fSmrg } 143a966c04fSmrg} 144a966c04fSmrg 145a966c04fSmrgint 146a966c04fSmrgmain(argc, argv) 147a966c04fSmrg int argc; 148a966c04fSmrg char **argv; 149a966c04fSmrg{ 150a966c04fSmrg XpmImage image; 151a966c04fSmrg char *filename; 152a966c04fSmrg int ErrorStatus; 153a966c04fSmrg xpmData data; 154a966c04fSmrg 155a966c04fSmrg#ifdef USE_GETTEXT 156a966c04fSmrg setlocale(LC_ALL,""); 157a966c04fSmrg bindtextdomain("cxpm",LOCALEDIR); 158a966c04fSmrg textdomain("cxpm"); 159a966c04fSmrg#endif 160a966c04fSmrg 161a966c04fSmrg if (argc > 1) { 162a966c04fSmrg if (!strcmp(argv[1], "-?") || !strncmp(argv[1], "-h", 2)) { 163a966c04fSmrg /* L10N_Comments : Usage message produced by running cxpm -h 164a966c04fSmrg %s will be replaced by argv[0] (program name) */ 165a966c04fSmrg fprintf(stderr, gettext("Usage: %s [filename]\n"), argv[0]); 166a966c04fSmrg exit(1); 167a966c04fSmrg } 168a966c04fSmrg filename = argv[1]; 169a966c04fSmrg } else { 170a966c04fSmrg filename = NULL; 171a966c04fSmrg } 172a966c04fSmrg 173a966c04fSmrg xpmInitXpmImage(&image); 174a966c04fSmrg 175a966c04fSmrg if ((ErrorStatus = OpenReadFile(filename, &data)) != XpmSuccess) 176a966c04fSmrg ErrorMessage(ErrorStatus, NULL); 177a966c04fSmrg 178a966c04fSmrg ErrorStatus = xpmParseData(&data, &image, NULL); 179a966c04fSmrg ErrorMessage(ErrorStatus, &data); 180a966c04fSmrg 181a966c04fSmrg xpmDataClose(&data); 182a966c04fSmrg XpmFreeXpmImage(&image); 183a966c04fSmrg 184a966c04fSmrg exit(0); 185a966c04fSmrg} 186