summaryrefslogtreecommitdiffstats
path: root/bindings/perl/ghashtable_handling.c
blob: be7b11e3404889880729ea4dedcce379dd95e0f6 (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
/*
 * 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>

/**
 * set_hash_of_strings:
 * @hash: a #GHashTable variable
 * @hv: a perl hash
 */
void
set_hash_of_strings(GHashTable **hash, HV *hv)
{
	SV *data;
	char *key;
	I32 len;

	g_hash_table_remove_all(*hash);
	hv_iterinit(hv);
	while ((data = hv_iternextsv(hv, &key, &len))) {
		if (SvTYPE(data) != SVt_PV) {
			croak("hash contains non-strings values");
		}
	}
	hv_iterinit(hv);
	while ((data = hv_iternextsv(hv, &key, &len))) {
		g_hash_table_insert(*hash, g_strndup(key, len), g_strdup(SvPV_nolen(data)));
	}
}

/**
 * set_hash_of_objects:
 * @hash: a #GHashTable variable
 * @hv: a perl hash
 */
void
set_hash_of_objects(GHashTable **hash, HV *hv)
{
	SV *data;
	char *key;
	I32 len;

	g_hash_table_remove_all(*hash);
	hv_iterinit(hv);
	while ((data = hv_iternextsv(hv, &key, &len))) {
		if (! gperl_get_object(data)) {
			croak("hash contains non-strings values");
		}
	}
	hv_iterinit(hv);
	while ((data = hv_iternextsv(hv, &key, &len))) {
		g_hash_table_insert(*hash, g_strndup(key, len), g_object_ref(data));
	}
}

static void
__ht_foreach_get_hos(gpointer key, gpointer value, gpointer user_data)
{
	HV *hv = user_data;

	(void)hv_store(hv, key, strlen(key), newSVpv(value, 0), 0);
}


/**
 * get_hash_of_strings:
 * @hash: a #GHashTable of strings
 */
HV*
get_hash_of_strings(GHashTable *hash)
{
	HV *hv;

	hv = newHV();
	g_hash_table_foreach(hash, __ht_foreach_get_hos, hv);
	return hv;
}

static void
__ht_foreach_get_hoo(gpointer key, gpointer value, gpointer user_data)
{
	HV *hv = user_data;

	(void)hv_store(hv, key, strlen(key), gperl_new_object(value, FALSE), 0);
}

/**
 * get_hash_of_objects:
 * @hash: a #GHashTable of objects
 */
HV*
get_hash_of_objects(GHashTable *hash)
{
	HV *hv;

	hv = newHV();
	g_hash_table_foreach(hash, __ht_foreach_get_hoo, hv);
	return hv;
}