cxpm.c revision 2e2dd055
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 592e2dd055SmrgsGetc(xpmData *data, FILE *file) 60a966c04fSmrg{ 61a966c04fSmrg int c = getc(data->stream.file); 62a966c04fSmrg if (c == '\n') { 63a966c04fSmrg data->lineNum++; 64a966c04fSmrg data->charNum = 0; 65a966c04fSmrg } else { 66a966c04fSmrg data->charNum++; 67a966c04fSmrg } 68a966c04fSmrg return c; 69a966c04fSmrg} 70a966c04fSmrg 71a966c04fSmrgstatic void 722e2dd055SmrgsUngetc(xpmData *data, int c, FILE *file) 73a966c04fSmrg{ 74a966c04fSmrg ungetc(c, data->stream.file); 75a966c04fSmrg if (c == '\n') { 76a966c04fSmrg data->lineNum--; 77a966c04fSmrg data->charNum = 0; 78a966c04fSmrg } else { 79a966c04fSmrg data->charNum--; 80a966c04fSmrg } 81a966c04fSmrg} 82a966c04fSmrg 83a966c04fSmrg/* include all the code we need (yeah, I know, quite ugly...) */ 84a966c04fSmrg#include "data.c" 85a966c04fSmrg#include "parse.c" 86a966c04fSmrg#include "RdFToI.c" /* only for OpenReadFile and xpmDataClose */ 87a966c04fSmrg#include "hashtab.c" 88a966c04fSmrg#include "misc.c" 89a966c04fSmrg#include "Attrib.c" 90a966c04fSmrg#include "Image.c" 91a966c04fSmrg 922e2dd055Smrgstatic void 93a966c04fSmrgErrorMessage( 94a966c04fSmrg int ErrorStatus, 95a966c04fSmrg xpmData *data) 96a966c04fSmrg 97a966c04fSmrg{ 98a966c04fSmrg char *error = NULL; 99a966c04fSmrg 100a966c04fSmrg switch (ErrorStatus) { 101a966c04fSmrg case XpmSuccess: 102a966c04fSmrg return; 103a966c04fSmrg case XpmOpenFailed: 104a966c04fSmrg /* L10N_Comments : Error produced when filename does not exist 105a966c04fSmrg or insufficient permissions to open (i.e. cxpm /no/such/file ) */ 106a966c04fSmrg error = gettext("Cannot open file"); 107a966c04fSmrg break; 108a966c04fSmrg case XpmFileInvalid: 109a966c04fSmrg /* L10N_Comments : Error produced when filename can be read, but 110a966c04fSmrg is not an XPM file (i.e. cxpm /dev/null ) */ 111a966c04fSmrg error = gettext("Invalid XPM file"); 112a966c04fSmrg break; 113a966c04fSmrg case XpmNoMemory: 114a966c04fSmrg /* L10N_Comments : Error produced when filename can be read, but 115a966c04fSmrg is too big for memory 116a966c04fSmrg (i.e. limit datasize 32 ; cxpm /usr/dt/backdrops/Crochet.pm ) */ 117a966c04fSmrg error = gettext("Not enough memory"); 118a966c04fSmrg break; 119a966c04fSmrg case XpmColorFailed: 120a966c04fSmrg /* L10N_Comments : Error produced when filename can be read, but 121a966c04fSmrg contains an invalid color specification (need to create test case)*/ 122a966c04fSmrg error = gettext("Failed to parse color"); 123a966c04fSmrg break; 124a966c04fSmrg } 125a966c04fSmrg 126a966c04fSmrg if (error) { 127a966c04fSmrg /* L10N_Comments : Wrapper around above Xpm errors - %s is 128a966c04fSmrg replaced with the contents of the error message retrieved above */ 129a966c04fSmrg fprintf(stderr, gettext("Xpm Error: %s.\n"), error); 130a966c04fSmrg if (ErrorStatus == XpmFileInvalid && data) 131a966c04fSmrg /* L10N_Comments : Error produced when filename can be read, but 132a966c04fSmrg is not an XPM file (i.e. cxpm /dev/null ) */ 133a966c04fSmrg fprintf(stderr, gettext("Error found line %d near character %d\n"), 134a966c04fSmrg data->lineNum + 1, 135a966c04fSmrg data->charNum + 1); 136a966c04fSmrg exit(1); 137a966c04fSmrg } 138a966c04fSmrg} 139a966c04fSmrg 140a966c04fSmrgint 1412e2dd055Smrgmain(int argc, char **argv) 142a966c04fSmrg{ 143a966c04fSmrg XpmImage image; 144a966c04fSmrg char *filename; 145a966c04fSmrg int ErrorStatus; 146a966c04fSmrg xpmData data; 147a966c04fSmrg 148a966c04fSmrg#ifdef USE_GETTEXT 149a966c04fSmrg setlocale(LC_ALL,""); 150a966c04fSmrg bindtextdomain("cxpm",LOCALEDIR); 151a966c04fSmrg textdomain("cxpm"); 152a966c04fSmrg#endif 153a966c04fSmrg 154a966c04fSmrg if (argc > 1) { 155a966c04fSmrg if (!strcmp(argv[1], "-?") || !strncmp(argv[1], "-h", 2)) { 156a966c04fSmrg /* L10N_Comments : Usage message produced by running cxpm -h 157a966c04fSmrg %s will be replaced by argv[0] (program name) */ 158a966c04fSmrg fprintf(stderr, gettext("Usage: %s [filename]\n"), argv[0]); 159a966c04fSmrg exit(1); 160a966c04fSmrg } 161a966c04fSmrg filename = argv[1]; 162a966c04fSmrg } else { 163a966c04fSmrg filename = NULL; 164a966c04fSmrg } 165a966c04fSmrg 166a966c04fSmrg xpmInitXpmImage(&image); 167a966c04fSmrg 168a966c04fSmrg if ((ErrorStatus = OpenReadFile(filename, &data)) != XpmSuccess) 169a966c04fSmrg ErrorMessage(ErrorStatus, NULL); 170a966c04fSmrg 171a966c04fSmrg ErrorStatus = xpmParseData(&data, &image, NULL); 172a966c04fSmrg ErrorMessage(ErrorStatus, &data); 173a966c04fSmrg 174a966c04fSmrg xpmDataClose(&data); 175a966c04fSmrg XpmFreeXpmImage(&image); 176a966c04fSmrg 177a966c04fSmrg exit(0); 178a966c04fSmrg} 179