icalerror.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*======================================================================
  2. FILE: icalerror.h
  3. CREATOR: eric 09 May 1999
  4. (C) COPYRIGHT 2000, Eric Busboom <eric@softwarestudio.org>
  5. http://www.softwarestudio.org
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of either:
  8. The LGPL as published by the Free Software Foundation, version
  9. 2.1, available at: http://www.gnu.org/licenses/lgpl-2.1.html
  10. Or:
  11. The Mozilla Public License Version 1.0. You may obtain a copy of
  12. the License at http://www.mozilla.org/MPL/
  13. The original code is icalerror.h
  14. ======================================================================*/
  15. #ifndef ICALERROR_H
  16. #define ICALERROR_H
  17. #include "libical_ical_export.h"
  18. #include <assert.h>
  19. #include <stdio.h>
  20. #define ICAL_SETERROR_ISFUNC
  21. /** This routine is called before any error is triggered. It is called by
  22. icalerror_set_errno, so it does not appear in all of the macros below */
  23. LIBICAL_ICAL_EXPORT void icalerror_stop_here(void);
  24. LIBICAL_ICAL_EXPORT void icalerror_crash_here(void);
  25. typedef enum icalerrorenum
  26. {
  27. ICAL_NO_ERROR = 0,
  28. ICAL_BADARG_ERROR,
  29. ICAL_NEWFAILED_ERROR,
  30. ICAL_ALLOCATION_ERROR,
  31. ICAL_MALFORMEDDATA_ERROR,
  32. ICAL_PARSE_ERROR,
  33. ICAL_INTERNAL_ERROR, /* Like assert --internal consist. prob */
  34. ICAL_FILE_ERROR,
  35. ICAL_USAGE_ERROR,
  36. ICAL_UNIMPLEMENTED_ERROR,
  37. ICAL_UNKNOWN_ERROR /* Used for problems in input to icalerror_strerror() */
  38. } icalerrorenum;
  39. LIBICAL_ICAL_EXPORT icalerrorenum *icalerrno_return(void);
  40. #define icalerrno (*(icalerrno_return()))
  41. /** If true, libicu aborts after a call to icalerror_set_error
  42. *
  43. * @warning NOT THREAD SAFE -- recommended that you do not change
  44. * this in a multithreaded program.
  45. */
  46. LIBICAL_ICAL_EXPORT void icalerror_set_errors_are_fatal(int fatal);
  47. LIBICAL_ICAL_EXPORT int icalerror_get_errors_are_fatal(void);
  48. /* Warning messages */
  49. #ifdef __GNUC__ca
  50. #define icalerror_warn(message) \
  51. {fprintf(stderr, "%s(), %s:%d: %s\n", __FUNCTION__, __FILE__, __LINE__, message);}
  52. #else /* __GNU_C__ */
  53. #define icalerror_warn(message) \
  54. {fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, message);}
  55. #endif /* __GNU_C__ */
  56. LIBICAL_ICAL_EXPORT void icalerror_clear_errno(void);
  57. LIBICAL_ICAL_EXPORT void _icalerror_set_errno(icalerrorenum);
  58. /* Make an individual error fatal or non-fatal. */
  59. typedef enum icalerrorstate
  60. {
  61. ICAL_ERROR_FATAL, /* Not fatal */
  62. ICAL_ERROR_NONFATAL, /* Fatal */
  63. ICAL_ERROR_DEFAULT, /* Use the value of icalerror_errors_are_fatal */
  64. ICAL_ERROR_UNKNOWN /* Asked state for an unknown error type */
  65. } icalerrorstate;
  66. LIBICAL_ICAL_EXPORT const char *icalerror_strerror(icalerrorenum e);
  67. LIBICAL_ICAL_EXPORT const char *icalerror_perror(void);
  68. LIBICAL_ICAL_EXPORT void ical_bt(void);
  69. LIBICAL_ICAL_EXPORT void icalerror_set_error_state(icalerrorenum error, icalerrorstate);
  70. LIBICAL_ICAL_EXPORT icalerrorstate icalerror_get_error_state(icalerrorenum error);
  71. LIBICAL_ICAL_EXPORT icalerrorenum icalerror_error_from_string(const char *str);
  72. #if !defined(ICAL_SETERROR_ISFUNC)
  73. #define icalerror_set_errno(x) \
  74. icalerrno = x; \
  75. if(icalerror_get_error_state(x) == ICAL_ERROR_FATAL || \
  76. (icalerror_get_error_state(x) == ICAL_ERROR_DEFAULT && \
  77. icalerror_get_errors_are_fatal() == 1)){ \
  78. icalerror_warn(icalerror_strerror(x)); \
  79. ical_bt(); \
  80. assert(0); \
  81. } }
  82. #else
  83. LIBICAL_ICAL_EXPORT void icalerror_set_errno(icalerrorenum x);
  84. #endif
  85. #if !defined(ICAL_ERRORS_ARE_FATAL)
  86. #define ICAL_ERRORS_ARE_FATAL 0
  87. #endif
  88. #if ICAL_ERRORS_ARE_FATAL == 1
  89. #undef NDEBUG
  90. #endif
  91. #define icalerror_check_value_type(value,type);
  92. #define icalerror_check_property_type(value,type);
  93. #define icalerror_check_parameter_type(value,type);
  94. #define icalerror_check_component_type(value,type);
  95. /* Assert with a message */
  96. #if ICAL_ERRORS_ARE_FATAL == 1
  97. #ifdef __GNUC__
  98. #define icalerror_assert(test,message) \
  99. if (!(test)) { \
  100. fprintf(stderr, "%s(), %s:%d: %s\n", __FUNCTION__, __FILE__, __LINE__, message); \
  101. icalerror_stop_here(); \
  102. abort();}
  103. #else /*__GNUC__*/
  104. #define icalerror_assert(test,message) \
  105. if (!(test)) { \
  106. fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, message); \
  107. icalerror_stop_here(); \
  108. abort();}
  109. #endif /*__GNUC__*/
  110. #else /* ICAL_ERRORS_ARE_FATAL */
  111. #define icalerror_assert(test,message)
  112. #endif /* ICAL_ERRORS_ARE_FATAL */
  113. /* Check & abort if check fails */
  114. #define icalerror_check_arg(test,arg) \
  115. if (!(test)) { \
  116. icalerror_set_errno(ICAL_BADARG_ERROR); \
  117. }
  118. /* Check & return void if check fails*/
  119. #define icalerror_check_arg_rv(test,arg) \
  120. if (!(test)) { \
  121. icalerror_set_errno(ICAL_BADARG_ERROR); \
  122. return; \
  123. }
  124. /* Check & return 0 if check fails*/
  125. #define icalerror_check_arg_rz(test,arg) \
  126. if (!(test)) { \
  127. icalerror_set_errno(ICAL_BADARG_ERROR); \
  128. return 0; \
  129. }
  130. /* Check & return an error if check fails*/
  131. #define icalerror_check_arg_re(test,arg,error) \
  132. if (!(test)) { \
  133. icalerror_stop_here(); \
  134. assert(0); \
  135. return error; \
  136. }
  137. /* Check & return something*/
  138. #define icalerror_check_arg_rx(test,arg,x) \
  139. if (!(test)) { \
  140. icalerror_set_errno(ICAL_BADARG_ERROR); \
  141. return x; \
  142. }
  143. /* String interfaces to set an error to NONFATAL and restore it to its original value */
  144. LIBICAL_ICAL_EXPORT icalerrorstate icalerror_supress(const char *error);
  145. LIBICAL_ICAL_EXPORT void icalerror_restore(const char *error, icalerrorstate es);
  146. #endif /* !ICALERROR_H */