moo_parse_markup_file()

master
Yevgen Muntyan 2008-08-31 14:53:51 -05:00
parent a5c2c921b2
commit 4e3b78d262
2 changed files with 103 additions and 33 deletions

View File

@ -1,7 +1,7 @@
/*
* moomarkup.c
*
* Copyright (C) 2004-2007 by Yevgen Muntyan <muntyan@math.tamu.edu>
* Copyright (C) 2004-2008 by Yevgen Muntyan <muntyan@math.tamu.edu>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -31,27 +31,14 @@ moo_markup_doc_get_type (void)
return type;
}
typedef void (*markup_start_element_func) (GMarkupParseContext *context,
const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values,
gpointer user_data,
GError **error);
typedef void (*markup_end_element_func) (GMarkupParseContext *context,
const gchar *element_name,
gpointer user_data,
GError **error);
typedef void (*markup_text_func) (GMarkupParseContext *context,
const gchar *text,
gsize text_len,
gpointer user_data,
GError **error);
typedef void (*markup_passthrough_func) (GMarkupParseContext *context,
const gchar *passthrough_text,
gsize text_len,
gpointer user_data,
GError **error);
GQuark
moo_parse_error_quark (void)
{
static GQuark q;
if (G_UNLIKELY (!q))
q = g_quark_from_static_string ("moo-parse-error");
return q;
}
typedef struct {
MooMarkupDoc *doc;
@ -173,10 +160,10 @@ markup_parse_memory (const char *buffer,
G_GNUC_UNUSED const char *filename,
GError **error)
{
GMarkupParser parser = { (markup_start_element_func) start_element,
(markup_end_element_func) end_element,
(markup_text_func) text,
(markup_passthrough_func) passthrough,
GMarkupParser parser = { (MooMarkupStartElementFunc) start_element,
(MooMarkupEndElementFunc) end_element,
(MooMarkupTextFunc) text,
(MooMarkupPassthroughFunc) passthrough,
NULL};
ParserState state;
@ -223,6 +210,8 @@ moo_markup_parse_file (const char *filename,
g_return_val_if_fail (filename != NULL, NULL);
/* XXX use moo_parse_markup_file here */
if (!g_file_get_contents (filename, &content, &len, error))
return NULL;
@ -1318,3 +1307,52 @@ MOO_MARKUP_COMMENT_CHECK_CAST (gpointer n)
g_return_val_if_fail (node->type == MOO_MARKUP_COMMENT_NODE, NULL);
return (MooMarkupComment*) n;
}
gboolean
moo_parse_markup_file (const char *filename,
const GMarkupParser *parser,
gpointer data,
GError **error)
{
GMarkupParseContext *ctx;
MooFileReader *reader;
char buf[1024];
gsize size;
gboolean seen_error = FALSE;
g_return_val_if_fail (filename != NULL, FALSE);
g_return_val_if_fail (parser != NULL, FALSE);
g_return_val_if_fail (!error || !*error, FALSE);
if (!(reader = moo_text_reader_new (filename, error)))
return FALSE;
ctx = g_markup_parse_context_new (parser, 0, data, NULL);
while (TRUE)
{
if (!moo_file_reader_read (reader, buf, sizeof buf, &size, error))
{
seen_error = TRUE;
break;
}
if (!size)
break;
if (!g_markup_parse_context_parse (ctx, buf, size, error))
{
seen_error = TRUE;
break;
}
}
if (!seen_error && !g_markup_parse_context_end_parse (ctx, error))
seen_error = TRUE;
g_markup_parse_context_free (ctx);
moo_file_reader_close (reader);
return !seen_error;
}

View File

@ -1,7 +1,7 @@
/*
* moomarkup.h
*
* Copyright (C) 2004-2007 by Yevgen Muntyan <muntyan@math.tamu.edu>
* Copyright (C) 2004-2008 by Yevgen Muntyan <muntyan@math.tamu.edu>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -19,6 +19,12 @@ G_BEGIN_DECLS
#define MOO_TYPE_MARKUP_DOC (moo_markup_doc_get_type())
#define MOO_PARSE_ERROR (moo_parse_error_quark ())
enum {
MOO_PARSE_ERROR_INVALID_CONTENT
};
typedef enum {
MOO_MARKUP_DOC_NODE,
MOO_MARKUP_ELEMENT_NODE,
@ -33,6 +39,26 @@ typedef struct _MooMarkupElement MooMarkupElement;
typedef struct _MooMarkupText MooMarkupText;
typedef struct _MooMarkupText MooMarkupComment;
typedef void (*MooMarkupStartElementFunc) (GMarkupParseContext *context,
const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values,
gpointer user_data,
GError **error);
typedef void (*MooMarkupEndElementFunc) (GMarkupParseContext *context,
const gchar *element_name,
gpointer user_data,
GError **error);
typedef void (*MooMarkupTextFunc) (GMarkupParseContext *context,
const gchar *text,
gsize text_len,
gpointer user_data,
GError **error);
typedef void (*MooMarkupPassthroughFunc) (GMarkupParseContext *context,
const gchar *passthrough_text,
gsize text_len,
gpointer user_data,
GError **error);
#if 0 && defined(ENABLE_DEBUG)
#define MOO_MARKUP_NODE(n) (MOO_MARKUP_NODE_CHECK_CAST(n))
@ -114,13 +140,14 @@ struct _MooMarkupText {
};
GType moo_markup_doc_get_type (void);
GType moo_markup_doc_get_type (void) G_GNUC_CONST;
GQuark moo_parse_error_quark (void) G_GNUC_CONST;
MooMarkupNode *MOO_MARKUP_NODE_CHECK_CAST (gpointer node);
MooMarkupDoc *MOO_MARKUP_DOC_CHECK_CAST (gpointer node);
MooMarkupElement *MOO_MARKUP_ELEMENT_CHECK_CAST (gpointer node);
MooMarkupText *MOO_MARKUP_TEXT_CHECK_CAST (gpointer node);
MooMarkupComment *MOO_MARKUP_COMMENT_CHECK_CAST (gpointer node);
MooMarkupNode *MOO_MARKUP_NODE_CHECK_CAST (gpointer node);
MooMarkupDoc *MOO_MARKUP_DOC_CHECK_CAST (gpointer node);
MooMarkupElement *MOO_MARKUP_ELEMENT_CHECK_CAST (gpointer node);
MooMarkupText *MOO_MARKUP_TEXT_CHECK_CAST (gpointer node);
MooMarkupComment *MOO_MARKUP_COMMENT_CHECK_CAST (gpointer node);
MooMarkupDoc *moo_markup_doc_new (const char *name);
@ -180,6 +207,11 @@ MooMarkupNode *moo_markup_create_text_element (MooMarkupNode *parent,
const char *path,
const char *content);
gboolean moo_parse_markup_file (const char *filename,
const GMarkupParser *parser,
gpointer data,
GError **error);
void _moo_markup_set_modified (MooMarkupDoc *doc,
gboolean modified);
void _moo_markup_set_track_modified (MooMarkupDoc *doc,