Changeset 5e718d9 in mainline for uspace/lib/bithenge/print.c


Ignore:
Timestamp:
2012-08-21T10:04:16Z (12 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
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.
Message:

Merge with upstream (lp:~wtachi/helenos/bithenge)

File:
1 moved

Legend:

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

    r0da6c04 r5e718d9  
    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}
     
    159181        bithenge_blob_t *blob = bithenge_node_as_blob(node);
    160182        aoff64_t pos = 0;
    161         char buffer[1024];
     183        uint8_t buffer[1024];
    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 {
    166                 rc = bithenge_blob_read(blob, pos, buffer, &size);
     189                rc = bithenge_blob_read(blob, pos, (char *)buffer, &size);
    167190                if (rc != EOK)
    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)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.