GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Files
GLib Helpers

Files

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

Character Sets

int safe_utf8_collate (const char *str1, const char *str2)
 
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)
 
gchar * gnc_utf8_strip_invalid_strdup (const gchar *str)
 
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)
 

Message Logging

void gnc_scm_log_warn (const gchar *msg)
 
void gnc_scm_log_error (const gchar *msg)
 
void gnc_scm_log_msg (const gchar *msg)
 
void gnc_scm_log_debug (const gchar *msg)
 

glib Miscellaneous Functions

void gnc_gpid_kill (GPid pid)
 

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

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

260 {
261  if (list == NULL || *list == NULL)
262  return;
263 
264  // if it's the first element.
265  if (cut_point->prev == NULL)
266  {
267  *list = NULL;
268  return;
269  }
270 
271  cut_point->prev->next = NULL;
272  cut_point->prev = NULL;
273 }
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 248 of file gnc-glib-utils.c.

249 {
250  GList *rtn = NULL;
251  for (; list != NULL; list = list->next)
252  {
253  rtn = g_list_append(rtn, (*fn)(list->data, user_data));
254  }
255  return rtn;
256 }
void gnc_gpid_kill ( GPid  pid)

Kill a process. On UNIX send a SIGKILL, on Windows call TerminateProcess.

Parameters
pidThe process ID.

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

300 {
301 #ifdef G_OS_WIN32
302  if (!TerminateProcess((HANDLE) pid, 0))
303  {
304  gchar *msg = g_win32_error_message(GetLastError());
305  g_warning("Could not kill child process: %s", msg ? msg : "(null)");
306  g_free(msg);
307  }
308 #else /* !G_OS_WIN32 */
309  if (kill(pid, SIGKILL))
310  {
311  g_warning("Could not kill child process: %s", g_strerror(errno));
312  }
313 #endif /* G_OS_WIN32 */
314 }
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 212 of file gnc-glib-utils.c.

213 {
214  gchar * locale_str;
215  gsize bytes_written = 0;
216  GError * err = NULL;
217 
218  /* Convert from UTF-8 to the encoding used in the current locale. */
219  locale_str = g_locale_from_utf8(str, -1, NULL, &bytes_written, &err);
220  if (err)
221  {
222  g_warning("g_locale_from_utf8 failed: %s", err->message);
223  g_error_free(err);
224  }
225 
226  return locale_str;
227 }
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 230 of file gnc-glib-utils.c.

231 {
232  gchar * utf8_str;
233  gsize bytes_written = 0;
234  GError * err = NULL;
235 
236  /* Convert to UTF-8 from the encoding used in the current locale. */
237  utf8_str = g_locale_to_utf8(str, -1, NULL, &bytes_written, &err);
238  if (err)
239  {
240  g_warning("g_locale_to_utf8 failed: %s", err->message);
241  g_error_free(err);
242  }
243 
244  return utf8_str;
245 }
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 http://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.
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 http://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)
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 http://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 }
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 }