Move GMarkup docs inline

This commit is contained in:
Matthias Clasen 2011-01-17 23:46:20 -05:00
parent dc8b03027d
commit d2347f34fd
4 changed files with 752 additions and 960 deletions

View File

@ -17,6 +17,7 @@ hash_tables.sgml
iochannels.sgml iochannels.sgml
linked_lists_double.sgml linked_lists_double.sgml
linked_lists_single.sgml linked_lists_single.sgml
markup.sgml
memory_chunks.sgml memory_chunks.sgml
memory.sgml memory.sgml
option.sgml option.sgml

View File

@ -1,327 +0,0 @@
<!-- ##### SECTION Title ##### -->
Simple XML Subset Parser
<!-- ##### SECTION Short_Description ##### -->
parses a subset of XML
<!-- ##### SECTION Long_Description ##### -->
<para>
The "GMarkup" parser is intended to parse a simple markup format
that's a subset of XML. This is a small, efficient, easy-to-use
parser. It should not be used if you expect to interoperate with other
applications generating full-scale XML. However, it's very useful for
application data files, config files, etc. where you know your
application will be the only one writing the file. Full-scale XML
parsers should be able to parse the subset used by GMarkup, so you can
easily migrate to full-scale XML at a later time if the need arises.
</para>
<para>
GMarkup is not guaranteed to signal an error on all invalid XML; the
parser may accept documents that an XML parser would not. However, XML
documents which are not well-formed<footnote id="wellformed">Being wellformed
is a weaker condition than being valid. See the
<ulink url="http://www.w3.org/TR/REC-xml/">XML specification</ulink> for
definitions of these terms.</footnote> are not considered valid GMarkup
documents.
</para>
<para>
Simplifications to XML include:
<itemizedlist>
<listitem>
<para>
Only UTF-8 encoding is allowed.
</para>
</listitem>
<listitem>
<para>
No user-defined entities.
</para>
</listitem>
<listitem>
<para>
Processing instructions, comments and the doctype declaration are "passed
through" but are not interpreted in any way.
</para>
</listitem>
<listitem>
<para>
No DTD or validation.
</para>
</listitem>
</itemizedlist>
</para>
<para>
The markup format does support:
<itemizedlist>
<listitem>
<para>
Elements
</para>
</listitem>
<listitem>
<para>
Attributes
</para>
</listitem>
<listitem>
<para>
5 standard entities: <literal>&amp;amp; &amp;lt; &amp;gt; &amp;quot; &amp;apos;</literal>
</para>
</listitem>
<listitem>
<para>
Character references
</para>
</listitem>
<listitem>
<para>
Sections marked as CDATA
</para>
</listitem>
</itemizedlist>
</para>
<!-- ##### SECTION See_Also ##### -->
<para>
</para>
<!-- ##### SECTION Stability_Level ##### -->
<!-- ##### SECTION Image ##### -->
<!-- ##### ENUM GMarkupError ##### -->
<para>
Error codes returned by markup parsing.
</para>
@G_MARKUP_ERROR_BAD_UTF8: text being parsed was not valid UTF-8
@G_MARKUP_ERROR_EMPTY: document contained nothing, or only whitespace
@G_MARKUP_ERROR_PARSE: document was ill-formed
@G_MARKUP_ERROR_UNKNOWN_ELEMENT: error should be set by #GMarkupParser functions; element wasn't known
@G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE: error should be set by #GMarkupParser functions; attribute wasn't known
@G_MARKUP_ERROR_INVALID_CONTENT: error should be set by #GMarkupParser functions; content was invalid
@G_MARKUP_ERROR_MISSING_ATTRIBUTE: error should be set by #GMarkupParser functions; a required attribute was missing
<!-- ##### MACRO G_MARKUP_ERROR ##### -->
<para>
Error domain for markup parsing. Errors in this domain will
be from the #GMarkupError enumeration. See #GError for information on
error domains.
</para>
<!-- ##### ENUM GMarkupParseFlags ##### -->
<para>
Flags that affect the behaviour of the parser.
</para>
@G_MARKUP_DO_NOT_USE_THIS_UNSUPPORTED_FLAG: flag you should not use.
@G_MARKUP_TREAT_CDATA_AS_TEXT: When this flag is set, CDATA marked
sections are not passed literally to the @passthrough function of
the parser. Instead, the content of the section (without the
<literal>&lt;![CDATA[</literal> and <literal>]]&gt;</literal>) is
passed to the @text function. This flag was added in GLib 2.12.
@G_MARKUP_PREFIX_ERROR_POSITION: Normally errors caught by GMarkup
itself have line/column information prefixed to them to let the
caller know the location of the error. When this flag is set the
location information is also prefixed to errors generated by the
#GMarkupParser implementation functions.
<!-- ##### STRUCT GMarkupParseContext ##### -->
<para>
A parse context is used to parse a stream of bytes that you expect to
contain marked-up text. See g_markup_parse_context_new(),
#GMarkupParser, and so on for more details.
</para>
<!-- ##### STRUCT GMarkupParser ##### -->
<para>
Any of the fields in #GMarkupParser can be %NULL, in which case they
will be ignored. Except for the @error function, any of these
callbacks can set an error; in particular the
%G_MARKUP_ERROR_UNKNOWN_ELEMENT, %G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
and %G_MARKUP_ERROR_INVALID_CONTENT errors are intended to be set
from these callbacks. If you set an error from a callback,
g_markup_parse_context_parse() will report that error back to its caller.
</para>
@start_element: Callback to invoke when the opening tag of an element
is seen.
@end_element: Callback to invoke when the closing tag of an element is seen.
Note that this is also called for empty tags like
<literal>&lt;empty/&gt;</literal>.
@text: Callback to invoke when some text is seen (text is always
inside an element). Note that the text of an element may be spread
over multiple calls of this function. If the %G_MARKUP_TREAT_CDATA_AS_TEXT
flag is set, this function is also called for the content of CDATA marked
sections.
@passthrough: Callback to invoke for comments, processing instructions
and doctype declarations; if you're re-writing the parsed document,
write the passthrough text back out in the same position. If the
%G_MARKUP_TREAT_CDATA_AS_TEXT flag is not set, this function is also
called for CDATA marked sections.
@error: Callback to invoke when an error occurs.
<!-- ##### FUNCTION g_markup_escape_text ##### -->
<para>
</para>
@text:
@length:
@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>
</para>
@context:
@error:
@Returns:
<!-- ##### FUNCTION g_markup_parse_context_free ##### -->
<para>
</para>
@context:
<!-- ##### FUNCTION g_markup_parse_context_get_position ##### -->
<para>
</para>
@context:
@line_number:
@char_number:
<!-- ##### FUNCTION g_markup_parse_context_get_element ##### -->
<para>
</para>
@context:
@Returns:
<!-- ##### FUNCTION g_markup_parse_context_get_element_stack ##### -->
<para>
</para>
@context:
@Returns:
<!-- ##### FUNCTION g_markup_parse_context_get_user_data ##### -->
<para>
</para>
@context:
@Returns:
<!-- ##### FUNCTION g_markup_parse_context_new ##### -->
<para>
</para>
@parser:
@flags:
@user_data:
@user_data_dnotify:
@Returns:
<!-- ##### FUNCTION g_markup_parse_context_parse ##### -->
<para>
</para>
@context:
@text:
@text_len:
@error:
@Returns:
<!-- ##### FUNCTION g_markup_parse_context_push ##### -->
<para>
</para>
@context:
@parser:
@user_data:
<!-- ##### FUNCTION g_markup_parse_context_pop ##### -->
<para>
</para>
@context:
@Returns:
<!-- ##### ENUM GMarkupCollectType ##### -->
<para>
</para>
@G_MARKUP_COLLECT_INVALID:
@G_MARKUP_COLLECT_STRING:
@G_MARKUP_COLLECT_STRDUP:
@G_MARKUP_COLLECT_BOOLEAN:
@G_MARKUP_COLLECT_TRISTATE:
@G_MARKUP_COLLECT_OPTIONAL:
<!-- ##### FUNCTION g_markup_collect_attributes ##### -->
<para>
</para>
@element_name:
@attribute_names:
@attribute_values:
@error:
@first_type:
@first_attr:
@Varargs:
@Returns:

View File

@ -35,6 +35,52 @@
#include "gtestutils.h" #include "gtestutils.h"
#include "glibintl.h" #include "glibintl.h"
/**
* SECTION:markup
* @Title: Simple XML Subset Parser
* @Short_description: parses a subset of XML
* @See_also: <ulink url="http://www.w3.org/TR/REC-xml/">XML
* Specification</ulink>
*
* The "GMarkup" parser is intended to parse a simple markup format
* that's a subset of XML. This is a small, efficient, easy-to-use
* parser. It should not be used if you expect to interoperate with
* other applications generating full-scale XML. However, it's very
* useful for application data files, config files, etc. where you
* know your application will be the only one writing the file.
* Full-scale XML parsers should be able to parse the subset used by
* GMarkup, so you can easily migrate to full-scale XML at a later
* time if the need arises.
*
* GMarkup is not guaranteed to signal an error on all invalid XML;
* the parser may accept documents that an XML parser would not.
* However, XML documents which are not well-formed<footnote
* id="wellformed">Being wellformed is a weaker condition than being
* valid. See the <ulink url="http://www.w3.org/TR/REC-xml/">XML
* specification</ulink> for definitions of these terms.</footnote>
* are not considered valid GMarkup documents.
*
* Simplifications to XML include:
* <itemizedlist>
* <listitem>Only UTF-8 encoding is allowed</listitem>
* <listitem>No user-defined entities</listitem>
* <listitem>Processing instructions, comments and the doctype declaration
* are "passed through" but are not interpreted in any way</listitem>
* <listitem>No DTD or validation.</listitem>
* </itemizedlist>
*
* The markup format does support:
* <itemizedlist>
* <listitem>Elements</listitem>
* <listitem>Attributes</listitem>
* <listitem>5 standard entities:
* <literal>&amp;amp; &amp;lt; &amp;gt; &amp;quot; &amp;apos;</literal>
* </listitem>
* <listitem>Character references</listitem>
* <listitem>Sections marked as CDATA</listitem>
* </itemizedlist>
*/
GQuark GQuark
g_markup_error_quark (void) g_markup_error_quark (void)
{ {
@ -156,13 +202,14 @@ string_blank (GString *string)
* @parser: a #GMarkupParser * @parser: a #GMarkupParser
* @flags: one or more #GMarkupParseFlags * @flags: one or more #GMarkupParseFlags
* @user_data: user data to pass to #GMarkupParser functions * @user_data: user data to pass to #GMarkupParser functions
* @user_data_dnotify: user data destroy notifier called when the parse context is freed * @user_data_dnotify: user data destroy notifier called when
* the parse context is freed
* *
* Creates a new parse context. A parse context is used to parse * Creates a new parse context. A parse context is used to parse
* marked-up documents. You can feed any number of documents into * marked-up documents. You can feed any number of documents into
* a context, as long as no errors occur; once an error occurs, * a context, as long as no errors occur; once an error occurs,
* the parse context can't continue to parse text (you have to free it * the parse context can't continue to parse text (you have to
* and create a new parse context). * free it and create a new parse context).
* *
* Return value: a new #GMarkupParseContext * Return value: a new #GMarkupParseContext
**/ **/
@ -221,7 +268,7 @@ g_markup_parse_context_new (const GMarkupParser *parser,
} }
static void static void
string_full_free (gpointer ptr, gpointer user_data) string_full_free (gpointer ptr)
{ {
g_string_free (ptr, TRUE); g_string_free (ptr, TRUE);
} }
@ -232,10 +279,11 @@ static void clear_attributes (GMarkupParseContext *context);
* g_markup_parse_context_free: * g_markup_parse_context_free:
* @context: a #GMarkupParseContext * @context: a #GMarkupParseContext
* *
* Frees a #GMarkupParseContext. Can't be called from inside * Frees a #GMarkupParseContext.
* one of the #GMarkupParser functions. Can't be called while *
* a subparser is pushed. * This function can't be called from inside one of the
**/ * #GMarkupParser functions or while a subparser is pushed.
*/
void void
g_markup_parse_context_free (GMarkupParseContext *context) g_markup_parse_context_free (GMarkupParseContext *context)
{ {
@ -251,12 +299,10 @@ g_markup_parse_context_free (GMarkupParseContext *context)
g_free (context->attr_names); g_free (context->attr_names);
g_free (context->attr_values); g_free (context->attr_values);
g_slist_foreach (context->tag_stack_gstr, string_full_free, NULL); g_slist_free_full (context->tag_stack_gstr, string_full_free);
g_slist_free (context->tag_stack_gstr);
g_slist_free (context->tag_stack); g_slist_free (context->tag_stack);
g_slist_foreach (context->spare_chunks, string_full_free, NULL); g_slist_free_full (context->spare_chunks, string_full_free);
g_slist_free (context->spare_chunks);
g_slist_free (context->spare_list_nodes); g_slist_free (context->spare_list_nodes);
if (context->partial_chunk) if (context->partial_chunk)
@ -287,7 +333,8 @@ mark_error (GMarkupParseContext *context,
} }
} }
static void set_error (GMarkupParseContext *context, static void
set_error (GMarkupParseContext *context,
GError **error, GError **error,
GMarkupError code, GMarkupError code,
const gchar *format, const gchar *format,
@ -328,8 +375,9 @@ set_error (GMarkupParseContext *context,
s = g_strdup_vprintf (format, args); s = g_strdup_vprintf (format, args);
va_end (args); va_end (args);
/* Make sure that the GError message is valid UTF-8 even if it is /* Make sure that the GError message is valid UTF-8
* complaining about invalid UTF-8 in the markup: */ * even if it is complaining about invalid UTF-8 in the markup
*/
s_valid = _g_utf8_make_valid (s); s_valid = _g_utf8_make_valid (s);
set_error_literal (context, error, code, s); set_error_literal (context, error, code, s);
@ -357,9 +405,11 @@ propagate_error (GMarkupParseContext *context,
((c) == '=' || (c) == '/' || (c) == '>' || (c) == ' ') ((c) == '=' || (c) == '/' || (c) == '>' || (c) == ' ')
static gboolean static gboolean
slow_name_validate (GMarkupParseContext *context, const char *name, GError **error) slow_name_validate (GMarkupParseContext *context,
const gchar *name,
GError **error)
{ {
const char *p = name; const gchar *p = name;
if (!g_utf8_validate (name, strlen (name), NULL)) if (!g_utf8_validate (name, strlen (name), NULL))
{ {
@ -402,7 +452,9 @@ slow_name_validate (GMarkupParseContext *context, const char *name, GError **err
* Use me for elements, attributes etc. * Use me for elements, attributes etc.
*/ */
static gboolean static gboolean
name_validate (GMarkupParseContext *context, const char *name, GError **error) name_validate (GMarkupParseContext *context,
const gchar *name,
GError **error)
{ {
char mask; char mask;
const char *p; const char *p;
@ -437,7 +489,10 @@ name_validate (GMarkupParseContext *context, const char *name, GError **error)
} }
static gboolean static gboolean
text_validate (GMarkupParseContext *context, const char *p, int len, GError **error) text_validate (GMarkupParseContext *context,
const gchar *p,
gint len,
GError **error)
{ {
if (!g_utf8_validate (p, len, NULL)) if (!g_utf8_validate (p, len, NULL))
{ {
@ -897,10 +952,12 @@ clear_attributes (GMarkupParseContext *context)
} }
/* This has to be a separate function to ensure the alloca's /* This has to be a separate function to ensure the alloca's
are unwound on exit - otherwise we grow & blow the stack * are unwound on exit - otherwise we grow & blow the stack
with large documents */ * with large documents
*/
static inline void static inline void
emit_start_element (GMarkupParseContext *context, GError **error) emit_start_element (GMarkupParseContext *context,
GError **error)
{ {
int i; int i;
const gchar *start_name; const gchar *start_name;
@ -943,17 +1000,19 @@ emit_start_element (GMarkupParseContext *context, GError **error)
* @text_len: length of @text in bytes * @text_len: length of @text in bytes
* @error: return location for a #GError * @error: return location for a #GError
* *
* Feed some data to the #GMarkupParseContext. The data need not * Feed some data to the #GMarkupParseContext.
* be valid UTF-8; an error will be signaled if it's invalid. *
* The data need not be an entire document; you can feed a document * The data need not be valid UTF-8; an error will be signaled if
* into the parser incrementally, via multiple calls to this function. * it's invalid. The data need not be an entire document; you can
* Typically, as you receive data from a network connection or file, * feed a document into the parser incrementally, via multiple calls
* you feed each received chunk of data into this function, aborting * to this function. Typically, as you receive data from a network
* the process if an error occurs. Once an error is reported, no further * connection or file, you feed each received chunk of data into this
* data may be fed to the #GMarkupParseContext; all errors are fatal. * function, aborting the process if an error occurs. Once an error
* is reported, no further data may be fed to the #GMarkupParseContext;
* all errors are fatal.
* *
* Return value: %FALSE if an error occurred, %TRUE on success * Return value: %FALSE if an error occurred, %TRUE on success
**/ */
gboolean gboolean
g_markup_parse_context_parse (GMarkupParseContext *context, g_markup_parse_context_parse (GMarkupParseContext *context,
const gchar *text, const gchar *text,
@ -1380,7 +1439,6 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
/* The text has ended at the open angle. Call the text /* The text has ended at the open angle. Call the text
* callback. * callback.
*/ */
if (unescape_gstring_inplace (context, context->partial_chunk, &is_ascii, error) && if (unescape_gstring_inplace (context, context->partial_chunk, &is_ascii, error) &&
(is_ascii || text_validate (context, context->partial_chunk->str, (is_ascii || text_validate (context, context->partial_chunk->str,
context->partial_chunk->len, error))) context->partial_chunk->len, error)))
@ -1627,11 +1685,12 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
* *
* Signals to the #GMarkupParseContext that all data has been * Signals to the #GMarkupParseContext that all data has been
* fed into the parse context with g_markup_parse_context_parse(). * fed into the parse context with g_markup_parse_context_parse().
*
* This function reports an error if the document isn't complete, * This function reports an error if the document isn't complete,
* for example if elements are still open. * for example if elements are still open.
* *
* Return value: %TRUE on success, %FALSE if an error was set * Return value: %TRUE on success, %FALSE if an error was set
**/ */
gboolean gboolean
g_markup_parse_context_end_parse (GMarkupParseContext *context, g_markup_parse_context_end_parse (GMarkupParseContext *context,
GError **error) GError **error)
@ -1749,7 +1808,6 @@ g_markup_parse_context_end_parse (GMarkupParseContext *context,
/** /**
* g_markup_parse_context_get_element: * g_markup_parse_context_get_element:
* @context: a #GMarkupParseContext * @context: a #GMarkupParseContext
* @returns: the name of the currently open element, or %NULL
* *
* Retrieves the name of the currently open element. * Retrieves the name of the currently open element.
* *
@ -1757,8 +1815,10 @@ g_markup_parse_context_end_parse (GMarkupParseContext *context,
* give the element_name as passed to those functions. For the parent * give the element_name as passed to those functions. For the parent
* elements, see g_markup_parse_context_get_element_stack(). * elements, see g_markup_parse_context_get_element_stack().
* *
* Returns: the name of the currently open element, or %NULL
*
* Since: 2.2 * Since: 2.2
**/ */
G_CONST_RETURN gchar * G_CONST_RETURN gchar *
g_markup_parse_context_get_element (GMarkupParseContext *context) g_markup_parse_context_get_element (GMarkupParseContext *context)
{ {
@ -1775,6 +1835,7 @@ g_markup_parse_context_get_element (GMarkupParseContext *context)
* @context: a #GMarkupParseContext * @context: a #GMarkupParseContext
* *
* Retrieves the element stack from the internal state of the parser. * Retrieves the element stack from the internal state of the parser.
*
* The returned #GSList is a list of strings where the first item is * The returned #GSList is a list of strings where the first item is
* the currently open tag (as would be returned by * the currently open tag (as would be returned by
* g_markup_parse_context_get_element()) and the next item is its * g_markup_parse_context_get_element()) and the next item is its
@ -1788,7 +1849,7 @@ g_markup_parse_context_get_element (GMarkupParseContext *context)
* Returns: the element stack, which must not be modified * Returns: the element stack, which must not be modified
* *
* Since: 2.16 * Since: 2.16
**/ */
G_CONST_RETURN GSList * G_CONST_RETURN GSList *
g_markup_parse_context_get_element_stack (GMarkupParseContext *context) g_markup_parse_context_get_element_stack (GMarkupParseContext *context)
{ {
@ -1799,15 +1860,14 @@ g_markup_parse_context_get_element_stack (GMarkupParseContext *context)
/** /**
* g_markup_parse_context_get_position: * g_markup_parse_context_get_position:
* @context: a #GMarkupParseContext * @context: a #GMarkupParseContext
* @line_number: return location for a line number, or %NULL * @line_number: (allow-none): return location for a line number, or %NULL
* @char_number: return location for a char-on-line number, or %NULL * @char_number: (allow-none): return location for a char-on-line number, or %NULL
* *
* Retrieves the current line number and the number of the character on * Retrieves the current line number and the number of the character on
* that line. Intended for use in error messages; there are no strict * that line. Intended for use in error messages; there are no strict
* semantics for what constitutes the "current" line number other than * semantics for what constitutes the "current" line number other than
* "the best number we could come up with for error messages." * "the best number we could come up with for error messages."
* */
**/
void void
g_markup_parse_context_get_position (GMarkupParseContext *context, g_markup_parse_context_get_position (GMarkupParseContext *context,
gint *line_number, gint *line_number,
@ -1826,16 +1886,18 @@ g_markup_parse_context_get_position (GMarkupParseContext *context,
* g_markup_parse_context_get_user_data: * g_markup_parse_context_get_user_data:
* @context: a #GMarkupParseContext * @context: a #GMarkupParseContext
* *
* Returns the user_data associated with @context. This will either * Returns the user_data associated with @context.
* be the user_data that was provided to g_markup_parse_context_new() *
* or to the most recent call of g_markup_parse_context_push(). * This will either be the user_data that was provided to
* g_markup_parse_context_new() or to the most recent call
* of g_markup_parse_context_push().
* *
* Returns: the provided user_data. The returned data belongs to * Returns: the provided user_data. The returned data belongs to
* the markup context and will be freed when g_markup_context_free() * the markup context and will be freed when g_markup_context_free()
* is called. * is called.
* *
* Since: 2.18 * Since: 2.18
**/ */
gpointer gpointer
g_markup_parse_context_get_user_data (GMarkupParseContext *context) g_markup_parse_context_get_user_data (GMarkupParseContext *context)
{ {
@ -1994,19 +2056,19 @@ g_markup_parse_context_push (GMarkupParseContext *context,
* This function exists to collect the user_data allocated by a * This function exists to collect the user_data allocated by a
* matching call to g_markup_parse_context_push(). It must be called * matching call to g_markup_parse_context_push(). It must be called
* in the end_element handler corresponding to the start_element * in the end_element handler corresponding to the start_element
* handler during which g_markup_parse_context_push() was called. You * handler during which g_markup_parse_context_push() was called.
* must not call this function from the error callback -- the * You must not call this function from the error callback -- the
* @user_data is provided directly to the callback in that case. * @user_data is provided directly to the callback in that case.
* *
* This function is not intended to be directly called by users * This function is not intended to be directly called by users
* interested in invoking subparsers. Instead, it is intended to be * interested in invoking subparsers. Instead, it is intended to
* used by the subparsers themselves to implement a higher-level * be used by the subparsers themselves to implement a higher-level
* interface. * interface.
* *
* Returns: the user_data passed to g_markup_parse_context_push(). * Returns: the user data passed to g_markup_parse_context_push()
* *
* Since: 2.18 * Since: 2.18
**/ */
gpointer gpointer
g_markup_parse_context_pop (GMarkupParseContext *context) g_markup_parse_context_pop (GMarkupParseContext *context)
{ {
@ -2103,7 +2165,7 @@ append_escaped_text (GString *str,
* valid XML 1.1 and will be accepted by the GMarkup parser. * valid XML 1.1 and will be accepted by the GMarkup parser.
* *
* Return value: a newly allocated string with the escaped text * Return value: a newly allocated string with the escaped text
**/ */
gchar* gchar*
g_markup_escape_text (const gchar *text, g_markup_escape_text (const gchar *text,
gssize length) gssize length)
@ -2122,7 +2184,7 @@ g_markup_escape_text (const gchar *text,
return g_string_free (str, FALSE); return g_string_free (str, FALSE);
} }
/** /*
* find_conversion: * find_conversion:
* @format: a printf-style format string * @format: a printf-style format string
* @after: location to store a pointer to the character after * @after: location to store a pointer to the character after
@ -2135,7 +2197,7 @@ g_markup_escape_text (const gchar *text,
* *
* Return value: pointer to the next conversion in @format, * Return value: pointer to the next conversion in @format,
* or %NULL, if none. * or %NULL, if none.
**/ */
static const char * static const char *
find_conversion (const char *format, find_conversion (const char *format,
const char **after) const char **after)
@ -2261,9 +2323,9 @@ find_conversion (const char *format,
* operation. Free with g_free(). * operation. Free with g_free().
* *
* Since: 2.4 * Since: 2.4
**/ */
char * gchar *
g_markup_vprintf_escaped (const char *format, g_markup_vprintf_escaped (const gchar *format,
va_list args) va_list args)
{ {
GString *format1; GString *format1;
@ -2416,9 +2478,9 @@ g_markup_vprintf_escaped (const char *format,
* operation. Free with g_free(). * operation. Free with g_free().
* *
* Since: 2.4 * Since: 2.4
**/ */
char * gchar *
g_markup_printf_escaped (const char *format, ...) g_markup_printf_escaped (const gchar *format, ...)
{ {
char *result; char *result;
va_list args; va_list args;
@ -2466,43 +2528,34 @@ g_markup_parse_boolean (const char *string,
/** /**
* GMarkupCollectType: * GMarkupCollectType:
* @G_MARKUP_COLLECT_INVALID: used to terminate the list of attributes * @G_MARKUP_COLLECT_INVALID: used to terminate the list of attributes
* to collect. * to collect
* @G_MARKUP_COLLECT_STRING: collect the string pointer directly from * @G_MARKUP_COLLECT_STRING: collect the string pointer directly from
* the attribute_values[] array. Expects a * the attribute_values[] array. Expects a parameter of type (const
* parameter of type (const char **). If * char **). If %G_MARKUP_COLLECT_OPTIONAL is specified and the
* %G_MARKUP_COLLECT_OPTIONAL is specified * attribute isn't present then the pointer will be set to %NULL
* 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 * @G_MARKUP_COLLECT_STRDUP: as with %G_MARKUP_COLLECT_STRING, but
* expects a parameter of type (char **) and * expects a parameter of type (char **) and g_strdup()s the
* g_strdup()s the returned pointer. The * returned pointer. The pointer must be freed with g_free()
* pointer must be freed with g_free().
* @G_MARKUP_COLLECT_BOOLEAN: expects a parameter of type (gboolean *) * @G_MARKUP_COLLECT_BOOLEAN: expects a parameter of type (gboolean *)
* and parses the attribute value as a * and parses the attribute value as a boolean. Sets %FALSE if the
* boolean. Sets %FALSE if the attribute * attribute isn't present. Valid boolean values consist of
* isn't present. Valid boolean values * (case-insensitive) "false", "f", "no", "n", "0" and "true", "t",
* consist of (case insensitive) "false", * "yes", "y", "1"
* "f", "no", "n", "0" and "true", "t",
* "yes", "y", "1".
* @G_MARKUP_COLLECT_TRISTATE: as with %G_MARKUP_COLLECT_BOOLEAN, but * @G_MARKUP_COLLECT_TRISTATE: as with %G_MARKUP_COLLECT_BOOLEAN, but
* in the case of a missing attribute a * in the case of a missing attribute a value is set that compares
* value is set that compares equal to * equal to neither %FALSE nor %TRUE G_MARKUP_COLLECT_OPTIONAL is
* neither %FALSE nor %TRUE. * implied
* G_MARKUP_COLLECT_OPTIONAL is implied. * @G_MARKUP_COLLECT_OPTIONAL: can be bitwise ORed with the other fields.
* @G_MARKUP_COLLECT_OPTIONAL: can be bitwise ORed with the other * If present, allows the attribute not to appear. A default value
* fields. If present, allows the * is set depending on what value type is used
* 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 * A mixed enumerated type and flags field. You must specify one type
* (string, strdup, boolean, tristate). Additionally, you may * (string, strdup, boolean, tristate). Additionally, you may optionally
* optionally bitwise OR the type with the flag * bitwise OR the type with the flag %G_MARKUP_COLLECT_OPTIONAL.
* %G_MARKUP_COLLECT_OPTIONAL.
* *
* It is likely that this enum will be extended in the future to * It is likely that this enum will be extended in the future to
* support other types. * support other types.
**/ */
/** /**
* g_markup_collect_attributes: * g_markup_collect_attributes:
@ -2510,46 +2563,36 @@ g_markup_parse_boolean (const char *string,
* @attribute_names: the attribute names * @attribute_names: the attribute names
* @attribute_values: the attribute values * @attribute_values: the attribute values
* @error: a pointer to a #GError or %NULL * @error: a pointer to a #GError or %NULL
* @first_type: the #GMarkupCollectType of the * @first_type: the #GMarkupCollectType of the first attribute
* first attribute
* @first_attr: the name of the first attribute * @first_attr: the name of the first attribute
* @...: a pointer to the storage location of the * @...: a pointer to the storage location of the first attribute
* first attribute (or %NULL), followed by * (or %NULL), followed by more types names and pointers, ending
* more types names and pointers, ending * with %G_MARKUP_COLLECT_INVALID
* with %G_MARKUP_COLLECT_INVALID.
* *
* Collects the attributes of the element from the * Collects the attributes of the element from the data passed to the
* data passed to the #GMarkupParser start_element * #GMarkupParser start_element function, dealing with common error
* function, dealing with common error conditions * conditions and supporting boolean values.
* and supporting boolean values.
* *
* This utility function is not required to write * This utility function is not required to write a parser but can save
* a parser but can save a lot of typing. * a lot of typing.
* *
* The @element_name, @attribute_names, * The @element_name, @attribute_names, @attribute_values and @error
* @attribute_values and @error parameters passed * parameters passed to the start_element callback should be passed
* to the start_element callback should be passed
* unmodified to this function. * unmodified to this function.
* *
* Following these arguments is a list of * Following these arguments is a list of "supported" attributes to collect.
* "supported" attributes to collect. It is an * It is an error to specify multiple attributes with the same name. If any
* error to specify multiple attributes with the * attribute not in the list appears in the @attribute_names array then an
* same name. If any attribute not in the list
* appears in the @attribute_names array then an
* unknown attribute error will result. * unknown attribute error will result.
* *
* The #GMarkupCollectType field allows specifying * The #GMarkupCollectType field allows specifying the type of collection
* the type of collection to perform and if a * to perform and if a given attribute must appear or is optional.
* given attribute must appear or is optional.
* *
* The attribute name is simply the name of the * The attribute name is simply the name of the attribute to collect.
* attribute to collect.
* *
* The pointer should be of the appropriate type * The pointer should be of the appropriate type (see the descriptions
* (see the descriptions under * under #GMarkupCollectType) and may be %NULL in case a particular
* #GMarkupCollectType) and may be %NULL in case a * attribute is to be allowed but ignored.
* particular attribute is to be allowed but
* ignored.
* *
* This function deals with issuing errors for missing attributes * This function deals with issuing errors for missing attributes
* (of type %G_MARKUP_ERROR_MISSING_ATTRIBUTE), unknown attributes * (of type %G_MARKUP_ERROR_MISSING_ATTRIBUTE), unknown attributes

View File

@ -32,6 +32,22 @@
G_BEGIN_DECLS G_BEGIN_DECLS
/**
* GMarkupError:
* @G_MARKUP_ERROR_BAD_UTF8: text being parsed was not valid UTF-8
* @G_MARKUP_ERROR_EMPTY: document contained nothing, or only whitespace
* @G_MARKUP_ERROR_PARSE: document was ill-formed
* @G_MARKUP_ERROR_UNKNOWN_ELEMENT: error should be set by #GMarkupParser
* functions; element wasn't known
* @G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE: error should be set by #GMarkupParser
* functions; attribute wasn't known
* @G_MARKUP_ERROR_INVALID_CONTENT: error should be set by #GMarkupParser
* functions; content was invalid
* @G_MARKUP_ERROR_MISSING_ATTRIBUTE: error should be set by #GMarkupParser
* functions; a required attribute was missing
*
* Error codes returned by markup parsing.
*/
typedef enum typedef enum
{ {
G_MARKUP_ERROR_BAD_UTF8, G_MARKUP_ERROR_BAD_UTF8,
@ -46,10 +62,33 @@ typedef enum
G_MARKUP_ERROR_MISSING_ATTRIBUTE G_MARKUP_ERROR_MISSING_ATTRIBUTE
} GMarkupError; } GMarkupError;
/**
* G_MARKUP_ERROR:
*
* Error domain for markup parsing.
* Errors in this domain will be from the #GMarkupError enumeration.
* See #GError for information on error domains.
*/
#define G_MARKUP_ERROR g_markup_error_quark () #define G_MARKUP_ERROR g_markup_error_quark ()
GQuark g_markup_error_quark (void); GQuark g_markup_error_quark (void);
/**
* GMarkupParseFlags:
* @G_MARKUP_DO_NOT_USE_THIS_UNSUPPORTED_FLAG: flag you should not use
* @G_MARKUP_TREAT_CDATA_AS_TEXT: When this flag is set, CDATA marked
* sections are not passed literally to the @passthrough function of
* the parser. Instead, the content of the section (without the
* <literal>&lt;![CDATA[</literal> and <literal>]]&gt;</literal>) is
* passed to the @text function. This flag was added in GLib 2.12
* @G_MARKUP_PREFIX_ERROR_POSITION: Normally errors caught by GMarkup
* itself have line/column information prefixed to them to let the
* caller know the location of the error. When this flag is set the
* location information is also prefixed to errors generated by the
* #GMarkupParser implementation functions
*
* Flags that affect the behaviour of the parser.
*/
typedef enum typedef enum
{ {
G_MARKUP_DO_NOT_USE_THIS_UNSUPPORTED_FLAG = 1 << 0, G_MARKUP_DO_NOT_USE_THIS_UNSUPPORTED_FLAG = 1 << 0,
@ -57,9 +96,45 @@ typedef enum
G_MARKUP_PREFIX_ERROR_POSITION = 1 << 2 G_MARKUP_PREFIX_ERROR_POSITION = 1 << 2
} GMarkupParseFlags; } GMarkupParseFlags;
/**
* GMarkupParseContext:
*
* A parse context is used to parse a stream of bytes that
* you expect to contain marked-up text.
*
* See g_markup_parse_context_new(), #GMarkupParser, and so
* on for more details.
*/
typedef struct _GMarkupParseContext GMarkupParseContext; typedef struct _GMarkupParseContext GMarkupParseContext;
typedef struct _GMarkupParser GMarkupParser; typedef struct _GMarkupParser GMarkupParser;
/**
* GMarkupParser:
* @start_element: Callback to invoke when the opening tag of an element
* is seen.
* @end_element: Callback to invoke when the closing tag of an element
* is seen. Note that this is also called for empty tags like
* <literal>&lt;empty/&gt;</literal>.
* @text: Callback to invoke when some text is seen (text is always
* inside an element). Note that the text of an element may be spread
* over multiple calls of this function. If the
* %G_MARKUP_TREAT_CDATA_AS_TEXT flag is set, this function is also
* called for the content of CDATA marked sections.
* @passthrough: Callback to invoke for comments, processing instructions
* and doctype declarations; if you're re-writing the parsed document,
* write the passthrough text back out in the same position. If the
* %G_MARKUP_TREAT_CDATA_AS_TEXT flag is not set, this function is also
* called for CDATA marked sections.
* @error: Callback to invoke when an error occurs.
*
* Any of the fields in #GMarkupParser can be %NULL, in which case they
* will be ignored. Except for the @error function, any of these callbacks
* can set an error; in particular the %G_MARKUP_ERROR_UNKNOWN_ELEMENT,
* %G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE, and %G_MARKUP_ERROR_INVALID_CONTENT
* errors are intended to be set from these callbacks. If you set an error
* from a callback, g_markup_parse_context_parse() will report that error
* back to its caller.
*/
struct _GMarkupParser struct _GMarkupParser
{ {
/* Called for open tags <foo bar="baz"> */ /* Called for open tags <foo bar="baz"> */