Imported Upstream version 3.2.0
[debian/amanda] / perl / amglue / ghashtable.c
1 /*
2  * Copyright (c) 2007,2008,2009 Zmanda, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published
6  * by the Free Software Foundation.
7  *
8  * This program 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 General Public License
11  * for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16  *
17  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19  */
20
21 #include "amglue.h"
22 #include "conffile.h"
23
24 /* PERL_MAGIC_tied is not defined in perl 5.6 */
25 #if !defined PERL_MAGIC_tied
26 #define PERL_MAGIC_tied 'P'
27 #endif
28
29 static void 
30 foreach_fn(gpointer key_p, gpointer value_p, gpointer user_data_p)
31 {
32     char *key = key_p;
33     char *value = value_p;
34     HV *hv = user_data_p;
35     hv_store(hv, key, strlen(key), newSVpv(value, 0), 0);
36 }
37
38 SV *
39 g_hash_table_to_hashref(GHashTable *hash)
40 {
41     HV *hv = (HV *)sv_2mortal((SV *)newHV());
42
43     g_hash_table_foreach(hash, foreach_fn, hv);
44
45     return newRV((SV *)hv);
46 }
47
48 static void 
49 foreach_fn_gslist(gpointer key_p, gpointer value_p, gpointer user_data_p)
50 {
51     char   *key = key_p;
52     GSList *value_s = value_p;
53     GSList *value;
54     HV     *hv = user_data_p;
55     AV *list = newAV();
56
57     for(value=value_s; value != NULL; value = value->next) {
58         av_push(list, newSVpv(value->data, 0));
59     }
60
61     hv_store(hv, key, strlen(key), newRV_noinc((SV*)list), 0);
62 }
63
64 SV *
65 g_hash_table_to_hashref_gslist(GHashTable *hash)
66 {
67     HV *hv = (HV *)sv_2mortal((SV *)newHV());
68
69     g_hash_table_foreach(hash, foreach_fn_gslist, hv);
70
71     return newRV((SV *)hv);
72 }
73
74 static void 
75 foreach_fn_property(gpointer key_p, gpointer value_p, gpointer user_data_p)
76 {
77     char       *key = key_p;
78     property_t *property = value_p;
79     GSList     *value;
80     HV         *hv = user_data_p;
81     AV         *list = newAV();
82     HV         *property_hv = newHV();
83     SV         *val;
84
85     hv_store(property_hv, "append", strlen("append"), newSViv(property->append), 0);
86     hv_store(property_hv, "priority", strlen("priority"), newSViv(property->priority), 0);
87     for(value=property->values; value != NULL; value = value->next) {
88         av_push(list, newSVpv(value->data, 0));
89     }
90     hv_store(property_hv, "values", strlen("values"), newRV_noinc((SV*)list), 0);
91
92     val = newRV_noinc((SV*)property_hv);
93     hv_store(hv, key, strlen(key), val, 0);
94     mg_set(val);
95     SvREFCNT_dec(val);
96 }
97
98 SV *
99 g_hash_table_to_hashref_property(GHashTable *hash)
100 {
101     HV *hv;
102     HV *stash;
103     SV *tie;
104
105     hv = newHV();
106     tie = newRV_noinc((SV*)newHV());
107     stash = gv_stashpv("Amanda::Config::FoldingHash", GV_ADD);
108     sv_bless(tie, stash);
109     hv_magic(hv, (GV*)tie, PERL_MAGIC_tied);
110
111     hv = (HV *)sv_2mortal((SV *)hv);
112     g_hash_table_foreach(hash, foreach_fn_property, hv);
113
114     return newRV((SV *)hv);
115 }
116