-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathStringBuilder.cpp
164 lines (136 loc) · 3.97 KB
/
StringBuilder.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "StringBuilder.hpp"
using namespace OTF;
StringBuilder::StringBuilder(size_t maxLength) {
this->maxLength = maxLength;
buffer = new char[maxLength];
}
StringBuilder::~StringBuilder() {
delete buffer;
}
void StringBuilder::bprintf(const char *format, va_list args) {
// Don't do anything if the buffer already contains invalid data.
if (!valid) {
return;
}
size_t res = vsnprintf(&buffer[length], maxLength - length, format, args);
if (streaming && ((res >= maxLength) || (length + res >= maxLength))) {
// If in streaming mode flush the buffer and continue writing if the data doesn't fit.
stream_write(buffer, length, streaming);
first_message = false;
stream_flush();
clear();
res = vsnprintf(&buffer[length], maxLength - length, format, args);
}
totalLength += res;
length += res;
// The builder is invalid if the string fits perfectly in the buffer since there wouldn't be room for the null terminator.
if (length >= maxLength) {
// snprintf will not allow more than the specified number of characters to be written to the buffer, so the length will be the buffer size.
length = maxLength;
valid = false;
}
}
void StringBuilder::bprintf(const char *const format, ...) {
va_list args;
va_start(args, format);
bprintf(format, args);
va_end(args);
}
#if defined(ARDUINO)
void StringBuilder::bprintf(const __FlashStringHelper *const format, va_list args) {
bprintf((const char *) format, args);
}
void StringBuilder::bprintf(const __FlashStringHelper *const format, ...) {
va_list args;
va_start(args, format);
bprintf(format, args);
va_end(args);
}
#endif
size_t StringBuilder::_write(const char *data, size_t data_length, bool use_pgm) {
if (!valid) {
return -1;
}
size_t write_index = 0;
while (write_index < data_length) {
// Write as much data as possible to the buffer.
size_t remaining = data_length - write_index;
size_t write_length = maxLength - length - 1;
if (write_length > remaining) {
write_length = remaining;
}
// If the buffer is full, flush it and continue writing.
if (write_length == 0) {
if (streaming) {
stream_write(buffer, length, streaming);
first_message = false;
stream_flush();
clear();
} else {
// If the buffer is full and there is no stream to write to, the builder is invalid.
valid = false;
return -1;
}
} else {
// Copy the data to the buffer.
#if defined(ARDUINO)
if (use_pgm) {
memcpy_P(&buffer[length], &data[write_index], write_length);
} else {
memcpy(&buffer[length], &data[write_index], write_length);
}
#else
memcpy(&buffer[length], &data[write_index], write_length);
#endif
length += write_length;
totalLength += write_length;
write_index += write_length;
// Null-terminate the buffer.
buffer[length] = '\0';
}
}
return data_length;
}
size_t StringBuilder::write(const char *data, size_t data_length) {
return _write(data, data_length, false);
}
#if defined(ARDUINO)
size_t StringBuilder::write_P(const __FlashStringHelper *const data, size_t data_length) {
return _write((const char *) data, data_length, true);
}
#endif
void StringBuilder::enableStream(stream_write_t write, stream_flush_t flush, stream_end_t end) {
streaming = true;
first_message = true;
stream_write = write;
stream_flush = flush;
stream_end = end;
}
bool StringBuilder::end() {
if (stream_end) {
stream_write(buffer, length, streaming);
stream_end();
return true;
}
return false;
}
char *StringBuilder::toString() const {
return &buffer[0];
}
size_t StringBuilder::getLength() const {
return length;
}
bool StringBuilder::isValid() {
return valid;
}
void StringBuilder::clear() {
length = 0;
buffer[0] = '\0';
valid = true;
}
size_t StringBuilder::getMaxLength() const {
return maxLength;
}
size_t StringBuilder::getTotalLength() const {
return totalLength;
}