make output of array/config same format as lighttpd.conf, with auto indention
git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-merge-1.4.x@554 152afb58-edef-0310-8abb-c4023f1b3aa9
This commit is contained in:
parent
e3a27bec4e
commit
6044dbc5b3
83
src/array.c
83
src/array.c
|
@ -235,18 +235,91 @@ void array_print_indent(int depth) {
|
|||
}
|
||||
}
|
||||
|
||||
size_t array_get_max_key_length(array *a) {
|
||||
size_t maxlen, i;
|
||||
|
||||
maxlen = 0;
|
||||
for (i = 0; i < a->used; i ++) {
|
||||
data_unset *du = a->data[i];
|
||||
size_t len = strlen(du->key->ptr);
|
||||
|
||||
if (len > maxlen) {
|
||||
maxlen = len;
|
||||
}
|
||||
}
|
||||
return maxlen;
|
||||
}
|
||||
|
||||
static inline int str_int_equal(const char *str, int i) {
|
||||
char buf[16];
|
||||
snprintf(buf, sizeof(buf), "%d", i);
|
||||
return strcmp(str, buf) == 0;
|
||||
}
|
||||
|
||||
int array_print(array *a, int depth) {
|
||||
size_t i;
|
||||
size_t maxlen;
|
||||
int oneline = 1;
|
||||
|
||||
fprintf(stderr, "{\n");
|
||||
if (a->used > 5) {
|
||||
oneline = 0;
|
||||
}
|
||||
for (i = 0; i < a->used && oneline; i++) {
|
||||
data_unset *du = a->data[i];
|
||||
if (!str_int_equal(du->key->ptr, i)) {
|
||||
oneline = 0;
|
||||
break;
|
||||
}
|
||||
switch (du->type) {
|
||||
case TYPE_INTEGER:
|
||||
case TYPE_STRING:
|
||||
case TYPE_COUNT:
|
||||
break;
|
||||
default:
|
||||
oneline = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (oneline) {
|
||||
fprintf(stderr, "(");
|
||||
for (i = 0; i < a->used; i++) {
|
||||
data_unset *du = a->data[i];
|
||||
if (i != 0) {
|
||||
fprintf(stderr, ", ");
|
||||
}
|
||||
du->print(du, depth + 1);
|
||||
}
|
||||
fprintf(stderr, ")");
|
||||
return 0;
|
||||
}
|
||||
|
||||
maxlen = array_get_max_key_length(a);
|
||||
fprintf(stderr, "(\n");
|
||||
for (i = 0; i < a->used; i++) {
|
||||
data_unset *du = a->data[i];
|
||||
array_print_indent(depth + 1);
|
||||
fprintf(stderr, "%d:%s: ", i, a->data[i]->key->ptr);
|
||||
a->data[i]->print(a->data[i], depth + 1);
|
||||
fprintf(stderr, "\n");
|
||||
if (!str_int_equal(du->key->ptr, i)) {
|
||||
int j;
|
||||
|
||||
if (i && (i % 5) == 0) {
|
||||
fprintf(stderr, "# %d\n", i);
|
||||
array_print_indent(depth + 1);
|
||||
}
|
||||
fprintf(stderr, "\"%s\"", du->key->ptr);
|
||||
for (j = maxlen - strlen(du->key->ptr); j > 0; j --) {
|
||||
fprintf(stderr, " ");
|
||||
}
|
||||
fprintf(stderr, " => ");
|
||||
}
|
||||
du->print(du, depth + 1);
|
||||
fprintf(stderr, ",\n");
|
||||
}
|
||||
if (!(i && (i - 1 % 5) == 0)) {
|
||||
array_print_indent(depth + 1);
|
||||
fprintf(stderr, "# %d\n", i);
|
||||
}
|
||||
array_print_indent(depth);
|
||||
fprintf(stderr, "}");
|
||||
fprintf(stderr, ")");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -142,5 +142,6 @@ data_unset *array_get_element(array *a, const char *key);
|
|||
data_unset *array_replace(array *a, data_unset *du);
|
||||
int array_strcasecmp(const char *a, size_t a_len, const char *b, size_t b_len);
|
||||
void array_print_indent(int depth);
|
||||
size_t array_get_max_key_length(array *a);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -42,7 +42,6 @@ static int data_array_insert_dup(data_unset *dst, data_unset *src) {
|
|||
static void data_array_print(const data_unset *d, int depth) {
|
||||
data_array *ds = (data_array *)d;
|
||||
|
||||
fprintf(stderr, "array ");
|
||||
array_print(ds->value, depth);
|
||||
}
|
||||
|
||||
|
|
|
@ -53,7 +53,9 @@ static int data_config_insert_dup(data_unset *dst, data_unset *src) {
|
|||
|
||||
static void data_config_print(const data_unset *d, int depth) {
|
||||
data_config *ds = (data_config *)d;
|
||||
array *a = (array *)ds->value;
|
||||
size_t i;
|
||||
size_t maxlen;
|
||||
|
||||
if (0 == ds->context_ndx) {
|
||||
fprintf(stderr, "config {\n");
|
||||
|
@ -61,30 +63,49 @@ static void data_config_print(const data_unset *d, int depth) {
|
|||
else {
|
||||
fprintf(stderr, "$%s %s \"%s\" {\n",
|
||||
ds->comp_key->ptr, ds->op->ptr, ds->string->ptr);
|
||||
array_print_indent(depth + 1);
|
||||
fprintf(stderr, "# block %d\n", ds->context_ndx);
|
||||
}
|
||||
depth ++;
|
||||
|
||||
maxlen = array_get_max_key_length(a);
|
||||
for (i = 0; i < a->used; i ++) {
|
||||
data_unset *du = a->data[i];
|
||||
size_t len = strlen(du->key->ptr);
|
||||
size_t j;
|
||||
|
||||
array_print_indent(depth);
|
||||
fprintf(stderr, "%s", du->key->ptr);
|
||||
for (j = maxlen - len; j > 0; j --) {
|
||||
fprintf(stderr, " ");
|
||||
}
|
||||
fprintf(stderr, " = ");
|
||||
du->print(du, depth);
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
array_print_indent(depth + 1);
|
||||
fprintf(stderr, "context_ndx: %d\n", ds->context_ndx);
|
||||
array_print_indent(depth + 1);
|
||||
fprintf(stderr, "context: ");
|
||||
array_print(ds->value, depth + 1);
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
if (ds->childs) {
|
||||
fprintf(stderr, "\n");
|
||||
for (i = 0; i < ds->childs->used; i ++) {
|
||||
data_unset *du = ds->childs->data[i];
|
||||
|
||||
/* only the 1st block of chaining */
|
||||
if (NULL == ((data_config *)du)->prev) {
|
||||
fprintf(stderr, "\n");
|
||||
array_print_indent(depth + 1);
|
||||
du->print(du, depth + 1);
|
||||
array_print_indent(depth);
|
||||
du->print(du, depth);
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
depth --;
|
||||
array_print_indent(depth);
|
||||
fprintf(stderr, "}");
|
||||
if (0 != ds->context_ndx) {
|
||||
fprintf(stderr, " # end of $%s %s \"%s\"",
|
||||
ds->comp_key->ptr, ds->op->ptr, ds->string->ptr);
|
||||
}
|
||||
|
||||
if (ds->next) {
|
||||
fprintf(stderr, "\n");
|
||||
|
|
Loading…
Reference in New Issue