Changeset 5e718d9 in mainline for uspace/lib/bithenge/print.c
- Timestamp:
- 2012-08-21T10:04:16Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 67edca6
- Parents:
- 0da6c04 (diff), 6a97f2e (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/bithenge/print.c
r0da6c04 r5e718d9 38 38 39 39 #include <errno.h> 40 #include <stdarg.h> 40 41 #include <stdio.h> 41 42 #include "blob.h" … … 47 48 bool first; 48 49 int depth; 50 char *buffer; 51 size_t buffer_size; 49 52 } state_t; 50 53 54 static void state_printf(state_t *state, const char *format, ...) 55 { 56 va_list ap; 57 va_start(ap, format); 58 if (state->buffer) { 59 int rc = vsnprintf(state->buffer, state->buffer_size, format, 60 ap); 61 if (rc > 0 && (size_t)rc >= state->buffer_size) 62 rc = state->buffer_size - 1; 63 if (rc > 0) { 64 state->buffer += rc; 65 state->buffer_size -= rc; 66 } 67 } else { 68 vprintf(format, ap); 69 } 70 va_end(ap); 71 } 72 51 73 static int print_node(state_t *, bithenge_node_t *); 52 74 53 75 static void newline(state_t *state) 54 76 { 55 printf("\n");77 state_printf(state, "\n"); 56 78 for (int i = 0; i < state->depth; i++) { 57 printf(" ");79 state_printf(state, " "); 58 80 } 59 81 } … … 74 96 int rc = EOK; 75 97 if (!state->first) 76 printf(",");98 state_printf(state, ","); 77 99 newline(state); 78 100 state->first = false; … … 80 102 && bithenge_node_type(key) != BITHENGE_NODE_STRING; 81 103 if (add_quotes) 82 printf("\"");104 state_printf(state, "\""); 83 105 rc = print_node(state, key); 84 106 if (rc != EOK) 85 107 goto end; 86 108 if (add_quotes) 87 printf("\"");88 printf(": ");109 state_printf(state, "\""); 110 state_printf(state, ": "); 89 111 rc = print_node(state, value); 90 112 if (rc != EOK) … … 99 121 { 100 122 int rc; 101 printf("{");123 state_printf(state, "{"); 102 124 increase_depth(state); 103 125 state->first = true; … … 109 131 newline(state); 110 132 state->first = false; 111 printf("}");133 state_printf(state, "}"); 112 134 return EOK; 113 135 } … … 118 140 switch (state->type) { 119 141 case BITHENGE_PRINT_PYTHON: 120 printf(value ? "True" : "False");142 state_printf(state, value ? "True" : "False"); 121 143 break; 122 144 case BITHENGE_PRINT_JSON: 123 printf(value ? "true" : "false");145 state_printf(state, value ? "true" : "false"); 124 146 break; 125 147 } … … 130 152 { 131 153 bithenge_int_t value = bithenge_integer_node_value(node); 132 printf("%" BITHENGE_PRId, value);154 state_printf(state, "%" BITHENGE_PRId, value); 133 155 return EOK; 134 156 } … … 137 159 { 138 160 const char *value = bithenge_string_node_value(node); 139 printf("\"");161 state_printf(state, "\""); 140 162 for (string_iterator_t i = string_iterator(value); !string_iterator_done(&i); ) { 141 163 wchar_t ch; … … 144 166 return rc; 145 167 if (ch == '"' || ch == '\\') { 146 printf("\\%lc", (wint_t) ch);168 state_printf(state, "\\%lc", (wint_t) ch); 147 169 } else if (ch <= 0x1f) { 148 printf("\\u%04x", (unsigned int) ch);170 state_printf(state, "\\u%04x", (unsigned int) ch); 149 171 } else { 150 printf("%lc", (wint_t) ch);172 state_printf(state, "%lc", (wint_t) ch); 151 173 } 152 174 } 153 printf("\"");175 state_printf(state, "\""); 154 176 return EOK; 155 177 } … … 159 181 bithenge_blob_t *blob = bithenge_node_as_blob(node); 160 182 aoff64_t pos = 0; 161 charbuffer[1024];183 uint8_t buffer[1024]; 162 184 aoff64_t size = sizeof(buffer); 163 185 int rc; 164 printf(state->type == BITHENGE_PRINT_PYTHON ? "b\"" : "\""); 186 state_printf(state, 187 state->type == BITHENGE_PRINT_PYTHON ? "b\"" : "\""); 165 188 do { 166 rc = bithenge_blob_read(blob, pos, buffer, &size);189 rc = bithenge_blob_read(blob, pos, (char *)buffer, &size); 167 190 if (rc != EOK) 168 191 return rc; 169 192 for (aoff64_t i = 0; i < size; i++) 170 printf("\\x%02x", (unsigned int)(uint8_t)buffer[i]); 193 state_printf(state, "\\x%02x", 194 (unsigned int)buffer[i]); 171 195 pos += size; 172 196 } while (size == sizeof(buffer)); 173 printf("\"");197 state_printf(state, "\""); 174 198 return EOK; 175 199 } … … 192 216 } 193 217 194 /** Print a tree as text .218 /** Print a tree as text to stdout. 195 219 * @param type The format to use. 196 220 * @param tree The root node of the tree to print. … … 198 222 int bithenge_print_node(bithenge_print_type_t type, bithenge_node_t *tree) 199 223 { 200 state_t state = {type, true, 0 };224 state_t state = {type, true, 0, NULL, 0}; 201 225 return print_node(&state, tree); 202 226 } 203 227 228 /** Print a tree as text into a buffer. 229 * @param[in,out] str Holds a pointer to the buffer; changed to point to the 230 * null character. 231 * @param[in,out] size Holds the size of the buffer; changed to hold the 232 * remaining size. 233 * @param type The format to use. 234 * @param tree The root node of the tree to print. 235 * @return EOK on success or an error code from errno.h. */ 236 int bithenge_print_node_to_string(char **str, size_t *size, 237 bithenge_print_type_t type, bithenge_node_t *tree) 238 { 239 state_t state = {type, true, 0, *str, *size}; 240 int rc = print_node(&state, tree); 241 *str = state.buffer; 242 *size = state.buffer_size; 243 return rc; 244 } 245 204 246 /** @} 205 247 */
Note:
See TracChangeset
for help on using the changeset viewer.