PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/code_oth/error.c

http://research-code-base-animesh.googlecode.com/
C | 270 lines | 164 code | 35 blank | 71 comment | 33 complexity | abb641152095760527a7615a56dfa604 MD5 | raw file
  1. /* -----------------------------------------------------------------------------
  2. * error.c
  3. *
  4. * Error handling functions. These are used to issue warnings and
  5. * error messages.
  6. *
  7. * Author(s) : David Beazley (beazley@cs.uchicago.edu)
  8. *
  9. * Copyright (C) 1999-2000. The University of Chicago
  10. * See the file LICENSE for information on usage and redistribution.
  11. * ----------------------------------------------------------------------------- */
  12. #include "swig.h"
  13. #include <stdarg.h>
  14. #include <ctype.h>
  15. char cvsroot_error_c[] = "$Header: /cvsroot/swig/SWIG/Source/Swig/error.c,v 1.9 2004/10/18 01:50:46 marcelomatus Exp $";
  16. /* -----------------------------------------------------------------------------
  17. * Commentary on the warning filter.
  18. *
  19. * The warning filter is a string of numbers prefaced by (-) or (+) to
  20. * indicate whether or not a warning message is displayed. For example:
  21. *
  22. * "-304-201-140+210+201"
  23. *
  24. * The filter string is scanned left to right and the first occurrence
  25. * of a warning number is used to determine printing behavior.
  26. *
  27. * The same number may appear more than once in the string. For example, in the
  28. * above string, "201" appears twice. This simply means that warning 201
  29. * was disabled after it was previously enabled. This may only be temporary
  30. * setting--the first number may be removed later in which case the warning
  31. * is reenabled.
  32. * ----------------------------------------------------------------------------- */
  33. #if defined(_WIN32)
  34. # define DEFAULT_ERROR_MSG_FORMAT EMF_MICROSOFT
  35. #else
  36. # define DEFAULT_ERROR_MSG_FORMAT EMF_STANDARD
  37. #endif
  38. static ErrorMessageFormat msg_format = DEFAULT_ERROR_MSG_FORMAT;
  39. static int silence = 0; /* Silent operation */
  40. static String *filter = 0; /* Warning filter */
  41. static int warnall = 0;
  42. static int nwarning = 0;
  43. static int nerrors = 0;
  44. static int init_fmt = 0;
  45. static char wrn_wnum_fmt[64];
  46. static char wrn_nnum_fmt[64];
  47. static char err_line_fmt[64];
  48. static char err_eof_fmt[64];
  49. static String *format_filename(const String_or_char *filename);
  50. /* -----------------------------------------------------------------------------
  51. * Swig_warning()
  52. *
  53. * Issue a warning message
  54. * ----------------------------------------------------------------------------- */
  55. void
  56. Swig_warning(int wnum, const String_or_char *filename, int line, const char *fmt, ...) {
  57. String *out;
  58. char *msg;
  59. int wrn = 1;
  60. va_list ap;
  61. if (silence) return;
  62. if (!init_fmt) Swig_error_msg_format(DEFAULT_ERROR_MSG_FORMAT);
  63. va_start(ap,fmt);
  64. out = NewString("");
  65. vPrintf(out,fmt,ap);
  66. {
  67. char temp[64], *t;
  68. t = temp;
  69. msg = Char(out);
  70. while (isdigit((int) *msg)) {
  71. *(t++) = *(msg++);
  72. }
  73. if (t != temp) {
  74. msg++;
  75. *t = 0;
  76. wnum = atoi(temp);
  77. }
  78. }
  79. /* Check in the warning filter */
  80. if (filter) {
  81. char temp[32];
  82. char *c;
  83. char *f = Char(filter);
  84. sprintf(temp,"%d",wnum);
  85. while(*f != '\0' && (c = strstr(f,temp))) {
  86. if (*(c-1) == '-') wrn = 0; /* Warning disabled */
  87. if (*(c-1) == '+') wrn = 1; /* Warning enabled */
  88. f += strlen(temp);
  89. }
  90. }
  91. if (warnall || wrn) {
  92. String *formatted_filename = format_filename(filename);
  93. if (wnum) {
  94. Printf(stderr, wrn_wnum_fmt, formatted_filename, line, wnum);
  95. } else {
  96. Printf(stderr, wrn_nnum_fmt, formatted_filename, line);
  97. }
  98. Printf(stderr,"%s",msg);
  99. nwarning++;
  100. Delete(formatted_filename);
  101. }
  102. Delete(out);
  103. va_end(ap);
  104. }
  105. /* -----------------------------------------------------------------------------
  106. * Swig_error()
  107. *
  108. * Issue an error message
  109. * ----------------------------------------------------------------------------- */
  110. void
  111. Swig_error(const String_or_char *filename, int line, const char *fmt, ...) {
  112. va_list ap;
  113. String *formatted_filename = NULL;
  114. if (silence) return;
  115. if (!init_fmt) Swig_error_msg_format(DEFAULT_ERROR_MSG_FORMAT);
  116. va_start(ap,fmt);
  117. formatted_filename = format_filename(filename);
  118. if (line > 0) {
  119. Printf(stderr, err_line_fmt, formatted_filename, line);
  120. } else {
  121. Printf(stderr, err_eof_fmt, formatted_filename);
  122. }
  123. vPrintf(stderr,fmt,ap);
  124. va_end(ap);
  125. nerrors++;
  126. Delete(formatted_filename);
  127. }
  128. /* -----------------------------------------------------------------------------
  129. * Swig_error_count()
  130. *
  131. * Returns number of errors received.
  132. * ----------------------------------------------------------------------------- */
  133. int
  134. Swig_error_count(void) {
  135. return nerrors;
  136. }
  137. /* -----------------------------------------------------------------------------
  138. * Swig_error_silent()
  139. *
  140. * Set silent flag
  141. * ----------------------------------------------------------------------------- */
  142. void
  143. Swig_error_silent(int s) {
  144. silence = s;
  145. }
  146. /* -----------------------------------------------------------------------------
  147. * Swig_warnfilter()
  148. *
  149. * Takes a comma separate list of warning numbers and puts in the filter.
  150. * ----------------------------------------------------------------------------- */
  151. void
  152. Swig_warnfilter(const String_or_char *wlist, int add) {
  153. char *c;
  154. String *s;
  155. if (!filter) filter = NewString("");
  156. s = NewString(wlist);
  157. c = Char(s);
  158. c = strtok(c,", ");
  159. while (c) {
  160. if (isdigit((int) *c) || (*c == '+') || (*c == '-')) {
  161. if (add) {
  162. Insert(filter,0,c);
  163. if (isdigit((int) *c)) {
  164. Insert(filter,0,"-");
  165. }
  166. } else {
  167. char temp[32];
  168. if (isdigit((int) *c)) {
  169. sprintf(temp,"-%s",c);
  170. } else {
  171. strcpy(temp,c);
  172. }
  173. Replace(filter,temp,"", DOH_REPLACE_FIRST);
  174. }
  175. }
  176. c = strtok(NULL,", ");
  177. }
  178. Delete(s);
  179. }
  180. void
  181. Swig_warnall(void) {
  182. warnall = 1;
  183. }
  184. /* -----------------------------------------------------------------------------
  185. * Swig_warn_count()
  186. *
  187. * Return the number of warnings
  188. * ----------------------------------------------------------------------------- */
  189. int
  190. Swig_warn_count(void) {
  191. return nwarning;
  192. }
  193. /* -----------------------------------------------------------------------------
  194. * Swig_error_msg_format()
  195. *
  196. * Set the type of error/warning message display
  197. * ----------------------------------------------------------------------------- */
  198. void
  199. Swig_error_msg_format(ErrorMessageFormat format) {
  200. const char* error = "Error";
  201. const char* warning = "Warning";
  202. const char* fmt_eof = 0;
  203. const char* fmt_line = 0;
  204. /* here 'format' could be directly a string instead of an enum, but
  205. by now a switch is used to translated into one. */
  206. switch (format) {
  207. case EMF_MICROSOFT:
  208. fmt_line = "%s(%d)";
  209. fmt_eof = "%s(999999)"; /* Is there a special character for EOF? Just use a large number. */
  210. break;
  211. case EMF_STANDARD:
  212. default:
  213. fmt_line = "%s:%d";
  214. fmt_eof = "%s:EOF";
  215. }
  216. sprintf(wrn_wnum_fmt, "%s: %s(%%d): ", fmt_line, warning);
  217. sprintf(wrn_nnum_fmt, "%s: %s: ", fmt_line, warning);
  218. sprintf(err_line_fmt, "%s: %s: ", fmt_line, error);
  219. sprintf(err_eof_fmt, "%s: %s: ", fmt_eof, error);
  220. msg_format = format;
  221. init_fmt = 1;
  222. }
  223. /* -----------------------------------------------------------------------------
  224. * format_filename()
  225. *
  226. * Remove double backslashes in Windows filename paths for display
  227. * ----------------------------------------------------------------------------- */
  228. static String *
  229. format_filename(const String_or_char *filename) {
  230. String *formatted_filename = NewString(filename);
  231. #if defined(_WIN32)
  232. Replaceall(formatted_filename,"\\\\","\\");
  233. #endif
  234. return formatted_filename;
  235. }