Re: gtk_message_dialog_new_with_markup() revisited
- From: Owen Taylor <otaylor redhat com>
- To: Matthias Clasen <maclas gmx de>
- Cc: gtk-devel-list gnome org
- Subject: Re: gtk_message_dialog_new_with_markup() revisited
- Date: Thu, 11 Sep 2003 20:18:30 -0400
On Thu, 2003-09-11 at 17:59, Matthias Clasen wrote:
> Am Don, 2003-09-11 um 23.47 schrieb Owen Taylor:
> > On Wed, 2003-08-13 at 18:03, Matthias Clasen wrote:
> > > Am Mit, 2003-08-13 um 16.31 schrieb Owen Taylor:
> > > > *However*, any extension to printf() syntax, whether new formats
> > > > character or a new flag, removes the ability to use G_GNUC_PRINTF() and
> > > > that is pretty much a killer objection from my perspective. Those
> > > > warnings are just too valuable to give up.
> > > >
> > > > We could hijack %#s which has no standard meaning; gcc passes that
> > > > currently. But I'm not sure that we can count on that for the future.
> > >
> > > Hmm, I hadn't thought about that. Yes, loosing those warnings would be
> > > bad.
> > >
> > > So maybe your idea is better, although I still don't like the fact that
> > > it makes %s in gtk_message_dialog_new_with_markup() behave differently
> > > from every other printf()-like function in glib and gtk. If this trap is
> > > really so bad, then maybe we should just remove
> > > gtk_message_dialog_new_with_markup() and add
> > > gtk_message_dialog_set_markup() instead ?
> >
> > Hey Matthias -
> >
> > Any further thoughts on this? My feeling is that
> > gtk_message_dialog_new_with_markup() with magic %s is what most people
> > want most of the time, and thus makes sense.
> >
> > Should we go ahead and:
> >
> > - Add g_markup_printf_escaped() using my code from bugzilla
>
> Agreed.
Done, patch I committed is attached.
> > - Make gtk_dialog_new_with_markup() use it.
> > - Add gtk_dialog_set_markup()
>
> I guess you meant gtk_message_dialog_... here ? I'm ok with it.
Still need to do this.
Regards,
Owen
Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/glib/ChangeLog,v
retrieving revision 1.1399
diff -u -p -u -r1.1399 ChangeLog
--- ChangeLog 7 Sep 2003 22:36:57 -0000 1.1399
+++ ChangeLog 12 Sep 2003 00:14:24 -0000
@@ -1,3 +1,11 @@
+Thu Sep 11 20:11:05 2003 Owen Taylor <otaylor redhat com>
+
+ * glib/gmarkup.c: Add g_markup_printf_escaped(),
+ g_markup_vprintf_escaped().
+
+ * tests/markup-escape-test.c (main): Test for
+ g_markup_escape_text(), g_markup_printf_escaped().
+
Mon Sep 8 00:31:10 2003 Stefan Westerfeld <stefan space twc de>
* glib/gbsearcharray.h: inserted casts for C++.
Index: glib/gmarkup.c
===================================================================
RCS file: /cvs/gnome/glib/glib/gmarkup.c,v
retrieving revision 1.25
diff -u -p -u -r1.25 gmarkup.c
--- glib/gmarkup.c 30 Mar 2003 22:02:19 -0000 1.25
+++ glib/gmarkup.c 12 Sep 2003 00:14:24 -0000
@@ -1,6 +1,6 @@
/* gmarkup.c - Simple XML-like parser
*
- * Copyright 2000 Red Hat, Inc.
+ * Copyright 2000, 2003 Red Hat, Inc.
*
* GLib is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
@@ -20,6 +20,7 @@
#include "config.h"
+#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
@@ -1825,4 +1826,306 @@ g_markup_escape_text (const gchar *text,
append_escaped_text (str, text, length);
return g_string_free (str, FALSE);
+}
+
+/**
+ * find_conversion:
+ * @format: a printf-style format string
+ * @after: location to store a pointer to the character after
+ * the returned conversion. On a %NULL return, returns the
+ * pointer to the trailing NUL in the string
+ *
+ * Find the next conversion in a printf-style format string.
+ * Partially based on code from printf-parser.c,
+ * Copyright (C) 1999-2000, 2002-2003 Free Software Foundation, Inc.
+ *
+ * Return value: pointer to the next conversion in @format,
+ * or %NULL, if none.
+ **/
+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;
+}
+
+/**
+ * g_markup_vprintf_escaped:
+ * @format: printf() style format string
+ * @args: variable argument list, similar to vprintf()
+ *
+ * Formats the data in @args according to @format, escaping
+ * all string and character arguments in the fashion
+ * of g_markup_escape_text(). See g_markup_printf_escaped().
+ *
+ * Return value: newly allocated result from formatting
+ * operation. Free with g_free().
+ **/
+char *
+g_markup_vprintf_escaped (const char *format,
+ va_list args)
+{
+ GString *format1;
+ GString *format2;
+ GString *result = NULL;
+ gchar *output1 = NULL;
+ gchar *output2 = NULL;
+ const char *p, *op1, *op2;
+ va_list args2;
+
+ /* The technique here, is that we make two format strings that
+ * have the identical conversions in the identical order to the
+ * original strings, but differ in the text in-between. We
+ * then use the normal g_strdup_vprintf() to format the arguments
+ * with the two new format strings. By comparing the results,
+ * we can figure out what segments of the output come from
+ * the the original format string, and what from the arguments,
+ * and thus know what portions of the string to escape.
+ *
+ * For instance, for:
+ *
+ * g_markup_printf_escaped ("%s ate %d apples", "Susan & Fred", 5);
+ *
+ * We form the two format strings "%sX%dX" and %sY%sY". The results
+ * of formatting with those two strings are
+ *
+ * "%sX%dX" => "Susan & FredX5X"
+ * "%sY%dY" => "Susan & FredY5Y"
+ *
+ * To find the span of the first argument, we find the first position
+ * where the two arguments differ, which tells us that the first
+ * argument formatted to "Susan & Fred". We then escape that
+ * to "Susan & Fred" and join up with the intermediate portions
+ * of the format string and the second argument to get
+ * "Susan & Fred ate 5 apples".
+ */
+
+ /* Create the two modified format strings
+ */
+ 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);
+
+ /* Iterate through the original format string again,
+ * copying the non-conversion portions and the escaped
+ * converted arguments to the output string.
+ */
+ 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) /* The end, after points to the trailing \0 */
+ {
+ 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++;
+ }
+
+ escaped = g_markup_escape_text (output_start, op1 - output_start);
+ 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;
+}
+
+/**
+ * g_markup_printf_escaped:
+ * @format: printf() style format string
+ * @Varargs: the arguments to insert in the format string
+ *
+ * Formats arguments according to @format, escaping
+ * all string and character arguments in the fashion
+ * of g_markup_escape_text(). This is useful when you
+ * want to insert literal strings into XML-style markup
+ * output, without having to worry that the strings
+ * might themselves contain markup.
+ *
+ * <informalexample><programlisting>
+ * const char *store = "Fortnum & Mason";
+ * const char *item = "Tea";
+ * char *output;
+ *
+ * output = g_markup_printf_escaped ("<purchase>"
+ * "<store>%s</store>"
+ * "<item>%s</item>"
+ * "</purchase>",
+ * store, item);
+ * </programlisting></informalexample>
+ *
+ * Return value: newly allocated result from formatting
+ * operation. Free with g_free().
+ **/
+char *
+g_markup_printf_escaped (const char *format, ...)
+{
+ char *result;
+ va_list args;
+
+ va_start (args, format);
+ result = g_markup_vprintf_escaped (format, args);
+ va_end (args);
+
+ return result;
}
Index: docs/reference/glib/glib-sections.txt
===================================================================
RCS file: /cvs/gnome/glib/docs/reference/glib/glib-sections.txt,v
retrieving revision 1.70
diff -u -p -u -r1.70 glib-sections.txt
--- docs/reference/glib/glib-sections.txt 29 Jul 2003 22:31:40 -0000 1.70
+++ docs/reference/glib/glib-sections.txt 12 Sep 2003 00:14:24 -0000
@@ -843,6 +843,8 @@ GMarkupParseFlags
GMarkupParseContext
GMarkupParser
g_markup_escape_text
+g_markup_printf_escaped
+g_markup_vprintf_escaped
g_markup_parse_context_end_parse
g_markup_parse_context_free
g_markup_parse_context_get_position
Index: docs/reference/glib/tmpl/markup.sgml
===================================================================
RCS file: /cvs/gnome/glib/docs/reference/glib/tmpl/markup.sgml,v
retrieving revision 1.9
diff -u -p -u -r1.9 markup.sgml
--- docs/reference/glib/tmpl/markup.sgml 1 Sep 2002 13:04:01 -0000 1.9
+++ docs/reference/glib/tmpl/markup.sgml 12 Sep 2003 00:14:24 -0000
@@ -153,6 +153,26 @@ passthrough text back out in the same po
@Returns:
+<!-- ##### FUNCTION g_markup_printf_escaped ##### -->
+<para>
+
+</para>
+
+ format:
+ Varargs:
+ Returns:
+
+
+<!-- ##### FUNCTION g_markup_vprintf_escaped ##### -->
+<para>
+
+</para>
+
+ format:
+ args:
+ Returns:
+
+
<!-- ##### FUNCTION g_markup_parse_context_end_parse ##### -->
<para>
Index: tests/Makefile.am
===================================================================
RCS file: /cvs/gnome/glib/tests/Makefile.am,v
retrieving revision 1.58
diff -u -p -u -r1.58 Makefile.am
--- tests/Makefile.am 28 Jul 2003 19:30:05 -0000 1.58
+++ tests/Makefile.am 12 Sep 2003 00:14:24 -0000
@@ -73,6 +73,7 @@ test_programs = \
iochannel-test \
list-test \
mainloop-test \
+ markup-escape-test \
module-test \
node-test \
patterntest \
@@ -121,6 +122,7 @@ iochannel_test_LDADD = $(progs_ldadd)
list_test_LDADD = $(progs_ldadd)
mainloop_test_LDADD = $(thread_ldadd)
markup_test_LDADD = $(progs_ldadd)
+markup_escape_test_LDADD = $(progs_ldadd)
module_test_LDADD = $(module_ldadd) $(module_test_exp)
module_test_LDFLAGS = $(G_MODULE_LDFLAGS)
node_test_LDADD = $(progs_ldadd)
Index: tests/markup-escape-test.c
===================================================================
RCS file: tests/markup-escape-test.c
diff -N tests/markup-escape-test.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ tests/markup-escape-test.c 12 Sep 2003 00:14:24 -0000
@@ -0,0 +1,95 @@
+#undef G_DISABLE_ASSERT
+#undef G_LOG_DOMAIN
+
+#include <stdarg.h>
+#include <string.h>
+#include <glib.h>
+
+static void test_format (const gchar *format,
+ const gchar *expected, ...) G_GNUC_PRINTF (1, 3);
+
+static gboolean error = FALSE;
+
+static void
+test (const gchar *original,
+ const gchar *expected)
+{
+ gchar *result = g_markup_escape_text (original, -1);
+
+ if (strcmp (result, expected) != 0)
+ {
+ g_printerr ("g_markup_escape_text(): expected '%s', got '%s'\n",
+ expected, result);
+ error = TRUE;
+ }
+
+ g_free (result);
+}
+
+static void
+test_format (const gchar *format,
+ const gchar *expected,
+ ...)
+{
+ gchar *result;
+
+ va_list args;
+
+ va_start (args, expected);
+ result = g_markup_vprintf_escaped (format, args);
+ va_end (args);
+
+ if (strcmp (result, expected) != 0)
+ {
+ g_printerr ("g_markup_printf_escaped(): expected '%s', got '%s'\n",
+ expected, result);
+ error = TRUE;
+ }
+
+ g_free (result);
+}
+
+int main (int argc, char **argv)
+{
+ /* Tests for g_markup_escape_text() */
+ test ("&", "&");
+ test ("<", "<");
+ test (">", ">");
+ test ("'", "'");
+ test ("\"", """);
+
+ test ("", "");
+ test ("A", "A");
+ test ("A&", "A&");
+ test ("&A", "&A");
+ test ("A&A", "A&A");
+ test ("&&A", "&&A");
+ test ("A&&", "A&&");
+ test ("A&&A", "A&&A");
+ test ("A&A&A", "A&A&A");
+
+ /* Tests for g_markup_printf_escaped() */
+ test_format ("A", "A");
+ test_format ("A%s", "A&", "&");
+ test_format ("%sA", "&A", "&");
+ test_format ("A%sA", "A&A", "&");
+ test_format ("%s%sA", "&&A", "&", "&");
+ test_format ("A%s%s", "A&&", "&", "&");
+ test_format ("A%s%sA", "A&&A", "&", "&");
+ test_format ("A%sA%sA", "A&A&A", "&", "&");
+
+ test_format ("%s", "<B>&",
+ "<B>&");
+ test_format ("%c%c", "<&",
+ '<', '&');
+ test_format (".%c.%c.", ".<.&.",
+ '<', '&');
+ test_format ("%s", "",
+ "");
+ test_format ("%-5s", "A ",
+ "A");
+ test_format ("%2$s%1$s", "B.A.",
+ "A.", "B.");
+
+ return error ? 1 : 0;
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]