From 4f6253aa9fec99260b8bb7b9b2e9003f5259b600 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Mon, 17 Sep 2018 10:02:07 +0200 Subject: Import arduinojson and ubjson. Only partially working at the moment --- include/lib/ArduinoJson/Polyfills/attributes.hpp | 29 +++++++ include/lib/ArduinoJson/Polyfills/ctype.hpp | 18 +++++ include/lib/ArduinoJson/Polyfills/isFloat.hpp | 38 +++++++++ include/lib/ArduinoJson/Polyfills/isInteger.hpp | 19 +++++ include/lib/ArduinoJson/Polyfills/math.hpp | 19 +++++ include/lib/ArduinoJson/Polyfills/parseFloat.hpp | 90 ++++++++++++++++++++++ include/lib/ArduinoJson/Polyfills/parseInteger.hpp | 41 ++++++++++ 7 files changed, 254 insertions(+) create mode 100644 include/lib/ArduinoJson/Polyfills/attributes.hpp create mode 100644 include/lib/ArduinoJson/Polyfills/ctype.hpp create mode 100644 include/lib/ArduinoJson/Polyfills/isFloat.hpp create mode 100644 include/lib/ArduinoJson/Polyfills/isInteger.hpp create mode 100644 include/lib/ArduinoJson/Polyfills/math.hpp create mode 100644 include/lib/ArduinoJson/Polyfills/parseFloat.hpp create mode 100644 include/lib/ArduinoJson/Polyfills/parseInteger.hpp (limited to 'include/lib/ArduinoJson/Polyfills') diff --git a/include/lib/ArduinoJson/Polyfills/attributes.hpp b/include/lib/ArduinoJson/Polyfills/attributes.hpp new file mode 100644 index 0000000..b49091d --- /dev/null +++ b/include/lib/ArduinoJson/Polyfills/attributes.hpp @@ -0,0 +1,29 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#ifdef _MSC_VER // Visual Studio + +#define FORCE_INLINE // __forceinline causes C4714 when returning std::string +#define NO_INLINE __declspec(noinline) +#define DEPRECATED(msg) __declspec(deprecated(msg)) + +#elif defined(__GNUC__) // GCC or Clang + +#define FORCE_INLINE __attribute__((always_inline)) +#define NO_INLINE __attribute__((noinline)) +#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) +#define DEPRECATED(msg) __attribute__((deprecated(msg))) +#else +#define DEPRECATED(msg) __attribute__((deprecated)) +#endif + +#else // Other compilers + +#define FORCE_INLINE +#define NO_INLINE +#define DEPRECATED(msg) + +#endif diff --git a/include/lib/ArduinoJson/Polyfills/ctype.hpp b/include/lib/ArduinoJson/Polyfills/ctype.hpp new file mode 100644 index 0000000..2d52703 --- /dev/null +++ b/include/lib/ArduinoJson/Polyfills/ctype.hpp @@ -0,0 +1,18 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +inline bool isdigit(char c) { + return '0' <= c && c <= '9'; +} + +inline bool issign(char c) { + return '-' == c || c == '+'; +} +} +} diff --git a/include/lib/ArduinoJson/Polyfills/isFloat.hpp b/include/lib/ArduinoJson/Polyfills/isFloat.hpp new file mode 100644 index 0000000..973b89f --- /dev/null +++ b/include/lib/ArduinoJson/Polyfills/isFloat.hpp @@ -0,0 +1,38 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include // for strcmp +#include "./ctype.hpp" + +namespace ArduinoJson { +namespace Internals { + +inline bool isFloat(const char* s) { + if (!s) return false; + + if (!strcmp(s, "NaN")) return true; + if (issign(*s)) s++; + if (!strcmp(s, "Infinity")) return true; + if (*s == '\0') return false; + + while (isdigit(*s)) s++; + + if (*s == '.') { + s++; + while (isdigit(*s)) s++; + } + + if (*s == 'e' || *s == 'E') { + s++; + if (issign(*s)) s++; + if (!isdigit(*s)) return false; + while (isdigit(*s)) s++; + } + + return *s == '\0'; +} +} +} diff --git a/include/lib/ArduinoJson/Polyfills/isInteger.hpp b/include/lib/ArduinoJson/Polyfills/isInteger.hpp new file mode 100644 index 0000000..8049079 --- /dev/null +++ b/include/lib/ArduinoJson/Polyfills/isInteger.hpp @@ -0,0 +1,19 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "./ctype.hpp" + +namespace ArduinoJson { +namespace Internals { + +inline bool isInteger(const char* s) { + if (!s || !*s) return false; + if (issign(*s)) s++; + while (isdigit(*s)) s++; + return *s == '\0'; +} +} // namespace Internals +} // namespace ArduinoJson diff --git a/include/lib/ArduinoJson/Polyfills/math.hpp b/include/lib/ArduinoJson/Polyfills/math.hpp new file mode 100644 index 0000000..48773ed --- /dev/null +++ b/include/lib/ArduinoJson/Polyfills/math.hpp @@ -0,0 +1,19 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { +template +bool isNaN(T x) { + return x != x; +} + +template +bool isInfinity(T x) { + return x != 0.0 && x * 2 == x; +} +} +} diff --git a/include/lib/ArduinoJson/Polyfills/parseFloat.hpp b/include/lib/ArduinoJson/Polyfills/parseFloat.hpp new file mode 100644 index 0000000..49b0f6f --- /dev/null +++ b/include/lib/ArduinoJson/Polyfills/parseFloat.hpp @@ -0,0 +1,90 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "../TypeTraits/FloatTraits.hpp" +#include "./ctype.hpp" +#include "./math.hpp" + +namespace ArduinoJson { +namespace Internals { + +template +inline T parseFloat(const char* s) { + typedef FloatTraits traits; + typedef typename traits::mantissa_type mantissa_t; + typedef typename traits::exponent_type exponent_t; + + if (!s) return 0; // NULL + + bool negative_result = false; + switch (*s) { + case '-': + negative_result = true; + s++; + break; + case '+': + s++; + break; + } + + if (*s == 't') return 1; // true + if (*s == 'n' || *s == 'N') return traits::nan(); + if (*s == 'i' || *s == 'I') + return negative_result ? -traits::inf() : traits::inf(); + + mantissa_t mantissa = 0; + exponent_t exponent_offset = 0; + + while (isdigit(*s)) { + if (mantissa < traits::mantissa_max / 10) + mantissa = mantissa * 10 + (*s - '0'); + else + exponent_offset++; + s++; + } + + if (*s == '.') { + s++; + while (isdigit(*s)) { + if (mantissa < traits::mantissa_max / 10) { + mantissa = mantissa * 10 + (*s - '0'); + exponent_offset--; + } + s++; + } + } + + int exponent = 0; + if (*s == 'e' || *s == 'E') { + s++; + bool negative_exponent = false; + if (*s == '-') { + negative_exponent = true; + s++; + } else if (*s == '+') { + s++; + } + + while (isdigit(*s)) { + exponent = exponent * 10 + (*s - '0'); + if (exponent + exponent_offset > traits::exponent_max) { + if (negative_exponent) + return negative_result ? -0.0f : 0.0f; + else + return negative_result ? -traits::inf() : traits::inf(); + } + s++; + } + if (negative_exponent) exponent = -exponent; + } + exponent += exponent_offset; + + T result = traits::make_float(static_cast(mantissa), exponent); + + return negative_result ? -result : result; +} +} +} diff --git a/include/lib/ArduinoJson/Polyfills/parseInteger.hpp b/include/lib/ArduinoJson/Polyfills/parseInteger.hpp new file mode 100644 index 0000000..e8f1974 --- /dev/null +++ b/include/lib/ArduinoJson/Polyfills/parseInteger.hpp @@ -0,0 +1,41 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include + +#include "../Configuration.hpp" +#include "./ctype.hpp" + +namespace ArduinoJson { +namespace Internals { +template +T parseInteger(const char *s) { + if (!s) return 0; // NULL + + if (*s == 't') return 1; // "true" + + T result = 0; + bool negative_result = false; + + switch (*s) { + case '-': + negative_result = true; + s++; + break; + case '+': + s++; + break; + } + + while (isdigit(*s)) { + result = T(result * 10 + T(*s - '0')); + s++; + } + + return negative_result ? T(~result + 1) : result; +} +} +} -- cgit v1.2.3