summaryrefslogtreecommitdiff
path: root/include/lib/ArduinoJson/Serialization/Writers/StaticStringWriter.hpp
blob: 3132b352e962b5e7a115f62cebcf6b29173460b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// ArduinoJson - https://arduinojson.org
// Copyright Benoit Blanchon 2014-2021
// MIT License

#pragma once

#include "lib/ArduinoJson/Namespace.hpp"

namespace ARDUINOJSON_NAMESPACE {

class StaticStringWriter {
 public:
  StaticStringWriter(char *buf, size_t size) : end(buf + size), p(buf) {}

  size_t write(uint8_t c) {
    if (p >= end)
      return 0;
    *p++ = static_cast<char>(c);
    return 1;
  }

  size_t write(const uint8_t *s, size_t n) {
    char *begin = p;
    while (p < end && n > 0) {
      *p++ = static_cast<char>(*s++);
      n--;
    }
    return size_t(p - begin);
  }

 private:
  char *end;
  char *p;
};
}  // namespace ARDUINOJSON_NAMESPACE