Minh Nguyen / ArduinoJson
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FlashStringAdapter.hpp Source File

FlashStringAdapter.hpp

00001 // ArduinoJson - arduinojson.org
00002 // Copyright Benoit Blanchon 2014-2021
00003 // MIT License
00004 
00005 #pragma once
00006 
00007 #include <ArduinoJson/Polyfills/pgmspace.hpp>
00008 #include <ArduinoJson/Strings/FlashStringIterator.hpp>
00009 #include <ArduinoJson/Strings/IsString.hpp>
00010 #include <ArduinoJson/Strings/StoragePolicy.hpp>
00011 
00012 namespace ARDUINOJSON_NAMESPACE {
00013 
00014 class FlashStringAdapter {
00015  public:
00016   FlashStringAdapter(const __FlashStringHelper* str) : _str(str) {}
00017 
00018   int compare(const char* other) const {
00019     if (!other && !_str)
00020       return 0;
00021     if (!_str)
00022       return -1;
00023     if (!other)
00024       return 1;
00025     return -strcmp_P(other, reinterpret_cast<const char*>(_str));
00026   }
00027 
00028   bool equals(const char* expected) const {
00029     return compare(expected) == 0;
00030   }
00031 
00032   bool isNull() const {
00033     return !_str;
00034   }
00035 
00036   void copyTo(char* p, size_t n) const {
00037     memcpy_P(p, reinterpret_cast<const char*>(_str), n);
00038   }
00039 
00040   size_t size() const {
00041     if (!_str)
00042       return 0;
00043     return strlen_P(reinterpret_cast<const char*>(_str));
00044   }
00045 
00046   FlashStringIterator begin() const {
00047     return FlashStringIterator(_str);
00048   }
00049 
00050   typedef storage_policies::store_by_copy storage_policy;
00051 
00052  private:
00053   const __FlashStringHelper* _str;
00054 };
00055 
00056 inline FlashStringAdapter adaptString(const __FlashStringHelper* str) {
00057   return FlashStringAdapter(str);
00058 }
00059 
00060 template <>
00061 struct IsString<const __FlashStringHelper*> : true_type {};
00062 }  // namespace ARDUINOJSON_NAMESPACE