AceUtils
0.1
Small and light Arduino utilties and libraries.
src
print_string
PrintString.h
1
/*
2
MIT License
3
4
Copyright (c) 2020 Brian T. Park
5
6
Permission is hereby granted, free of charge, to any person obtaining a copy
7
of this software and associated documentation files (the "Software"), to deal
8
in the Software without restriction, including without limitation the rights
9
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
copies of the Software, and to permit persons to whom the Software is
11
furnished to do so, subject to the following conditions:
12
13
The above copyright notice and this permission notice shall be included in all
14
copies or substantial portions of the Software.
15
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
SOFTWARE.
23
*/
24
25
#ifndef PRINT_STRING_PRINT_STRING_H
26
#define PRINT_STRING_PRINT_STRING_H
27
28
#include <stddef.h>
// size_t
29
#include <Print.h>
30
31
namespace
print_string {
32
63
class
PrintStringBase
:
public
Print {
64
public
:
65
size_t
write(uint8_t c)
override
{
66
if
(index_ < size_ - 1) {
67
buf_[index_] = c;
68
index_++;
69
return
1;
70
}
else
{
71
return
0;
72
}
73
}
74
75
size_t
write(
const
uint8_t *buf,
size_t
size)
override
{
76
if
(buf ==
nullptr
)
return
0;
77
78
size_t
n = 0;
79
while
(size-- > 0) {
80
size_t
ret = write(*buf++);
81
if
(ret == 0)
break
;
82
n++;
83
}
84
return
n;
85
}
86
87
// ESP32 version of Print class does not define a virtual flush() method so
88
// we can't use the 'override' keyword.
89
#ifdef ESP32
90
void
flush() {
91
index_ = 0;
92
}
93
#else
94
void
flush()
override
{
95
index_ = 0;
96
}
97
#endif
98
104
const
char
*
getCstr
()
const
{
105
buf_[index_] =
'\0'
;
106
return
buf_;
107
}
108
110
size_t
length
()
const
{
111
return
index_;
112
}
113
114
protected
:
115
PrintStringBase
(uint16_t size,
char
* buf):
116
size_(size),
117
buf_(buf) {}
118
119
private
:
120
uint16_t
const
size_;
121
uint16_t index_ = 0;
122
123
// This is the pointer to actualBuf_ which is defined in the subclasses.
124
// Instead of storing it (and taking up precious RAM), maybe there's a way
125
// to calculate this pointer by simply extending the pointer from the last
126
// element of this object (i.e. &index_), and thereby saving some memory.
127
// But I am not convinced that I have the knowledge do that properly
128
// without triggering UB (undefined behavior) in the C++ language. Do I
129
// define buf_[0] here and will it point exactly where actualBuf_[] is
130
// allocated? Or do I use [&PrintStringBase + sizeof(PrintStringBase)] to
131
// calculate the pointer to actualBuf_. I just don't know what's actually
132
// allowed in the language spec versus something that works by pure luck on
133
// a particular microcontroller and compiler. So I'll pay the cost of the 2
134
// extra bytes (8-bit) or 4 extra bytes (32-bit processors) of RAM and
135
// store the pointer to actualBuf_ explicitly here in the base class.
136
char
*
const
buf_;
137
};
138
203
template
<u
int
16_t SIZE>
204
class
PrintString
:
public
PrintStringBase
{
205
public
:
206
PrintString
():
PrintStringBase
(SIZE, actualBuf_) {}
207
208
private
:
209
char
actualBuf_[SIZE];
210
};
211
212
}
213
214
#endif
print_string::PrintStringBase
Base class for all template instances of the PrintString<SIZE> class.
Definition:
PrintString.h:63
print_string::PrintStringBase::length
size_t length() const
Return the length of the internal c-string buffer.
Definition:
PrintString.h:110
print_string::PrintString
An implementation of Print that writes to an in-memory buffer supporting strings less than 65535 in l...
Definition:
PrintString.h:204
print_string::PrintStringBase::getCstr
const char * getCstr() const
Return the NUL terminated c-string buffer.
Definition:
PrintString.h:104
Generated by
1.8.17