RTduino Source Code Reference Manual
Stream.h
1 /*
2  * Copyright (c) 2021-2022, RTduino Development Team
3  *
4  * SPDX-License-Identifier: LGPL-v2.1
5  *
6  * https://github.com/RTduino/RTduino
7  * https://gitee.com/rtduino/RTduino
8  *
9  * Change Logs:
10  * Date Author Notes
11  * 2021-12-10 Meco Man port to RTduino
12  */
13 /*
14  Stream.h - base class for character-based streams.
15  Copyright (c) 2010 David A. Mellis. All right reserved.
16 
17  This library is free software; you can redistribute it and/or
18  modify it under the terms of the GNU Lesser General Public
19  License as published by the Free Software Foundation; either
20  version 2.1 of the License, or (at your option) any later version.
21 
22  This library is distributed in the hope that it will be useful,
23  but WITHOUT ANY WARRANTY; without even the implied warranty of
24  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25  Lesser General Public License for more details.
26 
27  You should have received a copy of the GNU Lesser General Public
28  License along with this library; if not, write to the Free Software
29  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 
31  parsing functions based on TextFinder library by Michael Margolis
32 */
33 
34 #ifndef Stream_h
35 #define Stream_h
36 
37 #include <inttypes.h>
38 #include "Print.h"
39 
40 // compatibility macros for testing
41 /*
42 #define getInt() parseInt()
43 #define getInt(ignore) parseInt(ignore)
44 #define getFloat() parseFloat()
45 #define getFloat(ignore) parseFloat(ignore)
46 #define getString( pre_string, post_string, buffer, length)
47 readBytesBetween( pre_string, terminator, buffer, length)
48 */
49 
50 // This enumeration provides the lookahead options for parseInt(), parseFloat()
51 // The rules set out here are used until either the first valid character is found
52 // or a time out occurs due to lack of input.
53 enum LookaheadMode{
54  SKIP_ALL, // All invalid characters are ignored.
55  SKIP_NONE, // Nothing is skipped, and the stream is not touched unless the first waiting character is valid.
56  SKIP_WHITESPACE // Only tabs, spaces, line feeds & carriage returns are skipped.
57 };
58 
59 #define NO_IGNORE_CHAR '\x01' // a char not found in a valid ASCII numeric field
60 
61 class Stream : public Print
62 {
63  protected:
64  unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
65  unsigned long _startMillis; // used for timeout measurement
66  int timedRead(void); // read stream with timeout
67  int timedPeek(void); // peek stream with timeout
68  int peekNextDigit(LookaheadMode lookahead, bool detectDecimal); // returns the next numeric digit in the stream or -1 if timeout
69 
70  public:
71  virtual int available() = 0;
72  virtual int read() = 0;
73  virtual int peek() = 0;
74 
75  Stream():_startMillis(0)
76  {
77  _timeout = 1000;
78  }
79  virtual ~Stream() {}
80 
81 // parsing methods
82 
83  void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
84  unsigned long getTimeout(void) { return _timeout; }
85 
86  bool find(char *target); // reads data from the stream until the target string is found
87  bool find(uint8_t *target) { return find ((char *)target); }
88  // returns true if target string is found, false if timed out (see setTimeout)
89 
90  bool find(char *target, size_t length); // reads data from the stream until the target string of given length is found
91  bool find(uint8_t *target, size_t length) { return find ((char *)target, length); }
92  // returns true if target string is found, false if timed out
93 
94  bool find(char target) { return find (&target, 1); }
95 
96  bool findUntil(char *target, char *terminator); // as find but search ends if the terminator string is found
97  bool findUntil(uint8_t *target, char *terminator) { return findUntil((char *)target, terminator); }
98 
99  bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found
100  bool findUntil(uint8_t *target, size_t targetLen, char *terminate, size_t termLen) {return findUntil((char *)target, targetLen, terminate, termLen); }
101 
102  long parseInt(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR);
103  // returns the first valid (long) integer value from the current position.
104  // lookahead determines how parseInt looks ahead in the stream.
105  // See LookaheadMode enumeration at the top of the file.
106  // Lookahead is terminated by the first character that is not a valid part of an integer.
107  // Once parsing commences, 'ignore' will be skipped in the stream.
108 
109  float parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR);
110  // float version of parseInt
111 
112  size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer
113  size_t readBytes( uint8_t *buffer, size_t length) { return readBytes((char *)buffer, length); }
114  // terminates if length characters have been read or timeout (see setTimeout)
115  // returns the number of characters placed in the buffer (0 means no valid data found)
116 
117  size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character
118  size_t readBytesUntil( char terminator, uint8_t *buffer, size_t length) { return readBytesUntil(terminator, (char *)buffer, length); }
119  // terminates if length characters have been read, timeout, or if the terminator character detected
120  // returns the number of characters placed in the buffer (0 means no valid data found)
121 
122  // Arduino String functions to be added here
123  String readString(void);
124  String readStringUntil(char terminator);
125 
126  protected:
127  long parseInt(char ignore) { return parseInt(SKIP_ALL, ignore); }
128  float parseFloat(char ignore) { return parseFloat(SKIP_ALL, ignore); }
129  // These overload exists for compatibility with any class that has derived
130  // Stream and used parseFloat/Int with a custom ignore character. To keep
131  // the public API simple, these overload remains protected.
132 
133  struct MultiTarget {
134  const char *str; // string you're searching for
135  size_t len; // length of string you're searching for
136  size_t index; // index used by the search routine.
137  };
138 
139  // This allows you to search for an arbitrary number of strings.
140  // Returns index of the target that is found first or -1 if timeout occurs.
141  int findMulti(struct MultiTarget *targets, int tCount);
142 };
143 
144 #undef NO_IGNORE_CHAR
145 
146 #endif /* Stream_h */
Definition: Print.h:47
Definition: Stream.h:62
Definition: Stream.h:133