AceCommon  1.6.1
Arduino library for low-level common functions and features with no external dependencies
PrintStr.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 ACE_COMMON_PRINT_STR_H
26 #define ACE_COMMON_PRINT_STR_H
27 
28 #include <stddef.h> // size_t
29 #include <Print.h>
30 
31 namespace ace_common {
32 
63 class PrintStrBase: public Print {
64  public:
66  size_t write(uint8_t c) override {
67  if (index_ < size_ - 1) {
68  buf_[index_] = c;
69  index_++;
70  return 1;
71  } else {
72  return 0;
73  }
74  }
75 
77  size_t write(const uint8_t *buf, size_t size) override {
78  if (buf == nullptr) return 0;
79 
80  size_t n = 0;
81  while (size-- > 0) {
82  size_t ret = write(*buf++);
83  if (ret == 0) break;
84  n++;
85  }
86  return n;
87  }
88 
115  #if defined(ARDUINO_AVR_ATTINY1634) \
116  || defined(ARDUINO_AVR_ATTINY828) \
117  || defined(ARDUINO_AVR_ATTINYX313) \
118  || defined(ARDUINO_AVR_ATTINYX4) \
119  || defined(ARDUINO_AVR_ATTINYX41) \
120  || defined(ARDUINO_AVR_ATTINYX43) \
121  || defined(ARDUINO_AVR_ATTINYX5) \
122  || defined(ARDUINO_AVR_ATTINYX61) \
123  || defined(ARDUINO_AVR_ATTINYX7) \
124  || defined(ARDUINO_AVR_ATTINYX8)
125  void flush() {
126  #else
127  void flush() override {
128  #endif
129  index_ = 0;
130  }
131 
142  const char* cstr() const {
143  buf_[index_] = '\0';
144  return buf_;
145  }
146 
148  const char* getCstr() const { return cstr(); }
149 
154  size_t length() const { return index_; }
155 
156  protected:
163  PrintStrBase(uint16_t size, char* buf):
164  size_(size),
165  buf_(buf) {}
166 
167  private:
168  // Disable copy constructor and assignment operator
169  PrintStrBase(const PrintStrBase&) = delete;
170  PrintStrBase& operator=(const PrintStrBase&) = delete;
171 
172  // These member variables are declared together for more efficient packing
173  // on 32-bit processors.
174  uint16_t const size_;
175  uint16_t index_ = 0;
176 
177  protected:
199  char* const buf_;
200 };
201 
266 template <uint16_t SIZE>
267 class PrintStr: public PrintStrBase {
268  public:
269  PrintStr(): PrintStrBase(SIZE, actualBuf_) {}
270 
271  private:
272  char actualBuf_[SIZE];
273 };
274 
294 class PrintStrN: public PrintStrBase {
295  public:
297  PrintStrN(uint16_t size):
298  PrintStrBase(size, new char[size]) {}
299 
305  delete[] buf_;
306  }
307 };
308 
309 }
310 
311 #endif
Base class for all template instances of the PrintStr<SIZE> class.
Definition: PrintStr.h:63
char *const buf_
This is the pointer to the character array buffer.
Definition: PrintStr.h:199
PrintStrBase(uint16_t size, char *buf)
Constructor.
Definition: PrintStr.h:163
const char * cstr() const
Return the NUL terminated c-string buffer.
Definition: PrintStr.h:142
size_t write(uint8_t c) override
Write a single character into the internal buffer.
Definition: PrintStr.h:66
const char * getCstr() const
Backwards compatible version of cstr().
Definition: PrintStr.h:148
size_t write(const uint8_t *buf, size_t size) override
Write the buf string of size into the internal buffer.
Definition: PrintStr.h:77
void flush() override
Clear the internal buffer.
Definition: PrintStr.h:127
size_t length() const
Return the length of the internal c-string buffer, not including the NUL terminator.
Definition: PrintStr.h:154
An alternate implementation of PrintStr that allocates the character array in the heap,...
Definition: PrintStr.h:294
PrintStrN(uint16_t size)
Create an instance with an internal buffer of size on the heap.
Definition: PrintStr.h:297
~PrintStrN()
Delete the internal buffer on the heap.
Definition: PrintStr.h:304
An implementation of Print that writes to an in-memory buffer supporting strings less than 65535 in l...
Definition: PrintStr.h:267