Changeset 6be4142 in mainline for uspace/app/bithenge/print.c


Ignore:
Timestamp:
2012-08-12T04:53:47Z (12 years ago)
Author:
Sean Bartell <wingedtachikoma@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
1b6b76d
Parents:
0153c87
Message:

Bithenge: print transform errors; fixes and fat.bh improvements

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bithenge/print.c

    r0153c87 r6be4142  
    3838
    3939#include <errno.h>
     40#include <stdarg.h>
    4041#include <stdio.h>
    4142#include "blob.h"
     
    4748        bool first;
    4849        int depth;
     50        char *buffer;
     51        size_t buffer_size;
    4952} state_t;
    5053
     54static 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
    5173static int print_node(state_t *, bithenge_node_t *);
    5274
    5375static void newline(state_t *state)
    5476{
    55         printf("\n");
     77        state_printf(state, "\n");
    5678        for (int i = 0; i < state->depth; i++) {
    57                 printf("    ");
     79                state_printf(state, "    ");
    5880        }
    5981}
     
    7496        int rc = EOK;
    7597        if (!state->first)
    76                 printf(",");
     98                state_printf(state, ",");
    7799        newline(state);
    78100        state->first = false;
     
    80102            && bithenge_node_type(key) != BITHENGE_NODE_STRING;
    81103        if (add_quotes)
    82                 printf("\"");
     104                state_printf(state, "\"");
    83105        rc = print_node(state, key);
    84106        if (rc != EOK)
    85107                goto end;
    86108        if (add_quotes)
    87                 printf("\"");
    88         printf(": ");
     109                state_printf(state, "\"");
     110        state_printf(state, ": ");
    89111        rc = print_node(state, value);
    90112        if (rc != EOK)
     
    99121{
    100122        int rc;
    101         printf("{");
     123        state_printf(state, "{");
    102124        increase_depth(state);
    103125        state->first = true;
     
    109131                newline(state);
    110132        state->first = false;
    111         printf("}");
     133        state_printf(state, "}");
    112134        return EOK;
    113135}
     
    118140        switch (state->type) {
    119141        case BITHENGE_PRINT_PYTHON:
    120                 printf(value ? "True" : "False");
     142                state_printf(state, value ? "True" : "False");
    121143                break;
    122144        case BITHENGE_PRINT_JSON:
    123                 printf(value ? "true" : "false");
     145                state_printf(state, value ? "true" : "false");
    124146                break;
    125147        }
     
    130152{
    131153        bithenge_int_t value = bithenge_integer_node_value(node);
    132         printf("%" BITHENGE_PRId, value);
     154        state_printf(state, "%" BITHENGE_PRId, value);
    133155        return EOK;
    134156}
     
    137159{
    138160        const char *value = bithenge_string_node_value(node);
    139         printf("\"");
     161        state_printf(state, "\"");
    140162        for (string_iterator_t i = string_iterator(value); !string_iterator_done(&i); ) {
    141163                wchar_t ch;
     
    144166                        return rc;
    145167                if (ch == '"' || ch == '\\') {
    146                         printf("\\%lc", (wint_t) ch);
     168                        state_printf(state, "\\%lc", (wint_t) ch);
    147169                } else if (ch <= 0x1f) {
    148                         printf("\\u%04x", (unsigned int) ch);
     170                        state_printf(state, "\\u%04x", (unsigned int) ch);
    149171                } else {
    150                         printf("%lc", (wint_t) ch);
     172                        state_printf(state, "%lc", (wint_t) ch);
    151173                }
    152174        }
    153         printf("\"");
     175        state_printf(state, "\"");
    154176        return EOK;
    155177}
     
    162184        aoff64_t size = sizeof(buffer);
    163185        int rc;
    164         printf(state->type == BITHENGE_PRINT_PYTHON ? "b\"" : "\"");
     186        state_printf(state,
     187            state->type == BITHENGE_PRINT_PYTHON ? "b\"" : "\"");
    165188        do {
    166189                rc = bithenge_blob_read(blob, pos, buffer, &size);
     
    168191                        return rc;
    169192                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)(uint8_t)buffer[i]);
    171195                pos += size;
    172196        } while (size == sizeof(buffer));
    173         printf("\"");
     197        state_printf(state, "\"");
    174198        return EOK;
    175199}
     
    192216}
    193217
    194 /** Print a tree as text.
     218/** Print a tree as text to stdout.
    195219 * @param type The format to use.
    196220 * @param tree The root node of the tree to print.
     
    198222int bithenge_print_node(bithenge_print_type_t type, bithenge_node_t *tree)
    199223{
    200         state_t state = {type, true, 0};
     224        state_t state = {type, true, 0, NULL, 0};
    201225        return print_node(&state, tree);
    202226}
    203227
     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. */
     236int 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
    204246/** @}
    205247 */
Note: See TracChangeset for help on using the changeset viewer.