[tracker] libtracker-common: Readd tracker_uri_printf_escaped



commit fcd386ed6b44c5c84a39ffbefacebace3f9bfca8
Author: Jürg Billeter <j bitron ch>
Date:   Mon Dec 28 14:33:17 2009 +0100

    libtracker-common: Readd tracker_uri_printf_escaped
    
    This partially reverts commit 202df57, which incorrectly replaced uri
    escaping by markup escaping.

 src/libtracker-common/tracker-utils.c |  202 +++++++++++++++++++++++++++++++++
 src/libtracker-common/tracker-utils.h |   12 +--
 2 files changed, 206 insertions(+), 8 deletions(-)
---
diff --git a/src/libtracker-common/tracker-utils.c b/src/libtracker-common/tracker-utils.c
index 79d3b61..b771cf6 100644
--- a/src/libtracker-common/tracker-utils.c
+++ b/src/libtracker-common/tracker-utils.c
@@ -218,6 +218,208 @@ tracker_seconds_to_string (gdouble  seconds_elapsed,
 	return str;
 }
 
+static const char *
+find_conversion (const char  *format,
+                 const char **after)
+{
+	const char *start = format;
+	const char *cp;
+
+	while (*start != '\0' && *start != '%')
+		start++;
+
+	if (*start == '\0') {
+		*after = start;
+		return NULL;
+	}
+
+	cp = start + 1;
+
+	if (*cp == '\0') {
+		*after = cp;
+		return NULL;
+	}
+
+	/* Test for positional argument.  */
+	if (*cp >= '0' && *cp <= '9') {
+		const char *np;
+
+		for (np = cp; *np >= '0' && *np <= '9'; np++)
+			;
+		if (*np == '$')
+			cp = np + 1;
+	}
+
+	/* Skip the flags.  */
+	for (;;) {
+		if (*cp == '\'' ||
+		    *cp == '-' ||
+		    *cp == '+' ||
+		    *cp == ' ' ||
+		    *cp == '#' ||
+		    *cp == '0')
+			cp++;
+		else
+			break;
+	}
+
+	/* Skip the field width.  */
+	if (*cp == '*') {
+		cp++;
+
+		/* Test for positional argument.  */
+		if (*cp >= '0' && *cp <= '9') {
+			const char *np;
+
+			for (np = cp; *np >= '0' && *np <= '9'; np++)
+				;
+			if (*np == '$')
+				cp = np + 1;
+		}
+	} else {
+		for (; *cp >= '0' && *cp <= '9'; cp++)
+			;
+	}
+
+	/* Skip the precision.  */
+	if (*cp == '.') {
+		cp++;
+		if (*cp == '*') {
+			/* Test for positional argument.  */
+			if (*cp >= '0' && *cp <= '9') {
+				const char *np;
+
+				for (np = cp; *np >= '0' && *np <= '9'; np++)
+					;
+				if (*np == '$')
+					cp = np + 1;
+			}
+		} else {
+			for (; *cp >= '0' && *cp <= '9'; cp++)
+				;
+		}
+	}
+
+	/* Skip argument type/size specifiers.  */
+	while (*cp == 'h' ||
+	       *cp == 'L' ||
+	       *cp == 'l' ||
+	       *cp == 'j' ||
+	       *cp == 'z' ||
+	       *cp == 'Z' ||
+	       *cp == 't')
+		cp++;
+
+	/* Skip the conversion character.  */
+	cp++;
+
+	*after = cp;
+	return start;
+}
+
+gchar *
+tracker_uri_vprintf_escaped (const gchar *format,
+                             va_list      args)
+{
+	GString *format1;
+	GString *format2;
+	GString *result = NULL;
+	gchar *output1 = NULL;
+	gchar *output2 = NULL;
+	const char *p;
+	char       *op1, *op2;
+	va_list args2;
+
+	format1 = g_string_new (NULL);
+	format2 = g_string_new (NULL);
+	p = format;
+	while (TRUE) {
+		const char *after;
+		const char *conv = find_conversion (p, &after);
+		if (!conv)
+			break;
+
+		g_string_append_len (format1, conv, after - conv);
+		g_string_append_c (format1, 'X');
+		g_string_append_len (format2, conv, after - conv);
+		g_string_append_c (format2, 'Y');
+
+		p = after;
+	}
+
+	/* Use them to format the arguments
+	 */
+	G_VA_COPY (args2, args);
+
+	output1 = g_strdup_vprintf (format1->str, args);
+	va_end (args);
+	if (!output1)
+		goto cleanup;
+
+	output2 = g_strdup_vprintf (format2->str, args2);
+	va_end (args2);
+	if (!output2)
+		goto cleanup;
+
+	result = g_string_new (NULL);
+
+	op1 = output1;
+	op2 = output2;
+	p = format;
+	while (TRUE) {
+		const char *after;
+		const char *output_start;
+		const char *conv = find_conversion (p, &after);
+		char *escaped;
+
+		if (!conv) {
+			g_string_append_len (result, p, after - p);
+			break;
+		}
+
+		g_string_append_len (result, p, conv - p);
+		output_start = op1;
+		while (*op1 == *op2) {
+			op1++;
+			op2++;
+		}
+
+		*op1 = '\0';
+		escaped = g_uri_escape_string (output_start, NULL, FALSE);
+		g_string_append (result, escaped);
+		g_free (escaped);
+
+		p = after;
+		op1++;
+		op2++;
+	}
+
+ cleanup:
+	g_string_free (format1, TRUE);
+	g_string_free (format2, TRUE);
+	g_free (output1);
+	g_free (output2);
+
+	if (result)
+		return g_string_free (result, FALSE);
+	else
+		return NULL;
+}
+
+gchar *
+tracker_uri_printf_escaped (const gchar *format, ...)
+{
+	char *result;
+	va_list args;
+
+	va_start (args, format);
+	result = tracker_uri_vprintf_escaped (format, args);
+	va_end (args);
+
+	return result;
+}
+
+
 gchar *
 tracker_coalesce (gint n_values,
                   ...)
diff --git a/src/libtracker-common/tracker-utils.h b/src/libtracker-common/tracker-utils.h
index 3335375..6371c94 100644
--- a/src/libtracker-common/tracker-utils.h
+++ b/src/libtracker-common/tracker-utils.h
@@ -42,6 +42,10 @@ gchar *  tracker_seconds_estimate_to_string (gdouble      seconds_elapsed,
                                              guint        items_remaining);
 gchar *  tracker_seconds_to_string          (gdouble      seconds_elapsed,
                                              gboolean     short_string);
+gchar*   tracker_uri_vprintf_escaped        (const gchar *format,
+                                             va_list      args);
+gchar*   tracker_uri_printf_escaped         (const gchar *format,
+                                             ...);
 gchar *  tracker_coalesce                   (gint n_values,
                                              ...);
 gchar *  tracker_merge                      (const gchar *delim, gint n_values,
@@ -51,14 +55,6 @@ gchar *  tracker_text_normalize             (const gchar *text,
                                              guint        max_words,
                                              guint       *n_words);
 
-/* Currently we define an alias here because right now we are using
- * GLib's escape functions, however, it is likely in the future that
- * Nepomuk's standard for URI escaping varies slightly and we want to
- * easily be able to have our own implementation without changing the
- * whole code base.
- */
-#define  tracker_uri_printf_escaped         g_markup_printf_escaped
-
 G_END_DECLS
 
 #endif /* __LIBTRACKER_COMMON_UTILS_H__ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]