Minh Nguyen / ArduinoJson
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers StaticStringWriter.hpp Source File

StaticStringWriter.hpp

00001 // ArduinoJson - arduinojson.org
00002 // Copyright Benoit Blanchon 2014-2021
00003 // MIT License
00004 
00005 #pragma once
00006 
00007 #include <ArduinoJson/Namespace.hpp>
00008 
00009 namespace ARDUINOJSON_NAMESPACE {
00010 
00011 // A Print implementation that allows to write in a char[]
00012 class StaticStringWriter {
00013  public:
00014   StaticStringWriter(char *buf, size_t size) : end(buf + size - 1), p(buf) {
00015     *p = '\0';
00016   }
00017 
00018   size_t write(uint8_t c) {
00019     if (p >= end)
00020       return 0;
00021     *p++ = static_cast<char>(c);
00022     *p = '\0';
00023     return 1;
00024   }
00025 
00026   size_t write(const uint8_t *s, size_t n) {
00027     char *begin = p;
00028     while (p < end && n > 0) {
00029       *p++ = static_cast<char>(*s++);
00030       n--;
00031     }
00032     *p = '\0';
00033     return size_t(p - begin);
00034   }
00035 
00036  private:
00037   char *end;
00038   char *p;
00039 };
00040 }  // namespace ARDUINOJSON_NAMESPACE