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 if (ds
->cond
!= CONFIG_COND_ELSE
) {
67 fprintf(stdout
, "$%s %s \"%s\" {\n",
68 ds
->comp_key
->ptr
, ds
->op
->ptr
, ds
->string
->ptr
);
70 fprintf(stdout
, "{\n");
72 array_print_indent(depth
+ 1);
73 fprintf(stdout
, "# block %d\n", ds
->context_ndx
);
77 maxlen
= array_get_max_key_length(a
);
78 for (i
= 0; i
< a
->used
; i
++) {
79 data_unset
*du
= a
->data
[i
];
80 size_t len
= strlen(du
->key
->ptr
);
83 array_print_indent(depth
);
84 fprintf(stdout
, "%s", du
->key
->ptr
);
85 for (j
= maxlen
- len
; j
> 0; j
--) {
88 fprintf(stdout
, " = ");
90 fprintf(stdout
, "\n");
93 fprintf(stdout
, "\n");
94 for (i
= 0; i
< ds
->children
.used
; i
++) {
95 data_config
*dc
= ds
->children
.data
[i
];
97 /* only the 1st block of chaining */
98 if (NULL
== dc
->prev
) {
99 fprintf(stdout
, "\n");
100 array_print_indent(depth
);
101 dc
->print((data_unset
*) dc
, depth
);
102 fprintf(stdout
, "\n");
107 array_print_indent(depth
);
108 fprintf(stdout
, "}");
109 if (0 != ds
->context_ndx
) {
110 if (ds
->cond
!= CONFIG_COND_ELSE
) {
111 fprintf(stdout
, " # end of $%s %s \"%s\"",
112 ds
->comp_key
->ptr
, ds
->op
->ptr
, ds
->string
->ptr
);
114 fprintf(stdout
, " # end of else");
119 fprintf(stdout
, "\n");
120 array_print_indent(depth
);
121 fprintf(stdout
, "else ");
122 ds
->next
->print((data_unset
*)ds
->next
, depth
);
126 data_config
*data_config_init(void) {
129 ds
= calloc(1, sizeof(*ds
));
131 ds
->key
= buffer_init();
132 ds
->op
= buffer_init();
133 ds
->comp_key
= buffer_init();
134 ds
->value
= array_init();
135 vector_config_weak_init(&ds
->children
);
137 ds
->copy
= data_config_copy
;
138 ds
->free
= data_config_free
;
139 ds
->reset
= data_config_reset
;
140 ds
->insert_dup
= data_config_insert_dup
;
141 ds
->print
= data_config_print
;
142 ds
->type
= TYPE_CONFIG
;