]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/string_utils.h
2720f1097e405e66f5dce2a705b49c0936c029e5
[mirror_lxc.git] / src / lxc / string_utils.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #ifndef __LXC_STRING_UTILS_H
4 #define __LXC_STRING_UTILS_H
5
6 #include <stdarg.h>
7
8 #include "config.h"
9
10 #include "initutils.h"
11 #include "macro.h"
12
13 /* convert variadic argument lists to arrays (for execl type argument lists) */
14 extern char **lxc_va_arg_list_to_argv(va_list ap, size_t skip, int do_strdup);
15 extern const char **lxc_va_arg_list_to_argv_const(va_list ap, size_t skip);
16
17 /*
18 * Some simple string functions; if they return pointers, they are allocated
19 * buffers.
20 */
21 extern char *lxc_string_replace(const char *needle, const char *replacement,
22 const char *haystack);
23 extern bool lxc_string_in_array(const char *needle, const char **haystack);
24 extern char *lxc_string_join(const char *sep, const char **parts,
25 bool use_as_prefix);
26 /*
27 * Normalize and split path: Leading and trailing / are removed, multiple
28 * / are compactified, .. and . are resolved (.. on the top level is considered
29 * identical to .).
30 * Examples:
31 * / -> { NULL }
32 * foo/../bar -> { bar, NULL }
33 * ../../ -> { NULL }
34 * ./bar/baz/.. -> { bar, NULL }
35 * foo//bar -> { foo, bar, NULL }
36 */
37 extern char **lxc_normalize_path(const char *path);
38
39 /* remove multiple slashes from the path, e.g. ///foo//bar -> /foo/bar */
40 extern char *lxc_deslashify(const char *path);
41 extern char *lxc_append_paths(const char *first, const char *second);
42
43 /*
44 * Note: the following two functions use strtok(), so they will never
45 * consider an empty element, even if two delimiters are next to
46 * each other.
47 */
48 extern bool lxc_string_in_list(const char *needle, const char *haystack,
49 char sep);
50 extern char **lxc_string_split(const char *string, char sep);
51 extern char **lxc_string_split_and_trim(const char *string, char sep);
52 extern char **lxc_string_split_quoted(char *string);
53
54 /* Append string to NULL-terminated string array. */
55 extern int lxc_append_string(char ***list, char *entry);
56
57 /* Some simple array manipulation utilities */
58 typedef void (*lxc_free_fn)(void *);
59 typedef void *(*lxc_dup_fn)(void *);
60 extern int lxc_grow_array(void ***array, size_t *capacity, size_t new_size,
61 size_t capacity_increment);
62 extern void lxc_free_array(void **array, lxc_free_fn element_free_fn);
63 extern size_t lxc_array_len(void **array);
64
65 extern void **lxc_append_null_to_array(void **array, size_t count);
66 extern void remove_trailing_newlines(char *l);
67
68 /* Helper functions to parse numbers. */
69 extern int lxc_safe_uint(const char *numstr, unsigned int *converted);
70 extern int lxc_safe_int(const char *numstr, int *converted);
71 extern int lxc_safe_long(const char *numstr, long int *converted);
72 extern int lxc_safe_long_long(const char *numstr, long long int *converted);
73 extern int lxc_safe_ulong(const char *numstr, unsigned long *converted);
74 extern int lxc_safe_uint64(const char *numstr, uint64_t *converted, int base);
75 /* Handles B, kb, MB, GB. Detects overflows and reports -ERANGE. */
76 extern int parse_byte_size_string(const char *s, int64_t *converted);
77
78 /*
79 * Concatenate all passed-in strings into one path. Do not fail. If any piece
80 * is not prefixed with '/', add a '/'.
81 */
82 __attribute__((sentinel)) extern char *must_concat(size_t *len, const char *first, ...);
83 __attribute__((sentinel)) extern char *must_make_path(const char *first, ...);
84 __attribute__((sentinel)) extern char *must_append_path(char *first, ...);
85
86 /* Return copy of string @entry. Do not fail. */
87 extern char *must_copy_string(const char *entry);
88
89 /* Re-allocate a pointer, do not fail */
90 extern void *must_realloc(void *orig, size_t sz);
91
92 extern int lxc_char_left_gc(const char *buffer, size_t len);
93
94 extern int lxc_char_right_gc(const char *buffer, size_t len);
95
96 extern char *lxc_trim_whitespace_in_place(char *buffer);
97
98 extern int lxc_is_line_empty(const char *line);
99 extern void remove_trailing_slashes(char *p);
100
101 static inline bool is_empty_string(const char *s)
102 {
103 return !s || strcmp(s, "") == 0;
104 }
105
106 #endif /* __LXC_STRING_UTILS_H */