Skip to content

Commit

Permalink
Pretty: move the two functions taking FILE* to a separate file
Browse files Browse the repository at this point in the history
This now allows the cborpretty.c source to be compiled even in C
freestanding environments.

Signed-off-by: Thiago Macieira <[email protected]>
  • Loading branch information
thiagomacieira committed Jan 2, 2018
1 parent ee63f79 commit f39dcb8
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 63 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
#
Expand Down
2 changes: 2 additions & 0 deletions Makefile.nmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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
Expand Down
7 changes: 3 additions & 4 deletions src/cbor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -598,14 +595,16 @@ 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)
{
CborValue copy = *value;
return cbor_value_to_pretty_advance_flags(out, &copy, CborPrettyDefaultFlags);
}

#endif /* __STDC_HOSTED__ check */

#ifdef __cplusplus
Expand Down
59 changes: 0 additions & 59 deletions src/cborpretty.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
#include <float.h>
#include <inttypes.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

/**
Expand Down Expand Up @@ -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, "<nesting too deep, recursion stopped>");
Expand Down Expand Up @@ -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);
}

/** @} */
85 changes: 85 additions & 0 deletions src/cborpretty_stdio.c
Original file line number Diff line number Diff line change
@@ -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 <stdarg.h>
#include <stdio.h>

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);
}

0 comments on commit f39dcb8

Please sign in to comment.