| 1 | /* |
| 2 | * |
| 3 | * Print plug-in driver utility functions for the GIMP. |
| 4 | * |
| 5 | * Copyright 1997-2000 Michael Sweet ([email protected]) and |
| 6 | * Robert Krawitz ([email protected]) |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the Free |
| 10 | * Software Foundation; either version 2 of the License, or (at your option) |
| 11 | * any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 16 | * for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 20 | */ |
| 21 | |
| 22 | #ifdef HAVE_CONFIG_H |
| 23 | #include <config.h> |
| 24 | #endif |
| 25 | #include <gutenprint/gutenprint.h> |
| 26 | #include "gutenprint-internal.h" |
| 27 | #include <gutenprint/gutenprint-intl-internal.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | static void |
| 31 | free_list_element(void *item) |
| 32 | { |
| 33 | stp_param_string_t *string = (stp_param_string_t *) (item); |
| 34 | stp_free(stpi_cast_safe(string->name)); |
| 35 | stp_free(stpi_cast_safe(string->text)); |
| 36 | stp_free(string); |
| 37 | } |
| 38 | |
| 39 | static const char * |
| 40 | namefunc(const void *item) |
| 41 | { |
| 42 | const stp_param_string_t *string = (const stp_param_string_t *) (item); |
| 43 | return string->name; |
| 44 | } |
| 45 | |
| 46 | static void * |
| 47 | copyfunc(const void *item) |
| 48 | { |
| 49 | const stp_param_string_t *string = (const stp_param_string_t *) (item); |
| 50 | stp_param_string_t *new_string = stp_malloc(sizeof(stp_param_string_t)); |
| 51 | new_string->name = stp_strdup(string->name); |
| 52 | new_string->text = stp_strdup(string->text); |
| 53 | return new_string; |
| 54 | } |
| 55 | |
| 56 | static const char * |
| 57 | long_namefunc(const void *item) |
| 58 | { |
| 59 | const stp_param_string_t *string = (const stp_param_string_t *) (item); |
| 60 | return string->text; |
| 61 | } |
| 62 | |
| 63 | stp_string_list_t * |
| 64 | stp_string_list_create(void) |
| 65 | { |
| 66 | stp_list_t *ret = stp_list_create(); |
| 67 | stp_list_set_freefunc(ret, free_list_element); |
| 68 | stp_list_set_namefunc(ret, namefunc); |
| 69 | stp_list_set_copyfunc(ret, copyfunc); |
| 70 | stp_list_set_long_namefunc(ret, long_namefunc); |
| 71 | return (stp_string_list_t *) ret; |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | stp_string_list_destroy(stp_string_list_t *list) |
| 76 | { |
| 77 | stp_list_destroy((stp_list_t *) list); |
| 78 | } |
| 79 | |
| 80 | stp_param_string_t * |
| 81 | stp_string_list_param(const stp_string_list_t *list, size_t element) |
| 82 | { |
| 83 | stp_list_item_t *answer = |
| 84 | stp_list_get_item_by_index((const stp_list_t *)list, element); |
| 85 | if (answer) |
| 86 | return (stp_param_string_t *) stp_list_item_get_data(answer); |
| 87 | else |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | stp_param_string_t * |
| 92 | stp_string_list_find(const stp_string_list_t *list, const char *name) |
| 93 | { |
| 94 | stp_list_item_t *answer = |
| 95 | stp_list_get_item_by_name((const stp_list_t *)list, name); |
| 96 | if (answer) |
| 97 | return (stp_param_string_t *) stp_list_item_get_data(answer); |
| 98 | else |
| 99 | return NULL; |
| 100 | } |
| 101 | |
| 102 | size_t |
| 103 | stp_string_list_count(const stp_string_list_t *list) |
| 104 | { |
| 105 | return stp_list_get_length((const stp_list_t *)list); |
| 106 | } |
| 107 | |
| 108 | stp_string_list_t * |
| 109 | stp_string_list_create_copy(const stp_string_list_t *list) |
| 110 | { |
| 111 | return (stp_string_list_t *) stp_list_copy((const stp_list_t *)list); |
| 112 | } |
| 113 | |
| 114 | stp_string_list_t * |
| 115 | stp_string_list_create_from_params(const stp_param_string_t *list, |
| 116 | size_t count) |
| 117 | { |
| 118 | size_t i = 0; |
| 119 | stp_string_list_t *retval = stp_string_list_create(); |
| 120 | for (i = 0; i < count; i++) |
| 121 | stp_string_list_add_string(retval, list[i].name, list[i].text); |
| 122 | return retval; |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | stp_string_list_add_string(stp_string_list_t *list, |
| 127 | const char *name, |
| 128 | const char *text) |
| 129 | { |
| 130 | stp_param_string_t *new_string = stp_malloc(sizeof(stp_param_string_t)); |
| 131 | do |
| 132 | { |
| 133 | const char *xname = name; |
| 134 | while (*xname) |
| 135 | { |
| 136 | if (!isalnum(*xname) && |
| 137 | *xname != '_' && *xname != '-' && *xname != '+') |
| 138 | { |
| 139 | stp_erprintf("Gutenprint: bad string %s (%s)\n", name, text); |
| 140 | break; |
| 141 | } |
| 142 | xname++; |
| 143 | } |
| 144 | } while(0); |
| 145 | new_string->name = stp_strdup(name); |
| 146 | new_string->text = stp_strdup(text); |
| 147 | stp_list_item_create((stp_list_t *) list, NULL, new_string); |
| 148 | } |
| 149 | |
| 150 | void |
| 151 | stp_string_list_add_string_unsafe(stp_string_list_t *list, |
| 152 | const char *name, |
| 153 | const char *text) |
| 154 | { |
| 155 | stp_param_string_t *new_string = stp_malloc(sizeof(stp_param_string_t)); |
| 156 | new_string->name = stp_strdup(name); |
| 157 | new_string->text = stp_strdup(text); |
| 158 | stp_list_item_create((stp_list_t *) list, NULL, new_string); |
| 159 | } |
| 160 | |
| 161 | void |
| 162 | stp_string_list_remove_string(stp_string_list_t *list, |
| 163 | const char *name) |
| 164 | { |
| 165 | stp_list_item_t *item = |
| 166 | stp_list_get_item_by_name((const stp_list_t *) list, name); |
| 167 | if (item) |
| 168 | stp_list_item_destroy((stp_list_t *) list, item); |
| 169 | } |
| 170 | |
| 171 | int |
| 172 | stp_string_list_is_present(const stp_string_list_t *list, |
| 173 | const char *value) |
| 174 | { |
| 175 | if (list && value && |
| 176 | stp_list_get_item_by_name((const stp_list_t *) list, value)) |
| 177 | return 1; |
| 178 | else |
| 179 | return 0; |
| 180 | } |