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 
49  NameBuffer() = default;
50 
51  NameBuffer(char* buffer, uint16_t size, uint16_t length = 0) : buffer(buffer), size(size), length(length)
52  {
53  }
54 
59  {
60  }
61 
62  operator const char*() const
63  {
64  return buffer;
65  }
66 
67  char* begin()
68  {
69  return buffer;
70  }
71 
72  const char* c_str() const
73  {
74  return buffer;
75  }
76 
77  explicit operator String() const
78  {
79  return String(buffer, length);
80  }
81 
82  bool operator==(const char* other)
83  {
84  return other != nullptr && strncmp(buffer, other, length) == 0;
85  }
86 
93  int copy(const char* src, uint16_t srclen)
94  {
95  length = srclen;
96  uint16_t copylen = std::min(srclen, size);
97  if(copylen != 0) {
98  memcpy(buffer, src, copylen);
99  }
100  terminate();
101  return (copylen == srclen) ? FS_OK : Error::BufferTooSmall;
102  }
103 
104  int copy(const char* src)
105  {
106  return copy(src, strlen(src));
107  }
108 
109  int copy(const NameBuffer& name)
110  {
111  return copy(name.buffer, name.length);
112  }
113 
119  int addSep()
120  {
121  // No separator required if path is empty
122  if(length == 0) {
123  return FS_OK;
124  }
125 
126  if(length + 1 < size) {
127  buffer[length++] = '/';
128  buffer[length] = '\0';
129  return FS_OK;
130  }
131 
132  ++length;
133  return Error::BufferTooSmall;
134  }
135 
142  char* endptr()
143  {
144  return buffer + length;
145  }
146 
152  uint16_t space()
153  {
154  return (length < size) ? size - length : 0;
155  }
156 
160  void terminate()
161  {
162  if(length < size) {
163  buffer[length] = '\0';
164  } else if(size != 0) {
165  buffer[size - 1] = '\0';
166  }
167  }
168 
173  bool overflow() const
174  {
175  return length >= size;
176  }
177 
178  bool endsWith(const char* suffix) const
179  {
180  if(suffix == nullptr) {
181  return false;
182  }
183  auto suffix_len = strlen(suffix);
184  if(length < suffix_len || suffix_len == 0) {
185  return false;
186  }
187  return memcmp(&buffer[length - suffix_len], suffix, suffix_len) == 0;
188  }
189 };
190 
194 struct FileNameBuffer : public NameBuffer {
195 public:
196  FileNameBuffer() : NameBuffer(buffer, sizeof(buffer))
197  {
198  }
199 
200 private:
201  // maximum size for a byte-length string, + 1 char for nul terminator
202  char buffer[256];
203 };
204 
205 } // namespace IFS
The String class.
Definition: WString.h:133
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:194
FileNameBuffer()
Definition: NameBuffer.h:196
defines a 'safe' name buffer
Definition: NameBuffer.h:44
uint16_t space()
get the number of free characters available
Definition: NameBuffer.h:152
int copy(const NameBuffer &name)
Definition: NameBuffer.h:109
bool operator==(const char *other)
Definition: NameBuffer.h:82
char * endptr()
get a pointer to the next write position
Definition: NameBuffer.h:142
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:93
char * begin()
Definition: NameBuffer.h:67
NameBuffer(char *buffer, uint16_t size, uint16_t length=0)
Definition: NameBuffer.h:51
int copy(const char *src)
Definition: NameBuffer.h:104
NameBuffer()=default
bool endsWith(const char *suffix) const
Definition: NameBuffer.h:178
NameBuffer(String &s)
Make a NameBuffer point to contents of a String.
Definition: NameBuffer.h:58
const char * c_str() const
Definition: NameBuffer.h:72
bool overflow() const
determine if name buffer overflowed
Definition: NameBuffer.h:173
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:160
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:119