| 1 |
/* |
| 2 |
* FCML - Free Code Manipulation Library. |
| 3 |
* Copyright (C) 2010-2020 Slawomir Wojtasiak |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <string.h> |
| 21 |
#include "fcml_env_int.h" |
| 22 |
#include "fcml_ceh.h" |
| 23 |
|
| 24 |
void fcml_fn_ceh_free_error_info(fcml_st_ceh_error_info *error_info) { |
| 25 |
if (error_info) { |
| 26 |
if (error_info->message) { |
| 27 |
fcml_fn_env_str_strfree(error_info->message); |
| 28 |
error_info->message = NULL; |
| 29 |
} |
| 30 |
fcml_fn_env_memory_free(error_info); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
void fcml_fn_ceh_move_errors(fcml_st_ceh_error_container *destination, |
| 35 |
fcml_st_ceh_error_container *source) { |
| 36 |
if (source != NULL && destination != NULL) { |
| 37 |
/* Clean destination if there are any errors. */ |
| 38 |
fcml_fn_ceh_free_errors_only(destination); |
| 39 |
destination->errors = source->errors; |
| 40 |
destination->last_error = source->last_error; |
| 41 |
source->errors = NULL; |
| 42 |
source->last_error = NULL; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
void fcml_fn_ceh_free_errors_only(fcml_st_ceh_error_container *error_container) { |
| 47 |
if (error_container) { |
| 48 |
fcml_st_ceh_error_info *next = error_container->errors; |
| 49 |
while (next) { |
| 50 |
fcml_st_ceh_error_info *curr = next; |
| 51 |
next = next->next_error; |
| 52 |
fcml_fn_ceh_free_error_info(curr); |
| 53 |
} |
| 54 |
error_container->errors = NULL; |
| 55 |
error_container->last_error = NULL; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
void fcml_fn_ceh_free_errors_only_with_level( |
| 60 |
fcml_st_ceh_error_container *error_container, |
| 61 |
fcml_en_ceh_error_level level) { |
| 62 |
if (error_container) { |
| 63 |
fcml_st_ceh_error_info **prev = &(error_container->errors); |
| 64 |
fcml_st_ceh_error_info *next = error_container->errors; |
| 65 |
while (next) { |
| 66 |
fcml_st_ceh_error_info *curr = next; |
| 67 |
next = next->next_error; |
| 68 |
if (curr->level == level) { |
| 69 |
*prev = curr->next_error; |
| 70 |
fcml_fn_ceh_free_error_info(curr); |
| 71 |
} else { |
| 72 |
prev = &(curr->next_error); |
| 73 |
} |
| 74 |
} |
| 75 |
if (*prev == NULL) { |
| 76 |
error_container->last_error = NULL; |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
fcml_st_ceh_error_info* fcml_fn_ceh_alloc_error_info(const fcml_string message, |
| 82 |
fcml_ceh_error code, fcml_en_ceh_error_level level) { |
| 83 |
FCML_ENV_ALLOC_CLEAR(error_info, fcml_st_ceh_error_info); |
| 84 |
if (error_info) { |
| 85 |
error_info->message = fcml_fn_env_str_strdup(message); |
| 86 |
if (!error_info->message) { |
| 87 |
fcml_fn_env_memory_free(error_info); |
| 88 |
return NULL; |
| 89 |
} |
| 90 |
error_info->code = code; |
| 91 |
error_info->level = level; |
| 92 |
} |
| 93 |
return error_info; |
| 94 |
} |
| 95 |
|
| 96 |
fcml_st_ceh_error_info* fcml_fn_ceh_add_error( |
| 97 |
fcml_st_ceh_error_container *error_container, const fcml_string message, |
| 98 |
fcml_ceh_error code, fcml_en_ceh_error_level level) { |
| 99 |
|
| 100 |
fcml_st_ceh_error_info *error_info; |
| 101 |
|
| 102 |
/* Container is null.*/ |
| 103 |
if (!error_container) { |
| 104 |
return NULL; |
| 105 |
} |
| 106 |
|
| 107 |
/* Checks if there is already such error reported. */ |
| 108 |
fcml_st_ceh_error_info *current = error_container->errors; |
| 109 |
while (current) { |
| 110 |
if (current->code == code) { |
| 111 |
// Already added. |
| 112 |
return current; |
| 113 |
} |
| 114 |
current = current->next_error; |
| 115 |
} |
| 116 |
|
| 117 |
error_info = fcml_fn_ceh_alloc_error_info(message, code, level); |
| 118 |
if (!error_info) { |
| 119 |
return NULL; |
| 120 |
} |
| 121 |
|
| 122 |
fcml_st_ceh_error_info *last_error = error_container->last_error; |
| 123 |
if (last_error) { |
| 124 |
last_error->next_error = error_info; |
| 125 |
} else { |
| 126 |
error_container->errors = error_info; |
| 127 |
} |
| 128 |
|
| 129 |
error_container->last_error = error_info; |
| 130 |
|
| 131 |
return error_info; |
| 132 |
} |