+
+/*
+ * 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.
+ *
+ * Returns: 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().
+ *
+ * Returns: newly allocated result from formatting
+ * operation. Free with g_free().
+ *
+ * Since: 2.4
+ */
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
+
+gchar *
+g_markup_vprintf_escaped (const gchar *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 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);
+
+ if (!output1)
+ {
+ va_end (args2);
+ 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;
+}
+
+#pragma GCC diagnostic pop
+
+/**
+ * g_markup_printf_escaped:
+ * @format: printf() style format string
+ * @...: 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.
+ *
+ * |[<!-- language="C" -->
+ * 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);
+ * ]|
+ *
+ * Returns: newly allocated result from formatting
+ * operation. Free with g_free().
+ *
+ * Since: 2.4
+ */
+gchar *
+g_markup_printf_escaped (const gchar *format, ...)
+{
+ char *result;
+ va_list args;
+
+ va_start (args, format);
+ result = g_markup_vprintf_escaped (format, args);
+ va_end (args);
+
+ return result;
+}
+
+static gboolean
+g_markup_parse_boolean (const char *string,
+ gboolean *value)
+{
+ char const * const falses[] = { "false", "f", "no", "n", "0" };
+ char const * const trues[] = { "true", "t", "yes", "y", "1" };
+ int i;
+
+ for (i = 0; i < G_N_ELEMENTS (falses); i++)
+ {
+ if (g_ascii_strcasecmp (string, falses[i]) == 0)
+ {
+ if (value != NULL)
+ *value = FALSE;
+
+ return TRUE;
+ }
+ }
+
+ for (i = 0; i < G_N_ELEMENTS (trues); i++)
+ {
+ if (g_ascii_strcasecmp (string, trues[i]) == 0)
+ {
+ if (value != NULL)
+ *value = TRUE;
+
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+}
+
+/**
+ * GMarkupCollectType:
+ * @G_MARKUP_COLLECT_INVALID: used to terminate the list of attributes
+ * to collect
+ * @G_MARKUP_COLLECT_STRING: collect the string pointer directly from
+ * the attribute_values[] array. Expects a parameter of type (const
+ * char **). If %G_MARKUP_COLLECT_OPTIONAL is specified and the
+ * attribute isn't present then the pointer will be set to %NULL
+ * @G_MARKUP_COLLECT_STRDUP: as with %G_MARKUP_COLLECT_STRING, but
+ * expects a parameter of type (char **) and g_strdup()s the
+ * returned pointer. The pointer must be freed with g_free()
+ * @G_MARKUP_COLLECT_BOOLEAN: expects a parameter of type (gboolean *)
+ * and parses the attribute value as a boolean. Sets %FALSE if the
+ * attribute isn't present. Valid boolean values consist of
+ * (case-insensitive) "false", "f", "no", "n", "0" and "true", "t",
+ * "yes", "y", "1"
+ * @G_MARKUP_COLLECT_TRISTATE: as with %G_MARKUP_COLLECT_BOOLEAN, but
+ * in the case of a missing attribute a value is set that compares
+ * equal to neither %FALSE nor %TRUE G_MARKUP_COLLECT_OPTIONAL is
+ * implied
+ * @G_MARKUP_COLLECT_OPTIONAL: can be bitwise ORed with the other fields.
+ * If present, allows the attribute not to appear. A default value
+ * is set depending on what value type is used
+ *
+ * A mixed enumerated type and flags field. You must specify one type
+ * (string, strdup, boolean, tristate). Additionally, you may optionally
+ * bitwise OR the type with the flag %G_MARKUP_COLLECT_OPTIONAL.
+ *
+ * It is likely that this enum will be extended in the future to
+ * support other types.
+ */
+
+/**
+ * g_markup_collect_attributes:
+ * @element_name: the current tag name
+ * @attribute_names: the attribute names
+ * @attribute_values: the attribute values
+ * @error: a pointer to a #GError or %NULL
+ * @first_type: the #GMarkupCollectType of the first attribute
+ * @first_attr: the name of the first attribute
+ * @...: a pointer to the storage location of the first attribute
+ * (or %NULL), followed by more types names and pointers, ending
+ * with %G_MARKUP_COLLECT_INVALID
+ *
+ * Collects the attributes of the element from the data passed to the
+ * #GMarkupParser start_element function, dealing with common error
+ * conditions and supporting boolean values.
+ *
+ * This utility function is not required to write a parser but can save
+ * a lot of typing.
+ *
+ * The @element_name, @attribute_names, @attribute_values and @error
+ * parameters passed to the start_element callback should be passed
+ * unmodified to this function.
+ *
+ * Following these arguments is a list of "supported" attributes to collect.
+ * It is an error to specify multiple attributes with the same name. If any
+ * attribute not in the list appears in the @attribute_names array then an
+ * unknown attribute error will result.
+ *
+ * The #GMarkupCollectType field allows specifying the type of collection
+ * to perform and if a given attribute must appear or is optional.
+ *
+ * The attribute name is simply the name of the attribute to collect.
+ *
+ * The pointer should be of the appropriate type (see the descriptions
+ * under #GMarkupCollectType) and may be %NULL in case a particular
+ * attribute is to be allowed but ignored.
+ *
+ * This function deals with issuing errors for missing attributes
+ * (of type %G_MARKUP_ERROR_MISSING_ATTRIBUTE), unknown attributes
+ * (of type %G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE) and duplicate
+ * attributes (of type %G_MARKUP_ERROR_INVALID_CONTENT) as well
+ * as parse errors for boolean-valued attributes (again of type
+ * %G_MARKUP_ERROR_INVALID_CONTENT). In all of these cases %FALSE
+ * will be returned and @error will be set as appropriate.
+ *
+ * Returns: %TRUE if successful
+ *
+ * Since: 2.16
+ **/
+gboolean
+g_markup_collect_attributes (const gchar *element_name,
+ const gchar **attribute_names,
+ const gchar **attribute_values,
+ GError **error,
+ GMarkupCollectType first_type,
+ const gchar *first_attr,
+ ...)
+{
+ GMarkupCollectType type;
+ const gchar *attr;
+ guint64 collected;
+ int written;
+ va_list ap;
+ int i;
+
+ type = first_type;
+ attr = first_attr;
+ collected = 0;
+ written = 0;
+
+ va_start (ap, first_attr);
+ while (type != G_MARKUP_COLLECT_INVALID)
+ {
+ gboolean mandatory;
+ const gchar *value;
+
+ mandatory = !(type & G_MARKUP_COLLECT_OPTIONAL);
+ type &= (G_MARKUP_COLLECT_OPTIONAL - 1);
+
+ /* tristate records a value != TRUE and != FALSE
+ * for the case where the attribute is missing
+ */
+ if (type == G_MARKUP_COLLECT_TRISTATE)
+ mandatory = FALSE;
+
+ for (i = 0; attribute_names[i]; i++)
+ if (i >= 40 || !(collected & (G_GUINT64_CONSTANT(1) << i)))
+ if (!strcmp (attribute_names[i], attr))
+ break;
+
+ /* ISO C99 only promises that the user can pass up to 127 arguments.
+ * Subtracting the first 4 arguments plus the final NULL and dividing
+ * by 3 arguments per collected attribute, we are left with a maximum
+ * number of supported attributes of (127 - 5) / 3 = 40.
+ *
+ * In reality, nobody is ever going to call us with anywhere close to
+ * 40 attributes to collect, so it is safe to assume that if i > 40
+ * then the user has given some invalid or repeated arguments. These
+ * problems will be caught and reported at the end of the function.
+ *
+ * We know at this point that we have an error, but we don't know
+ * what error it is, so just continue...
+ */
+ if (i < 40)
+ collected |= (G_GUINT64_CONSTANT(1) << i);
+
+ value = attribute_values[i];
+
+ if (value == NULL && mandatory)
+ {
+ g_set_error (error, G_MARKUP_ERROR,
+ G_MARKUP_ERROR_MISSING_ATTRIBUTE,
+ "element '%s' requires attribute '%s'",
+ element_name, attr);
+
+ va_end (ap);
+ goto failure;
+ }
+
+ switch (type)
+ {
+ case G_MARKUP_COLLECT_STRING:
+ {
+ const char **str_ptr;
+
+ str_ptr = va_arg (ap, const char **);
+
+ if (str_ptr != NULL)
+ *str_ptr = value;
+ }
+ break;
+
+ case G_MARKUP_COLLECT_STRDUP:
+ {
+ char **str_ptr;
+
+ str_ptr = va_arg (ap, char **);
+
+ if (str_ptr != NULL)
+ *str_ptr = g_strdup (value);
+ }
+ break;
+
+ case G_MARKUP_COLLECT_BOOLEAN:
+ case G_MARKUP_COLLECT_TRISTATE:
+ if (value == NULL)
+ {
+ gboolean *bool_ptr;
+
+ bool_ptr = va_arg (ap, gboolean *);
+
+ if (bool_ptr != NULL)
+ {
+ if (type == G_MARKUP_COLLECT_TRISTATE)
+ /* constructivists rejoice!
+ * neither false nor true...
+ */
+ *bool_ptr = -1;
+
+ else /* G_MARKUP_COLLECT_BOOLEAN */
+ *bool_ptr = FALSE;
+ }
+ }
+ else
+ {
+ if (!g_markup_parse_boolean (value, va_arg (ap, gboolean *)))
+ {
+ g_set_error (error, G_MARKUP_ERROR,
+ G_MARKUP_ERROR_INVALID_CONTENT,
+ "element '%s', attribute '%s', value '%s' "
+ "cannot be parsed as a boolean value",
+ element_name, attr, value);
+
+ va_end (ap);
+ goto failure;
+ }
+ }
+
+ break;
+
+ default:
+ g_assert_not_reached ();
+ }
+
+ type = va_arg (ap, GMarkupCollectType);
+ attr = va_arg (ap, const char *);
+ written++;
+ }
+ va_end (ap);
+
+ /* ensure we collected all the arguments */
+ for (i = 0; attribute_names[i]; i++)
+ if ((collected & (G_GUINT64_CONSTANT(1) << i)) == 0)
+ {
+ /* attribute not collected: could be caused by two things.
+ *
+ * 1) it doesn't exist in our list of attributes
+ * 2) it existed but was matched by a duplicate attribute earlier
+ *
+ * find out.
+ */
+ int j;
+
+ for (j = 0; j < i; j++)
+ if (strcmp (attribute_names[i], attribute_names[j]) == 0)
+ /* duplicate! */
+ break;
+
+ /* j is now the first occurrence of attribute_names[i] */
+ if (i == j)
+ g_set_error (error, G_MARKUP_ERROR,
+ G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
+ "attribute '%s' invalid for element '%s'",
+ attribute_names[i], element_name);
+ else
+ g_set_error (error, G_MARKUP_ERROR,
+ G_MARKUP_ERROR_INVALID_CONTENT,
+ "attribute '%s' given multiple times for element '%s'",
+ attribute_names[i], element_name);
+
+ goto failure;
+ }
+
+ return TRUE;
+
+failure:
+ /* replay the above to free allocations */
+ type = first_type;
+ attr = first_attr;
+
+ va_start (ap, first_attr);
+ while (type != G_MARKUP_COLLECT_INVALID)
+ {
+ gpointer ptr;
+
+ ptr = va_arg (ap, gpointer);
+
+ if (ptr != NULL)
+ {
+ switch (type & (G_MARKUP_COLLECT_OPTIONAL - 1))
+ {
+ case G_MARKUP_COLLECT_STRDUP:
+ if (written)
+ g_free (*(char **) ptr);
+
+ case G_MARKUP_COLLECT_STRING:
+ *(char **) ptr = NULL;
+ break;
+
+ case G_MARKUP_COLLECT_BOOLEAN:
+ *(gboolean *) ptr = FALSE;
+ break;
+
+ case G_MARKUP_COLLECT_TRISTATE:
+ *(gboolean *) ptr = -1;
+ break;
+ }
+ }
+
+ type = va_arg (ap, GMarkupCollectType);
+ attr = va_arg (ap, const char *);
+ }
+ va_end (ap);
+
+ return FALSE;
+}