SafeString  4.1.27
SafeString is a safe, robust and debuggable replacement for string processing in Arduino
SafeStringReader.h
Go to the documentation of this file.
1 #ifndef SAFE_STRING_READER_H
2 #define SAFE_STRING_READER_H
3 /*
4  SafeStringReader.h a tokenizing Stream reader
5  by Matthew Ford
6  (c)2020 Forward Computing and Control Pty. Ltd.
7  This code is not warranted to be fit for any purpose. You may only use it at your own risk.
8  This code may be freely used for both private and commercial use.
9  Provide this copyright is maintained.
10 **/
11 #ifdef __cplusplus
12 #include <Arduino.h>
13 #include "SafeString.h"
14 // SafeString.h includes defines for Stream
15 
16 // handle namespace arduino
18 
37 #define createSafeStringReader(name, size, ...) \
38  char name ## _INPUT_BUFFER[(size)+2]; \
39  char name ## _TOKEN_BUFFER[(size)+2]; \
40  SafeString name ## _SF_INPUT((size)+2, name ## _INPUT_BUFFER, "", #name "_InputBuffer"); \
41  SafeStringReader name(name ## _SF_INPUT, (size)+2, name ## _TOKEN_BUFFER, #name, __VA_ARGS__ );
42 
43 
44 // size + 1 for delimiter, + 1 for '\0' actually token only ever size+1 for '\0' as delimiter not returned
45 // size is the maximum size of the token to be read ignoring the delimiter
46 
73 class SafeStringReader : public SafeString {
74  public:
75  // here buffSize is max size of the token + 1 for delimiter + 1 for terminating '\0;
76  explicit SafeStringReader(SafeString& _sfInput, size_t bufSize, char *tokenBuf, const char* _name, const char* delimiters, bool skipToDelimiterFlag = false, uint8_t echoInput = false, unsigned long timeout_ms = 0 );
77  explicit SafeStringReader(SafeString& _sfInput, size_t bufSize, char *tokenBuf, const char* _name, const char delimiter, bool skipToDelimiterFlag = false, uint8_t echoInput = false, unsigned long timeout_ms = 0 );
78 
85  void connect(Stream& stream); // clears getReadCount() as well
86 
95  void setTimeout(unsigned long ms);
96 
97 
106  bool read();
107 
115 
120  void echoOn();
121  void echoOff();
122 
130  void flushInput();
131 
139  void returnEmptyTokens(bool flag = true);
140 
147  bool end();
148 
155  size_t getReadCount();
156 
168 
169  /* Assignment operators **********************************
170  Set the SafeString to a char version of the assigned value.
171  For = (const char *) the contents are copied to the SafeString buffer
172  if the value is null or invalid,
173  or too large to be fit in the string's internal buffer
174  the string will be left empty
175  */
177  SafeStringReader & operator = (unsigned char c);
179  SafeStringReader & operator = (unsigned int num);
181  SafeStringReader & operator = (unsigned long num);
185  SafeStringReader & operator = (const char *cstr);
186  SafeStringReader & operator = (const __FlashStringHelper *str); // handle F(" .. ") values
187 
193  const char* debugInputBuffer(bool verbose = true);
194  const char* debugInputBuffer(const char* title, bool verbose = true);
195  const char* debugInputBuffer(const __FlashStringHelper *title, bool verbose = true);
196  const char* debugInputBuffer(SafeString &stitle, bool verbose = true);
197 
198  private:
199  SafeStringReader(const SafeStringReader& other);
200  void init(SafeString& _sfInput, const char* delimiters, bool skipToDelimiterFlag, uint8_t echoInput, unsigned long timeout_ms);
201  // void bufferInput(); // get more input
202  SafeString* sfInputPtr;
203  const char* delimiters;
204  bool skipToDelimiterFlag;
205  bool echoInput;
206  bool emptyTokensReturned; // default false
207  bool flagFlushInput; // true if flushing
208  unsigned long timeout_ms;
209  bool haveToken; // true if have token but read() not called yet
210  Stream *streamPtr;
211  size_t charCounter; // counts bytes read, useful for http streams
212  char internalCharDelimiter[2]; // used if char delimiter passed
213 };
214 
215 #include "SafeStringNameSpaceEnd.h"
216 
217 #endif // __cplusplus
218 #endif // SAFE_STRING_READER_H
To create SafeStrings use one of the four (4) macros createSafeString or cSF, createSafeStringFromCha...
Definition: SafeString.h:307
To create a SafeStringReader use the macro createSafeStringReader see the detailed description.
int getDelimiter()
getDelimiter() returns the delimiter that terminated the last token only valid when read() returns tr...
void connect(Stream &stream)
connect(Stream& stream) specifies the Stream to read chars from params stream – the Stream to read fr...
void flushInput()
flushInput() clears any buffered input and Stream RX buffer then sets skipToDelimiterFlag true Once t...
void setTimeout(unsigned long ms)
setTimeout sets the timeout to wait for more chars.
const char * debugInputBuffer(const __FlashStringHelper *title, bool verbose=true)
const char * debugInputBuffer(const char *title, bool verbose=true)
const char * debugInputBuffer(bool verbose=true)
debugInputBuffer These methods let you print out the current contents of the input buffer that the St...
void skipToDelimiter()
skipToDelimiter() discards the next token read Once the next delimiter is read or if the timeout is s...
SafeStringReader(SafeString &_sfInput, size_t bufSize, char *tokenBuf, const char *_name, const char *delimiters, bool skipToDelimiterFlag=false, uint8_t echoInput=false, unsigned long timeout_ms=0)
void echoOn()
echoOn(), echoOff() control echoing back to the input Stream all chars read default if echoOff();
bool read()
read() returns true if a delimited token has been read from the stream.
size_t getReadCount()
getReadCount() The SafeStringReader counts the number of chars read since the last connect( ) call.
const char * debugInputBuffer(SafeString &stitle, bool verbose=true)
void returnEmptyTokens(bool flag=true)
returnEmptyTokens By default empty tokens are not returned, i.e.
SafeStringReader(SafeString &_sfInput, size_t bufSize, char *tokenBuf, const char *_name, const char delimiter, bool skipToDelimiterFlag=false, uint8_t echoInput=false, unsigned long timeout_ms=0)
bool isSkippingToDelimiter()
isSkippingToDelimiter returns true if currently skipping to next delimiter
bool end()
end() returns true if have another token, terminates last token if any, disconnect from stream,...
SafeStringReader & operator=(char c)