1 void config_setting_copy_simple(config_setting_t * parent, const config_setting_t * src);
2 void config_setting_copy_elem(config_setting_t * parent, const config_setting_t * src);
3
4 void config_setting_copy_aggregate(config_setting_t * parent, const config_setting_t * src);
5 int config_setting_copy(config_setting_t * parent, const config_setting_t * src);
6
config_setting_copy_simple(config_setting_t * parent,const config_setting_t * src)7 void config_setting_copy_simple(config_setting_t * parent, const config_setting_t * src)
8 {
9 if(config_setting_is_aggregate(src))
10 {
11 config_setting_copy_aggregate(parent, src);
12 }
13 else
14 {
15 config_setting_t * set;
16
17 set = config_setting_add(parent, config_setting_name(src), config_setting_type(src));
18
19 if(CONFIG_TYPE_INT == config_setting_type(src))
20 {
21 config_setting_set_int(set, config_setting_get_int(src));
22 config_setting_set_format(set, src->format);
23 }
24 else if(CONFIG_TYPE_INT64 == config_setting_type(src))
25 {
26 config_setting_set_int64(set, config_setting_get_int64(src));
27 config_setting_set_format(set, src->format);
28 }
29 else if(CONFIG_TYPE_FLOAT == config_setting_type(src))
30 config_setting_set_float(set, config_setting_get_float(src));
31 else if(CONFIG_TYPE_STRING == config_setting_type(src))
32 config_setting_set_string(set, config_setting_get_string(src));
33 else if(CONFIG_TYPE_BOOL == config_setting_type(src))
34 config_setting_set_bool(set, config_setting_get_bool(src));
35 }
36 }
37
config_setting_copy_elem(config_setting_t * parent,const config_setting_t * src)38 void config_setting_copy_elem(config_setting_t * parent, const config_setting_t * src)
39 {
40 config_setting_t * set;
41
42 set = NULL;
43 if(config_setting_is_aggregate(src))
44 config_setting_copy_aggregate(parent, src);
45 else if(CONFIG_TYPE_INT == config_setting_type(src))
46 {
47 set = config_setting_set_int_elem(parent, -1, config_setting_get_int(src));
48 config_setting_set_format(set, src->format);
49 }
50 else if(CONFIG_TYPE_INT64 == config_setting_type(src))
51 {
52 set = config_setting_set_int64_elem(parent, -1, config_setting_get_int64(src));
53 config_setting_set_format(set, src->format);
54 }
55 else if(CONFIG_TYPE_FLOAT == config_setting_type(src))
56 set = config_setting_set_float_elem(parent, -1, config_setting_get_float(src));
57 else if(CONFIG_TYPE_STRING == config_setting_type(src))
58 set = config_setting_set_string_elem(parent, -1, config_setting_get_string(src));
59 else if(CONFIG_TYPE_BOOL == config_setting_type(src))
60 set = config_setting_set_bool_elem(parent, -1, config_setting_get_bool(src));
61 }
62
config_setting_copy_aggregate(config_setting_t * parent,const config_setting_t * src)63 void config_setting_copy_aggregate(config_setting_t * parent, const config_setting_t * src)
64 {
65 config_setting_t * newAgg;
66 int i, n;
67
68 newAgg = config_setting_add(parent, config_setting_name(src), config_setting_type(src));
69
70 n = config_setting_length(src);
71 for(i = 0; i < n; i++)
72 {
73 if(config_setting_is_group(src))
74 {
75 config_setting_copy_simple(newAgg, config_setting_get_elem(src, i));
76 }
77 else
78 {
79 config_setting_copy_elem(newAgg, config_setting_get_elem(src, i));
80 }
81 }
82 }
83
config_setting_copy(config_setting_t * parent,const config_setting_t * src)84 int config_setting_copy(config_setting_t * parent, const config_setting_t * src)
85 {
86 if((!config_setting_is_group(parent)) &&
87 (!config_setting_is_list(parent)))
88 return CONFIG_FALSE;
89
90 if(config_setting_is_aggregate(src))
91 {
92 config_setting_copy_aggregate(parent, src);
93 }
94 else
95 {
96 config_setting_copy_simple(parent, src);
97 }
98
99 return CONFIG_TRUE;
100 }
101
102
103 //Some sample code
104
105 //-----------------------------------------------------------------------------
main(int argc,char * argv[])106 int main(int argc, char *argv[])
107 //-----------------------------------------------------------------------------
108 {
109 config_t cfgSrc;
110 config_t cfgSrcCopy;
111 config_t cfgDst;
112
113
114 config_init(&cfgSrc);
115 config_init(&cfgSrcCopy);
116 config_init(&cfgDst);
117
118 if(CONFIG_FALSE == config_read_file(&cfgSrc, "/data/menu/cfgSrc.cfg"))
119 {
120 fprintf(stderr, "Failed to open cfgSrc.cfg\n");
121 }
122 if(CONFIG_FALSE == config_read_file(&cfgDst, "/data/menu/cfgDst.cfg"))
123 {
124 fprintf(stderr, "Failed to open cfgDst.cfg\n");
125 }
126
127 /*
128 printf("Dump cfgSrc.cfg\n");
129 DumpCfgSetting(config_root_setting(&cfgSrc));
130
131 printf("Dump cfgDst.cfg\n");
132 DumpCfgSetting(config_root_setting(&cfgDst));
133 */
134
135 config_setting_t * src;
136 config_setting_t * dst;
137
138 dst = config_lookup(&cfgDst, "grp1");
139
140 if((0 != (dst = config_lookup(&cfgDst, "grp1"))) &&
141 (0 != (src = config_lookup(&cfgSrc, "application.window"))))
142 //(0 != (src = config_lookup(&cfgSrc, "list"))))
143
144 {
145 if(CONFIG_FALSE == config_setting_copy(dst, src))
146 {
147 printf("Failed to copy src to dst\n");
148 }
149 }
150
151 config_setting_copy(config_root_setting(&cfgSrcCopy), config_root_setting(&cfgSrc));
152
153 config_write_file(&cfgDst, "/data/menu/cfgDstMod.cfg");
154 config_write_file(&cfgSrcCopy, "/data/menu/cfgSrcCpy.cfg");
155 config_write_file(&cfgSrc, "/data/menu/cfgSrcOrig.cfg");
156
157 config_destroy(&cfgSrc);
158 config_destroy(&cfgDst);
159
160 return 0;
161 }
162