09dbfb79b292fdb244ba372aab255e75819a3fdd
[debian/amanda] / perl / amglue / ghashtable.c
1 /*
2  * Copyright (c) 2005-2008 Zmanda Inc.  All Rights Reserved.
3  *
4  * This library is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 2.1 as
6  * published by the Free Software Foundation.
7  *
8  * This library is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
11  * License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation,
15  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
16  *
17  * Contact information: Zmanda Inc., 465 S Mathlida Ave, Suite 300
18  * Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
19  */
20
21 #include "amglue.h"
22 #include "conffile.h"
23
24 static void 
25 foreach_fn(gpointer key_p, gpointer value_p, gpointer user_data_p)
26 {
27     char *key = key_p;
28     char *value = value_p;
29     HV *hv = user_data_p;
30     hv_store(hv, key, strlen(key), newSVpv(value, 0), 0);
31 }
32
33 SV *
34 g_hash_table_to_hashref(GHashTable *hash)
35 {
36     HV *hv = (HV *)sv_2mortal((SV *)newHV());
37
38     g_hash_table_foreach(hash, foreach_fn, hv);
39
40     return newRV((SV *)hv);
41 }
42
43 static void 
44 foreach_fn_gslist(gpointer key_p, gpointer value_p, gpointer user_data_p)
45 {
46     char   *key = key_p;
47     GSList *value_s = value_p;
48     GSList *value;
49     HV     *hv = user_data_p;
50     AV *list = newAV();
51
52     for(value=value_s; value != NULL; value = value->next) {
53         av_push(list, newSVpv(value->data, 0));
54     }
55
56     hv_store(hv, key, strlen(key), newRV_noinc((SV*)list), 0);
57 }
58
59 SV *
60 g_hash_table_to_hashref_gslist(GHashTable *hash)
61 {
62     HV *hv = (HV *)sv_2mortal((SV *)newHV());
63
64     g_hash_table_foreach(hash, foreach_fn_gslist, hv);
65
66     return newRV((SV *)hv);
67 }
68
69 static void 
70 foreach_fn_property(gpointer key_p, gpointer value_p, gpointer user_data_p)
71 {
72     char       *key = key_p;
73     property_t *property = value_p;
74     GSList     *value;
75     HV         *hv = user_data_p;
76     AV         *list = newAV();
77     HV         *property_hv = newHV();
78
79     hv_store(property_hv, "append", strlen("append"), newSViv(property->append), 0);
80     hv_store(property_hv, "priority", strlen("priority"), newSViv(property->priority), 0);
81     for(value=property->values; value != NULL; value = value->next) {
82         av_push(list, newSVpv(value->data, 0));
83     }
84     hv_store(property_hv, "values", strlen("values"), newRV_noinc((SV*)list), 0);
85
86     hv_store(hv, key, strlen(key), newRV_noinc((SV*)property_hv), 0);
87 }
88
89 SV *
90 g_hash_table_to_hashref_property(GHashTable *hash)
91 {
92     HV *hv = (HV *)sv_2mortal((SV *)newHV());
93
94     g_hash_table_foreach(hash, foreach_fn_property, hv);
95
96     return newRV((SV *)hv);
97 }
98