OpenVAS Libraries  9.0.3
openvas_string.c
Go to the documentation of this file.
1 /* openvas-libraries/base
2  * $Id$
3  * Description: String utilities.
4  *
5  * Authors:
6  * Matthew Mundell <matt@mundell.ukfsn.org>
7  *
8  * Copyright:
9  * Copyright (C) 2009,2010 Greenbone Networks GmbH
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25 
36 #define TRACE 1
37 
38 #include <assert.h>
39 #include <ctype.h>
40 #include <glib.h>
41 #include <stdio.h>
42 #include <string.h> /* for strcmp */
43 #include <sys/types.h>
44 #include <unistd.h>
45 
46 // FIX
47 #if 0
48 #include "tracef.h"
49 #endif
50 #include "openvas_string.h"
51 
52 #undef G_LOG_DOMAIN
53 
56 #define G_LOG_DOMAIN "md string"
57 
71 void
72 openvas_append_string (gchar ** var, const gchar * string)
73 {
74  if (*var)
75  {
76  char *old = *var;
77  *var = g_strconcat (old, string, NULL);
78  g_free (old);
79  }
80  else
81  *var = g_strdup (string);
82 }
83 
101 void
102 openvas_append_text (gchar ** var, const gchar * string, gsize length)
103 {
104  if (*var)
105  {
106  char *old = *var;
107  *var = g_strconcat (old, string, NULL);
108  g_free (old);
109  }
110  else
111  *var = g_strndup (string, length);
112 }
113 
122 void
124 {
125  g_free (*var);
126  *var = NULL;
127 }
128 
143 char *
144 openvas_strip_space (char *string, char *end)
145 {
146  assert (string <= end);
147  if (string >= end)
148  return string;
149  end--;
150  while (string[0] == ' ' || string[0] == '\n')
151  {
152  string++;
153  if (string >= end)
154  {
155  end[0] = '\0';
156  return end;
157  }
158  }
159 
160  /* Here string is < end. */
161  if (end[0] == ' ' || end[0] == '\n')
162  {
163  end--;
164  while (end >= string && (end[0] == ' ' || end[0] == '\n'))
165  {
166  end--;
167  }
168  end[1] = '\0';
169  }
170  return string;
171 }
void openvas_append_text(gchar **var, const gchar *string, gsize length)
Append a string of a known length to a string variable.
void openvas_append_string(gchar **var, const gchar *string)
Append a string to a string variable.
gchar * string
char * openvas_strip_space(char *string, char *end)
"Strip" space and newline characters from either end of some memory.
void openvas_free_string_var(string *var)
Free a string variable.