common: move append_str to string-utils
authorSimon Marchi <simon.marchi@efficios.com>
Fri, 20 Aug 2021 20:10:37 +0000 (16:10 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 17 Dec 2021 05:31:09 +0000 (00:31 -0500)
Move the append_str function from filter-visitor-generate-bytecode.c to
the string-utils lib, so that it can be re-used elsewhere.

Change-Id: Ica09cb750ac7f3f2d6f3fb5fc786b683ceb6f79a
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/common/filter/filter-visitor-generate-bytecode.c
src/common/string-utils/string-utils.c
src/common/string-utils/string-utils.h

index 8bf87d7c6661e84dc4b920c16eb9c8851b393168..938f7d8a903febf4151183554f179b8bb17c25d8 100644 (file)
@@ -19,6 +19,7 @@
 #include "common/bytecode/bytecode.h"
 #include "common/compat/string.h"
 #include "common/macros.h"
+#include "common/string-utils/string-utils.h"
 #include "filter-ast.h"
 #include "filter-ir.h"
 
@@ -59,27 +60,6 @@ int visit_node_root(struct filter_parser_ctx *ctx, struct ir_op *node)
        return bytecode_push(&ctx->bytecode, &insn, 1, sizeof(insn));
 }
 
-static
-int append_str(char **s, const char *append)
-{
-       char *old = *s;
-       char *new;
-       size_t oldlen = (old == NULL) ? 0 : strlen(old);
-       size_t appendlen = strlen(append);
-
-       new = calloc(oldlen + appendlen + 1, 1);
-       if (!new) {
-               return -ENOMEM;
-       }
-       if (oldlen) {
-               strcpy(new, old);
-       }
-       strcat(new, append);
-       *s = new;
-       free(old);
-       return 0;
-}
-
 /*
  * 1: match
  * 0: no match
@@ -97,14 +77,14 @@ int load_expression_legacy_match(const struct ir_load_expression *exp,
        switch (op->type) {
        case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT:
                *op_type = BYTECODE_OP_GET_CONTEXT_REF;
-               if (append_str(symbol, "$ctx.")) {
+               if (strutils_append_str(symbol, "$ctx.")) {
                        return -ENOMEM;
                }
                need_dot = false;
                break;
        case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT:
                *op_type = BYTECODE_OP_GET_CONTEXT_REF;
-               if (append_str(symbol, "$app.")) {
+               if (strutils_append_str(symbol, "$app.")) {
                        return -ENOMEM;
                }
                need_dot = false;
@@ -130,10 +110,10 @@ int load_expression_legacy_match(const struct ir_load_expression *exp,
                case IR_LOAD_EXPRESSION_LOAD_FIELD:
                        goto end;
                case IR_LOAD_EXPRESSION_GET_SYMBOL:
-                       if (need_dot && append_str(symbol, ".")) {
+                       if (need_dot && strutils_append_str(symbol, ".")) {
                                return -ENOMEM;
                        }
-                       if (append_str(symbol, op->u.symbol)) {
+                       if (strutils_append_str(symbol, op->u.symbol)) {
                                return -ENOMEM;
                        }
                        break;
index 669f7d05fd01c64513b68128a740c8940fdcaa9d..526604d14e42a6f0d907fab86676ca2f2435e46f 100644 (file)
@@ -10,6 +10,7 @@
 #include <string.h>
 #include <stdbool.h>
 #include <assert.h>
+#include <errno.h>
 
 #include "string-utils.h"
 #include "../macros.h"
@@ -370,3 +371,24 @@ size_t strutils_array_of_strings_len(char * const *array)
 
        return count;
 }
+
+LTTNG_HIDDEN
+int strutils_append_str(char **s, const char *append)
+{
+       char *old = *s;
+       char *new;
+       size_t oldlen = (old == NULL) ? 0 : strlen(old);
+       size_t appendlen = strlen(append);
+
+       new = calloc(oldlen + appendlen + 1, 1);
+       if (!new) {
+               return -ENOMEM;
+       }
+       if (oldlen) {
+               strcpy(new, old);
+       }
+       strcat(new, append);
+       *s = new;
+       free(old);
+       return 0;
+}
index 9d287f3d9b4f7aa0849d4ecb42c1a7ef211f5ea1..55deba38b50369fe6f28fc4faea60c7ba22fd159 100644 (file)
@@ -34,4 +34,15 @@ void strutils_free_null_terminated_array_of_strings(char **array);
 LTTNG_HIDDEN
 size_t strutils_array_of_strings_len(char * const *array);
 
+/*
+ * Append `append` to the malloc-end string `str`.
+ *
+ * On success, `str` is free'd (if not NULL) and assigned a new malloc-ed
+ * string.  On failure, `str` is not modified.
+ *
+ * Return 0 on success, -ENOMEM on failure.
+ */
+LTTNG_HIDDEN
+int strutils_append_str(char **str, const char *append);
+
 #endif /* _STRING_UTILS_H */
This page took 0.029177 seconds and 5 git commands to generate.