SafeString  4.1.44
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 
157  bool end();
158 
165  size_t getReadCount();
166 
178 
179  /* Assignment operators **********************************
180  Set the SafeString to a char version of the assigned value.
181  For = (const char *) the contents are copied to the SafeString buffer
182  if the value is null or invalid,
183  or too large to be fit in the string's internal buffer
184  the string will be left empty
185  */
187  SafeStringReader & operator = (unsigned char c);
189  SafeStringReader & operator = (unsigned int num);
191  SafeStringReader & operator = (unsigned long num);
195  SafeStringReader & operator = (const char *cstr);
196  SafeStringReader & operator = (const __FlashStringHelper *str); // handle F(" .. ") values
197 
203  const char* debugInputBuffer(bool verbose = true);
204  const char* debugInputBuffer(const char* title, bool verbose = true);
205  const char* debugInputBuffer(const __FlashStringHelper *title, bool verbose = true);
206  const char* debugInputBuffer(SafeString &stitle, bool verbose = true);
207 
208  private:
209  SafeStringReader(const SafeStringReader& other);
210  void init(SafeString& _sfInput, const char* delimiters, bool skipToDelimiterFlag, uint8_t echoInput, unsigned long timeout_ms);
211  // void bufferInput(); // get more input
212  SafeString* sfInputPtr;
213  const char* delimiters;
214  bool skipToDelimiterFlag;
215  bool echoInput;
216  bool emptyTokensReturned; // default false
217  bool flagFlushInput; // true if flushing
218  unsigned long timeout_ms;
219  bool haveToken; // true if have token but read() not called yet
220  Stream *streamPtr;
221  size_t charCounter; // counts bytes read, useful for http streams
222  char internalCharDelimiter[2]; // used if char delimiter passed
223 };
224 
225 #include "SafeStringNameSpaceEnd.h"
226 
227 #endif // __cplusplus
228 #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.
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)