9 static data_unset
*data_config_copy(const data_unset
*s
) {
10 data_config
*src
= (data_config
*)s
;
11 data_config
*ds
= data_config_init();
13 buffer_copy_buffer(ds
->key
, src
->key
);
14 buffer_copy_buffer(ds
->comp_key
, src
->comp_key
);
15 array_free(ds
->value
);
16 ds
->value
= array_init_array(src
->value
);
17 return (data_unset
*)ds
;
20 static void data_config_free(data_unset
*d
) {
21 data_config
*ds
= (data_config
*)d
;
25 buffer_free(ds
->comp_key
);
27 array_free(ds
->value
);
28 vector_config_weak_clear(&ds
->children
);
30 if (ds
->string
) buffer_free(ds
->string
);
32 if (ds
->regex
) pcre_free(ds
->regex
);
33 if (ds
->regex_study
) pcre_free(ds
->regex_study
);
39 static void data_config_reset(data_unset
*d
) {
40 data_config
*ds
= (data_config
*)d
;
42 /* reused array elements */
43 buffer_reset(ds
->key
);
44 buffer_reset(ds
->comp_key
);
45 array_reset(ds
->value
);
48 static int data_config_insert_dup(data_unset
*dst
, data_unset
*src
) {
56 static void data_config_print(const data_unset
*d
, int depth
) {
57 data_config
*ds
= (data_config
*)d
;
58 array
*a
= (array
*)ds
->value
;
62 if (0 == ds
->context_ndx
) {
63 fprintf(stdout
, "config {\n");
66 fprintf(stdout
, "$%s %s \"%s\" {\n",
67 ds
->comp_key
->ptr
, ds
->op
->ptr
, ds
->string
->ptr
);
68 array_print_indent(depth
+ 1);
69 fprintf(stdout
, "# block %d\n", ds
->context_ndx
);
73 maxlen
= array_get_max_key_length(a
);
74 for (i
= 0; i
< a
->used
; i
++) {
75 data_unset
*du
= a
->data
[i
];
76 size_t len
= strlen(du
->key
->ptr
);
79 array_print_indent(depth
);
80 fprintf(stdout
, "%s", du
->key
->ptr
);
81 for (j
= maxlen
- len
; j
> 0; j
--) {
84 fprintf(stdout
, " = ");
86 fprintf(stdout
, "\n");
89 fprintf(stdout
, "\n");
90 for (i
= 0; i
< ds
->children
.used
; i
++) {
91 data_config
*dc
= ds
->children
.data
[i
];
93 /* only the 1st block of chaining */
94 if (NULL
== dc
->prev
) {
95 fprintf(stdout
, "\n");
96 array_print_indent(depth
);
97 dc
->print((data_unset
*) dc
, depth
);
98 fprintf(stdout
, "\n");
103 array_print_indent(depth
);
104 fprintf(stdout
, "}");
105 if (0 != ds
->context_ndx
) {
106 fprintf(stdout
, " # end of $%s %s \"%s\"",
107 ds
->comp_key
->ptr
, ds
->op
->ptr
, ds
->string
->ptr
);
111 fprintf(stdout
, "\n");
112 array_print_indent(depth
);
113 fprintf(stdout
, "else ");
114 ds
->next
->print((data_unset
*)ds
->next
, depth
);
118 data_config
*data_config_init(void) {
121 ds
= calloc(1, sizeof(*ds
));
123 ds
->key
= buffer_init();
124 ds
->op
= buffer_init();
125 ds
->comp_key
= buffer_init();
126 ds
->value
= array_init();
127 vector_config_weak_init(&ds
->children
);
129 ds
->copy
= data_config_copy
;
130 ds
->free
= data_config_free
;
131 ds
->reset
= data_config_reset
;
132 ds
->insert_dup
= data_config_insert_dup
;
133 ds
->print
= data_config_print
;
134 ds
->type
= TYPE_CONFIG
;