Skip to content

Commit 23ab30a

Browse files
cborpretty.c: add an internal flags argument
We'll use it soon. This commit is separate so it's easier to review the new functionality without the code changes to support them. Signed-off-by: Thiago Macieira <[email protected]>
1 parent 2f5d20e commit 23ab30a

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

src/cbor.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,11 +482,17 @@ CBOR_INLINE_API CborError cbor_value_get_double(const CborValue *value, double *
482482
}
483483

484484
/* Human-readable (dump) API */
485+
486+
enum CborPrettyFlags {
487+
CborPrettyDefaultFlags = 0
488+
};
489+
490+
CBOR_API CborError cbor_value_to_pretty_advance_flags(FILE *out, CborValue *value, int flags);
485491
CBOR_API CborError cbor_value_to_pretty_advance(FILE *out, CborValue *value);
486492
CBOR_INLINE_API CborError cbor_value_to_pretty(FILE *out, const CborValue *value)
487493
{
488494
CborValue copy = *value;
489-
return cbor_value_to_pretty_advance(out, &copy);
495+
return cbor_value_to_pretty_advance_flags(out, &copy, CborPrettyDefaultFlags);
490496
}
491497

492498
#ifdef __cplusplus

src/cborpretty.c

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/****************************************************************************
22
**
3-
** Copyright (C) 2016 Intel Corporation
3+
** Copyright (C) 2017 Intel Corporation
44
**
55
** Permission is hereby granted, free of charge, to any person obtaining a copy
66
** of this software and associated documentation files (the "Software"), to deal
@@ -110,6 +110,13 @@
110110
* If the map length is indeterminate, an underscore ("_") appears immediately after the opening brace.
111111
*/
112112

113+
/**
114+
* \enum CborPrettyFlags
115+
* The CborPrettyFlags enum contains flags that control the conversion of CBOR to text format.
116+
*
117+
* \value CborPrettyDefaultFlags Default conversion flags.
118+
*/
119+
113120
static int hexDump(FILE *out, const uint8_t *buffer, size_t n)
114121
{
115122
while (n--) {
@@ -238,16 +245,16 @@ static int utf8EscapedDump(FILE *out, const char *buffer, size_t n)
238245
return CborNoError;
239246
}
240247

241-
static CborError value_to_pretty(FILE *out, CborValue *it);
242-
static CborError container_to_pretty(FILE *out, CborValue *it, CborType containerType)
248+
static CborError value_to_pretty(FILE *out, CborValue *it, int flags);
249+
static CborError container_to_pretty(FILE *out, CborValue *it, CborType containerType, int flags)
243250
{
244251
const char *comma = "";
245252
while (!cbor_value_at_end(it)) {
246253
if (fprintf(out, "%s", comma) < 0)
247254
return CborErrorIO;
248255
comma = ", ";
249256

250-
CborError err = value_to_pretty(out, it);
257+
CborError err = value_to_pretty(out, it, flags);
251258
if (err)
252259
return err;
253260

@@ -257,14 +264,14 @@ static CborError container_to_pretty(FILE *out, CborValue *it, CborType containe
257264
/* map: that was the key, so get the value */
258265
if (fprintf(out, ": ") < 0)
259266
return CborErrorIO;
260-
err = value_to_pretty(out, it);
267+
err = value_to_pretty(out, it, flags);
261268
if (err)
262269
return err;
263270
}
264271
return CborNoError;
265272
}
266273

267-
static CborError value_to_pretty(FILE *out, CborValue *it)
274+
static CborError value_to_pretty(FILE *out, CborValue *it, int flags)
268275
{
269276
CborError err;
270277
CborType type = cbor_value_get_type(it);
@@ -286,7 +293,7 @@ static CborError value_to_pretty(FILE *out, CborValue *it)
286293
it->ptr = recursed.ptr;
287294
return err; /* parse error */
288295
}
289-
err = container_to_pretty(out, &recursed, type);
296+
err = container_to_pretty(out, &recursed, type, flags);
290297
if (err) {
291298
it->ptr = recursed.ptr;
292299
return err; /* parse error */
@@ -360,7 +367,7 @@ static CborError value_to_pretty(FILE *out, CborValue *it)
360367
err = cbor_value_advance_fixed(it);
361368
if (err)
362369
return err;
363-
err = value_to_pretty(out, it);
370+
err = value_to_pretty(out, it, flags);
364371
if (err)
365372
return err;
366373
if (fprintf(out, ")") < 0)
@@ -465,7 +472,26 @@ static CborError value_to_pretty(FILE *out, CborValue *it)
465472
*/
466473
CborError cbor_value_to_pretty_advance(FILE *out, CborValue *value)
467474
{
468-
return value_to_pretty(out, value);
475+
return value_to_pretty(out, value, CborPrettyDefaultFlags);
476+
}
477+
478+
/**
479+
* Converts the current CBOR type pointed by \a value to its textual
480+
* representation and writes it to the \a out stream. If an error occurs, this
481+
* function returns an error code similar to CborParsing.
482+
*
483+
* The textual representation can be controlled by the \a flags parameter (see
484+
* CborPrettyFlags for more information).
485+
*
486+
* If no error ocurred, this function advances \a value to the next element.
487+
* Often, concatenating the text representation of multiple elements can be
488+
* done by appending a comma to the output stream.
489+
*
490+
* \sa cbor_value_to_pretty(), cbor_value_to_json_advance()
491+
*/
492+
CborError cbor_value_to_pretty_advance_flags(FILE *out, CborValue *value, int flags)
493+
{
494+
return value_to_pretty(out, value, flags);
469495
}
470496

471497
/** @} */

0 commit comments

Comments
 (0)