medit/moo/mooutils/moomarkup.c

1291 lines
36 KiB
C
Raw Normal View History

2005-06-22 11:20:32 -07:00
/*
* mooutils/moomarkup.c
*
2007-04-07 01:21:52 -07:00
* Copyright (C) 2004-2007 by Yevgen Muntyan <muntyan@math.tamu.edu>
2005-06-22 11:20:32 -07:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* See COPYING file that comes with this distribution.
*/
#include "mooutils/moomarkup.h"
#include "mooutils/mooutils-fs.h"
#include "mooutils/mooutils-gobject.h"
#include "mooutils/mooutils-debug.h"
#include "mooutils/moocompat.h"
2005-06-22 11:20:32 -07:00
#include <string.h>
#include <glib.h>
2005-09-08 10:08:31 -07:00
GType
moo_markup_doc_get_type (void)
2005-06-22 11:20:32 -07:00
{
static GType type = 0;
2006-12-30 20:18:14 -08:00
if (G_UNLIKELY (!type))
2005-06-22 11:20:32 -07:00
type = g_boxed_type_register_static ("MooMarkupDoc",
(GBoxedCopyFunc)moo_markup_doc_ref,
(GBoxedFreeFunc)moo_markup_doc_unref);
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);
typedef struct {
MooMarkupDoc *doc;
MooMarkupNode *current;
} ParserState;
#define BUFSIZE 1024
static MooMarkupDoc *moo_markup_doc_new_priv (const char *name);
static void moo_markup_doc_set_name (MooMarkupDoc *doc,
const char *name);
2005-06-22 11:20:32 -07:00
static MooMarkupNode *moo_markup_element_new (MooMarkupDoc *doc,
MooMarkupNode *parent,
const char *name,
const char **attribute_names,
const char **attribute_values);
static MooMarkupNode *moo_markup_text_node_new (MooMarkupNodeType type,
MooMarkupDoc *doc,
MooMarkupNode *parent,
const char *text,
gsize text_len);
static void add_node (MooMarkupDoc *doc,
MooMarkupNode *parent,
MooMarkupNode *node);
static void moo_markup_doc_unref_private (MooMarkupDoc *doc);
static void moo_markup_element_free (MooMarkupElement *node);
static void moo_markup_text_node_free (MooMarkupNode *node);
static void moo_markup_node_free (MooMarkupNode *node);
static void collect_text_content (MooMarkupElement *node);
static void moo_markup_text_node_add_text (MooMarkupText *node,
const char *text,
gsize text_len);
static void moo_markup_element_print (MooMarkupElement *node,
GString *dest);
static void moo_markup_text_node_print (MooMarkupNode *node,
GString *dest);
static void moo_markup_comment_node_print (MooMarkupNode *node,
GString *dest);
2005-06-22 11:20:32 -07:00
static void start_element (GMarkupParseContext *context,
const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values,
ParserState *state,
GError **error);
static void end_element (GMarkupParseContext *context,
const gchar *element_name,
ParserState *state,
GError **error);
static void text (GMarkupParseContext *context,
const gchar *text,
gsize text_len,
ParserState *state,
GError **error);
static void passthrough (GMarkupParseContext *context,
const gchar *passthrough_text,
gsize text_len,
ParserState *state,
GError **error);
static void
set_modified (MooMarkupNode *node,
gboolean modified)
{
if (node->doc)
{
#ifdef MOO_DEBUG
if (modified && !node->doc->modified && node->doc->track_modified)
{
g_message ("markup doc %s modified",
node->doc->name ? node->doc->name : "<NULL>");
}
#endif
node->doc->modified = modified != 0;
}
}
void
_moo_markup_set_modified (MooMarkupDoc *doc,
gboolean modified)
{
g_return_if_fail (MOO_MARKUP_IS_DOC (doc));
set_modified (MOO_MARKUP_NODE (doc), modified);
}
void
_moo_markup_set_track_modified (MooMarkupDoc *doc,
gboolean track)
{
g_return_if_fail (MOO_MARKUP_IS_DOC (doc));
doc->track_modified = track != 0;
}
gboolean
_moo_markup_get_modified (MooMarkupDoc *doc)
{
g_return_val_if_fail (MOO_MARKUP_IS_DOC (doc), FALSE);
return doc->modified;
}
2005-09-08 10:08:31 -07:00
MooMarkupDoc*
moo_markup_parse_memory (const char *buffer,
int size,
GError **error)
2005-06-22 11:20:32 -07:00
{
GMarkupParser parser = {(markup_start_element_func)start_element,
(markup_end_element_func)end_element,
(markup_text_func)text,
(markup_passthrough_func)passthrough,
NULL};
MooMarkupDoc *doc;
ParserState state;
GMarkupParseContext *context;
if (size < 0)
size = strlen (buffer);
2005-06-22 11:20:32 -07:00
doc = moo_markup_doc_new_priv (NULL);
state.doc = doc;
state.current = MOO_MARKUP_NODE (doc);
context = g_markup_parse_context_new (&parser, (GMarkupParseFlags)0, &state, NULL);
_moo_markup_set_modified (doc, TRUE);
2005-09-08 12:17:23 -07:00
if (!g_markup_parse_context_parse (context, buffer, size, error) ||
!g_markup_parse_context_end_parse (context, error))
2005-06-22 11:20:32 -07:00
{
g_markup_parse_context_free (context);
moo_markup_doc_unref (doc);
return NULL;
}
g_markup_parse_context_free (context);
return doc;
}
2005-09-08 10:08:31 -07:00
MooMarkupDoc*
moo_markup_parse_file (const char *filename,
GError **error)
2005-06-22 11:20:32 -07:00
{
2005-08-02 14:59:34 -07:00
char *content;
2005-06-22 11:20:32 -07:00
MooMarkupDoc *doc;
2005-08-02 14:59:34 -07:00
g_return_val_if_fail (filename != NULL, NULL);
2005-07-26 04:10:14 -07:00
2005-08-02 14:59:34 -07:00
if (!g_file_get_contents (filename, &content, NULL, error))
2005-06-22 11:20:32 -07:00
return NULL;
2005-08-02 14:59:34 -07:00
doc = moo_markup_parse_memory (content, -1, error);
2005-06-22 11:20:32 -07:00
if (doc)
moo_markup_doc_set_name (doc, filename);
2005-08-02 14:59:34 -07:00
g_free (content);
2005-06-22 11:20:32 -07:00
return doc;
}
2005-09-08 10:08:31 -07:00
static void
start_element (G_GNUC_UNUSED GMarkupParseContext *ctx,
const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values,
ParserState *state,
G_GNUC_UNUSED GError **error)
2005-06-22 11:20:32 -07:00
{
MooMarkupNode *elm = moo_markup_element_new (state->doc,
state->current,
element_name,
attribute_names,
attribute_values);
g_assert (elm->parent == state->current);
2005-06-22 11:20:32 -07:00
state->current = elm;
}
2005-09-08 10:08:31 -07:00
static void
end_element (G_GNUC_UNUSED GMarkupParseContext *ctx,
G_GNUC_UNUSED const gchar *elm,
ParserState *state,
G_GNUC_UNUSED GError **error)
2005-06-22 11:20:32 -07:00
{
g_assert (state->current->type == MOO_MARKUP_ELEMENT_NODE);
collect_text_content (MOO_MARKUP_ELEMENT (state->current));
g_assert (state->current->parent != NULL);
state->current = state->current->parent;
}
2005-09-08 10:08:31 -07:00
static void
text (G_GNUC_UNUSED GMarkupParseContext *ctx,
const gchar *text,
gsize text_len,
ParserState *state,
G_GNUC_UNUSED GError **error)
2005-06-22 11:20:32 -07:00
{
if (MOO_MARKUP_IS_TEXT (state->current->last))
moo_markup_text_node_add_text (MOO_MARKUP_TEXT (state->current->last),
text, text_len);
else
moo_markup_text_node_new (MOO_MARKUP_TEXT_NODE,
state->doc, state->current,
text, text_len);
}
2005-09-08 10:08:31 -07:00
static void
passthrough (G_GNUC_UNUSED GMarkupParseContext *ctx,
const gchar *passthrough_text,
gsize text_len,
ParserState *state,
G_GNUC_UNUSED GError **error)
2005-06-22 11:20:32 -07:00
{
if (MOO_MARKUP_IS_COMMENT (state->current->last))
moo_markup_text_node_add_text (MOO_MARKUP_COMMENT (state->current->last),
passthrough_text, text_len);
else
moo_markup_text_node_new (MOO_MARKUP_COMMENT_NODE,
state->doc, state->current,
passthrough_text, text_len);
}
2005-09-08 10:08:31 -07:00
static MooMarkupDoc*
moo_markup_doc_new_priv (const char *name)
2005-06-22 11:20:32 -07:00
{
MooMarkupDoc *doc = g_new0 (MooMarkupDoc, 1);
doc->type = MOO_MARKUP_DOC_NODE;
doc->name = g_strdup (name ? name : "");
2005-06-22 11:20:32 -07:00
doc->doc = doc;
doc->ref_count = 1;
return doc;
}
static void
moo_markup_doc_set_name (MooMarkupDoc *doc,
const char *name)
{
char *tmp;
g_return_if_fail (MOO_MARKUP_IS_DOC (doc));
tmp = doc->name;
doc->name = g_strdup (name ? name : "");
g_free (tmp);
}
2005-09-08 10:08:31 -07:00
MooMarkupDoc*
moo_markup_doc_new (const char *name)
{
return moo_markup_doc_new_priv (name);
}
2005-09-08 10:08:31 -07:00
static MooMarkupNode*
moo_markup_element_new (MooMarkupDoc *doc,
MooMarkupNode *parent,
const char *name,
const char **attribute_names,
const char **attribute_values)
2005-06-22 11:20:32 -07:00
{
MooMarkupElement *elm = g_new0 (MooMarkupElement, 1);
add_node (doc, parent, MOO_MARKUP_NODE (elm));
elm->type = MOO_MARKUP_ELEMENT_NODE;
elm->name = g_strdup (name);
elm->attr_names = g_strdupv ((char**)attribute_names);
elm->attr_vals = g_strdupv ((char**)attribute_values);
if (elm->attr_names)
for (elm->n_attrs = 0; attribute_names[elm->n_attrs]; ++elm->n_attrs) ;
else
elm->n_attrs = 0;
2005-06-22 11:20:32 -07:00
return MOO_MARKUP_NODE (elm);
}
2005-09-08 10:08:31 -07:00
static MooMarkupNode*
moo_markup_text_node_new (MooMarkupNodeType type,
MooMarkupDoc *doc,
MooMarkupNode *parent,
const char *text,
gsize text_len)
2005-06-22 11:20:32 -07:00
{
MooMarkupText *node;
g_assert (type == MOO_MARKUP_TEXT_NODE || type == MOO_MARKUP_COMMENT_NODE);
node = g_new0 (MooMarkupText, 1);
add_node (doc, parent, MOO_MARKUP_NODE (node));
node->type = type;
2005-06-22 11:20:32 -07:00
if (type == MOO_MARKUP_TEXT_NODE)
node->name = g_strdup ("TEXT");
else
node->name = g_strdup ("COMMENT");
node->text = g_strndup (text, text_len);
node->size = text_len;
return MOO_MARKUP_NODE (node);
}
2005-09-08 10:08:31 -07:00
static void
add_node (MooMarkupDoc *doc,
MooMarkupNode *parent,
MooMarkupNode *node)
2005-06-22 11:20:32 -07:00
{
2005-09-08 10:08:31 -07:00
g_assert (MOO_MARKUP_IS_DOC (doc));
g_assert (parent != NULL);
g_assert (node != NULL);
2005-06-22 11:20:32 -07:00
node->doc = doc;
node->parent = parent;
2005-09-08 10:08:31 -07:00
2005-06-22 11:20:32 -07:00
if (parent->last)
{
parent->last->next = node;
node->prev = parent->last;
parent->last = node;
}
else
{
g_assert (parent->children == NULL);
g_assert (parent->last == NULL);
2005-06-22 11:20:32 -07:00
parent->children = node;
parent->last = node;
}
_moo_markup_set_modified (doc, TRUE);
2005-06-22 11:20:32 -07:00
}
2005-09-08 10:08:31 -07:00
static void
moo_markup_text_node_add_text (MooMarkupText *node,
const char *text,
gsize text_len)
2005-06-22 11:20:32 -07:00
{
char *tmp = g_new (char, node->size + text_len + 1);
memcpy (tmp, node->text, node->size);
memcpy (tmp + node->size, text, text_len);
2005-06-22 11:20:32 -07:00
g_free (node->text);
node->text = tmp;
node->size += text_len;
node->text[node->size] = 0;
set_modified (MOO_MARKUP_NODE (node), TRUE);
2005-06-22 11:20:32 -07:00
}
2005-09-08 10:08:31 -07:00
static void
collect_text_content (MooMarkupElement *node)
2005-06-22 11:20:32 -07:00
{
MooMarkupNode *child;
GString *text = NULL;
2005-06-22 11:20:32 -07:00
for (child = node->children; child != NULL; child = child->next)
{
if (MOO_MARKUP_IS_TEXT (child))
{
if (text)
{
g_string_append_len (text,
MOO_MARKUP_TEXT(child)->text,
MOO_MARKUP_TEXT(child)->size);
}
2005-06-22 11:20:32 -07:00
else
{
text = g_string_new_len (MOO_MARKUP_TEXT(child)->text,
MOO_MARKUP_TEXT(child)->size);
}
2005-06-22 11:20:32 -07:00
}
}
g_free (node->content);
if (text)
node->content = g_string_free (text, FALSE);
else
node->content = NULL;
2005-06-22 11:20:32 -07:00
}
2005-09-08 10:08:31 -07:00
static void
moo_markup_node_free (MooMarkupNode *node)
2005-06-22 11:20:32 -07:00
{
MooMarkupNode *child;
GSList *children = NULL, *l;
2005-06-22 11:20:32 -07:00
g_return_if_fail (node != NULL);
for (child = node->children; child != NULL; child = child->next)
children = g_slist_prepend (children, child);
for (l = children; l != NULL; l = l->next)
moo_markup_node_free (l->data);
g_free (node->name);
2005-06-22 11:20:32 -07:00
switch (node->type)
{
case MOO_MARKUP_DOC_NODE:
moo_markup_doc_unref_private (MOO_MARKUP_DOC (node));
break;
case MOO_MARKUP_ELEMENT_NODE:
moo_markup_element_free (MOO_MARKUP_ELEMENT (node));
break;
case MOO_MARKUP_TEXT_NODE:
case MOO_MARKUP_COMMENT_NODE:
moo_markup_text_node_free (node);
break;
default:
g_assert_not_reached ();
}
g_slist_free (children);
g_free (node);
2005-06-22 11:20:32 -07:00
}
2005-09-08 10:08:31 -07:00
static void
moo_markup_element_free (MooMarkupElement *node)
2005-06-22 11:20:32 -07:00
{
g_free (node->content);
g_strfreev (node->attr_names);
g_strfreev (node->attr_vals);
}
2005-09-08 10:08:31 -07:00
static void
moo_markup_text_node_free (MooMarkupNode *node)
2005-06-22 11:20:32 -07:00
{
g_assert (node->type == MOO_MARKUP_TEXT_NODE || node->type == MOO_MARKUP_COMMENT_NODE);
g_free (((MooMarkupText*)node)->text);
}
2005-09-08 10:08:31 -07:00
void
moo_markup_doc_unref (MooMarkupDoc *doc)
2005-06-22 11:20:32 -07:00
{
2005-09-07 04:19:26 -07:00
if (doc)
{
g_return_if_fail (MOO_MARKUP_IS_DOC (doc));
if (--(doc->ref_count)) return;
moo_markup_node_free (MOO_MARKUP_NODE (doc));
}
2005-06-22 11:20:32 -07:00
}
2005-09-08 10:08:31 -07:00
MooMarkupDoc*
moo_markup_doc_ref (MooMarkupDoc *doc)
2005-06-22 11:20:32 -07:00
{
g_return_val_if_fail (MOO_MARKUP_IS_DOC (doc), NULL);
++(doc->ref_count);
return doc;
}
2005-09-08 10:08:31 -07:00
static void
moo_markup_doc_unref_private (G_GNUC_UNUSED MooMarkupDoc *doc)
2005-06-22 11:20:32 -07:00
{
}
2005-09-08 10:08:31 -07:00
char*
moo_markup_node_get_string (MooMarkupNode *node)
2005-06-22 11:20:32 -07:00
{
MooMarkupNode *child;
GString *str = g_string_new ("");
2006-06-01 00:08:56 -07:00
g_return_val_if_fail (node != NULL, NULL);
for (child = node->children; child != NULL; child = child->next)
2005-06-22 11:20:32 -07:00
switch (child->type)
{
case MOO_MARKUP_ELEMENT_NODE:
moo_markup_element_print (MOO_MARKUP_ELEMENT (child), str);
break;
case MOO_MARKUP_TEXT_NODE:
moo_markup_text_node_print (child, str);
break;
case MOO_MARKUP_COMMENT_NODE:
moo_markup_comment_node_print (child, str);
break;
2005-06-22 11:20:32 -07:00
default:
g_assert_not_reached ();
}
return g_string_free (str, FALSE);
}
2005-09-08 10:08:31 -07:00
static void
moo_markup_element_print (MooMarkupElement *node,
GString *str)
2005-06-22 11:20:32 -07:00
{
guint i;
g_string_append_printf (str, "<%s", node->name);
for (i = 0; i < node->n_attrs; ++i)
g_string_append_printf (str, " %s=\"%s\"",
node->attr_names[i],
node->attr_vals[i]);
if (node->children)
{
MooMarkupNode *child;
g_string_append (str, ">");
for (child = node->children; child != NULL; child = child->next)
switch (child->type)
{
case MOO_MARKUP_ELEMENT_NODE:
moo_markup_element_print (MOO_MARKUP_ELEMENT (child), str);
break;
case MOO_MARKUP_COMMENT_NODE:
moo_markup_comment_node_print (child, str);
break;
case MOO_MARKUP_TEXT_NODE:
2005-06-22 11:20:32 -07:00
moo_markup_text_node_print (child, str);
break;
default:
g_assert_not_reached ();
}
g_string_append_printf (str, "</%s>", node->name);
}
else
g_string_append (str, "/>");
}
2005-09-08 10:08:31 -07:00
static void
moo_markup_text_node_print (MooMarkupNode *node,
GString *str)
2005-06-22 11:20:32 -07:00
{
2005-07-29 20:45:08 -07:00
char *p, *escaped;
2005-06-22 11:20:32 -07:00
MooMarkupText *text = (MooMarkupText*) node;
2005-07-29 20:45:08 -07:00
if (!g_utf8_validate (text->text, -1, (const char**) &p))
{
g_critical ("%s: invalid UTF8", G_STRLOC);
*p = 0;
}
escaped = g_markup_escape_text (text->text, -1);
g_string_append (str, escaped);
g_free (escaped);
2005-06-22 11:20:32 -07:00
}
static void
moo_markup_comment_node_print (MooMarkupNode *node,
GString *str)
{
char *p;
MooMarkupText *text = (MooMarkupText*) node;
if (!g_utf8_validate (text->text, -1, (const char**) &p))
{
g_critical ("%s: invalid UTF8", G_STRLOC);
*p = 0;
}
g_string_append (str, text->text);
}
const char*
moo_markup_get_content (MooMarkupNode *node)
{
g_return_val_if_fail (MOO_MARKUP_IS_ELEMENT (node), NULL);
return MOO_MARKUP_ELEMENT(node)->content;
}
2005-09-04 21:24:21 -07:00
static MooMarkupElement*
get_element (MooMarkupNode *node,
char **path)
{
MooMarkupNode *child;
if (!path[0])
return MOO_MARKUP_ELEMENT (node);
for (child = node->children; child; child = child->next)
{
if (MOO_MARKUP_IS_ELEMENT (child))
{
if (!strcmp (path[0], child->name))
2005-06-22 11:20:32 -07:00
return get_element (child, ++path);
}
}
return NULL;
}
MooMarkupNode*
2005-09-04 21:24:21 -07:00
moo_markup_get_element (MooMarkupNode *node,
const char *path)
2005-06-22 11:20:32 -07:00
{
char **p;
MooMarkupElement *elm;
g_return_val_if_fail (path != NULL, NULL);
2005-09-04 21:24:21 -07:00
g_return_val_if_fail (MOO_MARKUP_IS_ELEMENT (node) ||
MOO_MARKUP_IS_DOC (node), NULL);
2005-06-22 11:20:32 -07:00
p = g_strsplit (path, "/", 0);
2005-09-04 21:24:21 -07:00
g_return_val_if_fail (p != NULL, NULL);
2005-06-22 11:20:32 -07:00
elm = get_element (node, p);
2005-09-04 21:24:21 -07:00
2005-06-22 11:20:32 -07:00
g_strfreev (p);
return (MooMarkupNode*)elm;
2005-06-22 11:20:32 -07:00
}
2005-09-08 10:08:31 -07:00
const char*
moo_markup_get_prop (MooMarkupNode *node,
2005-09-08 10:08:31 -07:00
const char *prop_name)
2005-06-22 11:20:32 -07:00
{
guint i;
MooMarkupElement *elm;
2005-06-22 11:20:32 -07:00
g_return_val_if_fail (node != NULL && prop_name != NULL, NULL);
g_return_val_if_fail (MOO_MARKUP_IS_ELEMENT (node), NULL);
elm = MOO_MARKUP_ELEMENT(node);
for (i = 0 ; i < elm->n_attrs; ++i)
if (!strcmp (prop_name, elm->attr_names[i]))
return elm->attr_vals[i];
2005-06-22 11:20:32 -07:00
return NULL;
}
2005-09-08 10:08:31 -07:00
void
moo_markup_set_prop (MooMarkupNode *elm,
2005-09-08 10:08:31 -07:00
const char *prop_name,
const char *val)
2005-09-04 21:24:21 -07:00
{
guint i;
char **attr_names, **attr_vals;
MooMarkupElement *node;
2005-09-04 21:24:21 -07:00
g_return_if_fail (elm != NULL && prop_name != NULL);
g_return_if_fail (MOO_MARKUP_IS_ELEMENT (elm));
2005-09-04 21:24:21 -07:00
g_return_if_fail (!val || g_utf8_validate (val, -1, NULL));
set_modified (elm, TRUE);
2005-09-04 21:24:21 -07:00
/* XXX validate prop_name and val */
node = MOO_MARKUP_ELEMENT (elm);
2005-09-04 21:24:21 -07:00
for (i = 0 ; i < node->n_attrs; ++i)
if (!strcmp (prop_name, node->attr_names[i]))
{
if (val)
{
g_free (node->attr_vals[i]);
node->attr_vals[i] = g_strdup (val);
return;
}
else if (node->n_attrs == 1)
{
g_strfreev (node->attr_names);
g_strfreev (node->attr_vals);
node->attr_names = NULL;
node->attr_vals = NULL;
node->n_attrs = 0;
return;
}
else
{
guint j;
/* XXX just move them */
attr_names = g_new (char*, node->n_attrs);
attr_vals = g_new (char*, node->n_attrs);
for (j = 0; j < i; ++j)
{
attr_names[j] = node->attr_names[j];
attr_vals[j] = node->attr_vals[j];
}
for (j = i + 1; j < node->n_attrs; ++j)
{
attr_names[j-1] = node->attr_names[j];
attr_vals[j-1] = node->attr_vals[j];
}
attr_names[node->n_attrs - 1] = NULL;
attr_vals[node->n_attrs - 1] = NULL;
g_free (node->attr_names[i]);
g_free (node->attr_vals[i]);
g_free (node->attr_names);
g_free (node->attr_vals);
node->attr_names = attr_names;
node->attr_vals = attr_vals;
node->n_attrs--;
return;
}
}
if (!val)
return;
g_return_if_fail (g_utf8_validate (prop_name, -1, NULL));
attr_names = g_new (char*, node->n_attrs + 2);
attr_vals = g_new (char*, node->n_attrs + 2);
for (i = 0; i < node->n_attrs; ++i)
{
attr_names[i] = node->attr_names[i];
attr_vals[i] = node->attr_vals[i];
}
attr_names[node->n_attrs] = g_strdup (prop_name);
attr_vals[node->n_attrs] = g_strdup (val);
attr_names[node->n_attrs + 1] = NULL;
attr_vals[node->n_attrs + 1] = NULL;
g_free (node->attr_names);
g_free (node->attr_vals);
node->attr_names = attr_names;
node->attr_vals = attr_vals;
node->n_attrs++;
}
int
moo_markup_get_int_prop (MooMarkupNode *node,
const char *prop_name,
int default_val)
{
g_return_val_if_fail (MOO_MARKUP_IS_ELEMENT (node), default_val);
g_return_val_if_fail (prop_name != NULL, default_val);
2006-08-15 01:21:44 -07:00
return _moo_convert_string_to_int (moo_markup_get_prop (node, prop_name),
default_val);
}
void
moo_markup_set_int_prop (MooMarkupNode *node,
const char *prop_name,
int val)
{
moo_markup_set_prop (node, prop_name,
2006-08-15 01:21:44 -07:00
_moo_convert_int_to_string (val));
}
gboolean
moo_markup_get_bool_prop (MooMarkupNode *node,
const char *prop_name,
gboolean default_val)
{
g_return_val_if_fail (MOO_MARKUP_IS_ELEMENT (node), default_val);
g_return_val_if_fail (prop_name != NULL, default_val);
2006-08-15 01:21:44 -07:00
return _moo_convert_string_to_bool (moo_markup_get_prop (node, prop_name),
default_val);
}
void
moo_markup_set_bool_prop (MooMarkupNode *node,
const char *prop_name,
gboolean val)
{
moo_markup_set_prop (node, prop_name,
2006-08-15 01:21:44 -07:00
_moo_convert_bool_to_string (val));
}
MooMarkupNode*
2005-09-08 10:08:31 -07:00
moo_markup_get_root_element (MooMarkupDoc *doc,
const char *name)
2005-06-22 11:20:32 -07:00
{
MooMarkupNode *child;
2005-06-22 11:20:32 -07:00
for (child = doc->children; child; child = child->next)
{
2005-08-02 14:59:34 -07:00
if (MOO_MARKUP_IS_ELEMENT (child))
{
if (!name || !strcmp (child->name, name))
return child;
2005-08-02 14:59:34 -07:00
}
2005-06-22 11:20:32 -07:00
}
2005-08-02 14:59:34 -07:00
2005-06-22 11:20:32 -07:00
return NULL;
}
2005-09-08 10:08:31 -07:00
char*
moo_markup_element_get_path (MooMarkupElement *elm)
{
GString *path;
MooMarkupNode *node;
2005-07-29 04:51:36 -07:00
g_return_val_if_fail (elm != NULL, NULL);
path = g_string_new (elm->name);
for (node = elm->parent;
node != NULL && MOO_MARKUP_IS_ELEMENT (node);
node = node->parent)
{
g_string_prepend (path, "/");
g_string_prepend (path, node->name);
}
return g_string_free (path, FALSE);
}
2005-09-08 10:08:31 -07:00
void
moo_markup_delete_node (MooMarkupNode *node)
{
MooMarkupNode *parent, *child, *next, *prev;
g_return_if_fail (node != NULL);
g_return_if_fail (node->parent != NULL);
set_modified (node, TRUE);
parent = node->parent;
for (child = parent->children; child != NULL && child != node;
child = child->next) ;
g_return_if_fail (child == node);
next = node->next;
prev = node->prev;
if (parent->children == node)
{
g_assert (node->prev == NULL);
parent->children = next;
}
if (parent->last == node)
{
g_assert (node->next == NULL);
parent->last = prev;
}
if (prev)
prev->next = next;
if (next)
next->prev = prev;
moo_markup_node_free (node);
}
MooMarkupNode*
2005-09-08 10:08:31 -07:00
moo_markup_create_root_element (MooMarkupDoc *doc,
const char *name)
{
MooMarkupNode *elm;
g_return_val_if_fail (MOO_MARKUP_IS_DOC (doc), NULL);
g_return_val_if_fail (name != NULL, NULL);
elm = moo_markup_get_root_element (doc, name);
if (elm)
return elm;
elm = moo_markup_element_new (doc, MOO_MARKUP_NODE (doc), name, NULL, NULL);
return elm;
}
static MooMarkupNode*
2005-09-04 21:24:21 -07:00
create_element (MooMarkupNode *node,
char **path)
{
2005-09-04 21:24:21 -07:00
g_return_val_if_fail (MOO_MARKUP_IS_ELEMENT (node) || MOO_MARKUP_IS_DOC (node), NULL);
g_return_val_if_fail (path && path[0], NULL);
2005-09-04 21:24:21 -07:00
if (path[1])
{
MooMarkupNode *child = NULL;
for (child = node->children; child != NULL; child = child->next)
2005-09-04 21:24:21 -07:00
if (MOO_MARKUP_IS_ELEMENT (child) && child->name && !strcmp (child->name, path[0]))
break;
if (!child)
child = moo_markup_element_new (node->doc, MOO_MARKUP_NODE (node),
path[0], NULL, NULL);
2005-09-04 21:24:21 -07:00
return create_element (child, ++path);
}
else
{
return moo_markup_element_new (node->doc, MOO_MARKUP_NODE (node), path[0], NULL, NULL);
}
}
MooMarkupNode*
2005-09-04 21:24:21 -07:00
moo_markup_create_element (MooMarkupNode *parent,
const char *path)
{
char **pieces;
MooMarkupNode *elm;
2005-09-04 21:24:21 -07:00
g_return_val_if_fail (MOO_MARKUP_IS_ELEMENT (parent) ||
MOO_MARKUP_IS_DOC (parent), NULL);
g_return_val_if_fail (path != NULL, NULL);
pieces = g_strsplit (path, "/", 0);
2005-09-04 21:24:21 -07:00
g_return_val_if_fail (pieces != NULL, NULL);
elm = create_element (parent, pieces);
g_strfreev (pieces);
2005-09-04 21:24:21 -07:00
return elm;
}
2007-04-07 00:33:34 -07:00
MooMarkupNode *
2005-09-04 21:24:21 -07:00
moo_markup_create_text_element (MooMarkupNode *parent,
const char *path,
const char *content)
{
MooMarkupElement *elm;
g_return_val_if_fail (MOO_MARKUP_IS_ELEMENT (parent) ||
2006-12-04 02:54:12 -08:00
MOO_MARKUP_IS_DOC (parent), NULL);
2005-09-04 21:24:21 -07:00
g_return_val_if_fail (path != NULL, NULL);
g_return_val_if_fail (content != NULL, NULL);
g_return_val_if_fail (g_utf8_validate (content, -1, NULL), NULL);
elm = (MooMarkupElement*) moo_markup_create_element (parent, path);
2005-09-04 21:24:21 -07:00
if (!elm)
return NULL;
2006-12-04 02:54:12 -08:00
moo_markup_text_node_new (MOO_MARKUP_TEXT_NODE,
elm->doc, MOO_MARKUP_NODE (elm),
content, strlen (content));
2005-09-04 21:24:21 -07:00
g_free (elm->content);
elm->content = g_strdup (content);
return MOO_MARKUP_NODE (elm);
}
2007-04-07 00:33:34 -07:00
void
moo_markup_set_content (MooMarkupNode *node,
const char *content)
{
MooMarkupElement *elm;
g_return_if_fail (MOO_MARKUP_IS_ELEMENT (node));
elm = MOO_MARKUP_ELEMENT (node);
g_free (elm->content);
elm->content = g_strdup (content);
while (node->children)
moo_markup_delete_node (node->children);
if (content)
moo_markup_text_node_new (MOO_MARKUP_TEXT_NODE,
elm->doc, node,
content, strlen (content));
}
#if 0
MooMarkupNode*
2005-09-08 10:08:31 -07:00
moo_markup_create_file_element (MooMarkupNode *parent,
const char *path,
const char *filename)
{
char *filename_utf8;
MooMarkupNode *elm;
2005-09-08 10:08:31 -07:00
g_return_val_if_fail (filename != NULL, NULL);
filename_utf8 = g_filename_display_name (filename);
if (!filename_utf8)
{
g_warning ("%s: could not convert '%s' to utf8",
G_STRLOC, filename);
return NULL;
}
elm = moo_markup_create_text_element (parent, path, filename_utf8);
g_free (filename_utf8);
return elm;
}
char *
moo_markup_get_file_content (MooMarkupNode *node)
2005-09-08 10:08:31 -07:00
{
g_return_val_if_fail (MOO_MARKUP_IS_ELEMENT (node), NULL);
2005-09-08 10:08:31 -07:00
if (!MOO_MARKUP_ELEMENT(node)->content)
2005-09-08 10:08:31 -07:00
return NULL;
return g_filename_from_utf8 (MOO_MARKUP_ELEMENT(node)->content,
-1, NULL, NULL, NULL);
2005-09-08 10:08:31 -07:00
}
gboolean
moo_markup_save (MooMarkupDoc *doc,
const char *filename,
GError **error)
{
char *text;
gboolean result;
g_return_val_if_fail (MOO_MARKUP_IS_DOC (doc), FALSE);
g_return_val_if_fail (filename != NULL, FALSE);
text = moo_markup_node_get_string (MOO_MARKUP_NODE (doc));
g_return_val_if_fail (text != NULL, FALSE);
result = _moo_save_file_utf8 (filename, text, -1, error);
g_free (text);
return result;
}
#endif
2005-09-07 04:19:26 -07:00
#define INDENT_CHAR ' '
#ifdef __WIN32__
#define LINE_SEPARATOR "\r\n"
#elif defined(MOO_OS_DARWIN)
2005-09-07 04:19:26 -07:00
#define LINE_SEPARATOR "\r"
#else
#define LINE_SEPARATOR "\n"
#endif
static void
format_pretty_element (MooMarkupElement *elm,
GString *str,
guint indent,
guint indent_size)
{
gboolean isdir = FALSE;
gboolean empty = TRUE;
MooMarkupNode *child;
char *fill;
guint i;
g_return_if_fail (MOO_MARKUP_IS_ELEMENT (elm));
g_return_if_fail (str != NULL);
for (child = elm->children; child != NULL; child = child->next)
{
if (elm->content)
empty = FALSE;
if (MOO_MARKUP_IS_ELEMENT (child))
{
isdir = TRUE;
empty = FALSE;
break;
}
}
fill = g_strnfill (indent, INDENT_CHAR);
g_string_append_len (str, fill, indent);
g_string_append_printf (str, "<%s", elm->name);
for (i = 0; i < elm->n_attrs; ++i)
g_string_append_printf (str, " %s=\"%s\"",
elm->attr_names[i],
elm->attr_vals[i]);
if (empty)
{
g_string_append (str, "/>" LINE_SEPARATOR);
}
else if (isdir)
{
g_string_append (str, ">" LINE_SEPARATOR);
for (child = elm->children; child != NULL; child = child->next)
if (MOO_MARKUP_IS_ELEMENT (child))
format_pretty_element (MOO_MARKUP_ELEMENT (child), str,
indent + indent_size, indent_size);
g_string_append_printf (str, "%s</%s>" LINE_SEPARATOR,
fill, elm->name);
}
else
{
char *escaped = g_markup_escape_text (elm->content, -1);
g_string_append_printf (str, ">%s</%s>" LINE_SEPARATOR,
escaped, elm->name);
g_free (escaped);
}
g_free (fill);
}
#undef INDENT_CHAR
#undef LINE_SEPARATOR
gboolean
moo_markup_save_pretty (MooMarkupDoc *doc,
const char *filename,
guint indent,
GError **error)
{
GString *str;
MooMarkupNode *child;
gboolean result;
g_return_val_if_fail (MOO_MARKUP_IS_DOC (doc), FALSE);
g_return_val_if_fail (filename != NULL, FALSE);
str = g_string_new ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
for (child = doc->children; child != NULL; child = child->next)
if (MOO_MARKUP_IS_ELEMENT (child))
format_pretty_element (MOO_MARKUP_ELEMENT (child), str, 0, indent);
result = _moo_save_file_utf8 (filename, str->str, -1, error);
2005-09-07 04:19:26 -07:00
g_string_free (str, TRUE);
return result;
}
#if 0
2006-06-01 00:08:56 -07:00
char *
moo_markup_node_get_pretty_string (MooMarkupNode *node,
guint indent)
{
GString *str;
MooMarkupNode *child;
g_return_val_if_fail (node != NULL, NULL);
str = g_string_new (NULL);
for (child = node->children; child != NULL; child = child->next)
if (MOO_MARKUP_IS_ELEMENT (child))
format_pretty_element (MOO_MARKUP_ELEMENT (child), str, 0, indent);
return g_string_free (str, FALSE);
}
#endif
2006-06-01 00:08:56 -07:00
MooMarkupNode *
MOO_MARKUP_NODE_CHECK_CAST (gpointer n)
2005-06-22 11:20:32 -07:00
{
MooMarkupNode *node = (MooMarkupNode*) n;
g_return_val_if_fail (n != NULL, NULL);
g_return_val_if_fail (node->type == MOO_MARKUP_DOC_NODE ||
node->type == MOO_MARKUP_ELEMENT_NODE ||
node->type == MOO_MARKUP_TEXT_NODE ||
node->type == MOO_MARKUP_COMMENT_NODE,
NULL);
return node;
}
MooMarkupDoc *
MOO_MARKUP_DOC_CHECK_CAST (gpointer n)
2005-06-22 11:20:32 -07:00
{
MooMarkupNode *node = (MooMarkupNode*) n;
g_return_val_if_fail (n != NULL, NULL);
g_return_val_if_fail (node->type == MOO_MARKUP_DOC_NODE, NULL);
return (MooMarkupDoc*) n;
}
MooMarkupElement *
MOO_MARKUP_ELEMENT_CHECK_CAST (gpointer n)
2005-06-22 11:20:32 -07:00
{
MooMarkupNode *node = (MooMarkupNode*) n;
g_return_val_if_fail (n != NULL, NULL);
g_return_val_if_fail (node->type == MOO_MARKUP_ELEMENT_NODE, NULL);
return (MooMarkupElement*) n;
}
MooMarkupText *
MOO_MARKUP_TEXT_CHECK_CAST (gpointer n)
2005-06-22 11:20:32 -07:00
{
MooMarkupNode *node = (MooMarkupNode*) n;
g_return_val_if_fail (n != NULL, NULL);
g_return_val_if_fail (node->type == MOO_MARKUP_TEXT_NODE, NULL);
return (MooMarkupText*) n;
}
MooMarkupComment *
MOO_MARKUP_COMMENT_CHECK_CAST (gpointer n)
2005-06-22 11:20:32 -07:00
{
MooMarkupNode *node = (MooMarkupNode*) n;
g_return_val_if_fail (n != NULL, NULL);
g_return_val_if_fail (node->type == MOO_MARKUP_COMMENT_NODE, NULL);
return (MooMarkupComment*) n;
}