SafeString  4.1.45
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 
120  bool read();
121 
129 
134  void echoOn();
135  void echoOff();
136 
144  void flushInput();
145 
153  void returnEmptyTokens(bool flag = true);
154 
166  bool end();
167 
174  size_t getReadCount();
175 
187 
211 
212  /* Assignment operators **********************************
213  Set the SafeString to a char version of the assigned value.
214  For = (const char *) the contents are copied to the SafeString buffer
215  if the value is null or invalid,
216  or too large to be fit in the string's internal buffer
217  the string will be left empty
218  */
220  SafeStringReader & operator = (unsigned char c);
222  SafeStringReader & operator = (unsigned int num);
224  SafeStringReader & operator = (unsigned long num);
228  SafeStringReader & operator = (const char *cstr);
229  SafeStringReader & operator = (const __FlashStringHelper *str); // handle F(" .. ") values
230 
236  const char* debugInputBuffer(bool verbose = true);
237  const char* debugInputBuffer(const char* title, bool verbose = true);
238  const char* debugInputBuffer(const __FlashStringHelper *title, bool verbose = true);
239  const char* debugInputBuffer(SafeString &stitle, bool verbose = true);
240 
241  private:
242  SafeStringReader(const SafeStringReader& other);
243  void init(SafeString& _sfInput, const char* delimiters, bool skipToDelimiterFlag, uint8_t echoInput, unsigned long timeout_ms);
244  // void bufferInput(); // get more input
245  SafeString* sfInputPtr;
246  const char* delimiters;
247  bool skipToDelimiterFlag;
248  bool echoInput;
249  bool emptyTokensReturned; // default false
250  bool flagFlushInput; // true if flushing
251  bool longTokenDiscardedFlag; // true if the last read() detected and discarded an over-long token
252  unsigned long timeout_ms;
253  bool haveToken; // true if have token but read() not called yet
254  Stream *streamPtr;
255  size_t charCounter; // counts bytes read, useful for http streams
256  char internalCharDelimiter[2]; // used if char delimiter passed
257 };
258 
259 #include "SafeStringNameSpaceEnd.h"
260 
261 #endif // __cplusplus
262 #endif // SAFE_STRING_READER_H
To create SafeStrings use one of the four (4) macros createSafeString or cSF, createSafeStringFromCha...
Definition: SafeString.h:306
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.
bool longTokenDiscarded()
longTokenDiscarded() returns true if the last call to read() found a token longer than the size this ...
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, disconnects from the stream,...
SafeStringReader & operator=(char c)