]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/string_utils.h
4065e4e6b1b3c036058b3770703f7b6744f26449
[mirror_lxc.git] / src / lxc / string_utils.h
1 /* liblxcapi
2 *
3 * Copyright © 2018 Christian Brauner <christian.brauner@ubuntu.com>.
4 * Copyright © 2018 Canonical Ltd.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2, as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef __LXC_STRING_UTILS_H
21 #define __LXC_STRING_UTILS_H
22
23 #include "config.h"
24
25 #include "initutils.h"
26 #include "macro.h"
27
28 /* convert variadic argument lists to arrays (for execl type argument lists) */
29 extern char **lxc_va_arg_list_to_argv(va_list ap, size_t skip, int do_strdup);
30 extern const char **lxc_va_arg_list_to_argv_const(va_list ap, size_t skip);
31
32 /*
33 * Some simple string functions; if they return pointers, they are allocated
34 * buffers.
35 */
36 extern char *lxc_string_replace(const char *needle, const char *replacement,
37 const char *haystack);
38 extern bool lxc_string_in_array(const char *needle, const char **haystack);
39 extern char *lxc_string_join(const char *sep, const char **parts,
40 bool use_as_prefix);
41 /*
42 * Normalize and split path: Leading and trailing / are removed, multiple
43 * / are compactified, .. and . are resolved (.. on the top level is considered
44 * identical to .).
45 * Examples:
46 * / -> { NULL }
47 * foo/../bar -> { bar, NULL }
48 * ../../ -> { NULL }
49 * ./bar/baz/.. -> { bar, NULL }
50 * foo//bar -> { foo, bar, NULL }
51 */
52 extern char **lxc_normalize_path(const char *path);
53
54 /* remove multiple slashes from the path, e.g. ///foo//bar -> /foo/bar */
55 extern char *lxc_deslashify(const char *path);
56 extern char *lxc_append_paths(const char *first, const char *second);
57
58 /*
59 * Note: the following two functions use strtok(), so they will never
60 * consider an empty element, even if two delimiters are next to
61 * each other.
62 */
63 extern bool lxc_string_in_list(const char *needle, const char *haystack,
64 char sep);
65 extern char **lxc_string_split(const char *string, char sep);
66 extern char **lxc_string_split_and_trim(const char *string, char sep);
67 extern char **lxc_string_split_quoted(char *string);
68
69 /* Append string to NULL-terminated string array. */
70 extern int lxc_append_string(char ***list, char *entry);
71
72 /* Some simple array manipulation utilities */
73 typedef void (*lxc_free_fn)(void *);
74 typedef void *(*lxc_dup_fn)(void *);
75 extern int lxc_grow_array(void ***array, size_t *capacity, size_t new_size,
76 size_t capacity_increment);
77 extern void lxc_free_array(void **array, lxc_free_fn element_free_fn);
78 extern size_t lxc_array_len(void **array);
79
80 extern void **lxc_append_null_to_array(void **array, size_t count);
81 extern void remove_trailing_newlines(char *l);
82
83 /* Helper functions to parse numbers. */
84 extern int lxc_safe_uint(const char *numstr, unsigned int *converted);
85 extern int lxc_safe_int(const char *numstr, int *converted);
86 extern int lxc_safe_long(const char *numstr, long int *converted);
87 extern int lxc_safe_long_long(const char *numstr, long long int *converted);
88 extern int lxc_safe_ulong(const char *numstr, unsigned long *converted);
89 extern int lxc_safe_uint64(const char *numstr, uint64_t *converted, int base);
90 /* Handles B, kb, MB, GB. Detects overflows and reports -ERANGE. */
91 extern int parse_byte_size_string(const char *s, int64_t *converted);
92
93 /*
94 * Concatenate all passed-in strings into one path. Do not fail. If any piece
95 * is not prefixed with '/', add a '/'.
96 */
97 __attribute__((sentinel)) extern char *must_concat(const char *first, ...);
98 __attribute__((sentinel)) extern char *must_make_path(const char *first, ...);
99 __attribute__((sentinel)) extern char *must_append_path(char *first, ...);
100
101 /* Return copy of string @entry. Do not fail. */
102 extern char *must_copy_string(const char *entry);
103
104 /* Re-allocate a pointer, do not fail */
105 extern void *must_realloc(void *orig, size_t sz);
106
107 extern int lxc_char_left_gc(const char *buffer, size_t len);
108
109 extern int lxc_char_right_gc(const char *buffer, size_t len);
110
111 extern char *lxc_trim_whitespace_in_place(char *buffer);
112
113 extern int lxc_is_line_empty(const char *line);
114 extern void remove_trailing_slashes(char *p);
115
116 #endif /* __LXC_STRING_UTILS_H */