summaryrefslogtreecommitdiff
path: root/include/lib/ArduinoJson/StringTraits
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2018-09-17 10:02:07 +0200
committerDaniel Friesel <derf@finalrewind.org>2018-09-17 10:02:07 +0200
commit4f6253aa9fec99260b8bb7b9b2e9003f5259b600 (patch)
tree2d0a3fdd10e258ecce5fb220547b1c43b870d6d2 /include/lib/ArduinoJson/StringTraits
parent30c4f72770568749b4230a6b598e3fb87a065e91 (diff)
Import arduinojson and ubjson. Only partially working at the moment
Diffstat (limited to 'include/lib/ArduinoJson/StringTraits')
-rw-r--r--include/lib/ArduinoJson/StringTraits/ArduinoStream.hpp61
-rw-r--r--include/lib/ArduinoJson/StringTraits/CharPointer.hpp64
-rw-r--r--include/lib/ArduinoJson/StringTraits/FlashString.hpp61
-rw-r--r--include/lib/ArduinoJson/StringTraits/StdStream.hpp60
-rw-r--r--include/lib/ArduinoJson/StringTraits/StdString.hpp77
-rw-r--r--include/lib/ArduinoJson/StringTraits/StringTraits.hpp36
6 files changed, 359 insertions, 0 deletions
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 <Stream.h>
+
+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 <typename TStream>
+struct StringTraits<
+ TStream,
+ // match any type that is derived from Stream:
+ typename EnableIf<
+ IsBaseOf<Stream, typename RemoveReference<TStream>::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 <typename TChar>
+struct CharPointerTraits {
+ class Reader {
+ const TChar* _ptr;
+
+ public:
+ Reader(const TChar* ptr)
+ : _ptr(ptr ? ptr : reinterpret_cast<const TChar*>("")) {}
+
+ 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<const char*>(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 <typename Buffer>
+ static duplicate_t duplicate(const TChar* str, Buffer* buffer) {
+ if (!str) return NULL;
+ size_t size = strlen(reinterpret_cast<const char*>(str)) + 1;
+ void* dup = buffer->alloc(size);
+ if (dup != NULL) memcpy(dup, str, size);
+ return static_cast<duplicate_t>(dup);
+ }
+
+ static const bool has_append = false;
+ static const bool has_equals = true;
+ static const bool should_duplicate = !IsConst<TChar>::value;
+};
+
+// char*, unsigned char*, signed char*
+// const char*, const unsigned char*, const signed char*
+template <typename TChar>
+struct StringTraits<TChar*, typename EnableIf<IsChar<TChar>::value>::type>
+ : CharPointerTraits<TChar> {};
+} // 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<const __FlashStringHelper*, void> {
+ class Reader {
+ const char* _ptr;
+
+ public:
+ Reader(const __FlashStringHelper* ptr)
+ : _ptr(reinterpret_cast<const char*>(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<const char*>(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 <typename Buffer>
+ 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<duplicate_t>(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 <istream>
+
+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<char>(_stream.get());
+ }
+ };
+
+ static const bool has_append = false;
+ static const bool has_equals = false;
+};
+
+template <typename TStream>
+struct StringTraits<
+ TStream,
+ // match any type that is derived from std::istream:
+ typename EnableIf<IsBaseOf<
+ std::istream, typename RemoveReference<TStream>::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 <WString.h>
+#endif
+
+#if ARDUINOJSON_ENABLE_STD_STRING
+#include <string>
+#endif
+
+namespace ArduinoJson {
+namespace Internals {
+
+template <typename TString>
+struct StdStringTraits {
+ typedef const char* duplicate_t;
+
+ template <typename Buffer>
+ 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<duplicate_t>(dup);
+ }
+
+ static bool is_null(const TString& str) {
+ // Arduino's String::c_str() can return NULL
+ return !str.c_str();
+ }
+
+ struct Reader : CharPointerTraits<char>::Reader {
+ Reader(const TString& str) : CharPointerTraits<char>::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<String, void> : StdStringTraits<String> {};
+template <>
+struct StringTraits<StringSumHelper, void> : StdStringTraits<StringSumHelper> {
+};
+#endif
+
+#if ARDUINOJSON_ENABLE_STD_STRING
+template <>
+struct StringTraits<std::string, void> : StdStringTraits<std::string> {};
+#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 <string.h>
+#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 <typename TString, typename Enable = void>
+struct StringTraits {
+ static const bool has_append = false;
+ static const bool has_equals = false;
+};
+
+template <typename TString>
+struct StringTraits<const TString, void> : StringTraits<TString> {};
+
+template <typename TString>
+struct StringTraits<TString&, void> : StringTraits<TString> {};
+}
+}
+
+#include "ArduinoStream.hpp"
+#include "CharPointer.hpp"
+#include "FlashString.hpp"
+#include "StdStream.hpp"
+#include "StdString.hpp"