From: RadosÅ‚aw Korzeniewski Date: Thu, 21 Oct 2021 17:33:12 +0000 (+0200) Subject: pluginlib: Add pluginlib::parse_param_add_str(). X-Git-Tag: Beta-15.0.0~796 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=94fab0d1a88753b5be01b1229866d8cc6743365c;p=thirdparty%2Fbacula.git pluginlib: Add pluginlib::parse_param_add_str(). --- diff --git a/bacula/src/plugins/fd/pluginlib/pluginlib.cpp b/bacula/src/plugins/fd/pluginlib/pluginlib.cpp index 54837684c..499c17d1d 100644 --- a/bacula/src/plugins/fd/pluginlib/pluginlib.cpp +++ b/bacula/src/plugins/fd/pluginlib/pluginlib.cpp @@ -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 diff --git a/bacula/src/plugins/fd/pluginlib/pluginlib.h b/bacula/src/plugins/fd/pluginlib/pluginlib.h index 2f24ebe1f..558628c88 100644 --- a/bacula/src/plugins/fd/pluginlib/pluginlib.h +++ b/bacula/src/plugins/fd/pluginlib/pluginlib.h @@ -237,7 +237,6 @@ bool setup_param(bool ¶m, const char *pname, const char *name, const bool va bool setup_param(POOL_MEM ¶m, 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 ¶m); inline bool scan_parameter_str(const POOL_MEM &cmd, const char *prefix, POOL_MEM ¶m) { 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_ */ diff --git a/bacula/src/plugins/fd/pluginlib/pluginlib_test.cpp b/bacula/src/plugins/fd/pluginlib/pluginlib_test.cpp index c75110c2d..4bf65b75f 100644 --- a/bacula/src/plugins/fd/pluginlib/pluginlib_test.cpp +++ b/bacula/src/plugins/fd/pluginlib/pluginlib_test.cpp @@ -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 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(); }