/*
* 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 "../macros.hpp"
+#include "string-utils.hpp"
+
+#include <assert.h>
+#include <errno.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <stdbool.h>
#include <type_traits>
-#include <assert.h>
-#include <errno.h>
-
-#include "string-utils.h"
-#include "../macros.h"
enum star_glob_pattern_type_flags {
STAR_GLOB_PATTERN_TYPE_FLAG_NONE = 0,
STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY = 2,
};
-static
-star_glob_pattern_type_flags &operator|=(star_glob_pattern_type_flags &l,
- star_glob_pattern_type_flags r)
+static star_glob_pattern_type_flags& operator|=(star_glob_pattern_type_flags& l,
+ star_glob_pattern_type_flags r)
{
using T = std::underlying_type<star_glob_pattern_type_flags>::type;
- l = static_cast<star_glob_pattern_type_flags> (
- static_cast<T> (l) | static_cast<T> (r));
+ l = static_cast<star_glob_pattern_type_flags>(static_cast<T>(l) | static_cast<T>(r));
return l;
}
goto end;
}
- /* Fall through default case. */
+ /* fall through */
default:
got_star = false;
break;
*np = '\0';
}
-static
-enum star_glob_pattern_type_flags strutils_test_glob_pattern(const char *pattern)
+static enum star_glob_pattern_type_flags strutils_test_glob_pattern(const char *pattern)
{
- enum star_glob_pattern_type_flags ret =
- STAR_GLOB_PATTERN_TYPE_FLAG_NONE;
+ enum star_glob_pattern_type_flags ret = STAR_GLOB_PATTERN_TYPE_FLAG_NONE;
const char *p;
LTTNG_ASSERT(pattern);
*/
bool strutils_is_star_glob_pattern(const char *pattern)
{
- return strutils_test_glob_pattern(pattern) &
- STAR_GLOB_PATTERN_TYPE_FLAG_PATTERN;
+ return strutils_test_glob_pattern(pattern) & STAR_GLOB_PATTERN_TYPE_FLAG_PATTERN;
}
/*
*/
bool strutils_is_star_at_the_end_only_glob_pattern(const char *pattern)
{
- return strutils_test_glob_pattern(pattern) &
- STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY;
+ return strutils_test_glob_pattern(pattern) & STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY;
}
/*
const char *i;
LTTNG_ASSERT(input);
- output = (char *) zmalloc(strlen(input) + 1);
+ output = calloc<char>(strlen(input) + 1);
if (!output) {
goto end;
}
* Returns -1 if there's an error.
*/
int strutils_split(const char *input,
- char delim,
- bool escape_delim,
- struct lttng_dynamic_pointer_array *out_strings)
+ char delim,
+ bool escape_delim,
+ struct lttng_dynamic_pointer_array *out_strings)
{
int ret;
size_t at;
}
if (*s == delim) {
- size_t last_len = s - last - 1;
+ const size_t last_len = s - last - 1;
last = s;
number_of_substrings++;
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;
}
- ret = lttng_dynamic_pointer_array_add_pointer(
- out_strings, substring);
+ ret = lttng_dynamic_pointer_array_add_pointer(out_strings, substring);
if (ret) {
free(substring);
goto error;
return ret;
}
-size_t strutils_array_of_strings_len(char * const *array)
+size_t strutils_array_of_strings_len(char *const *array)
{
- char * const *item;
+ char *const *item;
size_t count = 0;
LTTNG_ASSERT(array);
{
char *old = *s;
char *new_str;
- size_t oldlen = (old == NULL) ? 0 : strlen(old);
- size_t appendlen = strlen(append);
+ const size_t oldlen = (old == nullptr) ? 0 : strlen(old);
+ const size_t appendlen = strlen(append);
- new_str = (char *) zmalloc(oldlen + appendlen + 1);
+ new_str = zmalloc<char>(oldlen + appendlen + 1);
if (!new_str) {
return -ENOMEM;
}
free(old);
return 0;
}
+
+int strutils_appendf(char **s, const char *fmt, ...)
+{
+ char *new_str;
+ const size_t oldlen = (*s) ? strlen(*s) : 0;
+ size_t addlen = 0;
+ int ret;
+ va_list args;
+
+ /* Compute length of formatted string we append. */
+ va_start(args, fmt);
+ ret = vsnprintf(nullptr, 0, fmt, args);
+ va_end(args);
+
+ if (ret == -1) {
+ goto end;
+ }
+
+ /* Allocate space for old string + new string + \0. */
+ addlen = ret + 1;
+ new_str = zmalloc<char>(oldlen + addlen);
+ 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 = vsnprintf(&new_str[oldlen], addlen, fmt, args);
+ va_end(args);
+
+ if (ret == -1) {
+ ret = -1;
+ goto end;
+ }
+
+ free(*s);
+ *s = new_str;
+ new_str = nullptr;
+
+end:
+ return ret;
+}