AceUtils  0.2
Useful Arduino utilties which are too small for separate libraries.
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 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_str {
32 
63 class PrintStrBase: 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  PrintStrBase(uint16_t size, char* buf):
116  size_(size),
117  buf_(buf) {}
118 
119  private:
120  // Disable copy constructor and assignment operator
121  PrintStrBase(const PrintStrBase&) = delete;
122  PrintStrBase& operator=(const PrintStrBase&) = delete;
123 
124  // These member variables are declared together for more efficient packing
125  // on 32-bit processors.
126  uint16_t const size_;
127  uint16_t index_ = 0;
128 
129  protected:
151  char* const buf_;
152 };
153 
218 template <uint16_t SIZE>
219 class PrintStr: public PrintStrBase {
220  public:
221  PrintStr(): PrintStrBase(SIZE, actualBuf_) {}
222 
223  private:
224  char actualBuf_[SIZE];
225 };
226 
246 class PrintStrN: public PrintStrBase {
247  public:
248  PrintStrN(uint16_t size):
249  PrintStrBase(size, new char[size]) {}
250 
251  ~PrintStrN() {
252  delete[] buf_;
253  }
254 };
255 
256 }
257 
258 #endif
print_str::PrintStrN
An alternate implementation of PrintStr that allocates the character array in the heap,...
Definition: PrintStr.h:246
print_str::PrintStrBase::buf_
char *const buf_
This is the pointer to the character array buffer.
Definition: PrintStr.h:151
print_str::PrintStrBase::length
size_t length() const
Return the length of the internal c-string buffer.
Definition: PrintStr.h:110
print_str::PrintStr
An implementation of Print that writes to an in-memory buffer supporting strings less than 65535 in l...
Definition: PrintStr.h:219
print_str::PrintStrBase
Base class for all template instances of the PrintStr<SIZE> class.
Definition: PrintStr.h:63
print_str::PrintStrBase::getCstr
const char * getCstr() const
Return the NUL terminated c-string buffer.
Definition: PrintStr.h:104