| 1 | /* |
| 2 | * Copyright 2013 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 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 | |
| 21 | #ifdef HAVE_CONFIG_H |
| 22 | #include <config.h> |
| 23 | #endif |
| 24 | |
| 25 | #include "_error.h" |
| 26 | #include "err_queue.h" |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | struct err_queue { |
| 31 | struct oscap_err_t *first; |
| 32 | struct oscap_err_t *last; |
| 33 | }; |
| 34 | |
| 35 | struct err_queue *err_queue_new(void) |
| 36 | { |
| 37 | return (struct err_queue *) calloc(1, sizeof(struct err_queue)); |
| 38 | } |
| 39 | |
| 40 | bool err_queue_push(struct err_queue *q, struct oscap_err_t *error) |
| 41 | { |
| 42 | if (q == NULL || error == NULL) { |
| 43 | assert(false); |
| 44 | return false; |
| 45 | } |
| 46 | error->next = NULL; |
| 47 | |
| 48 | if (q->last == NULL) { |
| 49 | q->first = error; |
| 50 | q->last = error; |
| 51 | } |
| 52 | else { |
| 53 | q->last->next = error; |
| 54 | q->last = error; |
| 55 | } |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | bool err_queue_is_empty(struct err_queue *q) |
| 60 | { |
| 61 | if (q == NULL) { |
| 62 | assert(false); |
| 63 | return true; |
| 64 | } |
| 65 | return q->last == NULL; |
| 66 | } |
| 67 | |
| 68 | struct oscap_err_t *err_queue_pop_first(struct err_queue *q) |
| 69 | { |
| 70 | if (q == NULL || q->first == NULL) { |
| 71 | assert(false); |
| 72 | return NULL; |
| 73 | } |
| 74 | |
| 75 | struct oscap_err_t *pom = q->first; |
| 76 | q->first = pom->next; |
| 77 | if (q->last == pom) |
| 78 | q->last = NULL; |
| 79 | return pom; |
| 80 | } |
| 81 | |
| 82 | const struct oscap_err_t *err_queue_get_last(struct err_queue *q) |
| 83 | { |
| 84 | if (q == NULL || q->last == NULL) { |
| 85 | assert(false); |
| 86 | return NULL; |
| 87 | } |
| 88 | return q->last; |
| 89 | } |
| 90 | |
| 91 | void err_queue_free(struct err_queue *q, oscap_destruct_func destructor) |
| 92 | { |
| 93 | if (q == NULL) |
| 94 | return; |
| 95 | |
| 96 | if (destructor != NULL) |
| 97 | while (!err_queue_is_empty(q)) |
| 98 | destructor(err_queue_pop_first(q)); |
| 99 | free(q); |
| 100 | } |
| 101 | |
| 102 | int err_queue_to_string(struct err_queue *q, char **result) |
| 103 | { |
| 104 | if (q == NULL || result == NULL) { |
| 105 | assert(false); |
| 106 | return 1; |
| 107 | } |
| 108 | |
| 109 | size_t size = 0; |
| 110 | struct oscap_err_t *err = q->first; |
| 111 | while (err != NULL) { |
| 112 | if (err->desc != NULL) { |
| 113 | int pom = strlen(err->desc); |
| 114 | if (pom != 0) |
| 115 | size += pom + 1; |
| 116 | } |
| 117 | err = err->next; |
| 118 | } |
| 119 | if (size == 0) { |
| 120 | *result = NULL; |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | *result = (char *) malloc(size + 1); |
| 125 | if (*result == NULL) |
| 126 | return 1; |
| 127 | char *pos = *result; |
| 128 | pos[0] = '\0'; |
| 129 | err = q->first; |
| 130 | while (err != NULL) { |
| 131 | if (err->desc != NULL) { |
| 132 | const size_t desc_len = strlen(err->desc); |
| 133 | memcpy(pos, err->desc, desc_len); |
| 134 | pos += desc_len; |
| 135 | *pos = '\n'; |
| 136 | pos++; |
| 137 | } |
| 138 | err = err->next; |
| 139 | } |
| 140 | (*result)[size-1] = '\0'; |
| 141 | return 0; |
| 142 | } |
| 143 |