GLib Helpers
[GLib]


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.


Files

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

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)

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.
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.
gchar * gnc_locale_to_utf8 (const gchar *str)
 Converts a string to UTF-8 from the encoding used for strings in the current locale.

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)


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

00254 {
00255   if (list == NULL || *list == NULL)
00256     return;
00257 
00258   // if it's the first element.
00259   if (cut_point->prev == NULL)
00260   {
00261     *list = NULL;
00262     return;
00263   }
00264 
00265   cut_point->prev->next = NULL;
00266   cut_point->prev = NULL;
00267 }

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

00243 {
00244   GList *rtn = NULL;
00245   for (; list != NULL; list = list->next)
00246   {
00247     rtn = g_list_append(rtn, (*fn)(list->data, user_data));
00248   }
00249   return rtn;
00250 }

void gnc_gpid_kill ( GPid  pid  ) 

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

Parameters:
pid The process ID.

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

00294 {
00295 #ifdef G_OS_WIN32
00296     if (!TerminateProcess((HANDLE) pid, 0)) {
00297         gchar *msg = g_win32_error_message(GetLastError());
00298         g_warning("Could not kill child process: %s", msg ? msg : "(null)");
00299         g_free(msg);
00300     }
00301 #else /* !G_OS_WIN32 */
00302     if (kill(pid, SIGKILL)) {
00303         g_warning("Could not kill child process: %s", g_strerror(errno));
00304     }
00305 #endif /* G_OS_WIN32 */
00306 }

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:
str A 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 208 of file gnc-glib-utils.c.

00209 {
00210   gchar *   locale_str;
00211   gsize     bytes_written = 0;
00212   GError *  err = NULL;
00213 
00214   /* Convert from UTF-8 to the encoding used in the current locale. */
00215   locale_str = g_locale_from_utf8(str, -1, NULL, &bytes_written, &err);
00216   if (err) {
00217     g_warning("g_locale_from_utf8 failed: %s", err->message);
00218     g_error_free(err);
00219   }
00220 
00221   return locale_str;
00222 }

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:
str A 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 225 of file gnc-glib-utils.c.

00226 {
00227   gchar *   utf8_str;
00228   gsize     bytes_written = 0;
00229   GError *  err = NULL;
00230 
00231   /* Convert to UTF-8 from the encoding used in the current locale. */
00232   utf8_str = g_locale_to_utf8(str, -1, NULL, &bytes_written, &err);
00233   if (err) {
00234     g_warning("g_locale_to_utf8 failed: %s", err->message);
00235     g_error_free(err);
00236   }
00237 
00238   return utf8_str;
00239 }

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:
str A pointer to the string to strip of invalid characters.

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

00185 {
00186   gchar *end;
00187   gint len;
00188 
00189   if (gnc_utf8_validate(str, -1, (const gchar **)&end))
00190     return;
00191 
00192   g_warning("Invalid utf8 string: %s", str);
00193   do {
00194     len = strlen(end);
00195     memmove(end, end+1, len);   /* shuffle the remainder one byte */
00196   } while (!gnc_utf8_validate(str, -1, (const gchar **)&end));
00197 }

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:
str A 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 200 of file gnc-glib-utils.c.

00201 {
00202   gchar *result = g_strdup (str);
00203   gnc_utf8_strip_invalid (result);
00204   return result;
00205 }

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:
str a pointer to character data
max_len max bytes to validate, or -1 to go until NUL
end return 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.

00126 {
00127 
00128   const gchar *p;
00129 
00130   g_return_val_if_fail (str != NULL, FALSE);
00131   
00132   if (end)
00133     *end = str;
00134   
00135   p = str;
00136   
00137   while ((max_len < 0 || (p - str) < max_len) && *p)
00138     {
00139       int i, mask = 0, len;
00140       gunichar result;
00141       unsigned char c = (unsigned char) *p;
00142       
00143       UTF8_COMPUTE (c, mask, len);
00144 
00145       if (len == -1)
00146         break;
00147 
00148       /* check that the expected number of bytes exists in str */
00149       if (max_len >= 0 &&
00150           ((max_len - (p - str)) < len))
00151         break;
00152         
00153       UTF8_GET (result, p, i, mask, len);
00154 
00155       if (UTF8_LENGTH (result) != len) /* Check for overlong UTF-8 */
00156         break;
00157 
00158       if (result == (gunichar)-1)
00159         break;
00160 
00161       if (!UNICODE_VALID (result))
00162         break;
00163       
00164       p += len;
00165     }
00166 
00167   if (end)
00168     *end = p;
00169 
00170   /* See that we covered the entire length if a length was
00171    * passed in, or that we ended on a nul if not
00172    */
00173   if (max_len >= 0 &&
00174       p != (str + max_len))
00175     return FALSE;
00176   else if (max_len < 0 &&
00177            *p != '\0')
00178     return FALSE;
00179   else
00180     return TRUE;
00181 }

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:
str1 The first string.
str2 The 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.

00038 {
00039   if (da && !(*da))
00040     da = NULL;
00041   if (db && !(*db))
00042     db = NULL;
00043 
00044   if (da && db)
00045     return g_utf8_collate(da, db);
00046   if (da)
00047     return 1;
00048   if (db)
00049     return -1;
00050   return 0;
00051 }


Generated on Thu Jul 3 05:07:15 2008 for GnuCash by  doxygen 1.5.2