NameBuffer.h
Go to the documentation of this file.
1 /****
2  * NameBuffer.h
3  *
4  * Created on: 31 Aug 2018
5  *
6  * Copyright 2019 mikee47 <mike@sillyhouse.net>
7  *
8  * This file is part of the IFS Library
9  *
10  * This library is free software: you can redistribute it and/or modify it under the terms of the
11  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
12  *
13  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15  * See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with this library.
18  * If not, see <https://www.gnu.org/licenses/>.
19  *
20  ****/
21 
22 #pragma once
23 
24 #include "Types.h"
25 #include "Error.h"
26 
27 namespace IFS
28 {
44 struct NameBuffer {
45  char* buffer{nullptr};
46  uint16_t size{0};
47  uint16_t length{0};
48 
50  {
51  }
52 
53  NameBuffer(char* buffer, uint16_t size, uint16_t length = 0) : buffer(buffer), size(size), length(length)
54  {
55  }
56 
61  {
62  }
63 
64  operator const char*() const
65  {
66  return buffer;
67  }
68 
69  char* begin()
70  {
71  return buffer;
72  }
73 
74  const char* c_str() const
75  {
76  return buffer;
77  }
78 
79  explicit operator String() const
80  {
81  return String(buffer, length);
82  }
83 
84  bool operator==(const char* other)
85  {
86  return other != nullptr && strncmp(buffer, other, length) == 0;
87  }
88 
95  int copy(const char* src, uint16_t srclen)
96  {
97  length = srclen;
98  uint16_t copylen = std::min(srclen, size);
99  if(copylen != 0) {
100  memcpy(buffer, src, copylen);
101  }
102  terminate();
103  return (copylen == srclen) ? FS_OK : Error::BufferTooSmall;
104  }
105 
106  int copy(const char* src)
107  {
108  return copy(src, strlen(src));
109  }
110 
111  int copy(const NameBuffer& name)
112  {
113  return copy(name.buffer, name.length);
114  }
115 
121  int addSep()
122  {
123  // No separator required if path is empty
124  if(length == 0) {
125  return FS_OK;
126  }
127 
128  if(length + 1 < size) {
129  buffer[length++] = '/';
130  buffer[length] = '\0';
131  return FS_OK;
132  }
133 
134  ++length;
135  return Error::BufferTooSmall;
136  }
137 
144  char* endptr()
145  {
146  return buffer + length;
147  }
148 
154  uint16_t space()
155  {
156  return (length < size) ? size - length : 0;
157  }
158 
162  void terminate()
163  {
164  if(length < size) {
165  buffer[length] = '\0';
166  } else if(size != 0) {
167  buffer[size - 1] = '\0';
168  }
169  }
170 
175  bool overflow() const
176  {
177  return length >= size;
178  }
179 
180  bool endsWith(const char* suffix) const
181  {
182  if(suffix == nullptr) {
183  return false;
184  }
185  auto suffix_len = strlen(suffix);
186  if(length < suffix_len || suffix_len == 0) {
187  return false;
188  }
189  return memcmp(&buffer[length - suffix_len], suffix, suffix_len) == 0;
190  }
191 };
192 
196 struct FileNameBuffer : public NameBuffer {
197 public:
198  FileNameBuffer() : NameBuffer(buffer, sizeof(buffer))
199  {
200  }
201 
202 private:
203  // maximum size for a byte-length string, + 1 char for nul terminator
204  char buffer[256];
205 };
206 
207 } // namespace IFS
The String class.
Definition: WString.h:137
constexpr ErrorCode FS_OK
Definition: Components/IFS/src/include/IFS/Error.h:130
Definition: DirectoryTemplate.h:37
a quick'n'dirty name buffer with maximum path allocation
Definition: NameBuffer.h:196
FileNameBuffer()
Definition: NameBuffer.h:198
defines a 'safe' name buffer
Definition: NameBuffer.h:44
uint16_t space()
get the number of free characters available
Definition: NameBuffer.h:154
int copy(const NameBuffer &name)
Definition: NameBuffer.h:111
bool operator==(const char *other)
Definition: NameBuffer.h:84
char * endptr()
get a pointer to the next write position
Definition: NameBuffer.h:144
uint16_t length
OUT: length of name.
Definition: NameBuffer.h:47
int copy(const char *src, uint16_t srclen)
copies text from a source buffer into a name buffer
Definition: NameBuffer.h:95
char * begin()
Definition: NameBuffer.h:69
NameBuffer(char *buffer, uint16_t size, uint16_t length=0)
Definition: NameBuffer.h:53
int copy(const char *src)
Definition: NameBuffer.h:106
bool endsWith(const char *suffix) const
Definition: NameBuffer.h:180
NameBuffer(String &s)
Make a NameBuffer point to contents of a String.
Definition: NameBuffer.h:60
const char * c_str() const
Definition: NameBuffer.h:74
bool overflow() const
determine if name buffer overflowed
Definition: NameBuffer.h:175
char * buffer
Buffer to store name.
Definition: NameBuffer.h:45
void terminate()
ensure the buffer has a nul terminator, even if it means overwriting content
Definition: NameBuffer.h:162
uint16_t size
IN: Size of buffer.
Definition: NameBuffer.h:46
int addSep()
When building file paths this method simplified appending separators.
Definition: NameBuffer.h:121
NameBuffer()
Definition: NameBuffer.h:49