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