]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
pluginlib: Add pluginlib::parse_param_add_str().
authorRadosław Korzeniewski <radoslaw@korzeniewski.net>
Thu, 21 Oct 2021 17:33:12 +0000 (19:33 +0200)
committerEric Bollengier <eric@baculasystems.com>
Thu, 14 Sep 2023 11:56:56 +0000 (13:56 +0200)
bacula/src/plugins/fd/pluginlib/pluginlib.cpp
bacula/src/plugins/fd/pluginlib/pluginlib.h
bacula/src/plugins/fd/pluginlib/pluginlib_test.cpp

index 54837684cb2c5b98b070518222f037eab4a4ce9c..499c17d1db909560dfc0991970cbc64cba312bc5 100644 (file)
@@ -604,7 +604,7 @@ bool parse_param_add_str(alist **list, const char *pname, const char *name, cons
             *list = New(alist(8, not_owned_by_alist));
          }
          param = get_pool_memory(PM_NAME);
-         Mmsg(param, "%s", value);
+         pm_strcpy(param, value);
          (*list)->append(param);
          DMsg2(DDEBUG, "add param: %s=%s\n", name, param);
          return true;
@@ -613,20 +613,6 @@ bool parse_param_add_str(alist **list, const char *pname, const char *name, cons
    return false;
 }
 
-bool parse_param_add_str(alist &list, const char *pname, const char *name, const char *value)
-{
-   POOLMEM *param;
-
-   if (bstrcasecmp(name, pname)){
-      param = get_pool_memory(PM_NAME);
-      pm_strcpy(param, value);
-      list.append(param);
-      DMsg2(DDEBUG, "add param: %s=%s\n", name, param);
-      return true;
-   }
-   return false;
-}
-
 /**
  * @brief Scans for `prefix` in `cmd`, when match copy remaining to `param`.
  *
@@ -677,3 +663,32 @@ void scan_and_terminate_str(POOL_MEM &buf, int msglen)
       buf.c_str()[msglen + 1] = '\0';
    }
 }
+
+namespace pluginlib
+{
+   /**
+    * @brief Render and add a parameter for string value to alist.
+    *       In this case POOL_MEM is created.
+    *
+    * @param list a list to append
+    * @param pname a name of the parameter to compare
+    * @param name a name of the parameter from parameter list
+    * @param value a value to render
+    * @return true if parameter was rendered
+    * @return false if it was not the parameter required
+    */
+   bool parse_param_add_str(alist &list, const char *pname, const char *name, const char *value)
+   {
+      if (bstrcasecmp(name, pname))
+      {
+         POOL_MEM *param = new POOL_MEM(PM_NAME);
+         pm_strcpy(*param, value);
+         list.append(param);
+         DMsg2(DDEBUG, "add param: %s=%s\n", name, (*param).c_str());
+         return true;
+      }
+
+      return false;
+   }
+
+}  // namespace pluginlib
index 2f24ebe1fbacdd76af01e607b6e44bda621619d4..558628c88e39682383ab6b1c25d20cfbed80186d 100644 (file)
@@ -237,7 +237,6 @@ bool setup_param(bool &param, const char *pname, const char *name, const bool va
 bool setup_param(POOL_MEM &param, const char *pname, const char *name, const char *value);
 
 bool parse_param_add_str(alist **list, const char *pname, const char *name, const char *value);
-bool parse_param_add_str(alist &list, const char *pname, const char *name, const char *value);
 
 bool scan_parameter_str(const char * cmd, const char *prefix, POOL_MEM &param);
 inline bool scan_parameter_str(const POOL_MEM &cmd, const char *prefix, POOL_MEM &param) { return scan_parameter_str(cmd.c_str(), prefix, param); }
@@ -246,4 +245,9 @@ inline bool scan_parameter_int(const POOL_MEM &cmd, const char *prefix, int &par
 
 void scan_and_terminate_str(POOL_MEM &buf, int msglen);
 
+namespace pluginlib
+{
+   bool parse_param_add_str(alist &list, const char *pname, const char *name, const char *value);
+}  // namespace pluginlib
+
 #endif /* _PLUGINLIB_H_ */
index c75110c2d427a37f5f5bf29510243236e358629f..4bf65b75f0e5162c9227a6630c9d4734e9a6cdcd 100644 (file)
@@ -27,6 +27,7 @@
 
 #include "pluginlib.h"
 #include "unittests.h"
+#include "smartalist.h"
 
 bFuncs *bfuncs;
 bInfo *binfo;
@@ -207,5 +208,38 @@ int main()
       ok(memcmp(ebuf.c_str(), testvect2[i].output, testvect2[i].len) == 0, testvect2[i].descr);
    }
 
+   {
+      // debug_level = 800;
+      alist mylist(10, not_owned_by_alist);
+      bool status = pluginlib::parse_param_add_str(mylist, "test", "test", "value");
+      ok(status, "test parse_param_add_str");
+      rok(mylist.size() == 1, "test alist size after append");
+      delete (POOL_MEM*)mylist.get(0);
+   }
+
+   {
+      // debug_level = 800;
+      smart_alist<POOL_MEM> mylist;
+      bool status = pluginlib::parse_param_add_str(mylist, "test", "test", "othervalue");
+      ok(status, "test parse_param_add_str with smart_alist");
+      rok(mylist.size() == 1, "test alist size after append");
+   }
+
+   // {
+   //    alist mylist(10, not_owned_by_alist);
+   //    mylist.append((void*)10);
+   //    mylist.append((void*)20);
+   //    mylist.append((void*)30);
+   //    ok(mylist.size() == 3, "test size");
+   //    uint64_t *a;
+   //    int i;
+   //    foreach_alist_index(i, a, &mylist)
+   //    {
+   //       printf("i:%d a:%ld\n", i, (uint64_t)a);
+   //       mylist.remove(i);
+   //       i--;
+   //    }
+   // }
+
    return report();
 }