1 /****************************************************************************
2 **
3 ** Copyright (C) 2017 Intel Corporation
4 **
5 ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 ** of this software and associated documentation files (the "Software"), to deal
7 ** in the Software without restriction, including without limitation the rights
8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 ** copies of the Software, and to permit persons to whom the Software is
10 ** furnished to do so, subject to the following conditions:
11 **
12 ** The above copyright notice and this permission notice shall be included in
13 ** all copies or substantial portions of the Software.
14 **
15 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 ** THE SOFTWARE.
22 **
23 ****************************************************************************/
24
25 #include "tinycbor/cbor.h"
26 #include <stdarg.h>
27 #include <stdio.h>
28
cbor_fprintf(void * out,const char * fmt,...)29 static CborError cbor_fprintf(void *out, const char *fmt, ...)
30 {
31 int n;
32
33 va_list list;
34 va_start(list, fmt);
35 n = vfprintf((FILE *)out, fmt, list);
36 va_end(list);
37
38 return n < 0 ? CborErrorIO : CborNoError;
39 }
40
41 /**
42 * \fn CborError cbor_value_to_pretty(FILE *out, const CborValue *value)
43 *
44 * Converts the current CBOR type pointed by \a value to its textual
45 * representation and writes it to the \a out stream. If an error occurs, this
46 * function returns an error code similar to CborParsing.
47 *
48 * \sa cbor_value_to_pretty_advance(), cbor_value_to_json_advance()
49 */
50
51 /**
52 * Converts the current CBOR type pointed by \a value to its textual
53 * representation and writes it to the \a out stream. If an error occurs, this
54 * function returns an error code similar to CborParsing.
55 *
56 * If no error ocurred, this function advances \a value to the next element.
57 * Often, concatenating the text representation of multiple elements can be
58 * done by appending a comma to the output stream.
59 *
60 * \sa cbor_value_to_pretty(), cbor_value_to_pretty_stream(), cbor_value_to_json_advance()
61 */
cbor_value_to_pretty_advance(FILE * out,CborValue * value)62 CborError cbor_value_to_pretty_advance(FILE *out, CborValue *value)
63 {
64 return cbor_value_to_pretty_stream(cbor_fprintf, out, value, CborPrettyDefaultFlags);
65 }
66
67 /**
68 * Converts the current CBOR type pointed by \a value to its textual
69 * representation and writes it to the \a out stream. If an error occurs, this
70 * function returns an error code similar to CborParsing.
71 *
72 * The textual representation can be controlled by the \a flags parameter (see
73 * CborPrettyFlags for more information).
74 *
75 * If no error ocurred, this function advances \a value to the next element.
76 * Often, concatenating the text representation of multiple elements can be
77 * done by appending a comma to the output stream.
78 *
79 * \sa cbor_value_to_pretty_stream(), cbor_value_to_pretty(), cbor_value_to_json_advance()
80 */
cbor_value_to_pretty_advance_flags(FILE * out,CborValue * value,int flags)81 CborError cbor_value_to_pretty_advance_flags(FILE *out, CborValue *value, int flags)
82 {
83 return cbor_value_to_pretty_stream(cbor_fprintf, out, value, flags);
84 }
85
86