Arduino KVComm  master
Key-Value pair communication library for Arduino
KV_Iterator.cpp
Go to the documentation of this file.
1 #ifdef ARDUINO
2 
3 #include <KVComm/include/KVComm/KV_Helpers.hpp> // nextWord, roundUpToWordSizeMultiple
5 
6 #include <AH/STL/algorithm> // find_if
7 #include <string.h> // strlen
8 
9 #else
10 
11 #include <KVComm/KV_Helpers.hpp> // nextWord, roundUpToWordSizeMultiple
12 #include <KVComm/KV_Iterator.hpp>
13 
14 #include <algorithm> // find_if
15 #include <cstring> // strlen
16 
17 #endif
18 
19 KV_Iterator::iterator::iterator() : kv(nullptr), remainingBufferLength(0) {}
20 
21 KV_Iterator::iterator::iterator(const uint8_t *buffer, size_t length)
22  : kv(buffer), remainingBufferLength(length) {
23  checkLength();
24 }
25 
27  size_t totalLength = 4 + nextWord(kv.getIDLength()) +
28  roundUpToWordSizeMultiple(kv.getDataLength());
29  remainingBufferLength -= totalLength;
30  kv = kv.getBuffer() + totalLength;
31  checkLength();
32  return *this;
33 }
34 
36 operator!=(const KV_Iterator::iterator &other) const {
37  return this->kv.getBuffer() != other.kv.getBuffer();
38 }
39 
40 #if 0
42 operator==(const KV_Iterator::iterator &other) const {
43  return this->kv.getBuffer() == other.kv.getBuffer();
44 }
45 #endif
46 
48  if (remainingBufferLength == 0) {
49  kv = nullptr;
50  } else if (!kv || kv.getIDLength() == 0) {
51  remainingBufferLength = 0;
52  kv = nullptr;
53  }
54 }
55 
56 #if !defined(ARDUINO) || defined(DOXYGEN)
57 std::string KV_Iterator::KV::getString() const {
58  if (!checkType<char>())
59  return nullptr; // LCOV_EXCL_LINE
60  return std::string(getData(), getData() + getDataLength() - 1);
61  // -1 because getDataLength() includes null terminator
62 }
63 #else
64 String KV_Iterator::KV::getString() const {
65  if (!checkType<char>())
66  return static_cast<const char *>(nullptr); // LCOV_EXCL_LINE
67  struct S : public String {
68  using String::copy;
69  } s;
70  s.copy(reinterpret_cast<const char *>(getData()), getDataLength() - 1);
71  return s;
72 }
73 #endif
74 
75 KV_Iterator::iterator KV_Iterator::find(const char *key) const {
76  return std::find_if(begin(), end(), [key](KV_Iterator::KV kv) {
77  return strcmp(kv.getID(), key) == 0;
78  });
79 }
KV_Iterator::find
iterator find(const char *key) const
Find the entry with the given key (iterates over entire dictionary).
Definition: KV_Iterator.cpp:75
KV_Iterator::iterator::kv
KV kv
Definition: KV_Iterator.hpp:305
KV_Iterator::KV
Definition: KV_Iterator.hpp:47
KV_Iterator::iterator::checkLength
void checkLength()
Definition: KV_Iterator.cpp:47
KV_Iterator::KV::getString
std::string getString() const
Get the character array as an std::string.
Definition: KV_Iterator.cpp:57
nextWord
size_t nextWord(size_t i)
Get the offset of the next 4-byte word.
Definition: KV_Helpers.hpp:18
KV_Iterator.hpp
Iterating over a dictionary generated by the KV_Builder, used for parsing and for checking if a key i...
KV_Iterator::iterator
Definition: KV_Iterator.hpp:268
KV_Iterator::iterator::operator++
iterator & operator++()
Advance the iterator.
Definition: KV_Iterator.cpp:26
KV_Iterator::KV::getBuffer
const uint8_t * getBuffer() const
Get a pointer to the beginning of the current element.
Definition: KV_Iterator.hpp:60
KV_Iterator::buffer
const uint8_t * buffer
Definition: KV_Iterator.hpp:318
KV_Iterator::end
static iterator end()
End/sentinel iterator.
Definition: KV_Iterator.hpp:312
roundUpToWordSizeMultiple
size_t roundUpToWordSizeMultiple(size_t i)
Round up a size to a multiple of 4-byte words.
Definition: KV_Helpers.hpp:24
KV_Iterator::iterator::iterator
iterator()
Default constructor. Used as "end" iterator (sentinel).
Definition: KV_Iterator.cpp:19
KV_Iterator::KV::getID
const char * getID() const
Get the identifier / key of the current element.
Definition: KV_Iterator.hpp:62
KV_Iterator::iterator::operator!=
bool operator!=(const iterator &other) const
Compare the iterator to the "end" iterator.
Definition: KV_Iterator.cpp:36
KV_Helpers.hpp
Helpers for address manipulation used to layout dictionary entries in memory.
AH::operator==
bool operator==(ArraySlice< T1, N1, Reverse1, Const1 > a, ArraySlice< T2, N2, Reverse2, Const2 > b)
Slice == Slice.
Definition: Array.hpp:307
KV_Iterator::begin
iterator begin() const
Iterator to the first key-value entry of the dictionary.
Definition: KV_Iterator.hpp:310