SafeString  4.1.40
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 
78 class SafeStringReader : public SafeString {
79  public:
80  // here buffSize is max size of the token + 1 for delimiter + 1 for terminating '\0;
81  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 );
82  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 );
83 
90  void connect(Stream& stream); // clears getReadCount() as well
91 
100  void setTimeout(unsigned long ms);
101 
102 
111  bool read();
112 
120 
125  void echoOn();
126  void echoOff();
127 
135  void flushInput();
136 
144  void returnEmptyTokens(bool flag = true);
145 
152  bool end();
153 
160  size_t getReadCount();
161 
173 
174  /* Assignment operators **********************************
175  Set the SafeString to a char version of the assigned value.
176  For = (const char *) the contents are copied to the SafeString buffer
177  if the value is null or invalid,
178  or too large to be fit in the string's internal buffer
179  the string will be left empty
180  */
182  SafeStringReader & operator = (unsigned char c);
184  SafeStringReader & operator = (unsigned int num);
186  SafeStringReader & operator = (unsigned long num);
190  SafeStringReader & operator = (const char *cstr);
191  SafeStringReader & operator = (const __FlashStringHelper *str); // handle F(" .. ") values
192 
198  const char* debugInputBuffer(bool verbose = true);
199  const char* debugInputBuffer(const char* title, bool verbose = true);
200  const char* debugInputBuffer(const __FlashStringHelper *title, bool verbose = true);
201  const char* debugInputBuffer(SafeString &stitle, bool verbose = true);
202 
203  private:
204  SafeStringReader(const SafeStringReader& other);
205  void init(SafeString& _sfInput, const char* delimiters, bool skipToDelimiterFlag, uint8_t echoInput, unsigned long timeout_ms);
206  // void bufferInput(); // get more input
207  SafeString* sfInputPtr;
208  const char* delimiters;
209  bool skipToDelimiterFlag;
210  bool echoInput;
211  bool emptyTokensReturned; // default false
212  bool flagFlushInput; // true if flushing
213  unsigned long timeout_ms;
214  bool haveToken; // true if have token but read() not called yet
215  Stream *streamPtr;
216  size_t charCounter; // counts bytes read, useful for http streams
217  char internalCharDelimiter[2]; // used if char delimiter passed
218 };
219 
220 #include "SafeStringNameSpaceEnd.h"
221 
222 #endif // __cplusplus
223 #endif // SAFE_STRING_READER_H
To create SafeStrings use one of the four (4) macros createSafeString or cSF, createSafeStringFromCha...
Definition: SafeString.h:300
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)