diff --git a/src/gm-support.c b/src/gm-support.c index 8720d91..2fb128c 100644 --- a/src/gm-support.c +++ b/src/gm-support.c @@ -1,5 +1,5 @@ #ifdef HAVE_CONFIG_H -# include +#include "../config.h" #endif #include @@ -25,16 +25,20 @@ static regex_t url_regexp; gchar * gm_fix_decimal_point(gchar *line, int len) { - int i; - struct lconv *l = localeconv(); + int i; + struct lconv *l = localeconv(); - for (i = 0; i < len; i++) { - if (line[i] == '.') { - line[i] = l->decimal_point[0]; - } - } + if (l->decimal_point[0] == '.') { + return line; + } + + for (i = 0; i < len; i++) { + if (line[i] == '.') { + line[i] = l->decimal_point[0]; + } + } - return line; + return line; } gchar * @@ -563,3 +567,81 @@ gm_default_charset() { return loc; } + +void +gm_notebook_focus_from_label(GtkNotebook *note, gchar *caption) { + int p = gtk_notebook_get_n_pages(note); + int i; + GtkWidget *child; + + for (i = 0; i < p; i++) { + child = gtk_notebook_get_nth_page(note, i); + if (!g_strcasecmp(gtk_notebook_get_tab_label_text(note, child), + caption)) { + gtk_notebook_set_current_page(note, i); + break; + } + } +} + +void +gm_string_skip_space(gchar **ptr) { + while (**ptr != '\0' && g_unichar_isspace(g_utf8_get_char(*ptr))) { + *ptr = g_utf8_next_char(*ptr); + } +} + +void +gm_string_skip_nonspace(gchar **ptr) { + while (**ptr != '\0' && !g_unichar_isspace(g_utf8_get_char(*ptr))) { + *ptr = g_utf8_next_char(*ptr); + } +} + +void +gm_string_skip_till(gchar **ptr, gchar const *find) { + gchar const *fptr; + gunichar check; + + while (**ptr != '\0') { + check = g_utf8_get_char(*ptr); + + for (fptr = find; *fptr; ++fptr) { + // CHECK: find should also be treated as utf8! + if (check == (gunichar)(*fptr)) { + break; + } + } + + *ptr = g_utf8_next_char(*ptr); + } +} + +gchar * +gm_to_utf8_with_fallback(gchar const *text, gssize len, gchar const *from, + gchar const *fallback) { + gchar *res; + gsize read, written; + GString *str = g_string_new(""); + + // TODO: use g_iconv instead of g_convert + + while ((res = g_convert(text, len, "UTF-8", from, &read, &written, NULL)) + == NULL) { + res = g_convert(text, read, "UTF-8", from, NULL, NULL, NULL); + str = g_string_append(str, res); + + str = g_string_append(str, fallback); + text = text + read + 1; + + if (len != -1) + len = len - read - 1; + } + + str = g_string_append(str, res); + g_free(res); + + res = str->str; + g_string_free(str, FALSE); + return res; +}