PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/cxpm/cxpm.c

#
C | 177 lines | 106 code | 20 blank | 51 comment | 17 complexity | 3872a3e103deef30c80bca283771738d MD5 | raw file
  1. /*
  2. * Copyright (C) 1998 Arnaud LE HORS
  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. * Arnaud LE HORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN 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 Arnaud LE HORS 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 Arnaud LE HORS.
  24. */
  25. /*****************************************************************************\
  26. * cxpm.c: *
  27. * *
  28. * Check XPM File program *
  29. * *
  30. * Developed by Arnaud Le Hors *
  31. \*****************************************************************************/
  32. #define CXPMPROG
  33. #ifdef HAVE_CONFIG_H
  34. #include <config.h>
  35. #endif
  36. #include "XpmI.h"
  37. #ifdef USE_GETTEXT
  38. #include <locale.h>
  39. #include <libintl.h>
  40. #else
  41. #define gettext(a) (a)
  42. #endif
  43. #undef xpmGetC
  44. #define xpmGetC(data) sGetc(data, data->stream.file)
  45. #define Getc sGetc
  46. #define Ungetc sUngetc
  47. /*
  48. * special getc and ungetc counting read lines and characters
  49. * note that 's' could stand both for "special" and "slow" ;-)
  50. */
  51. static int
  52. sGetc(xpmData *data, FILE *file)
  53. {
  54. int c = getc(data->stream.file);
  55. if (c == '\n') {
  56. data->lineNum++;
  57. data->charNum = 0;
  58. } else {
  59. data->charNum++;
  60. }
  61. return c;
  62. }
  63. static void
  64. sUngetc(xpmData *data, int c, FILE *file)
  65. {
  66. ungetc(c, data->stream.file);
  67. if (c == '\n') {
  68. data->lineNum--;
  69. data->charNum = 0;
  70. } else {
  71. data->charNum--;
  72. }
  73. }
  74. /* include all the code we need (yeah, I know, quite ugly...) */
  75. #include "data.c"
  76. #include "parse.c"
  77. #include "RdFToI.c" /* only for OpenReadFile and xpmDataClose */
  78. #include "hashtab.c"
  79. #include "misc.c"
  80. #include "Attrib.c"
  81. #include "Image.c"
  82. static void
  83. ErrorMessage(
  84. int ErrorStatus,
  85. xpmData *data)
  86. {
  87. char *error = NULL;
  88. switch (ErrorStatus) {
  89. case XpmSuccess:
  90. return;
  91. case XpmOpenFailed:
  92. /* L10N_Comments : Error produced when filename does not exist
  93. or insufficient permissions to open (i.e. cxpm /no/such/file ) */
  94. error = gettext("Cannot open file");
  95. break;
  96. case XpmFileInvalid:
  97. /* L10N_Comments : Error produced when filename can be read, but
  98. is not an XPM file (i.e. cxpm /dev/null ) */
  99. error = gettext("Invalid XPM file");
  100. break;
  101. case XpmNoMemory:
  102. /* L10N_Comments : Error produced when filename can be read, but
  103. is too big for memory
  104. (i.e. limit datasize 32 ; cxpm /usr/dt/backdrops/Crochet.pm ) */
  105. error = gettext("Not enough memory");
  106. break;
  107. case XpmColorFailed:
  108. /* L10N_Comments : Error produced when filename can be read, but
  109. contains an invalid color specification (need to create test case)*/
  110. error = gettext("Failed to parse color");
  111. break;
  112. }
  113. if (error) {
  114. /* L10N_Comments : Wrapper around above Xpm errors - %s is
  115. replaced with the contents of the error message retrieved above */
  116. fprintf(stderr, gettext("Xpm Error: %s.\n"), error);
  117. if (ErrorStatus == XpmFileInvalid && data)
  118. /* L10N_Comments : Error produced when filename can be read, but
  119. is not an XPM file (i.e. cxpm /dev/null ) */
  120. fprintf(stderr, gettext("Error found line %d near character %d\n"),
  121. data->lineNum + 1,
  122. data->charNum + 1);
  123. exit(1);
  124. }
  125. }
  126. int
  127. main(int argc, char **argv)
  128. {
  129. XpmImage image;
  130. char *filename;
  131. int ErrorStatus;
  132. xpmData data;
  133. #ifdef USE_GETTEXT
  134. setlocale(LC_ALL,"");
  135. bindtextdomain("cxpm",LOCALEDIR);
  136. textdomain("cxpm");
  137. #endif
  138. if (argc > 1) {
  139. if (!strcmp(argv[1], "-?") || !strncmp(argv[1], "-h", 2)) {
  140. /* L10N_Comments : Usage message produced by running cxpm -h
  141. %s will be replaced by argv[0] (program name) */
  142. fprintf(stderr, gettext("Usage: %s [filename]\n"), argv[0]);
  143. exit(1);
  144. }
  145. filename = argv[1];
  146. } else {
  147. filename = NULL;
  148. }
  149. xpmInitXpmImage(&image);
  150. if ((ErrorStatus = OpenReadFile(filename, &data)) != XpmSuccess)
  151. ErrorMessage(ErrorStatus, NULL);
  152. ErrorStatus = xpmParseData(&data, &image, NULL);
  153. ErrorMessage(ErrorStatus, &data);
  154. xpmDataClose(&data);
  155. XpmFreeXpmImage(&image);
  156. exit(0);
  157. }