GnuCash  5.6-133-gc519490283+
Files | Functions
GLib Helpers

The API in this file is designed to provide support functions that wrap the base glib functions and make them easier to use. More...

Files

file  gnc-glib-utils.h
 GLib helper routines.
 

Functions

gchar * gnc_g_list_stringjoin (GList *list_of_strings, const gchar *sep)
 Return a string joining a GList whose elements are gchar* strings. More...
 
gint gnc_list_length_cmp (const GList *list, size_t len)
 Scans the GList elements the minimum number of iterations required to test it against a specified size. More...
 

Character Sets

int safe_utf8_collate (const char *str1, const char *str2)
 Collate two UTF-8 strings. More...
 
gboolean gnc_utf8_validate (const gchar *str, gssize max_len, const gchar **end)
 Validates UTF-8 encoded text for use in GnuCash. More...
 
void gnc_utf8_strip_invalid (gchar *str)
 Strip any non-UTF-8 characters from a string. More...
 
gchar * gnc_utf8_strip_invalid_strdup (const gchar *str)
 Returns a newly allocated copy of the given string but with any non-UTF-8 character stripped from it. More...
 
void gnc_utf8_strip_invalid_and_controls (gchar *str)
 Strip any non-utf8 characters and any control characters (everything < 0x20, , ,
, , , and ) from a string. More...
 
gchar * gnc_locale_from_utf8 (const gchar *str)
 Converts a string from UTF-8 to the encoding used for strings in the current locale. More...
 
gchar * gnc_locale_to_utf8 (const gchar *str)
 Converts a string to UTF-8 from the encoding used for strings in the current locale. More...
 

GList Manipulation

typedef gpointer(* GncGMapFunc) (gpointer data, gpointer user_data)
 
GList * gnc_g_list_map (GList *list, GncGMapFunc fn, gpointer user_data)
 
void gnc_g_list_cut (GList **list, GList *cut_point)
 Cut a GList into two parts; the cut_point is the beginning of the new list; list may need to be modified, but will be the list before the cut_point.
 

Detailed Description

The API in this file is designed to provide support functions that wrap the base glib functions and make them easier to use.

Function Documentation

◆ gnc_g_list_map()

GList* gnc_g_list_map ( GList *  list,
GncGMapFunc  fn,
gpointer  user_data 
)
Returns
Caller-owned GList* of results of apply fn to list in order.

Definition at line 263 of file gnc-glib-utils.c.

264 {
265  GList *rtn = NULL;
266  for (; list != NULL; list = list->next)
267  {
268  rtn = g_list_prepend (rtn, (*fn)(list->data, user_data));
269  }
270  return g_list_reverse (rtn);
271 }

◆ gnc_g_list_stringjoin()

gchar* gnc_g_list_stringjoin ( GList *  list_of_strings,
const gchar *  sep 
)

Return a string joining a GList whose elements are gchar* strings.

Returns NULL if an empty GList* is passed through. The optional sep string will be used as separator. Must be g_freed when not needed anymore.

Parameters
list_of_stringsA GList of chars*
sepa separator or NULL
Returns
A newly allocated string that has to be g_free'd by the caller.

Definition at line 292 of file gnc-glib-utils.c.

293 {
294  gint seplen = sep ? strlen(sep) : 0;
295  gint length = -seplen;
296  gchar *retval, *p;
297 
298  for (GList *n = list_of_strings; n; n = n->next)
299  {
300  gchar *str = n->data;
301  if (str && *str)
302  length += strlen (str) + seplen;
303  }
304 
305  if (length <= 0)
306  return NULL;
307 
308  p = retval = (gchar*) g_malloc0 (length * sizeof (gchar) + 1);
309  for (GList *n = list_of_strings; n; n = n->next)
310  {
311  gchar *str = n->data;
312  if (!str || !str[0])
313  continue;
314  if (sep && (p != retval))
315  p = g_stpcpy (p, sep);
316  p = g_stpcpy (p, str);
317  }
318 
319  return retval;
320 }

◆ gnc_list_length_cmp()

gint gnc_list_length_cmp ( const GList *  list,
size_t  len 
)

Scans the GList elements the minimum number of iterations required to test it against a specified size.

Returns -1, 0 or 1 depending on whether the GList length has less, same, or more elements than the size specified.

Parameters
lstA GList
lenthe comparator length to compare the GList length with
Returns
A signed int comparing GList length with specified size.

Definition at line 323 of file gnc-glib-utils.c.

324 {
325  for (GList *lst = (GList*) list;; lst = g_list_next (lst), len--)
326  {
327  if (!lst) return (len ? -1 : 0);
328  if (!len) return 1;
329  }
330 }

◆ gnc_locale_from_utf8()

gchar* gnc_locale_from_utf8 ( const gchar *  str)

Converts a string from UTF-8 to the encoding used for strings in the current locale.

This essentially is a wrapper for g_locale_from_utf8 that can be swigified for use with Scheme to avoid adding a dependency for guile-glib.

Parameters
strA pointer to a UTF-8 encoded string to be converted.
Returns
A newly allocated string that has to be g_free'd by the caller. If an error occurs, NULL is returned.

Definition at line 227 of file gnc-glib-utils.c.

228 {
229  gchar * locale_str;
230  gsize bytes_written = 0;
231  GError * err = NULL;
232 
233  /* Convert from UTF-8 to the encoding used in the current locale. */
234  locale_str = g_locale_from_utf8(str, -1, NULL, &bytes_written, &err);
235  if (err)
236  {
237  g_warning("g_locale_from_utf8 failed: %s", err->message);
238  g_error_free(err);
239  }
240 
241  return locale_str;
242 }

◆ gnc_locale_to_utf8()

gchar* gnc_locale_to_utf8 ( const gchar *  str)

Converts a string to UTF-8 from the encoding used for strings in the current locale.

This essentially is a wrapper for g_locale_to_utf8 that can be swigified for use with Scheme to avoid adding a dependency for guile-glib.

Parameters
strA pointer to a string encoded according to locale.
Returns
A newly allocated string that has to be g_free'd by the caller. If an error occurs, NULL is returned.

Definition at line 245 of file gnc-glib-utils.c.

246 {
247  gchar * utf8_str;
248  gsize bytes_written = 0;
249  GError * err = NULL;
250 
251  /* Convert to UTF-8 from the encoding used in the current locale. */
252  utf8_str = g_locale_to_utf8(str, -1, NULL, &bytes_written, &err);
253  if (err)
254  {
255  g_warning("g_locale_to_utf8 failed: %s", err->message);
256  g_error_free(err);
257  }
258 
259  return utf8_str;
260 }

◆ gnc_utf8_strip_invalid()

void gnc_utf8_strip_invalid ( gchar *  str)

Strip any non-UTF-8 characters from a string.

This function rewrites the string "in place" instead of allocating and returning a new string. This allows it to operate on strings that are defined as character arrays in a larger data structure. Note that it also removes some subset of invalid XML characters, too. See https://www.w3.org/TR/REC-xml/#NT-Char linked from bug #346535

Parameters
strA pointer to the string to strip of invalid characters.

Definition at line 184 of file gnc-glib-utils.c.

185 {
186  gchar *end;
187  gint len;
188 
189  g_return_if_fail(str);
190 
191  if (gnc_utf8_validate(str, -1, (const gchar **)&end))
192  return;
193 
194  g_warning("Invalid utf8 string: %s", str);
195  do
196  {
197  len = strlen(end);
198  memmove(end, end + 1, len); /* shuffle the remainder one byte */
199  }
200  while (!gnc_utf8_validate(str, -1, (const gchar **)&end));
201 }
gboolean gnc_utf8_validate(const gchar *str, gssize max_len, const gchar **end)
Validates UTF-8 encoded text for use in GnuCash.

◆ gnc_utf8_strip_invalid_and_controls()

void gnc_utf8_strip_invalid_and_controls ( gchar *  str)

Strip any non-utf8 characters and any control characters (everything < 0x20, , ,
, , , and ) from a string.

Rewrites the string in-place.

Parameters
strPointer to the string to clean up.

Definition at line 212 of file gnc-glib-utils.c.

213 {
214  gchar *c = NULL;
215  const gchar *controls = "\b\f\n\r\t\v";
216  g_return_if_fail (str != NULL && strlen (str) > 0);
217  gnc_utf8_strip_invalid (str); /* First fix the UTF-8 */
218  for(c = str + strlen (str) - 1; c != str; --c)
219  {
220  gboolean line_control = ((unsigned char)(*c) < 0x20);
221  if (line_control || strchr(controls, *c) != NULL)
222  *c = ' '; /*replace controls with a single space. */
223  }
224 }
void gnc_utf8_strip_invalid(gchar *str)
Strip any non-UTF-8 characters from a string.

◆ gnc_utf8_strip_invalid_strdup()

gchar* gnc_utf8_strip_invalid_strdup ( const gchar *  str)

Returns a newly allocated copy of the given string but with any non-UTF-8 character stripped from it.

Note that it also removes some subset of invalid XML characters, too. See https://www.w3.org/TR/REC-xml/#NT-Char linked from bug #346535

Parameters
strA pointer to the string to be copied and stripped of non-UTF-8 characters.
Returns
A newly allocated string that has to be g_free'd by the caller.

Definition at line 204 of file gnc-glib-utils.c.

205 {
206  gchar *result = g_strdup (str);
207  gnc_utf8_strip_invalid (result);
208  return result;
209 }
void gnc_utf8_strip_invalid(gchar *str)
Strip any non-UTF-8 characters from a string.

◆ gnc_utf8_validate()

gboolean gnc_utf8_validate ( const gchar *  str,
gssize  max_len,
const gchar **  end 
)

Validates UTF-8 encoded text for use in GnuCash.

Validates the strict subset of UTF-8 that is valid XML text, as detailed in https://www.w3.org/TR/REC-xml/#NT-Char linked from bug #346535.

Copied from g_utf8_validate():

Validates UTF-8 encoded text, where str is the text to validate; if str is nul-terminated, then max_len can be -1, otherwise max_len should be the number of bytes to validate. If end is non-NULL, then the end of the valid range will be stored there (i.e. the start of the first invalid character if some bytes were invalid, or the end of the text being validated otherwise).

Returns TRUE if all of str was valid. Many GLib and GTK+ routines require valid UTF-8 as input; so data read from a file or the network should be checked with g_utf8_validate() before doing anything else with it.

Parameters
stra pointer to character data
max_lenmax bytes to validate, or -1 to go until NUL
endreturn location for end of valid data
Returns
TRUE if the text was valid UTF-8.

Definition at line 123 of file gnc-glib-utils.c.

126 {
127 
128  const gchar *p;
129 
130  g_return_val_if_fail (str != NULL, FALSE);
131 
132  if (end)
133  *end = str;
134 
135  p = str;
136 
137  while ((max_len < 0 || (p - str) < max_len) && *p)
138  {
139  int i, mask = 0, len;
140  gunichar result;
141  unsigned char c = (unsigned char) * p;
142 
143  UTF8_COMPUTE (c, mask, len);
144 
145  if (len == -1)
146  break;
147 
148  /* check that the expected number of bytes exists in str */
149  if (max_len >= 0 &&
150  ((max_len - (p - str)) < len))
151  break;
152 
153  UTF8_GET (result, p, i, mask, len);
154 
155  if (UTF8_LENGTH (result) != len) /* Check for overlong UTF-8 */
156  break;
157 
158  if (result == (gunichar) - 1)
159  break;
160 
161  if (!UNICODE_VALID (result))
162  break;
163 
164  p += len;
165  }
166 
167  if (end)
168  *end = p;
169 
170  /* See that we covered the entire length if a length was
171  * passed in, or that we ended on a nul if not
172  */
173  if (max_len >= 0 &&
174  p != (str + max_len))
175  return FALSE;
176  else if (max_len < 0 &&
177  *p != '\0')
178  return FALSE;
179  else
180  return TRUE;
181 }

◆ safe_utf8_collate()

int safe_utf8_collate ( const char *  str1,
const char *  str2 
)

Collate two UTF-8 strings.

This function performs basic argument checking before calling g_utf8_collate.

Parameters
str1The first string.
str2The first string.
Returns
Same return value as g_utf8_collate. The values are: < 0 if str1 compares before str2, 0 if they compare equal, > 0 if str1 compares after str2.

Definition at line 37 of file gnc-glib-utils.c.

38 {
39  if (da && !(*da))
40  da = NULL;
41  if (db && !(*db))
42  db = NULL;
43 
44  if (da && db)
45  return g_utf8_collate(da, db);
46  if (da)
47  return 1;
48  if (db)
49  return -1;
50  return 0;
51 }