GuSnippetInfo* snippets_parse (char* snippet) { gint i, start, end; GError* err = NULL; gchar** result = NULL; GRegex* regex = NULL; GMatchInfo* match_info = NULL; const gchar* holders[] = { "\\$([0-9]+)", "\\${([0-9]*):?([^}]*)}", "\\$(FILENAME)", "\\${(FILENAME)}", "\\$(BASENAME)", "\\${(BASENAME)}", "\\$(SELECTED_TEXT)", "\\${(SELECTED_TEXT)}" /* Anyway to combine these? */ }; GuSnippetInfo* info = snippet_info_new (snippet); for (i = 0; i < sizeof(holders) / sizeof(holders[0]); ++i) { if (! (regex = g_regex_new (holders[i], G_REGEX_DOTALL, 0, &err))) { slog (L_ERROR, "g_regex_new (): %s\n", err->message); g_error_free (err); return info; } g_regex_match (regex, snippet, 0, &match_info); while (g_match_info_matches (match_info)) { result = g_match_info_fetch_all (match_info); g_match_info_fetch_pos (match_info, 0, &start, &end); /* Convert start, end to UTF8 offset */ char* s_start = g_substr(snippet, 0, start); char* s_end = g_substr(snippet, 0, end); start = g_utf8_strlen(s_start, -1); end = g_utf8_strlen(s_end, -1); g_free(s_start); g_free(s_end); if (i < 2) { snippet_info_append_holder (info, atoi (result[1]), start, end -start, result[2]); } else { snippet_info_append_holder (info, -1, start, end -start, result[1]); } slog (L_DEBUG, "Placeholder: (%s, %s, %s)\n", result[0], result[1], result[2]); g_match_info_next (match_info, NULL); g_strfreev (result); } g_regex_unref (regex); g_match_info_free (match_info); } info->einfo = g_list_sort (info->einfo, snippet_info_pos_cmp); info->einfo_sorted = g_list_copy (info->einfo); info->einfo_sorted = g_list_sort (info->einfo_sorted, snippet_info_num_cmp); return info; }
/* Add the background color reference. */ static void add_background_color_reference (xmlXPathContextPtr context, xmlChar *ref) { xmlChar *xpath = (xmlChar *) g_strdup_printf ("/iwb/svg:svg/svg:rect[@id='%s']", (gchar *) ref); xmlXPathObjectPtr result = xmlXPathEvalExpression (xpath, context); xmlNodePtr node = result->nodesetval->nodeTab[0]; /* e.g. rbg(255,255,255) */ xmlChar *fill = xmlGetProp (node, (xmlChar *) "fill"); gint open_bracket_index = g_substrlastpos ((const gchar *)fill, "("); gint comma_index = g_substrlastpos ((const gchar *)fill, ","); gint close_bracket_index = g_substrlastpos ((const gchar *)fill, ")"); gchar *blue = g_substr ((const gchar *)fill, comma_index+1, close_bracket_index-1); guint16 bd = (guint16) g_ascii_strtoull (blue, NULL, 10); g_free(blue); gchar *remain_string = g_substr ((const gchar *)fill, open_bracket_index+1, comma_index-1); g_free ( (gchar*) fill); /* e.g. 255,255 */ comma_index = g_substrlastpos ((const gchar *)remain_string, ","); gchar *red = g_substr ((const gchar *)remain_string, 0, comma_index-1); guint16 rd = (guint16) g_ascii_strtoull (red, NULL, 10); g_free(red); gchar *green = g_substr ((const gchar *)remain_string, comma_index+1, strlen (remain_string)-1); guint16 gd = (guint16) g_ascii_strtoull (green, NULL, 10); g_free(green); g_free(remain_string); /* e.g. 255 */ xmlChar *alpha = xmlGetProp (node, (xmlChar *) "fill-opacity"); guint16 ad = (guint16) g_ascii_strtoull ((gchar *) alpha, NULL, 10); /* FFFFFFFF */ gchar *rgba = g_strdup_printf ("%02x%02x%02x%02x", rd, gd, bd, ad); if (g_strcmp0 (rgba, "00000000") != 0) { set_background_type (1); update_background_color (rgba); } else { update_background_color (rgba); set_background_color ("000000FF"); } g_free( (gchar *) alpha); g_free( (gchar *) xpath); }