root/trunk/src/data_config.c

Revision 1837, 2.9 kB (checked in by jan, 17 months ago)

removed the covering 'config { ... }' + indention to make the printed
configfile parsable

  • Property svn:eol-style set to native
Line 
1#include <string.h>
2#include <stdio.h>
3#include <stdlib.h>
4
5#include "array.h"
6
7static data_unset *data_config_copy(const data_unset *s) {
8        data_config *src = (data_config *)s;
9        data_config *ds = data_config_init();
10
11        buffer_copy_string_buffer(ds->key, src->key);
12        buffer_copy_string_buffer(ds->comp_key, src->comp_key);
13        array_free(ds->value);
14        ds->value = array_init_array(src->value);
15        return (data_unset *)ds;
16}
17
18static void data_config_free(data_unset *d) {
19        data_config *ds = (data_config *)d;
20
21        buffer_free(ds->key);
22        buffer_free(ds->op);
23        buffer_free(ds->comp_key);
24
25        array_free(ds->value);
26        array_free(ds->childs);
27
28        if (ds->string) buffer_free(ds->string);
29#ifdef HAVE_PCRE_H
30        if (ds->regex) pcre_free(ds->regex);
31        if (ds->regex_study) pcre_free(ds->regex_study);
32#endif
33
34        free(d);
35}
36
37static void data_config_reset(data_unset *d) {
38        data_config *ds = (data_config *)d;
39
40        /* reused array elements */
41        buffer_reset(ds->key);
42        buffer_reset(ds->comp_key);
43        array_reset(ds->value);
44}
45
46static int data_config_insert_dup(data_unset *dst, data_unset *src) {
47        UNUSED(dst);
48
49        src->free(src);
50
51        return 0;
52}
53
54static void data_config_print(const data_unset *d, int depth) {
55        data_config *ds = (data_config *)d;
56        array *a = (array *)ds->value;
57        size_t i;
58        size_t maxlen;
59
60        if (0 != ds->context_ndx) {
61                fprintf(stdout, "$%s %s \"%s\" {\n",
62                                ds->comp_key->ptr, ds->op->ptr, ds->string->ptr);
63                array_print_indent(depth + 1);
64                fprintf(stdout, "# block %d\n", ds->context_ndx);
65                depth ++;
66        }
67
68        maxlen = array_get_max_key_length(a);
69        for (i = 0; i < a->used; i ++) {
70                data_unset *du = a->data[i];
71                size_t len = strlen(du->key->ptr);
72                size_t j;
73
74                array_print_indent(depth);
75                fprintf(stdout, "%s", du->key->ptr);
76                for (j = maxlen - len; j > 0; j --) {
77                        fprintf(stdout, " ");
78                }
79                fprintf(stdout, " = ");
80                du->print(du, depth);
81                fprintf(stdout, "\n");
82        }
83
84        if (ds->childs) {
85                fprintf(stdout, "\n");
86                for (i = 0; i < ds->childs->used; i ++) {
87                        data_unset *du = ds->childs->data[i];
88
89                        /* only the 1st block of chaining */
90                        if (NULL == ((data_config *)du)->prev) {
91                                fprintf(stdout, "\n");
92                                array_print_indent(depth);
93                                du->print(du, depth);
94                                fprintf(stdout, "\n");
95                        }
96                }
97        }
98
99        if (0 != ds->context_ndx) {
100                depth --;
101                array_print_indent(depth);
102                fprintf(stdout, "}");
103
104                fprintf(stdout, " # end of $%s %s \"%s\"",
105                                ds->comp_key->ptr, ds->op->ptr, ds->string->ptr);
106        }
107
108        if (ds->next) {
109                fprintf(stdout, "\n");
110                array_print_indent(depth);
111                fprintf(stdout, "else ");
112                ds->next->print((data_unset *)ds->next, depth);
113        }
114}
115
116data_config *data_config_init(void) {
117        data_config *ds;
118
119        ds = calloc(1, sizeof(*ds));
120
121        ds->key = buffer_init();
122        ds->op = buffer_init();
123        ds->comp_key = buffer_init();
124        ds->value = array_init();
125        ds->childs = array_init();
126        ds->childs->is_weakref = 1;
127
128        ds->copy = data_config_copy;
129        ds->free = data_config_free;
130        ds->reset = data_config_reset;
131        ds->insert_dup = data_config_insert_dup;
132        ds->print = data_config_print;
133        ds->type = TYPE_CONFIG;
134
135        return ds;
136}
Note: See TracBrowser for help on using the browser.