diff options
author | Vincent Dupont <vincent@otakeys.com> | 2018-02-26 16:28:27 +0100 |
---|---|---|
committer | Vincent Dupont <vincent@otakeys.com> | 2018-02-26 16:28:27 +0100 |
commit | 0301ef69c312cc663a6d3014c8831cfb2cc790ff (patch) | |
tree | e3062a2a312a52df693ef2c6260147773116c0b1 | |
parent | 7bb482d7032dae3588ecd13f270d525b38e9bb21 (diff) | |
download | cn-cbor-0301ef69c312cc663a6d3014c8831cfb2cc790ff.tar.gz |
cn-create: add cn_cbor_float_create
-rw-r--r-- | include/cn-cbor/cn-cbor.h | 16 | ||||
-rw-r--r-- | src/cn-create.c | 13 | ||||
-rw-r--r-- | src/cn-encoder.c | 5 |
3 files changed, 34 insertions, 0 deletions
diff --git a/include/cn-cbor/cn-cbor.h b/include/cn-cbor/cn-cbor.h index cde9dfc..187a55c 100644 --- a/include/cn-cbor/cn-cbor.h +++ b/include/cn-cbor/cn-cbor.h @@ -53,6 +53,8 @@ typedef enum cn_cbor_type { CN_CBOR_SIMPLE, /** Doubles, floats, and half-floats */ CN_CBOR_DOUBLE, + /** Floats, and half-floats */ + CN_CBOR_FLOAT, /** An error has occurred */ CN_CBOR_INVALID } cn_cbor_type; @@ -92,6 +94,8 @@ typedef struct cn_cbor { unsigned long uint; /** CN_CBOR_DOUBLE */ double dbl; + /** CN_CBOR_FLOAT */ + float f; /** for use during parsing */ unsigned long count; } v; /* TBD: optimize immediate */ @@ -327,6 +331,18 @@ cn_cbor* cn_cbor_int_create(int64_t value #ifndef CBOR_NO_FLOAT /** + * Create a CBOR float. + * + * @param[in] value the value of the float + * @param[in] CBOR_CONTEXT Allocation context (only if USE_CBOR_CONTEXT is defined) + * @param[out] errp Error, if NULL is returned + * @return The created object, or NULL on error + */ +cn_cbor* cn_cbor_float_create(float value + CBOR_CONTEXT, + cn_cbor_errback *errp); + +/** * Create a CBOR double. * * @param[in] value the value of the double diff --git a/src/cn-create.c b/src/cn-create.c index 84a6ee9..4ddce3b 100644 --- a/src/cn-create.c +++ b/src/cn-create.c @@ -74,6 +74,19 @@ cn_cbor* cn_cbor_int_create(int64_t value } #ifndef CBOR_NO_FLOAT +cn_cbor* cn_cbor_float_create(float value + CBOR_CONTEXT, + cn_cbor_errback *errp) +{ + cn_cbor* ret; + INIT_CB(ret); + + ret->type = CN_CBOR_FLOAT; + ret->v.f = value; + + return ret; +} + cn_cbor* cn_cbor_double_create(double value CBOR_CONTEXT, cn_cbor_errback *errp) diff --git a/src/cn-encoder.c b/src/cn-encoder.c index 8593b39..d8a4d49 100644 --- a/src/cn-encoder.c +++ b/src/cn-encoder.c @@ -276,6 +276,11 @@ void _encoder_visitor(const cn_cbor *cb, int depth, void *context) CHECK(_write_double(ws, cb->v.dbl)); #endif /* CBOR_NO_FLOAT */ break; + case CN_CBOR_FLOAT: +#ifndef CBOR_NO_FLOAT + CHECK(_write_double(ws, cb->v.f)); +#endif /* CBOR_NO_FLOAT */ + break; case CN_CBOR_INVALID: ws->offset = -1; |