GnuCash  5.6-150-g038405b370+
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...
 
gchar * gnc_g_list_stringjoin_nodups (GList *list_of_strings, const gchar *sep)
 Like stringjoin but ensures that the string to be added isn't already part of the return string. 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 264 of file gnc-glib-utils.c.

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

◆ 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 345 of file gnc-glib-utils.c.

346 {
347  return gnc_g_list_stringjoin_internal (list_of_strings, sep, false);
348 }

◆ gnc_g_list_stringjoin_nodups()

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

Like stringjoin but ensures that the string to be added isn't already part of the return string.

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 351 of file gnc-glib-utils.c.

352 {
353  return gnc_g_list_stringjoin_internal (list_of_strings, sep, true);
354 }

◆ 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 357 of file gnc-glib-utils.c.

358 {
359  for (GList *lst = (GList*) list;; lst = g_list_next (lst), len--)
360  {
361  if (!lst) return (len ? -1 : 0);
362  if (!len) return 1;
363  }
364 }

◆ 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 228 of file gnc-glib-utils.c.

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

◆ 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 246 of file gnc-glib-utils.c.

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

◆ 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 185 of file gnc-glib-utils.c.

186 {
187  gchar *end;
188  gint len;
189 
190  g_return_if_fail(str);
191 
192  if (gnc_utf8_validate(str, -1, (const gchar **)&end))
193  return;
194 
195  g_warning("Invalid utf8 string: %s", str);
196  do
197  {
198  len = strlen(end);
199  memmove(end, end + 1, len); /* shuffle the remainder one byte */
200  }
201  while (!gnc_utf8_validate(str, -1, (const gchar **)&end));
202 }
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 213 of file gnc-glib-utils.c.

214 {
215  gchar *c = NULL;
216  const gchar *controls = "\b\f\n\r\t\v";
217  g_return_if_fail (str != NULL && strlen (str) > 0);
218  gnc_utf8_strip_invalid (str); /* First fix the UTF-8 */
219  for(c = str + strlen (str) - 1; c != str; --c)
220  {
221  gboolean line_control = ((unsigned char)(*c) < 0x20);
222  if (line_control || strchr(controls, *c) != NULL)
223  *c = ' '; /*replace controls with a single space. */
224  }
225 }
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 205 of file gnc-glib-utils.c.

206 {
207  gchar *result = g_strdup (str);
208  gnc_utf8_strip_invalid (result);
209  return result;
210 }
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 124 of file gnc-glib-utils.c.

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

◆ 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 38 of file gnc-glib-utils.c.

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