Back to index

lightning-sunbird  0.9+nobinonly
icalerror.c
Go to the documentation of this file.
00001 /* -*- Mode: C -*-
00002   ======================================================================
00003   FILE: icalerror.c
00004   CREATOR: eric 16 May 1999
00005   
00006   $Id: icalerror.c,v 1.17 2002/10/09 20:48:08 acampi Exp $
00007   $Locker:  $
00008     
00009 
00010  (C) COPYRIGHT 2000, Eric Busboom, http://www.softwarestudio.org
00011 
00012  This program is free software; you can redistribute it and/or modify
00013  it under the terms of either: 
00014 
00015     The LGPL as published by the Free Software Foundation, version
00016     2.1, available at: http://www.fsf.org/copyleft/lesser.html
00017 
00018   Or:
00019 
00020     The Mozilla Public License Version 1.0. You may obtain a copy of
00021     the License at http://www.mozilla.org/MPL/
00022 
00023   The original code is icalerror.c
00024 
00025  ======================================================================*/
00026 
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include "config.h"
00030 #endif
00031 
00032 #include <stdlib.h>         /* for malloc() */
00033 #include <string.h>         /* for strcmp */
00034 #include "icalerror.h"
00035 
00036 #ifdef HAVE_PTHREAD
00037 #include <pthread.h>
00038 
00039 static pthread_key_t  icalerrno_key;
00040 static pthread_once_t icalerrno_key_once = PTHREAD_ONCE_INIT;
00041 
00042 static void icalerrno_destroy(void* buf) {
00043   free(buf);
00044   pthread_setspecific(icalerrno_key, NULL);
00045 }
00046 
00047 static void icalerrno_key_alloc(void) {
00048   pthread_key_create(&icalerrno_key, icalerrno_destroy);
00049 }
00050 
00051 icalerrorenum *icalerrno_return(void) {
00052   icalerrorenum *_errno;
00053 
00054   pthread_once(&icalerrno_key_once, icalerrno_key_alloc);
00055   
00056   _errno = (icalerrorenum*) pthread_getspecific(icalerrno_key);
00057 
00058   if (!_errno) {
00059     _errno = malloc(sizeof(icalerrorenum));
00060     *_errno = ICAL_NO_ERROR;
00061     pthread_setspecific(icalerrno_key, _errno);
00062   }
00063   return _errno;
00064 }
00065 
00066 #else
00067 
00068 static icalerrorenum icalerrno_storage = ICAL_NO_ERROR;
00069 
00070 icalerrorenum *icalerrno_return(void) {
00071    return &icalerrno_storage;
00072 }
00073 
00074 #endif
00075 
00076 
00077 static int foo;
00078 
00079 void icalerror_stop_here(void)
00080 {
00081     foo++; /* Keep optimizers from removing routine */
00082 }
00083 
00084 void icalerror_crash_here(void)
00085 {
00086     int *p=0;
00087     *p = 1;
00088 
00089     assert( *p);
00090 }
00091 
00092 #ifdef ICAL_SETERROR_ISFUNC
00093 void icalerror_set_errno(icalerrorenum x) 
00094 {
00095     icalerrno = x; 
00096     if(icalerror_get_error_state(x)==ICAL_ERROR_FATAL || 
00097        (icalerror_get_error_state(x)==ICAL_ERROR_DEFAULT && 
00098         icalerror_errors_are_fatal == 1 )){ 
00099         icalerror_warn(icalerror_strerror(x)); 
00100         assert(0); 
00101     } 
00102 
00103 }
00104 #endif 
00105 
00106 void icalerror_clear_errno() {
00107     
00108     icalerrno = ICAL_NO_ERROR;
00109 }
00110 
00111 #ifdef ICAL_ERRORS_ARE_FATAL
00112 int icalerror_errors_are_fatal = 1;
00113 #else
00114 int icalerror_errors_are_fatal = 0;
00115 #endif
00116 
00117 struct icalerror_state {
00118     icalerrorenum error;
00119     icalerrorstate state; 
00120 };
00121 
00122 static struct icalerror_state error_state_map[] = 
00123 { 
00124     { ICAL_BADARG_ERROR,ICAL_ERROR_DEFAULT},
00125     { ICAL_NEWFAILED_ERROR,ICAL_ERROR_DEFAULT},
00126     { ICAL_ALLOCATION_ERROR,ICAL_ERROR_DEFAULT},
00127     { ICAL_MALFORMEDDATA_ERROR,ICAL_ERROR_DEFAULT}, 
00128     { ICAL_PARSE_ERROR,ICAL_ERROR_DEFAULT},
00129     { ICAL_INTERNAL_ERROR,ICAL_ERROR_DEFAULT}, 
00130     { ICAL_FILE_ERROR,ICAL_ERROR_DEFAULT},
00131     { ICAL_USAGE_ERROR,ICAL_ERROR_DEFAULT},
00132     { ICAL_UNIMPLEMENTED_ERROR,ICAL_ERROR_DEFAULT},
00133     { ICAL_UNKNOWN_ERROR,ICAL_ERROR_DEFAULT},
00134     { ICAL_NO_ERROR,ICAL_ERROR_DEFAULT}
00135 
00136 };
00137 
00138 struct icalerror_string_map {
00139     const char* str;
00140     icalerrorenum error;
00141     char name[160];
00142 };
00143 
00144 static struct icalerror_string_map string_map[] = 
00145 {
00146     {"BADARG",ICAL_BADARG_ERROR,"BADARG: Bad argument to function"},
00147     { "NEWFAILED",ICAL_NEWFAILED_ERROR,"NEWFAILED: Failed to create a new object via a *_new() routine"},
00148     { "ALLOCATION",ICAL_ALLOCATION_ERROR,"ALLOCATION: Failed to allocate new memory"},
00149     {"MALFORMEDDATA",ICAL_MALFORMEDDATA_ERROR,"MALFORMEDDATA: An input string was not correctly formed or a component has missing or extra properties"},
00150     { "PARSE",ICAL_PARSE_ERROR,"PARSE: Failed to parse a part of an iCal component"},
00151     {"INTERNAL",ICAL_INTERNAL_ERROR,"INTERNAL: Random internal error. This indicates an error in the library code, not an error in use"}, 
00152     { "FILE",ICAL_FILE_ERROR,"FILE: An operation on a file failed. Check errno for more detail."},
00153     { "USAGE",ICAL_USAGE_ERROR,"USAGE: Failed to propertyl sequence calls to a set of interfaces"},
00154     { "UNIMPLEMENTED",ICAL_UNIMPLEMENTED_ERROR,"UNIMPLEMENTED: This feature has not been implemented"},
00155     { "NO",ICAL_NO_ERROR,"NO: No error"},
00156     {"UNKNOWN",ICAL_UNKNOWN_ERROR,"UNKNOWN: Unknown error type -- icalerror_strerror() was probably given bad input"}
00157 };
00158 
00159 
00160 icalerrorenum icalerror_error_from_string(const char* str){
00161  
00162     icalerrorenum e;
00163     int i = 0;
00164 
00165     for( i = 0; string_map[i].error != ICAL_NO_ERROR; i++){
00166         if (strcmp(string_map[i].str,str) == 0){
00167             e = string_map[i].error;
00168         }
00169     }
00170 
00171     return e;
00172 }
00173 
00174 icalerrorstate icalerror_supress(const char* error){
00175 
00176     icalerrorenum e = icalerror_error_from_string(error);
00177     icalerrorstate es;
00178 
00179      if (e == ICAL_NO_ERROR){
00180         return ICAL_ERROR_UNKNOWN;
00181     }
00182 
00183 
00184     es = icalerror_get_error_state(e);
00185     icalerror_set_error_state(e,ICAL_ERROR_NONFATAL);
00186 
00187     return es;
00188 }
00189 
00190 char* icalerror_perror()
00191 {
00192     return icalerror_strerror(icalerrno);
00193 }
00194 
00195 void icalerror_restore(const char* error, icalerrorstate es){
00196 
00197 
00198     icalerrorenum e = icalerror_error_from_string(error);
00199 
00200     if (e != ICAL_NO_ERROR){
00201         icalerror_set_error_state(e,es);
00202     }
00203 
00204 }
00205 
00206 
00207 
00208 void icalerror_set_error_state( icalerrorenum error, 
00209                             icalerrorstate state)
00210 {
00211     int i;
00212 
00213     for(i = 0; error_state_map[i].error!= ICAL_NO_ERROR;i++){
00214        if(error_state_map[i].error == error){
00215            error_state_map[i].state = state;     
00216        }
00217     }
00218 }
00219 
00220 icalerrorstate icalerror_get_error_state( icalerrorenum error)
00221 {
00222     int i;
00223 
00224     for(i = 0; error_state_map[i].error!= ICAL_NO_ERROR;i++){
00225        if(error_state_map[i].error == error){
00226            return error_state_map[i].state;      
00227        }
00228     }
00229 
00230     return ICAL_ERROR_UNKNOWN;     
00231 }
00232 
00233 
00234 
00235 
00236 char* icalerror_strerror(icalerrorenum e) {
00237 
00238     int i;
00239 
00240     for (i=0; string_map[i].error != ICAL_UNKNOWN_ERROR; i++) {
00241        if (string_map[i].error == e) {
00242            return string_map[i].name;
00243        }
00244     }
00245 
00246     return string_map[i].name; /* Return string for ICAL_UNKNOWN_ERROR*/
00247     
00248 }
00249 
00250 
00251