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 --- .../lib/ArduinoJson/StringTraits/ArduinoStream.hpp | 61 +++++++++++++++++ .../lib/ArduinoJson/StringTraits/CharPointer.hpp | 64 ++++++++++++++++++ .../lib/ArduinoJson/StringTraits/FlashString.hpp | 61 +++++++++++++++++ include/lib/ArduinoJson/StringTraits/StdStream.hpp | 60 +++++++++++++++++ include/lib/ArduinoJson/StringTraits/StdString.hpp | 77 ++++++++++++++++++++++ .../lib/ArduinoJson/StringTraits/StringTraits.hpp | 36 ++++++++++ 6 files changed, 359 insertions(+) create mode 100644 include/lib/ArduinoJson/StringTraits/ArduinoStream.hpp create mode 100644 include/lib/ArduinoJson/StringTraits/CharPointer.hpp create mode 100644 include/lib/ArduinoJson/StringTraits/FlashString.hpp create mode 100644 include/lib/ArduinoJson/StringTraits/StdStream.hpp create mode 100644 include/lib/ArduinoJson/StringTraits/StdString.hpp create mode 100644 include/lib/ArduinoJson/StringTraits/StringTraits.hpp (limited to 'include/lib/ArduinoJson/StringTraits') diff --git a/include/lib/ArduinoJson/StringTraits/ArduinoStream.hpp b/include/lib/ArduinoJson/StringTraits/ArduinoStream.hpp new file mode 100644 index 0000000..5db0852 --- /dev/null +++ b/include/lib/ArduinoJson/StringTraits/ArduinoStream.hpp @@ -0,0 +1,61 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#if ARDUINOJSON_ENABLE_ARDUINO_STREAM + +#include + +namespace ArduinoJson { +namespace Internals { + +struct ArduinoStreamTraits { + class Reader { + Stream& _stream; + char _current, _next; + + public: + Reader(Stream& stream) : _stream(stream), _current(0), _next(0) {} + + void move() { + _current = _next; + _next = 0; + } + + char current() { + if (!_current) _current = read(); + return _current; + } + + char next() { + // assumes that current() has been called + if (!_next) _next = read(); + return _next; + } + + private: + char read() { + // don't use _stream.read() as it ignores the timeout + char c = 0; + _stream.readBytes(&c, 1); + return c; + } + }; + + static const bool has_append = false; + static const bool has_equals = false; +}; + +template +struct StringTraits< + TStream, + // match any type that is derived from Stream: + typename EnableIf< + IsBaseOf::type>::value>::type> + : ArduinoStreamTraits {}; +} +} + +#endif diff --git a/include/lib/ArduinoJson/StringTraits/CharPointer.hpp b/include/lib/ArduinoJson/StringTraits/CharPointer.hpp new file mode 100644 index 0000000..98896cc --- /dev/null +++ b/include/lib/ArduinoJson/StringTraits/CharPointer.hpp @@ -0,0 +1,64 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +template +struct CharPointerTraits { + class Reader { + const TChar* _ptr; + + public: + Reader(const TChar* ptr) + : _ptr(ptr ? ptr : reinterpret_cast("")) {} + + void move() { + ++_ptr; + } + + char current() const { + return char(_ptr[0]); + } + + char next() const { + return char(_ptr[1]); + } + }; + + static bool equals(const TChar* str, const char* expected) { + const char* actual = reinterpret_cast(str); + if (!actual || !expected) return actual == expected; + return strcmp(actual, expected) == 0; + } + + static bool is_null(const TChar* str) { + return !str; + } + + typedef const char* duplicate_t; + + template + static duplicate_t duplicate(const TChar* str, Buffer* buffer) { + if (!str) return NULL; + size_t size = strlen(reinterpret_cast(str)) + 1; + void* dup = buffer->alloc(size); + if (dup != NULL) memcpy(dup, str, size); + return static_cast(dup); + } + + static const bool has_append = false; + static const bool has_equals = true; + static const bool should_duplicate = !IsConst::value; +}; + +// char*, unsigned char*, signed char* +// const char*, const unsigned char*, const signed char* +template +struct StringTraits::value>::type> + : CharPointerTraits {}; +} // namespace Internals +} // namespace ArduinoJson diff --git a/include/lib/ArduinoJson/StringTraits/FlashString.hpp b/include/lib/ArduinoJson/StringTraits/FlashString.hpp new file mode 100644 index 0000000..0701b9b --- /dev/null +++ b/include/lib/ArduinoJson/StringTraits/FlashString.hpp @@ -0,0 +1,61 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#if ARDUINOJSON_ENABLE_PROGMEM + +namespace ArduinoJson { +namespace Internals { +template <> +struct StringTraits { + class Reader { + const char* _ptr; + + public: + Reader(const __FlashStringHelper* ptr) + : _ptr(reinterpret_cast(ptr)) {} + + void move() { + _ptr++; + } + + char current() const { + return pgm_read_byte_near(_ptr); + } + + char next() const { + return pgm_read_byte_near(_ptr + 1); + } + }; + + static bool equals(const __FlashStringHelper* str, const char* expected) { + const char* actual = reinterpret_cast(str); + if (!actual || !expected) return actual == expected; + return strcmp_P(expected, actual) == 0; + } + + static bool is_null(const __FlashStringHelper* str) { + return !str; + } + + typedef const char* duplicate_t; + + template + static duplicate_t duplicate(const __FlashStringHelper* str, Buffer* buffer) { + if (!str) return NULL; + size_t size = strlen_P((const char*)str) + 1; + void* dup = buffer->alloc(size); + if (dup != NULL) memcpy_P(dup, (const char*)str, size); + return static_cast(dup); + } + + static const bool has_append = false; + static const bool has_equals = true; + static const bool should_duplicate = true; +}; +} // namespace Internals +} // namespace ArduinoJson + +#endif diff --git a/include/lib/ArduinoJson/StringTraits/StdStream.hpp b/include/lib/ArduinoJson/StringTraits/StdStream.hpp new file mode 100644 index 0000000..227c744 --- /dev/null +++ b/include/lib/ArduinoJson/StringTraits/StdStream.hpp @@ -0,0 +1,60 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#if ARDUINOJSON_ENABLE_STD_STREAM + +#include + +namespace ArduinoJson { +namespace Internals { + +struct StdStreamTraits { + class Reader { + std::istream& _stream; + char _current, _next; + + public: + Reader(std::istream& stream) : _stream(stream), _current(0), _next(0) {} + + void move() { + _current = _next; + _next = 0; + } + + char current() { + if (!_current) _current = read(); + return _current; + } + + char next() { + // assumes that current() has been called + if (!_next) _next = read(); + return _next; + } + + private: + Reader& operator=(const Reader&); // Visual Studio C4512 + + char read() { + return _stream.eof() ? '\0' : static_cast(_stream.get()); + } + }; + + static const bool has_append = false; + static const bool has_equals = false; +}; + +template +struct StringTraits< + TStream, + // match any type that is derived from std::istream: + typename EnableIf::type>::value>::type> + : StdStreamTraits {}; +} +} + +#endif diff --git a/include/lib/ArduinoJson/StringTraits/StdString.hpp b/include/lib/ArduinoJson/StringTraits/StdString.hpp new file mode 100644 index 0000000..35f4461 --- /dev/null +++ b/include/lib/ArduinoJson/StringTraits/StdString.hpp @@ -0,0 +1,77 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#if ARDUINOJSON_ENABLE_STD_STRING || ARDUINOJSON_ENABLE_ARDUINO_STRING + +#if ARDUINOJSON_ENABLE_ARDUINO_STRING +#include +#endif + +#if ARDUINOJSON_ENABLE_STD_STRING +#include +#endif + +namespace ArduinoJson { +namespace Internals { + +template +struct StdStringTraits { + typedef const char* duplicate_t; + + template + static duplicate_t duplicate(const TString& str, Buffer* buffer) { + if (!str.c_str()) return NULL; // <- Arduino string can return NULL + size_t size = str.length() + 1; + void* dup = buffer->alloc(size); + if (dup != NULL) memcpy(dup, str.c_str(), size); + return static_cast(dup); + } + + static bool is_null(const TString& str) { + // Arduino's String::c_str() can return NULL + return !str.c_str(); + } + + struct Reader : CharPointerTraits::Reader { + Reader(const TString& str) : CharPointerTraits::Reader(str.c_str()) {} + }; + + static bool equals(const TString& str, const char* expected) { + // Arduino's String::c_str() can return NULL + const char* actual = str.c_str(); + if (!actual || !expected) return actual == expected; + return 0 == strcmp(actual, expected); + } + + static void append(TString& str, char c) { + str += c; + } + + static void append(TString& str, const char* s) { + str += s; + } + + static const bool has_append = true; + static const bool has_equals = true; + static const bool should_duplicate = true; +}; + +#if ARDUINOJSON_ENABLE_ARDUINO_STRING +template <> +struct StringTraits : StdStringTraits {}; +template <> +struct StringTraits : StdStringTraits { +}; +#endif + +#if ARDUINOJSON_ENABLE_STD_STRING +template <> +struct StringTraits : StdStringTraits {}; +#endif +} // namespace Internals +} // namespace ArduinoJson + +#endif diff --git a/include/lib/ArduinoJson/StringTraits/StringTraits.hpp b/include/lib/ArduinoJson/StringTraits/StringTraits.hpp new file mode 100644 index 0000000..dd5694b --- /dev/null +++ b/include/lib/ArduinoJson/StringTraits/StringTraits.hpp @@ -0,0 +1,36 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include +#include "../Configuration.hpp" +#include "../TypeTraits/EnableIf.hpp" +#include "../TypeTraits/IsBaseOf.hpp" +#include "../TypeTraits/IsChar.hpp" +#include "../TypeTraits/IsConst.hpp" +#include "../TypeTraits/RemoveReference.hpp" + +namespace ArduinoJson { +namespace Internals { + +template +struct StringTraits { + static const bool has_append = false; + static const bool has_equals = false; +}; + +template +struct StringTraits : StringTraits {}; + +template +struct StringTraits : StringTraits {}; +} +} + +#include "ArduinoStream.hpp" +#include "CharPointer.hpp" +#include "FlashString.hpp" +#include "StdStream.hpp" +#include "StdString.hpp" -- cgit v1.2.3