| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | #include | 
| 2 |  |  |  |  |  |  | #include | 
| 3 |  |  |  |  |  |  | #include "b_string.h" | 
| 4 |  |  |  |  |  |  | #include "b_error.h" | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 92 |  |  |  |  |  | b_error *b_error_new() { | 
| 7 |  |  |  |  |  |  | b_error *err; | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 92 | 50 |  |  |  |  | if ((err = malloc(sizeof(*err))) == NULL) { | 
| 10 | 0 |  |  |  |  |  | goto error_malloc; | 
| 11 |  |  |  |  |  |  | } | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 92 |  |  |  |  |  | err->type     = B_ERROR_OK; | 
| 14 | 92 |  |  |  |  |  | err->status   = 0; | 
| 15 | 92 |  |  |  |  |  | err->_errno   = 0; | 
| 16 | 92 |  |  |  |  |  | err->path     = NULL; | 
| 17 | 92 |  |  |  |  |  | err->message  = NULL; | 
| 18 | 92 |  |  |  |  |  | err->callback = NULL; | 
| 19 |  |  |  |  |  |  |  | 
| 20 | 92 |  |  |  |  |  | return err; | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | error_malloc: | 
| 23 | 0 |  |  |  |  |  | return NULL; | 
| 24 |  |  |  |  |  |  | } | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 66 |  |  |  |  |  | void b_error_set_callback(b_error *err, b_error_callback callback) { | 
| 27 | 66 | 50 |  |  |  |  | if (err == NULL) return; | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 66 |  |  |  |  |  | err->callback = callback; | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 8 |  |  |  |  |  | void b_error_set(b_error *err, enum b_error_type type, int _errno, char *message, b_string *path) { | 
| 33 | 8 | 50 |  |  |  |  | if (err == NULL) return; | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 8 |  |  |  |  |  | err->type   = type; | 
| 36 | 8 |  |  |  |  |  | err->_errno = _errno; | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 8 | 50 |  |  |  |  | if ( type == B_ERROR_FATAL ) { | 
| 39 | 0 |  |  |  |  |  | err->status = -1; | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 8 |  |  |  |  |  | b_string_free(err->message); | 
| 43 | 8 |  |  |  |  |  | b_string_free(err->path); | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 8 | 50 |  |  |  |  | if ((err->message = b_string_new(message)) == NULL) { | 
| 46 | 0 |  |  |  |  |  | goto error_string_dup_message; | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 8 | 50 |  |  |  |  | if ((err->path = b_string_dup(path)) == NULL) { | 
| 50 | 0 |  |  |  |  |  | goto error_string_dup_path; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 | 8 | 50 |  |  |  |  | if (err->callback) { | 
| 54 | 0 |  |  |  |  |  | err->callback(err); | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 8 |  |  |  |  |  | return; | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | error_string_dup_path: | 
| 60 | 0 |  |  |  |  |  | b_string_free(err->message); | 
| 61 | 0 |  |  |  |  |  | err->message = NULL; | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | error_string_dup_message: | 
| 64 | 0 |  |  |  |  |  | return; | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 7069 |  |  |  |  |  | void b_error_clear(b_error *err) { | 
| 68 | 7069 | 50 |  |  |  |  | if (err == NULL) return; | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 7069 |  |  |  |  |  | err->type   = B_ERROR_OK; | 
| 71 | 7069 |  |  |  |  |  | err->_errno = 0; | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 7069 |  |  |  |  |  | b_string_free(err->message); | 
| 74 | 7069 |  |  |  |  |  | err->message = NULL; | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 7069 |  |  |  |  |  | b_string_free(err->path); | 
| 77 | 7069 |  |  |  |  |  | err->path = NULL; | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 27 |  |  |  |  |  | int b_error_warn(b_error *err) { | 
| 81 | 27 | 50 |  |  |  |  | if (err == NULL) return 0; | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 27 |  |  |  |  |  | return err->type == B_ERROR_WARN; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 0 |  |  |  |  |  | int b_error_fatal(b_error *err) { | 
| 87 | 0 | 0 |  |  |  |  | if (err == NULL) return 0; | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 0 |  |  |  |  |  | return err->type == B_ERROR_FATAL; | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  |  | 
| 92 | 0 |  |  |  |  |  | int b_error_status(b_error *err) { | 
| 93 | 0 | 0 |  |  |  |  | if (err == NULL) return -1; | 
| 94 |  |  |  |  |  |  |  | 
| 95 | 0 |  |  |  |  |  | return err->status; | 
| 96 |  |  |  |  |  |  | } | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 0 |  |  |  |  |  | int b_error_errno(b_error *err) { | 
| 99 | 0 | 0 |  |  |  |  | if (err == NULL) return -1; | 
| 100 |  |  |  |  |  |  |  | 
| 101 | 0 |  |  |  |  |  | return err->_errno; | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  |  | 
| 104 | 0 |  |  |  |  |  | b_string *b_error_message(b_error *err) { | 
| 105 | 0 | 0 |  |  |  |  | if (err == NULL) return NULL; | 
| 106 |  |  |  |  |  |  |  | 
| 107 | 0 |  |  |  |  |  | return err->message; | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  |  | 
| 110 | 8 |  |  |  |  |  | b_string *b_error_path(b_error *err) { | 
| 111 | 8 | 50 |  |  |  |  | if (err == NULL) return NULL; | 
| 112 |  |  |  |  |  |  |  | 
| 113 | 8 |  |  |  |  |  | return err->path; | 
| 114 |  |  |  |  |  |  | } | 
| 115 |  |  |  |  |  |  |  | 
| 116 | 27 |  |  |  |  |  | void b_error_reset(b_error *err) { | 
| 117 | 27 | 50 |  |  |  |  | if (err == NULL) return; | 
| 118 |  |  |  |  |  |  |  | 
| 119 | 27 |  |  |  |  |  | b_string_free(err->message); | 
| 120 | 27 |  |  |  |  |  | err->message = NULL; | 
| 121 |  |  |  |  |  |  |  | 
| 122 | 27 |  |  |  |  |  | b_string_free(err->path); | 
| 123 | 27 |  |  |  |  |  | err->path = NULL; | 
| 124 |  |  |  |  |  |  |  | 
| 125 | 27 |  |  |  |  |  | err->status = 0; | 
| 126 | 27 |  |  |  |  |  | err->_errno = 0; | 
| 127 |  |  |  |  |  |  | } | 
| 128 |  |  |  |  |  |  |  | 
| 129 | 92 |  |  |  |  |  | void b_error_destroy(b_error *err) { | 
| 130 | 92 | 50 |  |  |  |  | if (err == NULL) return; | 
| 131 |  |  |  |  |  |  |  | 
| 132 | 92 |  |  |  |  |  | b_string_free(err->message); | 
| 133 | 92 |  |  |  |  |  | err->message = NULL; | 
| 134 |  |  |  |  |  |  |  | 
| 135 | 92 |  |  |  |  |  | b_string_free(err->path); | 
| 136 | 92 |  |  |  |  |  | err->path = NULL; | 
| 137 |  |  |  |  |  |  |  | 
| 138 | 92 |  |  |  |  |  | err->status   = 0; | 
| 139 | 92 |  |  |  |  |  | err->_errno   = 0; | 
| 140 | 92 |  |  |  |  |  | err->callback = NULL; | 
| 141 |  |  |  |  |  |  |  | 
| 142 | 92 |  |  |  |  |  | free(err); | 
| 143 |  |  |  |  |  |  | } |