From: Radosław Korzeniewski Date: Wed, 5 May 2021 16:05:56 +0000 (+0200) Subject: pluginlib: Update plugutil_str_split_to_alist. X-Git-Tag: Release-11.3.2~521 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7d43134da7b35eca1e1c897a97c6a57528e6b9d6;p=thirdparty%2Fbacula.git pluginlib: Update plugutil_str_split_to_alist. --- diff --git a/bacula/src/plugins/fd/pluginlib/pluginlib.cpp b/bacula/src/plugins/fd/pluginlib/pluginlib.cpp index 7d091a77a..8cb115a5e 100644 --- a/bacula/src/plugins/fd/pluginlib/pluginlib.cpp +++ b/bacula/src/plugins/fd/pluginlib/pluginlib.cpp @@ -17,11 +17,11 @@ Bacula(R) is a registered trademark of Kern Sibbald. */ /** - * @file commctx.h + * @file pluginlib.cpp * @author Radosław Korzeniewski (radoslaw@korzeniewski.net) * @brief Common definitions and utility functions for Inteos plugins. - * @version 2.1.0 - * @date 2021-02-10 + * @version 2.2.0 + * @date 2021-04-26 * * @copyright Copyright (c) 2021 All rights reserved. IP transferred to Bacula Systems according to agreement. */ @@ -263,49 +263,66 @@ bRC pluglib_mkpath(bpContext* ctx, char* path, bool isfatal) } /** - * @brief + * @brief It splits a string `str` into a separate substrings split on `sep` character. * - * @param str - * @param sep - * @return alist* + * @param str a string to split + * @param sep split separator character + * @return alist* newly allocated list of splitted substrings */ alist * plugutil_str_split_to_alist(const char * str, const char sep) { - POOL_MEM buf(PM_NAME); - const char * p; - const char * q; - const char * s; - alist * list; - - if (str == NULL || strlen(str) == 0){ - return NULL; - } - - list = New(alist(5, true)); - p = str; + alist * list = New(alist(5, true)); + plugutil_str_split_to_alist(*list, str, sep); + return list; +} - do { - // search for separator char - sep - q = strchr(p, sep); - if (q == NULL){ - // copy whole string from p to buf - pm_strcpy(buf, p); - } else { - // copy string from p up to q - pm_memcpy(buf, p, q - p + 1); - buf.c_str()[q - p] = '\0'; - p = q + 1; // next element - } - // in buf we have splitted string part - s = bstrdup(buf.c_str()); - list->append((void*)s); - } while (q != NULL); +/** + * @brief It splits a string `str` into a separate substrings split on `sep` character. + * + * @param list a list used to populate split results + * @param str a string to split + * @param sep split separator character + */ +void plugutil_str_split_to_alist(alist *list, const char * str, const char sep) +{ + plugutil_str_split_to_alist(*list, str, sep); +} - return list; +/** + * @brief It splits a string `str` into a separate substrings split on `sep` character. + * + * @param list a list used to populate split results + * @param str a string to split + * @param sep split separator character + */ +void plugutil_str_split_to_alist(alist &list, const char * str, const char sep) +{ + if (str != NULL && strlen(str) > 0) { + POOL_MEM buf(PM_NAME); + const char * p = str; + const char * q; + + do { + // search for separator char - sep + q = strchr(p, sep); + if (q == NULL){ + // copy whole string from p to buf + pm_strcpy(buf, p); + } else { + // copy string from p up to q + pm_memcpy(buf, p, q - p + 1); + buf.c_str()[q - p] = '\0'; + p = q + 1; // next element + } + // in buf we have splitted string part + const char * s = bstrdup(buf.c_str()); + list.append((void*)s); + } while (q != NULL); + } } /* - * Render a xe tool parameter for string value. + * Render an external tool parameter for string value. * * in: * bpContext - for Bacula debug and jobinfo messages @@ -332,7 +349,7 @@ bool render_param(POOLMEM **param, const char *pname, const char *fmt, const cha } /* - * Render a xe tool parameter for integer value. + * Render an external tool parameter for string value. * * in: * bpContext - for Bacula debug and jobinfo messages diff --git a/bacula/src/plugins/fd/pluginlib/pluginlib.h b/bacula/src/plugins/fd/pluginlib/pluginlib.h index 871dad3c3..254066092 100644 --- a/bacula/src/plugins/fd/pluginlib/pluginlib.h +++ b/bacula/src/plugins/fd/pluginlib/pluginlib.h @@ -20,8 +20,8 @@ * @file pluginlib.h * @author Radosław Korzeniewski (radoslaw@korzeniewski.net) * @brief Common definitions and utility functions for Inteos plugins. - * @version 2.1.0 - * @date 2021-02-10 + * @version 2.2.0 + * @date 2021-04-26 * * @copyright Copyright (c) 2021 All rights reserved. IP transferred to Bacula Systems according to agreement. */ @@ -181,6 +181,8 @@ inline bool isourpluginfname(const char *pluginprefix, const char *fname) return false; } +void plugutil_str_split_to_alist(alist &list, const char * str, const char sep = '.'); +void plugutil_str_split_to_alist(alist * list, const char * str, const char sep = '.'); alist * plugutil_str_split_to_alist(const char * str, const char sep = '.'); /** diff --git a/bacula/src/plugins/fd/pluginlib/pluginlib_test.cpp b/bacula/src/plugins/fd/pluginlib/pluginlib_test.cpp index c4f994b49..3cad82df8 100644 --- a/bacula/src/plugins/fd/pluginlib/pluginlib_test.cpp +++ b/bacula/src/plugins/fd/pluginlib/pluginlib_test.cpp @@ -42,44 +42,90 @@ struct vectstruct int main() { Unittests pluglib_test("pluglib_test"); - alist * list; char * s; // Pmsg0(0, "Initialize tests ...\n"); - list = plugutil_str_split_to_alist("123456789"); - ok(list != NULL, "default split"); - ok(list->size() == 1, "expect single strings"); - foreach_alist(s, list){ - ok(strlen(s) == 9, "check element length"); + { + alist * list; + + list = plugutil_str_split_to_alist("123456789"); + ok(list != NULL, "default split"); + ok(list->size() == 1, "expect single strings"); + foreach_alist(s, list){ + ok(strlen(s) == 9, "check element length"); + } + delete list; + + list = plugutil_str_split_to_alist("123.456"); + ok(list != NULL, "split: 123.456"); + ok(list->size() == 2, "expect two strings"); + foreach_alist(s, list){ + ok(strlen(s) == 3, "check element length"); + } + delete list; + + list = plugutil_str_split_to_alist("12345.56789.abcde"); + ok(list != NULL, "split: 12345.56789.abcde"); + ok(list->size() == 3, "expect three strings"); + foreach_alist(s, list){ + ok(strlen(s) == 5, "check element length"); + } + delete list; + + list = plugutil_str_split_to_alist("1.bacula..Eric.Kern"); + ok(list != NULL, "split: 1.bacula..Eric.Kern"); + ok(list->size() == 5, "expect three strings"); + ok(strcmp((char*)list->first(), "1") == 0, "check element 1"); + ok(strcmp((char*)list->next(), "bacula") == 0, "check element bacula"); + ok(strlen((char*)list->next()) == 0, "check empty element"); + ok(strcmp((char*)list->next(), "Eric") == 0, "check element Eric"); + ok(strcmp((char*)list->next(), "Kern") == 0, "check element Kern"); + delete list; } - delete list; - list = plugutil_str_split_to_alist("123.456"); - ok(list != NULL, "split: 123.456"); - ok(list->size() == 2, "expect two strings"); - foreach_alist(s, list){ - ok(strlen(s) == 3, "check element length"); + { + alist list; + + plugutil_str_split_to_alist(list, "123456789"); + ok(list.size() > 0, "default split"); + ok(list.size() == 1, "expect single strings"); + foreach_alist(s, &list){ + ok(strlen(s) == 9, "check element length"); + } } - delete list; - - list = plugutil_str_split_to_alist("12345.56789.abcde"); - ok(list != NULL, "split: 12345.56789.abcde"); - ok(list->size() == 3, "expect three strings"); - foreach_alist(s, list){ - ok(strlen(s) == 5, "check element length"); + { + alist list; + + plugutil_str_split_to_alist(list, "123.456"); + ok(list.size() > 0, "split: 123.45"); + ok(list.size() == 2, "expect two strings"); + foreach_alist(s, &list){ + ok(strlen(s) == 3, "check element length"); + } + } + { + alist list; + + plugutil_str_split_to_alist(list, "12345.56789.abcde"); + ok(list.size() > 0, "split: 12345.56789.abcde"); + ok(list.size() == 3, "expect three strings"); + foreach_alist(s, &list){ + ok(strlen(s) == 5, "check element length"); + } + } + { + alist list; + + plugutil_str_split_to_alist(list, "1.bacula..Eric.Kern"); + ok(list.size() > 0, "split: 1.bacula..Eric.Kern"); + ok(list.size() == 5, "expect three strings"); + ok(strcmp((char*)list.first(), "1") == 0, "check element 1"); + ok(strcmp((char*)list.next(), "bacula") == 0, "check element bacula"); + ok(strlen((char*)list.next()) == 0, "check empty element"); + ok(strcmp((char*)list.next(), "Eric") == 0, "check element Eric"); + ok(strcmp((char*)list.next(), "Kern") == 0, "check element Kern"); } - delete list; - - list = plugutil_str_split_to_alist("1.bacula..Eric.Kern"); - ok(list != NULL, "split: 1.bacula..Eric.Kern"); - ok(list->size() == 5, "expect three strings"); - ok(strcmp((char*)list->first(), "1") == 0, "check element 1"); - ok(strcmp((char*)list->next(), "bacula") == 0, "check element bacula"); - ok(strlen((char*)list->next()) == 0, "check empty element"); - ok(strcmp((char*)list->next(), "Eric") == 0, "check element Eric"); - ok(strcmp((char*)list->next(), "Kern") == 0, "check element Kern"); - delete list; POOL_MEM cmd1(PM_NAME); POOL_MEM param(PM_NAME);