Moved some bits around

This commit is contained in:
Yevgen Muntyan 2007-06-15 17:26:54 -05:00
parent 37cef28ff0
commit 7dad952b3a
2 changed files with 92 additions and 62 deletions

View File

@ -25,10 +25,12 @@ GType
moo_markup_doc_get_type (void) moo_markup_doc_get_type (void)
{ {
static GType type = 0; static GType type = 0;
if (G_UNLIKELY (!type)) if (G_UNLIKELY (!type))
type = g_boxed_type_register_static ("MooMarkupDoc", type = g_boxed_type_register_static ("MooMarkupDoc",
(GBoxedCopyFunc)moo_markup_doc_ref, (GBoxedCopyFunc) moo_markup_doc_ref,
(GBoxedFreeFunc)moo_markup_doc_unref); (GBoxedFreeFunc) moo_markup_doc_unref);
return type; return type;
} }
@ -54,16 +56,12 @@ typedef void (*markup_passthrough_func) (GMarkupParseContext *context,
gpointer user_data, gpointer user_data,
GError **error); GError **error);
typedef struct { typedef struct {
MooMarkupDoc *doc; MooMarkupDoc *doc;
MooMarkupNode *current; MooMarkupNode *current;
} ParserState; } ParserState;
#define BUFSIZE 1024
static MooMarkupDoc *moo_markup_doc_new_priv (const char *name); static MooMarkupDoc *moo_markup_doc_new_priv (const char *name);
static void moo_markup_doc_set_name (MooMarkupDoc *doc, static void moo_markup_doc_set_name (MooMarkupDoc *doc,
const char *name); const char *name);
@ -76,7 +74,12 @@ static MooMarkupNode *moo_markup_text_node_new (MooMarkupNodeType type,
MooMarkupDoc *doc, MooMarkupDoc *doc,
MooMarkupNode *parent, MooMarkupNode *parent,
const char *text, const char *text,
gsize text_len); gssize text_len);
static MooMarkupNode *moo_markup_text_node_new_take_string (MooMarkupNodeType type,
MooMarkupDoc *doc,
MooMarkupNode *parent,
char *text,
gssize text_len);
static void add_node (MooMarkupDoc *doc, static void add_node (MooMarkupDoc *doc,
MooMarkupNode *parent, MooMarkupNode *parent,
MooMarkupNode *node); MooMarkupNode *node);
@ -90,7 +93,7 @@ static void moo_markup_node_free (MooMarkupNode *node);
static void collect_text_content (MooMarkupElement *node); static void collect_text_content (MooMarkupElement *node);
static void moo_markup_text_node_add_text (MooMarkupText *node, static void moo_markup_text_node_add_text (MooMarkupText *node,
const char *text, const char *text,
gsize text_len); gssize text_len);
static void moo_markup_element_print (MooMarkupElement *node, static void moo_markup_element_print (MooMarkupElement *node,
GString *dest); GString *dest);
@ -167,57 +170,66 @@ _moo_markup_get_modified (MooMarkupDoc *doc)
} }
MooMarkupDoc* static MooMarkupDoc *
moo_markup_parse_memory (const char *buffer, markup_parse_memory (const char *buffer,
int size, gssize len,
GError **error) G_GNUC_UNUSED const char *filename,
GError **error)
{ {
GMarkupParser parser = {(markup_start_element_func)start_element, GMarkupParser parser = { (markup_start_element_func) start_element,
(markup_end_element_func)end_element, (markup_end_element_func) end_element,
(markup_text_func)text, (markup_text_func) text,
(markup_passthrough_func)passthrough, (markup_passthrough_func) passthrough,
NULL}; NULL};
MooMarkupDoc *doc;
ParserState state; ParserState state;
GMarkupParseContext *context; GMarkupParseContext *context;
if (size < 0) if (len < 0)
size = strlen (buffer); len = strlen (buffer);
doc = moo_markup_doc_new_priv (NULL); state.doc = moo_markup_doc_new_priv (NULL);
state.doc = doc; _moo_markup_set_modified (state.doc, TRUE);
state.current = MOO_MARKUP_NODE (doc);
context = g_markup_parse_context_new (&parser, (GMarkupParseFlags)0, &state, NULL);
_moo_markup_set_modified (doc, TRUE); state.current = MOO_MARKUP_NODE (state.doc);
context = g_markup_parse_context_new (&parser, (GMarkupParseFlags) 0, &state, NULL);
if (!g_markup_parse_context_parse (context, buffer, size, error) || if (!g_markup_parse_context_parse (context, buffer, len, error) ||
!g_markup_parse_context_end_parse (context, error)) !g_markup_parse_context_end_parse (context, error))
{ {
g_markup_parse_context_free (context); g_markup_parse_context_free (context);
moo_markup_doc_unref (doc); moo_markup_doc_unref (state.doc);
return NULL; return NULL;
} }
g_markup_parse_context_free (context); g_markup_parse_context_free (context);
return doc; return state.doc;
}
MooMarkupDoc *
moo_markup_parse_memory (const char *buffer,
int size,
GError **error)
{
g_return_val_if_fail (buffer != NULL, NULL);
return markup_parse_memory (buffer, size, "memory", error);
} }
MooMarkupDoc* MooMarkupDoc *
moo_markup_parse_file (const char *filename, moo_markup_parse_file (const char *filename,
GError **error) GError **error)
{ {
char *content; char *content;
MooMarkupDoc *doc; MooMarkupDoc *doc;
gsize len;
g_return_val_if_fail (filename != NULL, NULL); g_return_val_if_fail (filename != NULL, NULL);
if (!g_file_get_contents (filename, &content, NULL, error)) if (!g_file_get_contents (filename, &content, &len, error))
return NULL; return NULL;
doc = moo_markup_parse_memory (content, -1, error); doc = markup_parse_memory (content, len, filename, error);
if (doc) if (doc)
moo_markup_doc_set_name (doc, filename); moo_markup_doc_set_name (doc, filename);
@ -292,7 +304,7 @@ passthrough (G_GNUC_UNUSED GMarkupParseContext *ctx,
} }
static MooMarkupDoc* static MooMarkupDoc *
moo_markup_doc_new_priv (const char *name) moo_markup_doc_new_priv (const char *name)
{ {
MooMarkupDoc *doc = _moo_new0 (MooMarkupDoc); MooMarkupDoc *doc = _moo_new0 (MooMarkupDoc);
@ -320,14 +332,14 @@ moo_markup_doc_set_name (MooMarkupDoc *doc,
} }
MooMarkupDoc* MooMarkupDoc *
moo_markup_doc_new (const char *name) moo_markup_doc_new (const char *name)
{ {
return moo_markup_doc_new_priv (name); return moo_markup_doc_new_priv (name);
} }
static MooMarkupNode* static MooMarkupNode *
moo_markup_element_new (MooMarkupDoc *doc, moo_markup_element_new (MooMarkupDoc *doc,
MooMarkupNode *parent, MooMarkupNode *parent,
const char *name, const char *name,
@ -342,8 +354,8 @@ moo_markup_element_new (MooMarkupDoc *doc,
elm->type = MOO_MARKUP_ELEMENT_NODE; elm->type = MOO_MARKUP_ELEMENT_NODE;
elm->name = g_strdup (name); elm->name = g_strdup (name);
elm->attr_names = g_strdupv ((char**)attribute_names); elm->attr_names = g_strdupv ((char**) attribute_names);
elm->attr_vals = g_strdupv ((char**)attribute_values); elm->attr_vals = g_strdupv ((char**) attribute_values);
if (elm->attr_names) if (elm->attr_names)
for (elm->n_attrs = 0; attribute_names[elm->n_attrs]; ++elm->n_attrs) ; for (elm->n_attrs = 0; attribute_names[elm->n_attrs]; ++elm->n_attrs) ;
else else
@ -353,12 +365,12 @@ moo_markup_element_new (MooMarkupDoc *doc,
} }
static MooMarkupNode* static MooMarkupNode *
moo_markup_text_node_new (MooMarkupNodeType type, moo_markup_text_node_new_take_string (MooMarkupNodeType type,
MooMarkupDoc *doc, MooMarkupDoc *doc,
MooMarkupNode *parent, MooMarkupNode *parent,
const char *text, char *text,
gsize text_len) gssize text_len)
{ {
MooMarkupText *node; MooMarkupText *node;
@ -374,12 +386,30 @@ moo_markup_text_node_new (MooMarkupNodeType type,
else else
node->name = g_strdup ("COMMENT"); node->name = g_strdup ("COMMENT");
node->text = g_strndup (text, text_len); if (text_len < 0)
text_len = strlen (text);
node->text = text;
node->size = text_len; node->size = text_len;
return MOO_MARKUP_NODE (node); return MOO_MARKUP_NODE (node);
} }
static MooMarkupNode *
moo_markup_text_node_new (MooMarkupNodeType type,
MooMarkupDoc *doc,
MooMarkupNode *parent,
const char *text,
gssize text_len)
{
if (text_len < 0)
text_len = strlen (text);
return moo_markup_text_node_new_take_string (type, doc, parent,
g_strndup (text, text_len),
text_len);
}
static void static void
add_node (MooMarkupDoc *doc, add_node (MooMarkupDoc *doc,
@ -414,9 +444,17 @@ add_node (MooMarkupDoc *doc,
static void static void
moo_markup_text_node_add_text (MooMarkupText *node, moo_markup_text_node_add_text (MooMarkupText *node,
const char *text, const char *text,
gsize text_len) gssize text_len)
{ {
char *tmp = g_new (char, node->size + text_len + 1); char *tmp;
if (text_len < 0)
text_len = strlen (text);
if (text_len == 0)
return;
tmp = g_new (char, node->size + text_len + 1);
memcpy (tmp, node->text, node->size); memcpy (tmp, node->text, node->size);
memcpy (tmp + node->size, text, text_len); memcpy (tmp + node->size, text, text_len);
g_free (node->text); g_free (node->text);
@ -645,7 +683,7 @@ moo_markup_comment_node_print (MooMarkupNode *node,
*p = 0; *p = 0;
} }
g_string_append (str, text->text); g_string_append_printf (str, "<!--%s-->", text->text);
} }

View File

@ -21,11 +21,6 @@ G_BEGIN_DECLS
#define MOO_TYPE_MARKUP_DOC (moo_markup_doc_get_type()) #define MOO_TYPE_MARKUP_DOC (moo_markup_doc_get_type())
#if 0
// #define MOO_TYPE_MARKUP_NODE_TYPE (moo_markup_node_type_get_type ())
// GType moo_markup_node_type_get_type (void);
#endif
typedef enum { typedef enum {
MOO_MARKUP_DOC_NODE, MOO_MARKUP_DOC_NODE,
MOO_MARKUP_ELEMENT_NODE, MOO_MARKUP_ELEMENT_NODE,
@ -41,18 +36,18 @@ typedef struct _MooMarkupText MooMarkupText;
typedef struct _MooMarkupText MooMarkupComment; typedef struct _MooMarkupText MooMarkupComment;
#ifndef ENABLE_DEBUG #if 0 && defined(ENABLE_DEBUG)
#define MOO_MARKUP_NODE(n) ((MooMarkupNode*)n)
#define MOO_MARKUP_DOC(n) ((MooMarkupDoc*)(n))
#define MOO_MARKUP_ELEMENT(n) ((MooMarkupElement*)(n))
#define MOO_MARKUP_TEXT(n) ((MooMarkupText*)(n))
#define MOO_MARKUP_COMMENT(n) ((MooMarkupComment*)(n))
#else /* ENABLE_DEBUG */
#define MOO_MARKUP_NODE(n) (MOO_MARKUP_NODE_CHECK_CAST(n)) #define MOO_MARKUP_NODE(n) (MOO_MARKUP_NODE_CHECK_CAST(n))
#define MOO_MARKUP_DOC(n) (MOO_MARKUP_DOC_CHECK_CAST(n)) #define MOO_MARKUP_DOC(n) (MOO_MARKUP_DOC_CHECK_CAST(n))
#define MOO_MARKUP_ELEMENT(n) (MOO_MARKUP_ELEMENT_CHECK_CAST(n)) #define MOO_MARKUP_ELEMENT(n) (MOO_MARKUP_ELEMENT_CHECK_CAST(n))
#define MOO_MARKUP_TEXT(n) (MOO_MARKUP_TEXT_CHECK_CAST(n)) #define MOO_MARKUP_TEXT(n) (MOO_MARKUP_TEXT_CHECK_CAST(n))
#define MOO_MARKUP_COMMENT(n) (MOO_MARKUP_COMMENT_CHECK_CAST(n)) #define MOO_MARKUP_COMMENT(n) (MOO_MARKUP_COMMENT_CHECK_CAST(n))
#else /* ENABLE_DEBUG */
#define MOO_MARKUP_NODE(n) ((MooMarkupNode*)n)
#define MOO_MARKUP_DOC(n) ((MooMarkupDoc*)(n))
#define MOO_MARKUP_ELEMENT(n) ((MooMarkupElement*)(n))
#define MOO_MARKUP_TEXT(n) ((MooMarkupText*)(n))
#define MOO_MARKUP_COMMENT(n) ((MooMarkupComment*)(n))
#endif /* ENABLE_DEBUG */ #endif /* ENABLE_DEBUG */
#define MOO_MARKUP_IS_DOC(n) ((n) != NULL && MOO_MARKUP_NODE(n)->type == MOO_MARKUP_DOC_NODE) #define MOO_MARKUP_IS_DOC(n) ((n) != NULL && MOO_MARKUP_NODE(n)->type == MOO_MARKUP_DOC_NODE)
@ -62,7 +57,6 @@ typedef struct _MooMarkupText MooMarkupComment;
struct _MooMarkupNode { struct _MooMarkupNode {
void *_private; /* application data */
MooMarkupNodeType type; /* type of the node */ MooMarkupNodeType type; /* type of the node */
char *name; /* the name of the node */ char *name; /* the name of the node */
MooMarkupNode *children; /* parent->childs link */ MooMarkupNode *children; /* parent->childs link */
@ -75,7 +69,6 @@ struct _MooMarkupNode {
struct _MooMarkupDoc { struct _MooMarkupDoc {
void *_private; /* application data */
MooMarkupNodeType type; /* MOO_MARKUP_DOC_NODE */ MooMarkupNodeType type; /* MOO_MARKUP_DOC_NODE */
char *name; /* name/filename/URI of the document */ char *name; /* name/filename/URI of the document */
MooMarkupNode *children; /* the document tree */ MooMarkupNode *children; /* the document tree */
@ -84,6 +77,7 @@ struct _MooMarkupDoc {
MooMarkupNode *next; /* NULL */ MooMarkupNode *next; /* NULL */
MooMarkupNode *prev; /* NULL */ MooMarkupNode *prev; /* NULL */
MooMarkupDoc *doc; /* self */ MooMarkupDoc *doc; /* self */
guint ref_count; guint ref_count;
guint modified : 1; guint modified : 1;
guint track_modified : 1; guint track_modified : 1;
@ -91,7 +85,6 @@ struct _MooMarkupDoc {
struct _MooMarkupElement { struct _MooMarkupElement {
void *_private; /* application data */
MooMarkupNodeType type; /* MOO_MARKUP_ELEMENT_NODE */ MooMarkupNodeType type; /* MOO_MARKUP_ELEMENT_NODE */
char *name; /* name */ char *name; /* name */
MooMarkupNode *children; /* content */ MooMarkupNode *children; /* content */
@ -109,7 +102,6 @@ struct _MooMarkupElement {
struct _MooMarkupText { struct _MooMarkupText {
void *_private; /* application data */
MooMarkupNodeType type; /* MOO_MARKUP_TEXT_NODE */ MooMarkupNodeType type; /* MOO_MARKUP_TEXT_NODE */
char *name; /* "TEXT" */ char *name; /* "TEXT" */
MooMarkupNode *children; /* NULL */ MooMarkupNode *children; /* NULL */