Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
LineBuffer.h
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * LineBuffer.h - support for buffering/editing a line of text
8  *
9  * author mikee47 <mike@sillyhouse.net> Feb 2019
10  *
11  ****/
12 
13 #ifndef _SMING_CORE_DATA_BUFFER_LINE_BUFFER_H_
14 #define _SMING_CORE_DATA_BUFFER_LINE_BUFFER_H_
15 
16 #include <user_config.h>
17 
22 template <uint16_t BUFSIZE> class LineBuffer
23 {
24 public:
29  char addChar(char c);
30 
34  void clear()
35  {
36  length = 0;
37  }
38 
42  char* getBuffer()
43  {
44  return buffer;
45  }
46 
50  unsigned getLength() const
51  {
52  return length;
53  }
54 
60  bool startsWith(const char* text) const;
61 
67  bool contains(const char* text) const;
68 
73  bool backspace();
74 
75 private:
76  char buffer[BUFSIZE] = {'\0'};
77  uint16_t length = 0;
78 };
79 
80 template <uint16_t BUFSIZE> char LineBuffer<BUFSIZE>::addChar(char c)
81 {
82  if(c == '\n' || c == '\r') {
83  return '\n';
84  }
85 
86  if(c >= 0x20 && c < 0x7f && length < (BUFSIZE - 1)) {
87  buffer[length++] = c;
88  buffer[length] = '\0';
89  return c;
90  }
91 
92  return '\0';
93 }
94 
95 template <uint16_t BUFSIZE> bool LineBuffer<BUFSIZE>::backspace()
96 {
97  if(length == 0) {
98  return false;
99  } else {
100  --length;
101  buffer[length] = '\0';
102  return true;
103  }
104 }
105 
106 template <uint16_t BUFSIZE> bool LineBuffer<BUFSIZE>::startsWith(const char* text) const
107 {
108  auto len = strlen(text);
109  return memcmp(buffer, text, len) == 0;
110 }
111 
112 template <uint16_t BUFSIZE> bool LineBuffer<BUFSIZE>::contains(const char* text) const
113 {
114  return strstr(buffer, text) != nullptr;
115 }
116 
117 #endif /* _SMING_CORE_DATA_BUFFER_LINE_BUFFER_H_ */
bool startsWith(const char *text) const
Check for matching text at start of line, case-sensitive.
Definition: LineBuffer.h:106
bool backspace()
Remove last character from buffer.
Definition: LineBuffer.h:95
Class to enable buffering of a single line of text, with simple editing.
Definition: LineBuffer.h:22
char * getBuffer()
Get the text, nul-terminated.
Definition: LineBuffer.h:42
void clear()
Clear contents of buffer.
Definition: LineBuffer.h:34
unsigned getLength() const
Get number of characters in the text line.
Definition: LineBuffer.h:50
char addChar(char c)
Add a character to the buffer.
Definition: LineBuffer.h:80
bool contains(const char *text) const
Check for matching text anywhere in line, case-sensitive.
Definition: LineBuffer.h:112