Greenbone Vulnerability Management Libraries: base/strings.c Source File

Greenbone Vulnerability Management Libraries 23.1.1

strings.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009-2023 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
10
11#include "strings.h"
12
13#include <assert.h> /* for assert */
14#include <glib.h> /* for g_free, g_strconcat, gchar, g_strdup, g_strndup */
15
16#undef G_LOG_DOMAIN
20#define G_LOG_DOMAIN "libgvm base"
21
35void
36gvm_append_string (gchar **var, const gchar *string)
37{
38 if (*var)
39 {
40 char *old = *var;
41 *var = g_strconcat (old, string, NULL);
42 g_free (old);
43 }
44 else
45 *var = g_strdup (string);
46}
47
65void
66gvm_append_text (gchar **var, const gchar *string, gsize length)
67{
68 if (*var)
69 {
70 char *old = *var;
71 *var = g_strconcat (old, string, NULL);
72 g_free (old);
73 }
74 else
75 *var = g_strndup (string, length);
76}
77
86void
87gvm_free_string_var (gchar **var)
88{
89 g_free (*var);
90 *var = NULL;
91}
92
107char *
108gvm_strip_space (char *string, char *end)
109{
110 assert (string <= end);
111 if (string >= end)
112 return string;
113 end--;
114 while (string[0] == ' ' || string[0] == '\n')
115 {
116 string++;
117 if (string >= end)
118 {
119 end[0] = '\0';
120 return end;
121 }
122 }
123
124 /* Here string is < end. */
125 if (end[0] == ' ' || end[0] == '\n')
126 {
127 end--;
128 while (end >= string && (end[0] == ' ' || end[0] == '\n'))
129 {
130 end--;
131 }
132 end[1] = '\0';
133 }
134 return string;
135}
gvm_append_text
void gvm_append_text(gchar **var, const gchar *string, gsize length)
Append a string of a known length to a string variable.
Definition strings.c:66
gvm_append_string
void gvm_append_string(gchar **var, const gchar *string)
Append a string to a string variable.
Definition strings.c:36
gvm_strip_space
char * gvm_strip_space(char *string, char *end)
"Strip" space and newline characters from either end of some memory.
Definition strings.c:108
gvm_free_string_var
void gvm_free_string_var(gchar **var)
Free a string variable.
Definition strings.c:87
strings.h
String utilities.