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