Add type-checked versions of allocation and deallocations functions
[lttng-tools.git] / src / common / string-utils / string-utils.cpp
index 8dcc6b2aae9ce9fab7b3beeefbea0b495fc15db4..d172aeb4b0fd69aaf3284e00283c2cdcab9cd5ce 100644 (file)
@@ -1,20 +1,22 @@
 /*
  * Copyright (C) 2017 Philippe Proulx <pproulx@efficios.com>
  *
- * SPDX-License-Identifier: GPL-2.0-only
+ * SPDX-License-Identifier: LGPL-2.1-only
  *
  */
 
 #define _LGPL_SOURCE
 #include <stdlib.h>
+#include <stdio.h>
 #include <string.h>
 #include <stdbool.h>
 #include <type_traits>
 #include <assert.h>
 #include <errno.h>
+#include <stdarg.h>
 
-#include "string-utils.h"
-#include "../macros.h"
+#include "string-utils.hpp"
+#include "../macros.hpp"
 
 enum star_glob_pattern_type_flags {
        STAR_GLOB_PATTERN_TYPE_FLAG_NONE = 0,
@@ -64,7 +66,7 @@ void strutils_normalize_star_glob_pattern(char *pattern)
                                goto end;
                        }
 
-                       /* Fall through default case. */
+                       /* fall through */
                default:
                        got_star = false;
                        break;
@@ -146,7 +148,7 @@ char *strutils_unescape_string(const char *input, char only_char)
        const char *i;
 
        LTTNG_ASSERT(input);
-       output = (char *) zmalloc(strlen(input) + 1);
+       output = calloc<char>(strlen(input) + 1);
        if (!output) {
                goto end;
        }
@@ -297,7 +299,7 @@ int strutils_split(const char *input,
        for (at = 0, s = input; at < number_of_substrings; at++) {
                const char *ss;
                char *d;
-               char *substring = (char *) zmalloc(longest_substring_len + 1);
+               char *substring = calloc<char>(longest_substring_len + 1);
 
                if (!substring) {
                        goto error;
@@ -383,7 +385,7 @@ int strutils_append_str(char **s, const char *append)
        size_t oldlen = (old == NULL) ? 0 : strlen(old);
        size_t appendlen = strlen(append);
 
-       new_str = (char *) zmalloc(oldlen + appendlen + 1);
+       new_str = zmalloc<char>(oldlen + appendlen + 1);
        if (!new_str) {
                return -ENOMEM;
        }
@@ -395,3 +397,49 @@ int strutils_append_str(char **s, const char *append)
        free(old);
        return 0;
 }
+
+int strutils_appendf(char **s, const char *fmt, ...)
+{
+       char *new_str;
+       size_t oldlen = (*s) ? strlen(*s) : 0;
+       int ret;
+       va_list args;
+
+       /* Compute length of formatted string we append. */
+       va_start(args, fmt);
+       ret = vsnprintf(NULL, 0, fmt, args);
+       va_end(args);
+
+       if (ret == -1) {
+               goto end;
+       }
+
+       /* Allocate space for old string + new string + \0. */
+       new_str = zmalloc<char>(oldlen + ret + 1);
+       if (!new_str) {
+               ret = -ENOMEM;
+               goto end;
+       }
+
+       /* Copy old string, if there was one. */
+       if (oldlen) {
+               strcpy(new_str, *s);
+       }
+
+       /* Format new string in-place. */
+       va_start(args, fmt);
+       ret = vsprintf(&new_str[oldlen], fmt, args); 
+       va_end(args);
+
+       if (ret == -1) {
+               ret = -1;
+               goto end;
+       }
+
+       free(*s);
+       *s = new_str;
+       new_str = NULL;
+
+end:
+       return ret;
+}
This page took 0.02442 seconds and 4 git commands to generate.