summaryrefslogtreecommitdiffstats
path: root/bindings/perl/glist_handling.c
blob: 01deb2746941832a1bb2fd75c0e2ac748908f26b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * Lasso - A free implementation of the Liberty Alliance specifications.
 *
 * Copyright (C) 2004-2007 Entr'ouvert
 * http://lasso.entrouvert.org
 *
 * Authors: See AUTHORS file in top-level directory.
 *
 * 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.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <perl.h>
#include <glib.h>
#include <glib-object.h>
#include <lasso/xml/xml.h>
#include <lasso/utils.h>
#include "../utils.c"

static xmlBuffer*
xmlnode_to_xmlbuffer(xmlNode *node)
{
	xmlOutputBufferPtr output_buffer;
	xmlBuffer *buffer;

	if (! node)
		return NULL;

	buffer = xmlBufferCreate();
	output_buffer = xmlOutputBufferCreateBuffer(buffer, NULL);
	xmlNodeDumpOutput(output_buffer, NULL, node, 0, 0, NULL);
	xmlOutputBufferClose(output_buffer);
	xmlBufferAdd(buffer, BAD_CAST "", 1);

	return buffer;
}


/**
 * xmlnode_to_pv:
 * @node: an xmlNode* object
 * @do_free: do we need to free the node after the conversion
 *
 * Return value: a newly allocated SV/PV or under.
 */
static SV*
xmlnode_to_pv(xmlNode *node, gboolean do_free)
{
	xmlBuffer *buf;
	SV *pestring = NULL;

	if (node == NULL) {
		return &PL_sv_undef;
	}

	buf = xmlnode_to_xmlbuffer(node);
	if (buf == NULL) {
		pestring = &PL_sv_undef;
	} else {
		pestring = newSVpv((char*)xmlBufferContent(buf), 0);
	}
	if (do_free) {
		lasso_release_xml_node(node);
	}

	return pestring;
}

static xmlNode *
pv_to_xmlnode(SV *value) {
	STRLEN size;
	char *string;

	if (! SvPOK(value))
		return NULL;
	string = SvPV(value, size);
	if (! string)
		return NULL;

	return lasso_string_fragment_to_xmlnode(string, size);
}

/**
 * array_to_glist_string:
 * @array: a Perl array
 *
 * Convert a perl array to a #GList of strings.
 *
 * Return value: a newly create #GList
 */
G_GNUC_UNUSED static GList*
array_to_glist_string(AV *array)
{
	I32 len, i;
	GList *result = NULL;

	if (! array)
		return NULL;
	len = av_len(array);
	for (i=len-1; i >= 0; i--) {
		SV **sv;

		sv = av_fetch(array, i, 0);
		lasso_list_add_string(result, SvPV_nolen(*sv));
	}

	return result;
}

/**
 * array_to_glist_gobject:
 * @array: a perl array
 *
 * Convert a perl array of #GObject to a #GList of #GObject objects
 *
 * Return value: a newly created #GList of #GObject objects
 */
G_GNUC_UNUSED static GList*
array_to_glist_gobject(AV *array) {
       I32 len, i;
       GList *result = NULL;

       if (! array)
               return NULL;
       len = av_len(array);
       for (i=len-1; i >= 0; i--) {
               SV **sv;

               sv = av_fetch(array, i, 0);
               lasso_list_add_gobject(result, gperl_get_object(*sv));
       }

       return result;
}