gmarkup.h 6.55 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* gmarkup.h - Simple XML-like string parser/writer
 *
 *  Copyright 2000 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
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * GLib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with GLib; see the file COPYING.LIB.  If not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *   Boston, MA 02111-1307, USA.
 */

21
#if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
22 23 24
#error "Only <glib.h> can be included directly."
#endif

25 26 27
#ifndef __G_MARKUP_H__
#define __G_MARKUP_H__

28 29
#include <stdarg.h>

30
#include <glib/gerror.h>
Matthias Clasen's avatar
Matthias Clasen committed
31
#include <glib/gslist.h>
32

33
G_BEGIN_DECLS
34 35 36 37 38 39

typedef enum
{
  G_MARKUP_ERROR_BAD_UTF8,
  G_MARKUP_ERROR_EMPTY,
  G_MARKUP_ERROR_PARSE,
Matthias Clasen's avatar
Matthias Clasen committed
40
  /* The following are primarily intended for specific GMarkupParser
41 42 43 44
   * implementations to set.
   */
  G_MARKUP_ERROR_UNKNOWN_ELEMENT,
  G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
45 46
  G_MARKUP_ERROR_INVALID_CONTENT,
  G_MARKUP_ERROR_MISSING_ATTRIBUTE
Havoc Pennington's avatar
Added  
Havoc Pennington committed
47
} GMarkupError;
48 49 50

#define G_MARKUP_ERROR g_markup_error_quark ()

51
GQuark g_markup_error_quark (void);
52 53 54

typedef enum
{
55
  G_MARKUP_DO_NOT_USE_THIS_UNSUPPORTED_FLAG = 1 << 0,
56 57
  G_MARKUP_TREAT_CDATA_AS_TEXT              = 1 << 1,
  G_MARKUP_PREFIX_ERROR_POSITION            = 1 << 2
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
} GMarkupParseFlags;

typedef struct _GMarkupParseContext GMarkupParseContext;
typedef struct _GMarkupParser GMarkupParser;

struct _GMarkupParser
{
  /* Called for open tags <foo bar="baz"> */
  void (*start_element)  (GMarkupParseContext *context,
                          const gchar         *element_name,
                          const gchar        **attribute_names,
                          const gchar        **attribute_values,
                          gpointer             user_data,
                          GError             **error);

  /* Called for close tags </foo> */
  void (*end_element)    (GMarkupParseContext *context,
                          const gchar         *element_name,
                          gpointer             user_data,
                          GError             **error);

  /* Called for character data */
  /* text is not nul-terminated */
  void (*text)           (GMarkupParseContext *context,
                          const gchar         *text,
83
                          gsize                text_len,  
84 85 86 87 88 89 90 91 92 93
                          gpointer             user_data,
                          GError             **error);

  /* Called for strings that should be re-saved verbatim in this same
   * position, but are not otherwise interpretable.  At the moment
   * this includes comments and processing instructions.
   */
  /* text is not nul-terminated. */
  void (*passthrough)    (GMarkupParseContext *context,
                          const gchar         *passthrough_text,
94
                          gsize                text_len,  
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
                          gpointer             user_data,
                          GError             **error);

  /* Called on error, including one set by other
   * methods in the vtable. The GError should not be freed.
   */
  void (*error)          (GMarkupParseContext *context,
                          GError              *error,
                          gpointer             user_data);
};

GMarkupParseContext *g_markup_parse_context_new   (const GMarkupParser *parser,
                                                   GMarkupParseFlags    flags,
                                                   gpointer             user_data,
                                                   GDestroyNotify       user_data_dnotify);
void                 g_markup_parse_context_free  (GMarkupParseContext *context);
gboolean             g_markup_parse_context_parse (GMarkupParseContext *context,
                                                   const gchar         *text,
113
                                                   gssize               text_len,  
114
                                                   GError             **error);
115 116 117 118
void                 g_markup_parse_context_push  (GMarkupParseContext *context,
                                                   GMarkupParser       *parser,
                                                   gpointer             user_data);
gpointer             g_markup_parse_context_pop   (GMarkupParseContext *context);
119 120 121
                                                   
gboolean             g_markup_parse_context_end_parse (GMarkupParseContext *context,
                                                       GError             **error);
122
G_CONST_RETURN gchar *g_markup_parse_context_get_element (GMarkupParseContext *context);
123
G_CONST_RETURN GSList *g_markup_parse_context_get_element_stack (GMarkupParseContext *context);
124 125 126 127 128

/* For user-constructed error messages, has no precise semantics */
void                 g_markup_parse_context_get_position (GMarkupParseContext *context,
                                                          gint                *line_number,
                                                          gint                *char_number);
129
gpointer             g_markup_parse_context_get_user_data (GMarkupParseContext *context);
130 131 132

/* useful when saving */
gchar* g_markup_escape_text (const gchar *text,
133
                             gssize       length);  
134

135 136 137 138 139
gchar *g_markup_printf_escaped (const char *format,
				...) G_GNUC_PRINTF (1, 2);
gchar *g_markup_vprintf_escaped (const char *format,
				 va_list     args);

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
typedef enum
{
  G_MARKUP_COLLECT_INVALID,
  G_MARKUP_COLLECT_STRING,
  G_MARKUP_COLLECT_STRDUP,
  G_MARKUP_COLLECT_BOOLEAN,
  G_MARKUP_COLLECT_TRISTATE,

  G_MARKUP_COLLECT_OPTIONAL = (1 << 16)
} GMarkupCollectType;


/* useful from start_element */
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,
                                        ...);

161
G_END_DECLS
162 163

#endif /* __G_MARKUP_H__ */