NameBuffer.h
Go to the documentation of this file.
1 
22 #pragma once
23 
24 #include "Types.h"
25 #include "Error.h"
26 
27 namespace IFS
28 {
44 struct NameBuffer {
45  char* buffer{nullptr};
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 
90  int copy(const char* src, uint16_t srclen)
91  {
92  length = srclen;
93  uint16_t copylen = std::min(srclen, size);
94  if(copylen != 0) {
95  memcpy(buffer, src, copylen);
96  }
97  terminate();
98  return (copylen == srclen) ? FS_OK : Error::BufferTooSmall;
99  }
100 
101  int copy(const char* src)
102  {
103  return copy(src, strlen(src));
104  }
105 
106  int copy(const NameBuffer& name)
107  {
108  return copy(name.buffer, name.length);
109  }
110 
116  int addSep()
117  {
118  // No separator required if path is empty
119  if(length == 0) {
120  return FS_OK;
121  }
122 
123  if(length + 1 < size) {
124  buffer[length++] = '/';
125  buffer[length] = '\0';
126  return FS_OK;
127  }
128 
129  ++length;
130  return Error::BufferTooSmall;
131  }
132 
139  char* endptr()
140  {
141  return buffer + length;
142  }
143 
150  {
151  return (length < size) ? size - length : 0;
152  }
153 
157  void terminate()
158  {
159  if(length < size) {
160  buffer[length] = '\0';
161  } else if(size != 0) {
162  buffer[size - 1] = '\0';
163  }
164  }
165 
170  bool overflow() const
171  {
172  return length >= size;
173  }
174 };
175 
179 struct FileNameBuffer : public NameBuffer {
180 public:
182  {
183  }
184 
185 private:
186  // maximum size for a byte-length string, + 1 char for nul terminator
187  char buffer[256];
188 };
189 
190 } // namespace IFS
uint16_t length
OUT: length of name.
Definition: NameBuffer.h:47
a quick&#39;n&#39;dirty name buffer with maximum path allocation
Definition: NameBuffer.h:179
int addSep()
When building file paths this method simplified appending separators.
Definition: NameBuffer.h:116
uint16_t size
IN: Size of buffer.
Definition: NameBuffer.h:46
char * buffer
Buffer to store name.
Definition: NameBuffer.h:45
FileNameBuffer()
Definition: NameBuffer.h:181
NameBuffer()
Definition: NameBuffer.h:49
char * endptr()
get a pointer to the next write position
Definition: NameBuffer.h:139
int copy(const char *src, uint16_t srclen)
copies text from a source buffer into a name buffer
Definition: NameBuffer.h:90
The String class.
Definition: WString.h:136
bool overflow() const
determine if name buffer overflowed
Definition: NameBuffer.h:170
int copy(const NameBuffer &name)
Definition: NameBuffer.h:106
Definition: DirectoryTemplate.h:36
char * begin()
Definition: NameBuffer.h:69
void terminate()
ensure the buffer has a nul terminator, even if it means overwriting content
Definition: NameBuffer.h:157
int copy(const char *src)
Definition: NameBuffer.h:101
uint16_t space()
get the number of free characters available
Definition: NameBuffer.h:149
NameBuffer(char *buffer, uint16_t size, uint16_t length=0)
Definition: NameBuffer.h:53
NameBuffer(String &s)
Make a NameBuffer point to contents of a String.
Definition: NameBuffer.h:60
constexpr ErrorCode FS_OK
Definition: Error.h:130
defines a &#39;safe&#39; name buffer
Definition: NameBuffer.h:44
const char * c_str() const
Definition: NameBuffer.h:74