Files
ArduinoJson/include/ArduinoJson/Serialization/StaticStringBuilder.hpp

38 lines
713 B
C++
Raw Normal View History

// Copyright Benoit Blanchon 2014-2016
2014-10-23 23:39:22 +02:00
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
2014-10-16 00:11:23 +02:00
#pragma once
#include "../Print.hpp"
2014-10-16 00:11:23 +02:00
2014-10-23 19:54:00 +02:00
namespace ArduinoJson {
namespace Internals {
2014-11-06 14:48:14 +01:00
// A Print implementation that allows to write in a char[]
class StaticStringBuilder : public Print {
public:
StaticStringBuilder(char *buf, size_t size)
2014-10-23 19:54:00 +02:00
: buffer(buf), capacity(size - 1), length(0) {
buffer[0] = '\0';
2014-10-23 19:54:00 +02:00
}
2014-10-16 00:11:23 +02:00
virtual size_t write(uint8_t c) {
if (length >= capacity) return 0;
buffer[length++] = c;
buffer[length] = '\0';
return 1;
}
2014-10-16 00:11:23 +02:00
private:
2014-10-23 19:54:00 +02:00
char *buffer;
size_t capacity;
size_t length;
2014-10-23 19:54:00 +02:00
};
}
2014-10-23 23:45:36 +02:00
}