[mod_accesslog] %{ratio}n logs compression ratio (fixes #2133)
[lighttpd.git] / src / data_config.c
blob30c32bd20cc46643fcdcfa98876fd8e68a7d36f5
1 #include "first.h"
3 #include "array.h"
5 #include <string.h>
6 #include <stdio.h>
7 #include <stdlib.h>
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;
23 buffer_free(ds->key);
24 buffer_free(ds->op);
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);
31 #ifdef HAVE_PCRE_H
32 if (ds->regex) pcre_free(ds->regex);
33 if (ds->regex_study) pcre_free(ds->regex_study);
34 #endif
36 free(d);
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) {
49 UNUSED(dst);
51 src->free(src);
53 return 0;
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;
59 size_t i;
60 size_t maxlen;
62 if (0 == ds->context_ndx) {
63 fprintf(stdout, "config {\n");
65 else {
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);
69 } else {
70 fprintf(stdout, "{\n");
72 array_print_indent(depth + 1);
73 fprintf(stdout, "# block %d\n", ds->context_ndx);
75 depth ++;
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);
81 size_t j;
83 array_print_indent(depth);
84 fprintf(stdout, "%s", du->key->ptr);
85 for (j = maxlen - len; j > 0; j --) {
86 fprintf(stdout, " ");
88 fprintf(stdout, " = ");
89 du->print(du, depth);
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");
106 depth --;
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);
113 } else {
114 fprintf(stdout, " # end of else");
118 if (ds->next) {
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) {
127 data_config *ds;
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;
144 return ds;