]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
Update pluginlib.
authorRadosław Korzeniewski <radoslaw@korzeniewski.net>
Thu, 14 Jan 2021 10:00:01 +0000 (11:00 +0100)
committerEric Bollengier <eric@baculasystems.com>
Thu, 24 Mar 2022 08:03:00 +0000 (09:03 +0100)
bacula/src/plugins/fd/pluginlib/pluginlib.cpp
bacula/src/plugins/fd/pluginlib/pluginlib.h

index 64ec05a5f52f3635ea2c2553caeb5273e54c5285..0b79f8ec1e77eeb610558acd053056779a0dae08 100644 (file)
@@ -369,7 +369,7 @@ bool parse_param(POOL_MEM &param, const char *pname, const char *name, char *val
 {
    if (bstrcasecmp(name, pname)){
       pm_strcpy(param, value);
-      DMsg1(DDEBUG, "render param:%s\n", param.c_str());
+      DMsg1(DDEBUG, "parse param:%s\n", param.c_str());
       return true;
    }
    return false;
@@ -388,38 +388,98 @@ bool parse_param(POOL_MEM &param, const char *pname, const char *name, char *val
  *    True if parameter was rendered
  *    False if it was not the parameter required
  */
+// TODO: It should be called setup_param
 bool render_param(bool &param, const char *pname, const char *name, bool value)
 {
-   if (bstrcasecmp(name, pname)){
-      if (param){
-         param = value;
-         DMsg2(DDEBUG, "render param: %s=%s\n", pname, param ? "True" : "False");
-      }
+   if (bstrcasecmp(name, pname))
+   {
+      param = value;
+      DMsg2(DDEBUG, "render param: %s=%s\n", pname, param ? "True" : "False");
       return true;
    }
    return false;
 }
 
-/*
- * Setup XECOMMCTX parameter for boolean from string value.
- *  The parameter value will be false if value start with '0' character and
- *  will be true in any other case. So, when a plugin will have a following:
+/**
+ * @brief Set the up param value
+ *
+ * @param param the param variable where we will setup a parameter
+ * @param pname a name of the parameter to compare
+ * @param name a name of the parameter from parameter list
+ * @param value a value to setup
+ * @return true if parameter was handled
+ * @return false if it was not the parameter required, and param was not changed
+ */
+bool setup_param(int32_t &param, const char *pname, const char *name, const int32_t value)
+{
+   if (bstrcasecmp(name, pname))
+   {
+      param = value;
+      DMsg2(DDEBUG, "setup param: %s=%d\n", pname, param);
+      return true;
+   }
+   return false;
+}
+
+/**
+ * @brief Set the up param value
+ *
+ * @param param the param variable where we will setup a parameter
+ * @param pname a name of the parameter to compare
+ * @param name a name of the parameter from parameter list
+ * @param value a value to setup
+ * @return true if parameter was handled
+ * @return false if it was not the parameter required, and param was not changed
+ */
+bool setup_param(bool &param, const char *pname, const char *name, const bool value)
+{
+   if (bstrcasecmp(name, pname))
+   {
+      param = value;
+      DMsg2(DDEBUG, "render param: %s=%s\n", pname, param ? "True" : "False");
+      return true;
+   }
+   return false;
+}
+
+/**
+ * @brief Set the up param value
+ *
+ * @param param the param variable where we will setup a parameter
+ * @param pname a name of the parameter to compare
+ * @param name a name of the parameter from parameter list
+ * @param value a value to setup
+ * @return true if parameter was handled
+ * @return false if it was not the parameter required, and param was not changed
+ */
+bool setup_param(POOL_MEM &param, const char *pname, const char *name, const char *value)
+{
+   if (bstrcasecmp(name, pname))
+   {
+      pm_strcpy(param, value);
+      DMsg2(DDEBUG, "setup param: %s=%s\n", pname, param.c_str());
+      return true;
+   }
+   return false;
+}
+
+/**
+ * @brief Setup parameter for boolean from string value.
+ * The parameter value will be false if value start with '0' character and
+ * will be true in any other case. So, when a plugin will have a following:
  *    param
  *    param=xxx
  *    param=1
  *  then a param will be set to true.
  *
- * in:
- *    bpContext - for Bacula debug and jobinfo messages
- *    param - a pointer to the param variable where we will render a parameter
- *    pname - a name of the parameter to compare
- *    name - a name of the parameter from parameter list
- *    value - a value to render
- * out:
- *    True if parameter was rendered
- *    False if it was not the parameter required
+ * @param param the param variable where we will render a parameter
+ * @param pname a name of the parameter to compare
+ * @param name a name of the parameter from parameter list
+ * @param value a value to parse
+ * @return true if parameter was parsed
+ * @return false if it was not the parameter required
  */
-bool parse_param(bool &param, const char *pname, const char *name, char *value)
+bool parse_param(bool &param, const char *pname, const char *name, const char *value)
 {
    if (bstrcasecmp(name, pname)){
       if (value && *value == '0'){
@@ -434,17 +494,29 @@ bool parse_param(bool &param, const char *pname, const char *name, char *value)
 }
 
 /*
- * Setup Plugin parameter for integer from string value.
+ *
  *
  * in:
- *    param - a pointer to the param variable where we will render a parameter
- *    pname - a name of the parameter to compare
- *    name - a name of the parameter from parameter list
- *    value - a value to render
+ *    param - a pointer to
+ *    pname -
+ *    name -
+ *    value -
  * out:
  *    True if parameter was parsed
  *    False if it was not the parameter required
  */
+
+/**
+ * @brief Setup Plugin parameter for integer from string value.
+ *
+ * @param param the param variable where we will render a parameter
+ * @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
+ * @param err a pointer to error flag when conversion was unsuccessful, optional
+ * @return true
+ * @return false
+ */
 bool parse_param(int &param, const char *pname, const char *name, char *value, bool * err)
 {
    // clear error flag when requested
@@ -452,13 +524,16 @@ bool parse_param(int &param, const char *pname, const char *name, char *value, b
 
    if (value && bstrcasecmp(name, pname)){
       /* convert str to integer */
-      param = atoi(value);
-      if (param == 0){
-         /* error in conversion */
-         DMsg2(DERROR, "Invalid %s parameter: %s\n", name, value);
-         // setup error flag
-         if (err != NULL) *err = true;
-         return false;
+      param = strtol(value, NULL, 10);
+      if (param == LONG_MIN || param == LONG_MAX){
+         // error in conversion?
+         if (errno == ERANGE){
+            // yes, error
+            DMsg2(DERROR, "Invalid %s parameter: %s\n", name, value);
+            // setup error flag
+            if (err != NULL) *err = true;
+            return false;
+         }
       }
       DMsg2(DINFO, "%s parameter: %d\n", name, param);
 
@@ -481,18 +556,34 @@ bool parse_param(int &param, const char *pname, const char *name, char *value, b
  *    True if parameter was rendered
  *    False if it was not the parameter required
  */
-bool add_param_str(alist **list, const char *pname, const char *name, char *value)
+bool parse_param_add_str(alist **list, const char *pname, const char *name, const char *value)
 {
    POOLMEM *param;
 
-   if (bstrcasecmp(name, pname)){
-      if (!*list){
-         *list = New(alist(8, not_owned_by_alist));
+   if (list != NULL){
+      if (bstrcasecmp(name, pname)){
+         if (!*list){
+            *list = New(alist(8, not_owned_by_alist));
+         }
+         param = get_pool_memory(PM_NAME);
+         Mmsg(param, "%s", value);
+         (*list)->append(param);
+         DMsg2(DDEBUG, "add param: %s=%s\n", name, param);
+         return true;
       }
+   }
+   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);
-      Mmsg(param, "%s", value);
-      (*list)->append(param);
-      DMsg2(DDEBUG, "add param: %s=%s\n", name, value);
+      pm_strcpy(param, value);
+      list.append(param);
+      DMsg2(DDEBUG, "add param: %s=%s\n", name, param);
       return true;
    }
    return false;
index 4f8f4d1b1396c17470c54d4f977d039f44bf6ae1..74515855350b188a4acbff51535b478701961dc5 100644 (file)
@@ -159,10 +159,18 @@ alist * plugutil_str_split_to_alist(const char * str, const char sep = '.');
 bool render_param(POOLMEM **param, const char *pname, const char *fmt, const char *name, char *value);
 bool render_param(POOLMEM **param, const char *pname, const char *fmt, const char *name, int value);
 bool render_param(bool &param, const char *pname, const char *name, bool value);
+
 bool parse_param(bool &param, const char *pname, const char *name, char *value);
 bool parse_param(int &param, const char *pname, const char *name, char *value, bool *err = NULL);
+// inline bool parse_param(int32_t &param, const char *pname, const char *name, char *value, bool *err = NULL) { return parse_param((int&)param, pname, name, value, err); }
 bool parse_param(POOL_MEM &param, const char *pname, const char *name, char *value);
-bool add_param_str(alist **list, const char *pname, const char *name, char *value);
+
+bool setup_param(int32_t &param, const char *pname, const char *name, const int32_t value);
+bool setup_param(bool &param, const char *pname, const char *name, const bool value);
+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); }