Changeset 842ed146 in mainline
- Timestamp:
- 2012-07-24T22:19:16Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 43788b2
- Parents:
- e8e31d9
- Location:
- uspace/app/bithenge
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bithenge/Makefile.linux
re8e31d9 r842ed146 51 51 clean: 52 52 find . -name '*.o' -follow -exec rm \{\} \; 53 rm -f $(BINARY) -
uspace/app/bithenge/print.c
re8e31d9 r842ed146 33 33 * @file 34 34 * 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. 35 37 */ 36 38 … … 44 46 bithenge_print_type_t type; 45 47 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 51 static int print_node(state_t *, bithenge_node_t *); 52 53 static void newline(state_t *state) 54 { 55 printf("\n"); 56 for (int i = 0; i < state->depth; i++) { 57 printf(" "); 58 } 59 } 60 61 static void increase_depth(state_t *state) 62 { 63 state->depth++; 64 } 65 66 static void decrease_depth(state_t *state) 67 { 68 state->depth--; 69 } 70 71 static int print_internal_func(bithenge_node_t *key, bithenge_node_t *value, void *data) 72 { 73 state_t *state = (state_t *)data; 51 74 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 56 80 && bithenge_node_type(key) != BITHENGE_NODE_STRING; 57 81 if (add_quotes) 58 82 printf("\""); 59 rc = bithenge_print_node(data->type, key);83 rc = print_node(state, key); 60 84 if (rc != EOK) 61 85 goto end; … … 63 87 printf("\""); 64 88 printf(": "); 65 rc = bithenge_print_node(data->type, value);89 rc = print_node(state, value); 66 90 if (rc != EOK) 67 91 goto end; … … 72 96 } 73 97 74 static int print_internal( bithenge_print_type_t type, bithenge_node_t *node)98 static int print_internal(state_t *state, bithenge_node_t *node) 75 99 { 76 100 int rc; 77 print_internal_data_t data = { type, true };78 101 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); 80 105 if (rc != EOK) 81 106 return rc; 107 decrease_depth(state); 108 if (!state->first) 109 newline(state); 110 state->first = false; 82 111 printf("}"); 83 112 return EOK; 84 113 } 85 114 86 static int print_boolean( bithenge_print_type_t type, bithenge_node_t *node)115 static int print_boolean(state_t *state, bithenge_node_t *node) 87 116 { 88 117 bool value = bithenge_boolean_node_value(node); 89 switch ( type) {118 switch (state->type) { 90 119 case BITHENGE_PRINT_PYTHON: 91 120 printf(value ? "True" : "False"); … … 98 127 } 99 128 100 static int print_integer( bithenge_print_type_t type, bithenge_node_t *node)129 static int print_integer(state_t *state, bithenge_node_t *node) 101 130 { 102 131 bithenge_int_t value = bithenge_integer_node_value(node); … … 105 134 } 106 135 107 static int print_string( bithenge_print_type_t type, bithenge_node_t *node)136 static int print_string(state_t *state, bithenge_node_t *node) 108 137 { 109 138 const char *value = bithenge_string_node_value(node); … … 126 155 } 127 156 128 static int print_blob( bithenge_print_type_t type, bithenge_node_t *node)157 static int print_blob(state_t *state, bithenge_node_t *node) 129 158 { 130 159 bithenge_blob_t *blob = bithenge_node_as_blob(node); … … 133 162 aoff64_t size = sizeof(buffer); 134 163 int rc; 135 printf( type == BITHENGE_PRINT_PYTHON ? "b\"" : "\"");164 printf(state->type == BITHENGE_PRINT_PYTHON ? "b\"" : "\""); 136 165 do { 137 166 rc = bithenge_blob_read(blob, pos, buffer, &size); … … 146 175 } 147 176 177 static 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 148 194 /** Print a tree as text. 149 195 * @param type The format to use. … … 152 198 int bithenge_print_node(bithenge_print_type_t type, bithenge_node_t *tree) 153 199 { 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); 167 202 } 168 203
Note:
See TracChangeset
for help on using the changeset viewer.