#include <xml/savenode.h>
#include <xml/listnode.h>
#include <xml/attrnode.h>
#include <string.h>

#define ___ {
#define ____ }

/* ----------- simple versions - just copy the text as the attribute value */
void
xml_g_node_move_attribute_to_text (xml_GNode* node, const gchar* attrib)
{
    if (!attrib || !node || !node->text || node->off == node->end) return;
    
    ___ gchar* str = xml_node_attribute_lookup (node, attrib);
    if (! str) return;
    ___ int len = strlen (str);
    if (len > node->end - node->off) len = node->end - node->off;

    memcpy (node->text->str + node->off, str, len);

    xml_node_attribute_remove (node, attrib);
    ____;____;
}
void
xml_g_node_move_text_to_attribute (xml_GNode* node, const gchar* attrib)
{
    if (!attrib || !node || !node->text || node->off == node->end) return;
    
    xml_node_attribute_insert (node, 
				 g_strdup (attrib),
				 g_strndup (node->text->str + node->off,
					    node->end - node->off));

    memset (node->text->str + node->off, ' ', node->end - node->off);
}
/**
    save the text in the selected nodes to the new attribute - and blank
    the text area. The text can be pasted back from the attribute with its
 */
void
xml_path_node_text_to_attr (xml_GNode* tree, 
			    const gchar* nodesXE, const gchar* attrib)
{
    g_return_if_fail (tree);
    g_return_if_fail (attrib);
    xml_path_node_foreach (tree, nodesXE, 
		 (xml_GNodeForeachFunc) xml_g_node_move_text_to_attribute, 
		 (gpointer) attrib);
}
/**
    get the attribute from the selected nodes and paste the string back to
    the text area - as this is the inverse of xml_path_node_text_to_attr.
 */
void
xml_path_node_text_restore (xml_GNode* tree,
			    const gchar* nodesXE, const gchar* attrib)
{
    g_return_if_fail (tree);
    g_return_if_fail (attrib);
    xml_path_node_foreach (tree, nodesXE, 
		 (xml_GNodeForeachFunc) xml_g_node_move_attribute_to_text, 
		 (gpointer) attrib);
}
/* 
   Local variables:
   c-file-style: "stroustrup"
   End:
 */