Skip to content

Commit f39dcb8

Browse files
Pretty: move the two functions taking FILE* to a separate file
This now allows the cborpretty.c source to be compiled even in C freestanding environments. Signed-off-by: Thiago Macieira <[email protected]>
1 parent ee63f79 commit f39dcb8

File tree

5 files changed

+91
-63
lines changed

5 files changed

+91
-63
lines changed

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ TINYCBOR_SOURCES = \
2828
src/cborparser.c \
2929
src/cborparser_dup_string.c \
3030
src/cborpretty.c \
31+
src/cborpretty_stdio.c \
3132
src/cbortojson.c \
3233
src/cborvalidation.c \
3334
#

Makefile.nmake

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ TINYCBOR_SOURCES = \
88
src\cborparser.c \
99
src\cborparser_dup_string.c \
1010
src\cborpretty.c \
11+
src\cborpretty_stdio.c \
1112
src\cborvalidation.c
1213
TINYCBOR_OBJS = \
1314
src\cborerrorstrings.obj \
@@ -16,6 +17,7 @@ TINYCBOR_OBJS = \
1617
src\cborparser.obj \
1718
src\cborparser_dup_string.obj \
1819
src\cborpretty.obj \
20+
src\cborpretty_stdio.obj \
1921
src\cborvalidation.obj
2022

2123
all: lib\tinycbor.lib

src/cbor.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -573,9 +573,6 @@ enum CborValidationFlags {
573573

574574
CBOR_API CborError cbor_value_validate(const CborValue *it, int flags);
575575

576-
/* The following API requires a hosted C implementation (uses FILE*) */
577-
#if !defined(__STDC_HOSTED__) || __STDC_HOSTED__-0 == 1
578-
579576
/* Human-readable (dump) API */
580577

581578
enum CborPrettyFlags {
@@ -598,14 +595,16 @@ typedef CborError (*CborStreamFunction)(void *token, const char *fmt, ...)
598595
;
599596

600597
CBOR_API CborError cbor_value_to_pretty_stream(CborStreamFunction streamFunction, void *token, CborValue *value, int flags);
598+
599+
/* The following API requires a hosted C implementation (uses FILE*) */
600+
#if !defined(__STDC_HOSTED__) || __STDC_HOSTED__-0 == 1
601601
CBOR_API CborError cbor_value_to_pretty_advance_flags(FILE *out, CborValue *value, int flags);
602602
CBOR_API CborError cbor_value_to_pretty_advance(FILE *out, CborValue *value);
603603
CBOR_INLINE_API CborError cbor_value_to_pretty(FILE *out, const CborValue *value)
604604
{
605605
CborValue copy = *value;
606606
return cbor_value_to_pretty_advance_flags(out, &copy, CborPrettyDefaultFlags);
607607
}
608-
609608
#endif /* __STDC_HOSTED__ check */
610609

611610
#ifdef __cplusplus

src/cborpretty.c

-59
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
#include <float.h>
3737
#include <inttypes.h>
3838
#include <math.h>
39-
#include <stdarg.h>
40-
#include <stdio.h>
4139
#include <string.h>
4240

4341
/**
@@ -148,18 +146,6 @@
148146
* \value CborPrettyDefaultFlags Default conversion flags.
149147
*/
150148

151-
static CborError cbor_fprintf(void *out, const char *fmt, ...)
152-
{
153-
int n;
154-
155-
va_list list;
156-
va_start(list, fmt);
157-
n = vfprintf((FILE *)out, fmt, list);
158-
va_end(list);
159-
160-
return n < 0 ? CborErrorIO : CborNoError;
161-
}
162-
163149
static void printRecursionLimit(CborStreamFunction stream, void *out)
164150
{
165151
stream(out, "<nesting too deep, recursion stopped>");
@@ -541,49 +527,4 @@ CborError cbor_value_to_pretty_stream(CborStreamFunction streamFunction, void *t
541527
return value_to_pretty(streamFunction, token, value, flags, CBOR_PARSER_MAX_RECURSIONS);
542528
}
543529

544-
/**
545-
* \fn CborError cbor_value_to_pretty(FILE *out, const CborValue *value)
546-
*
547-
* Converts the current CBOR type pointed by \a value to its textual
548-
* representation and writes it to the \a out stream. If an error occurs, this
549-
* function returns an error code similar to CborParsing.
550-
*
551-
* \sa cbor_value_to_pretty_advance(), cbor_value_to_json_advance()
552-
*/
553-
554-
/**
555-
* Converts the current CBOR type pointed by \a value to its textual
556-
* representation and writes it to the \a out stream. If an error occurs, this
557-
* function returns an error code similar to CborParsing.
558-
*
559-
* If no error ocurred, this function advances \a value to the next element.
560-
* Often, concatenating the text representation of multiple elements can be
561-
* done by appending a comma to the output stream.
562-
*
563-
* \sa cbor_value_to_pretty(), cbor_value_to_pretty_stream(), cbor_value_to_json_advance()
564-
*/
565-
CborError cbor_value_to_pretty_advance(FILE *out, CborValue *value)
566-
{
567-
return value_to_pretty(cbor_fprintf, out, value, CborPrettyDefaultFlags, CBOR_PARSER_MAX_RECURSIONS);
568-
}
569-
570-
/**
571-
* Converts the current CBOR type pointed by \a value to its textual
572-
* representation and writes it to the \a out stream. If an error occurs, this
573-
* function returns an error code similar to CborParsing.
574-
*
575-
* The textual representation can be controlled by the \a flags parameter (see
576-
* CborPrettyFlags for more information).
577-
*
578-
* If no error ocurred, this function advances \a value to the next element.
579-
* Often, concatenating the text representation of multiple elements can be
580-
* done by appending a comma to the output stream.
581-
*
582-
* \sa cbor_value_to_pretty_stream(), cbor_value_to_pretty(), cbor_value_to_json_advance()
583-
*/
584-
CborError cbor_value_to_pretty_advance_flags(FILE *out, CborValue *value, int flags)
585-
{
586-
return value_to_pretty(cbor_fprintf, out, value, flags, CBOR_PARSER_MAX_RECURSIONS);
587-
}
588-
589530
/** @} */

src/cborpretty_stdio.c

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 "cbor.h"
26+
#include <stdarg.h>
27+
#include <stdio.h>
28+
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+
*/
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+
*/
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+

0 commit comments

Comments
 (0)