diff --git a/Makefile b/Makefile index eac77154..f3a2a712 100644 --- a/Makefile +++ b/Makefile @@ -28,6 +28,7 @@ TINYCBOR_SOURCES = \ src/cborparser.c \ src/cborparser_dup_string.c \ src/cborpretty.c \ + src/cborpretty_stdio.c \ src/cbortojson.c \ src/cborvalidation.c \ # diff --git a/Makefile.nmake b/Makefile.nmake index 2a16fde9..defc30aa 100644 --- a/Makefile.nmake +++ b/Makefile.nmake @@ -8,6 +8,7 @@ TINYCBOR_SOURCES = \ src\cborparser.c \ src\cborparser_dup_string.c \ src\cborpretty.c \ + src\cborpretty_stdio.c \ src\cborvalidation.c TINYCBOR_OBJS = \ src\cborerrorstrings.obj \ @@ -16,6 +17,7 @@ TINYCBOR_OBJS = \ src\cborparser.obj \ src\cborparser_dup_string.obj \ src\cborpretty.obj \ + src\cborpretty_stdio.obj \ src\cborvalidation.obj all: lib\tinycbor.lib diff --git a/src/cbor.h b/src/cbor.h index 6a09a67e..9fc78074 100644 --- a/src/cbor.h +++ b/src/cbor.h @@ -573,9 +573,6 @@ enum CborValidationFlags { CBOR_API CborError cbor_value_validate(const CborValue *it, int flags); -/* The following API requires a hosted C implementation (uses FILE*) */ -#if !defined(__STDC_HOSTED__) || __STDC_HOSTED__-0 == 1 - /* Human-readable (dump) API */ enum CborPrettyFlags { @@ -598,6 +595,9 @@ typedef CborError (*CborStreamFunction)(void *token, const char *fmt, ...) ; CBOR_API CborError cbor_value_to_pretty_stream(CborStreamFunction streamFunction, void *token, CborValue *value, int flags); + +/* The following API requires a hosted C implementation (uses FILE*) */ +#if !defined(__STDC_HOSTED__) || __STDC_HOSTED__-0 == 1 CBOR_API CborError cbor_value_to_pretty_advance_flags(FILE *out, CborValue *value, int flags); CBOR_API CborError cbor_value_to_pretty_advance(FILE *out, CborValue *value); CBOR_INLINE_API CborError cbor_value_to_pretty(FILE *out, const CborValue *value) @@ -605,7 +605,6 @@ CBOR_INLINE_API CborError cbor_value_to_pretty(FILE *out, const CborValue *value CborValue copy = *value; return cbor_value_to_pretty_advance_flags(out, ©, CborPrettyDefaultFlags); } - #endif /* __STDC_HOSTED__ check */ #ifdef __cplusplus diff --git a/src/cborpretty.c b/src/cborpretty.c index f38d6768..0995f512 100644 --- a/src/cborpretty.c +++ b/src/cborpretty.c @@ -36,8 +36,6 @@ #include #include #include -#include -#include #include /** @@ -148,18 +146,6 @@ * \value CborPrettyDefaultFlags Default conversion flags. */ -static CborError cbor_fprintf(void *out, const char *fmt, ...) -{ - int n; - - va_list list; - va_start(list, fmt); - n = vfprintf((FILE *)out, fmt, list); - va_end(list); - - return n < 0 ? CborErrorIO : CborNoError; -} - static void printRecursionLimit(CborStreamFunction stream, void *out) { stream(out, ""); @@ -541,49 +527,4 @@ CborError cbor_value_to_pretty_stream(CborStreamFunction streamFunction, void *t return value_to_pretty(streamFunction, token, value, flags, CBOR_PARSER_MAX_RECURSIONS); } -/** - * \fn CborError cbor_value_to_pretty(FILE *out, const CborValue *value) - * - * Converts the current CBOR type pointed by \a value to its textual - * representation and writes it to the \a out stream. If an error occurs, this - * function returns an error code similar to CborParsing. - * - * \sa cbor_value_to_pretty_advance(), cbor_value_to_json_advance() - */ - -/** - * Converts the current CBOR type pointed by \a value to its textual - * representation and writes it to the \a out stream. If an error occurs, this - * function returns an error code similar to CborParsing. - * - * If no error ocurred, this function advances \a value to the next element. - * Often, concatenating the text representation of multiple elements can be - * done by appending a comma to the output stream. - * - * \sa cbor_value_to_pretty(), cbor_value_to_pretty_stream(), cbor_value_to_json_advance() - */ -CborError cbor_value_to_pretty_advance(FILE *out, CborValue *value) -{ - return value_to_pretty(cbor_fprintf, out, value, CborPrettyDefaultFlags, CBOR_PARSER_MAX_RECURSIONS); -} - -/** - * Converts the current CBOR type pointed by \a value to its textual - * representation and writes it to the \a out stream. If an error occurs, this - * function returns an error code similar to CborParsing. - * - * The textual representation can be controlled by the \a flags parameter (see - * CborPrettyFlags for more information). - * - * If no error ocurred, this function advances \a value to the next element. - * Often, concatenating the text representation of multiple elements can be - * done by appending a comma to the output stream. - * - * \sa cbor_value_to_pretty_stream(), cbor_value_to_pretty(), cbor_value_to_json_advance() - */ -CborError cbor_value_to_pretty_advance_flags(FILE *out, CborValue *value, int flags) -{ - return value_to_pretty(cbor_fprintf, out, value, flags, CBOR_PARSER_MAX_RECURSIONS); -} - /** @} */ diff --git a/src/cborpretty_stdio.c b/src/cborpretty_stdio.c new file mode 100644 index 00000000..f905e868 --- /dev/null +++ b/src/cborpretty_stdio.c @@ -0,0 +1,85 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Intel Corporation +** +** Permission is hereby granted, free of charge, to any person obtaining a copy +** of this software and associated documentation files (the "Software"), to deal +** in the Software without restriction, including without limitation the rights +** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +** copies of the Software, and to permit persons to whom the Software is +** furnished to do so, subject to the following conditions: +** +** The above copyright notice and this permission notice shall be included in +** all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +** THE SOFTWARE. +** +****************************************************************************/ + +#include "cbor.h" +#include +#include + +static CborError cbor_fprintf(void *out, const char *fmt, ...) +{ + int n; + + va_list list; + va_start(list, fmt); + n = vfprintf((FILE *)out, fmt, list); + va_end(list); + + return n < 0 ? CborErrorIO : CborNoError; +} + +/** + * \fn CborError cbor_value_to_pretty(FILE *out, const CborValue *value) + * + * Converts the current CBOR type pointed by \a value to its textual + * representation and writes it to the \a out stream. If an error occurs, this + * function returns an error code similar to CborParsing. + * + * \sa cbor_value_to_pretty_advance(), cbor_value_to_json_advance() + */ + +/** + * Converts the current CBOR type pointed by \a value to its textual + * representation and writes it to the \a out stream. If an error occurs, this + * function returns an error code similar to CborParsing. + * + * If no error ocurred, this function advances \a value to the next element. + * Often, concatenating the text representation of multiple elements can be + * done by appending a comma to the output stream. + * + * \sa cbor_value_to_pretty(), cbor_value_to_pretty_stream(), cbor_value_to_json_advance() + */ +CborError cbor_value_to_pretty_advance(FILE *out, CborValue *value) +{ + return cbor_value_to_pretty_stream(cbor_fprintf, out, value, CborPrettyDefaultFlags); +} + +/** + * Converts the current CBOR type pointed by \a value to its textual + * representation and writes it to the \a out stream. If an error occurs, this + * function returns an error code similar to CborParsing. + * + * The textual representation can be controlled by the \a flags parameter (see + * CborPrettyFlags for more information). + * + * If no error ocurred, this function advances \a value to the next element. + * Often, concatenating the text representation of multiple elements can be + * done by appending a comma to the output stream. + * + * \sa cbor_value_to_pretty_stream(), cbor_value_to_pretty(), cbor_value_to_json_advance() + */ +CborError cbor_value_to_pretty_advance_flags(FILE *out, CborValue *value, int flags) +{ + return cbor_value_to_pretty_stream(cbor_fprintf, out, value, flags); +} +