Changeset 842ed146 in mainline


Ignore:
Timestamp:
2012-07-24T22:19:16Z (12 years ago)
Author:
Sean Bartell <wingedtachikoma@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
43788b2
Parents:
e8e31d9
Message:

Bithenge: use indentation in tree output

Location:
uspace/app/bithenge
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bithenge/Makefile.linux

    re8e31d9 r842ed146  
    5151clean:
    5252        find . -name '*.o' -follow -exec rm \{\} \;
     53        rm -f $(BINARY)
  • uspace/app/bithenge/print.c

    re8e31d9 r842ed146  
    3333 * @file
    3434 * Write a tree as JSON or other text formats.
     35 * @todo Allow more control over the printing style, and handle printing in
     36 * limited space.
    3537 */
    3638
     
    4446        bithenge_print_type_t type;
    4547        bool first;
    46 } print_internal_data_t;
    47 
    48 static int print_internal_func(bithenge_node_t *key, bithenge_node_t *value, void *data_)
    49 {
    50         print_internal_data_t *data = (print_internal_data_t *)data_;
     48        int depth;
     49} state_t;
     50
     51static int print_node(state_t *, bithenge_node_t *);
     52
     53static void newline(state_t *state)
     54{
     55        printf("\n");
     56        for (int i = 0; i < state->depth; i++) {
     57                printf("    ");
     58        }
     59}
     60
     61static void increase_depth(state_t *state)
     62{
     63        state->depth++;
     64}
     65
     66static void decrease_depth(state_t *state)
     67{
     68        state->depth--;
     69}
     70
     71static int print_internal_func(bithenge_node_t *key, bithenge_node_t *value, void *data)
     72{
     73        state_t *state = (state_t *)data;
    5174        int rc = EOK;
    52         if (!data->first)
    53                 printf(", ");
    54         data->first = false;
    55         bool add_quotes = data->type == BITHENGE_PRINT_JSON
     75        if (!state->first)
     76                printf(",");
     77        newline(state);
     78        state->first = false;
     79        bool add_quotes = state->type == BITHENGE_PRINT_JSON
    5680            && bithenge_node_type(key) != BITHENGE_NODE_STRING;
    5781        if (add_quotes)
    5882                printf("\"");
    59         rc = bithenge_print_node(data->type, key);
     83        rc = print_node(state, key);
    6084        if (rc != EOK)
    6185                goto end;
     
    6387                printf("\"");
    6488        printf(": ");
    65         rc = bithenge_print_node(data->type, value);
     89        rc = print_node(state, value);
    6690        if (rc != EOK)
    6791                goto end;
     
    7296}
    7397
    74 static int print_internal(bithenge_print_type_t type, bithenge_node_t *node)
     98static int print_internal(state_t *state, bithenge_node_t *node)
    7599{
    76100        int rc;
    77         print_internal_data_t data = { type, true };
    78101        printf("{");
    79         rc = bithenge_node_for_each(node, print_internal_func, &data);
     102        increase_depth(state);
     103        state->first = true;
     104        rc = bithenge_node_for_each(node, print_internal_func, state);
    80105        if (rc != EOK)
    81106                return rc;
     107        decrease_depth(state);
     108        if (!state->first)
     109                newline(state);
     110        state->first = false;
    82111        printf("}");
    83112        return EOK;
    84113}
    85114
    86 static int print_boolean(bithenge_print_type_t type, bithenge_node_t *node)
     115static int print_boolean(state_t *state, bithenge_node_t *node)
    87116{
    88117        bool value = bithenge_boolean_node_value(node);
    89         switch (type) {
     118        switch (state->type) {
    90119        case BITHENGE_PRINT_PYTHON:
    91120                printf(value ? "True" : "False");
     
    98127}
    99128
    100 static int print_integer(bithenge_print_type_t type, bithenge_node_t *node)
     129static int print_integer(state_t *state, bithenge_node_t *node)
    101130{
    102131        bithenge_int_t value = bithenge_integer_node_value(node);
     
    105134}
    106135
    107 static int print_string(bithenge_print_type_t type, bithenge_node_t *node)
     136static int print_string(state_t *state, bithenge_node_t *node)
    108137{
    109138        const char *value = bithenge_string_node_value(node);
     
    126155}
    127156
    128 static int print_blob(bithenge_print_type_t type, bithenge_node_t *node)
     157static int print_blob(state_t *state, bithenge_node_t *node)
    129158{
    130159        bithenge_blob_t *blob = bithenge_node_as_blob(node);
     
    133162        aoff64_t size = sizeof(buffer);
    134163        int rc;
    135         printf(type == BITHENGE_PRINT_PYTHON ? "b\"" : "\"");
     164        printf(state->type == BITHENGE_PRINT_PYTHON ? "b\"" : "\"");
    136165        do {
    137166                rc = bithenge_blob_read(blob, pos, buffer, &size);
     
    146175}
    147176
     177static int print_node(state_t *state, bithenge_node_t *tree)
     178{
     179        switch (bithenge_node_type(tree)) {
     180        case BITHENGE_NODE_INTERNAL:
     181                return print_internal(state, tree);
     182        case BITHENGE_NODE_BOOLEAN:
     183                return print_boolean(state, tree);
     184        case BITHENGE_NODE_INTEGER:
     185                return print_integer(state, tree);
     186        case BITHENGE_NODE_STRING:
     187                return print_string(state, tree);
     188        case BITHENGE_NODE_BLOB:
     189                return print_blob(state, tree);
     190        }
     191        return ENOTSUP;
     192}
     193
    148194/** Print a tree as text.
    149195 * @param type The format to use.
     
    152198int bithenge_print_node(bithenge_print_type_t type, bithenge_node_t *tree)
    153199{
    154         switch (bithenge_node_type(tree)) {
    155         case BITHENGE_NODE_INTERNAL:
    156                 return print_internal(type, tree);
    157         case BITHENGE_NODE_BOOLEAN:
    158                 return print_boolean(type, tree);
    159         case BITHENGE_NODE_INTEGER:
    160                 return print_integer(type, tree);
    161         case BITHENGE_NODE_STRING:
    162                 return print_string(type, tree);
    163         case BITHENGE_NODE_BLOB:
    164                 return print_blob(type, tree);
    165         }
    166         return ENOTSUP;
     200        state_t state = {type, true, 0};
     201        return print_node(&state, tree);
    167202}
    168203
Note: See TracChangeset for help on using the changeset viewer.