]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
pluginlib: Update plugutil_str_split_to_alist.
authorRadosław Korzeniewski <radoslaw@korzeniewski.net>
Wed, 5 May 2021 16:05:56 +0000 (18:05 +0200)
committerEric Bollengier <eric@baculasystems.com>
Thu, 24 Mar 2022 08:03:02 +0000 (09:03 +0100)
bacula/src/plugins/fd/pluginlib/pluginlib.cpp
bacula/src/plugins/fd/pluginlib/pluginlib.h
bacula/src/plugins/fd/pluginlib/pluginlib_test.cpp

index 7d091a77a6a406a0123b1a4b56881c3681da71e1..8cb115a5e99ff34250b091ac476f15371f1ecd5e 100644 (file)
    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
index 871dad3c3539eff9467ab763a8f5b66ce06cdb41..254066092e4abd5b329629681ceb7b7b47efd427 100644 (file)
@@ -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 = '.');
 
 /**
index c4f994b496045d5089d26306647e066204ba294b..3cad82df804da04cfca8eedf792a1f9b833df807 100644 (file)
@@ -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);