GnuCash fec5643+
Loading...
Searching...
No Matches
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.
 
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.
 
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.
 

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.
 

Character Sets

int safe_utf8_collate (const char *str1, const char *str2)
 Collate two UTF-8 strings.
 
int safe_utf8_collate_natural (const char *str1, const char *str2)
 Collate two UTF-8 strings naturally.
 
gboolean gnc_utf8_validate (const gchar *str, gssize max_len, const gchar **end)
 Validates UTF-8 encoded text for use in GnuCash.
 
void gnc_utf8_strip_invalid (gchar *str)
 Strip any non-UTF-8 characters from a string.
 
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.
 
void gnc_utf8_strip_invalid_and_controls (gchar *str)
 Strip any non-utf8 characters and any control characters (everything < 0x20, , \f,
, \r, \t, and \v) from a string.
 
gchar * gnc_locale_from_utf8 (const gchar *str)
 Converts a string from UTF-8 to the encoding used for strings in the current locale.
 
gchar * gnc_locale_to_utf8 (const gchar *str)
 Converts a string to UTF-8 from the encoding used for strings in the current locale.
 

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.

Typedef Documentation

◆ GncGMapFunc

typedef gpointer(* GncGMapFunc) (gpointer data, gpointer user_data)

Definition at line 168 of file gnc-glib-utils.h.

Function Documentation

◆ gnc_g_list_cut()

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.

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

305{
306 if (list == NULL || *list == NULL)
307 return;
308
309 // if it's the first element.
310 if (cut_point->prev == NULL)
311 {
312 *list = NULL;
313 return;
314 }
315
316 cut_point->prev->next = NULL;
317 cut_point->prev = NULL;
318}

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

294{
295 GList *rtn = NULL;
296 for (; list != NULL; list = list->next)
297 {
298 rtn = g_list_prepend (rtn, (*fn)(list->data, user_data));
299 }
300 return g_list_reverse (rtn);
301}

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

375{
376 return gnc_g_list_stringjoin_internal (list_of_strings, sep, false);
377}

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

381{
382 return gnc_g_list_stringjoin_internal (list_of_strings, sep, true);
383}

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

387{
388 for (GList *lst = (GList*) list;; lst = g_list_next (lst), len--)
389 {
390 if (!lst) return (len ? -1 : 0);
391 if (!len) return 1;
392 }
393}

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

258{
259 gchar * locale_str;
260 gsize bytes_written = 0;
261 GError * err = NULL;
262
263 /* Convert from UTF-8 to the encoding used in the current locale. */
264 locale_str = g_locale_from_utf8(str, -1, NULL, &bytes_written, &err);
265 if (err)
266 {
267 g_warning("g_locale_from_utf8 failed: %s", err->message);
268 g_error_free(err);
269 }
270
271 return locale_str;
272}

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

276{
277 gchar * utf8_str;
278 gsize bytes_written = 0;
279 GError * err = NULL;
280
281 /* Convert to UTF-8 from the encoding used in the current locale. */
282 utf8_str = g_locale_to_utf8(str, -1, NULL, &bytes_written, &err);
283 if (err)
284 {
285 g_warning("g_locale_to_utf8 failed: %s", err->message);
286 g_error_free(err);
287 }
288
289 return utf8_str;
290}

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

215{
216 gchar *end;
217 gint len;
218
219 g_return_if_fail(str);
220
221 if (gnc_utf8_validate(str, -1, (const gchar **)&end))
222 return;
223
224 g_warning("Invalid utf8 string: %s", str);
225 do
226 {
227 len = strlen(end);
228 memmove(end, end + 1, len); /* shuffle the remainder one byte */
229 }
230 while (!gnc_utf8_validate(str, -1, (const gchar **)&end));
231}
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, , \f,
, \r, \t, and \v) from a string.

Rewrites the string in-place.

Parameters
strPointer to the string to clean up.

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

243{
244 gchar *c = NULL;
245 const gchar *controls = "\b\f\n\r\t\v";
246 g_return_if_fail (str != NULL && strlen (str) > 0);
247 gnc_utf8_strip_invalid (str); /* First fix the UTF-8 */
248 for(c = str + strlen (str) - 1; c != str; --c)
249 {
250 gboolean line_control = ((unsigned char)(*c) < 0x20);
251 if (line_control || strchr(controls, *c) != NULL)
252 *c = ' '; /*replace controls with a single space. */
253 }
254}
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 234 of file gnc-glib-utils.c.

235{
236 gchar *result = g_strdup (str);
237 gnc_utf8_strip_invalid (result);
238 return result;
239}

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

156{
157
158 const gchar *p;
159
160 g_return_val_if_fail (str != NULL, FALSE);
161
162 if (end)
163 *end = str;
164
165 p = str;
166
167 while ((max_len < 0 || (p - str) < max_len) && *p)
168 {
169 int i, mask = 0, len;
170 gunichar result;
171 unsigned char c = (unsigned char) * p;
172
173 UTF8_COMPUTE (c, mask, len);
174
175 if (len == -1)
176 break;
177
178 /* check that the expected number of bytes exists in str */
179 if (max_len >= 0 &&
180 ((max_len - (p - str)) < len))
181 break;
182
183 UTF8_GET (result, p, i, mask, len);
184
185 if (UTF8_LENGTH (result) != len) /* Check for overlong UTF-8 */
186 break;
187
188 if (result == (gunichar) - 1)
189 break;
190
191 if (!UNICODE_VALID (result))
192 break;
193
194 p += len;
195 }
196
197 if (end)
198 *end = p;
199
200 /* See that we covered the entire length if a length was
201 * passed in, or that we ended on a nul if not
202 */
203 if (max_len >= 0 &&
204 p != (str + max_len))
205 return FALSE;
206 else if (max_len < 0 &&
207 *p != '\0')
208 return FALSE;
209 else
210 return TRUE;
211}

◆ 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}

◆ safe_utf8_collate_natural()

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

Collate two UTF-8 strings naturally.

This function performs basic argument checking before calling g_utf8_collate_key_for_filename.

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

56{
57 if (da && !(*da))
58 da = NULL;
59
60 if (db && !(*db))
61 db = NULL;
62
63 if (da && db)
64 {
65 gchar *a = g_utf8_collate_key_for_filename(da ? da : "", -1);
66 gchar *b = g_utf8_collate_key_for_filename(db ? db : "", -1);
67
68 int result = strcmp(a, b);
69
70 g_free(a);
71 g_free(b);
72
73 return result;
74 }
75 else if (da)
76 return 1;
77 else if (db)
78 return -1;
79
80 return 0;
81}