OpenVAS Libraries  9.0.3
openvas_string.c File Reference

String utilities. More...

#include <assert.h>
#include <ctype.h>
#include <glib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "openvas_string.h"
Include dependency graph for openvas_string.c:

Go to the source code of this file.

Macros

#define TRACE   1
 Trace flag. More...
 
#define G_LOG_DOMAIN   "md string"
 GLib log domain. More...
 

Functions

void openvas_append_string (gchar **var, const gchar *string)
 Append a string to a string variable. More...
 
void openvas_append_text (gchar **var, const gchar *string, gsize length)
 Append a string of a known length to a string variable. More...
 
void openvas_free_string_var (string *var)
 Free a string variable. More...
 
char * openvas_strip_space (char *string, char *end)
 "Strip" space and newline characters from either end of some memory. More...
 

Detailed Description

String utilities.

Definition in file openvas_string.c.

Macro Definition Documentation

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "md string"

GLib log domain.

Definition at line 56 of file openvas_string.c.

◆ TRACE

#define TRACE   1

Trace flag.

0 to turn off all tracing messages.

Definition at line 36 of file openvas_string.c.

Function Documentation

◆ openvas_append_string()

void openvas_append_string ( gchar **  var,
const gchar *  string 
)

Append a string to a string variable.

When the variable is NULL store a copy of the given string in the variable.

When the variable already contains a string replace the string with a new string that is the concatenation of the two, freeing the old string. It is up to the caller to free the given string if it was dynamically allocated.

Parameters
[in]varThe address of a string variable, that is, a pointer to a string.
[in]stringThe string to append to the string in the variable.

Definition at line 72 of file openvas_string.c.

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 }

◆ openvas_append_text()

void openvas_append_text ( gchar **  var,
const gchar *  string,
gsize  length 
)

Append a string of a known length to a string variable.

When the variable is NULL store a copy of the given string in the variable.

When the variable already contains a string replace the string with a new string that is the concatenation of the two, freeing the old string. It is up to the caller to free the given string if it was dynamically allocated.

The string must be NULL terminated, and the given length must be the actual length of the string.

Parameters
[in]varThe address of a string variable, that is, a pointer to a string.
[in]stringThe string to append to the string in the variable.
[in]lengthThe length of string.

Definition at line 102 of file openvas_string.c.

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 }

Referenced by append_to_credentials_password(), and append_to_credentials_username().

Here is the caller graph for this function:

◆ openvas_free_string_var()

void openvas_free_string_var ( string var)

Free a string variable.

Free the string in the variable and set the variable to NULL.

Parameters
[in]varThe address of a string variable, that is, a pointer to a string.

Definition at line 123 of file openvas_string.c.

124 {
125  g_free (*var);
126  *var = NULL;
127 }

◆ openvas_strip_space()

char* openvas_strip_space ( char *  string,
char *  end 
)

"Strip" space and newline characters from either end of some memory.

Return the given pointer moved forward past any spaces, replacing the first of any contiguous spaces at or before the end of the memory with a terminating NULL.

This is for use when string points into a static buffers.

Parameters
[in,out]stringThe start of the memory.
[in]endPointer to the byte after the end of the memory.
Returns
A new pointer into the string.

Definition at line 144 of file openvas_string.c.

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 }
gchar * string

References range::end.