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