diff options
Diffstat (limited to 'Master/tlpkg/lib/C/tlp-utils.c')
-rw-r--r-- | Master/tlpkg/lib/C/tlp-utils.c | 69 |
1 files changed, 68 insertions, 1 deletions
diff --git a/Master/tlpkg/lib/C/tlp-utils.c b/Master/tlpkg/lib/C/tlp-utils.c index eda65103584..864b7a0597c 100644 --- a/Master/tlpkg/lib/C/tlp-utils.c +++ b/Master/tlpkg/lib/C/tlp-utils.c @@ -29,9 +29,72 @@ void tlp_str_free(tlp_str str) free(str); } +tlp_str_list *tlp_str_list_new() +{ + tlp_str_list *list = tlp_new(tlp_str_list); + if (list == NULL) + return NULL; + + list->head = list->tail = NULL; + return list; +} + +tlp_str_list *tlp_str_list_from_array(const char *array[]) +{ + tlp_str_list *list = tlp_str_list_new(); + int i, size; + + if (list == NULL) + return NULL; + + size = sizeof(array) / sizeof(array[0]); + for (i = 0; i < size; i++) + if (tlp_str_list_append_bytes(list, array[i]) != TLP_OK) + { + tlp_str_list_free(list); + return NULL; + } + + return list; +} + +tlp_result tlp_str_list_append(tlp_str_list *list, tlp_str str) +{ + tlp_str_list_node *node = tlp_new(tlp_str_list_node); + + if (node == NULL) + return TLP_FAILED; + + node->str = str; + node->next = NULL; + + if (list->head == NULL) + list->head = node; + + if (list->tail == NULL) + list->tail = node; + + else + { + list->tail->next = node; + list->tail = node; + } + + return TLP_OK; +} + +tlp_result tlp_str_list_append_bytes(tlp_str_list *list, const char *bytes) +{ + tlp_str str = tlp_str_from_bytes(bytes); + if (str == NULL) + return TLP_FAILED; + + return tlp_str_list_append(list, str); +} + void tlp_str_list_free(tlp_str_list *list) { - tlp_str_list *node = list, *temp; + tlp_str_list_node *node = list->head, *temp; while (node != NULL) { @@ -43,6 +106,7 @@ void tlp_str_list_free(tlp_str_list *list) node = temp; } + free(list); list = NULL; } @@ -78,6 +142,9 @@ int tlp_str_start_with_bytes(tlp_str str, const char *prefix) int ret; prefix_str = tlp_str_from_bytes(prefix); + if (prefix_str == NULL) + return 0; + ret = tlp_str_start_with(str, prefix_str); tlp_str_free(prefix_str); |