Stream.h
Go to the documentation of this file.
1 /* $Id: Stream.h 1151 2011-06-06 21:13:05Z bhagman $
2 ||
3 || @author Brett Hagman <bhagman@wiring.org.co>
4 || @url http://wiring.org.co/
5 || @contribution Alexander Brevig <abrevig@wiring.org.co>
6 || @contribution David A. Mellis
7 ||
8 || @description
9 || | Base class for streams.
10 || |
11 || | Wiring Common API
12 || #
13 ||
14 || @notes
15 || | Originally discussed here:
16 || |
17 || | http://code.google.com/p/arduino/issues/detail?id=60
18 || #
19 ||
20 || @license Please see cores/Common/License.txt.
21 ||
22 */
23 
24 #pragma once
25 
26 #include "Print.h"
27 
32 class Stream : public Print
33 {
34 public:
35  virtual int available() = 0;
36  virtual int peek() = 0;
37  virtual int read() = 0;
38  virtual void flush() = 0;
39 
41  {
42  }
43 
44  // parsing methods
45 
49  void setTimeout(unsigned long timeout)
50  {
51  receiveTimeout = timeout;
52  }
53 
59  bool find(const char* target)
60  {
61  return findUntil(target, "");
62  }
63 
70  bool find(const char* target, size_t length)
71  {
72  return findUntil(target, length, nullptr, 0);
73  }
74 
78  bool findUntil(const char* target, const char* terminator);
79 
83  bool findUntil(const char* target, size_t targetLen, const char* terminate, size_t termLen);
84 
91  long parseInt();
92 
96  float parseFloat();
97 
106  virtual size_t readBytes(char* buffer, size_t length);
107 
115  size_t readBytesUntil(char terminator, char* buffer, size_t length);
116 
122  virtual String readString(size_t maxLen);
123 
124  String readStringUntil(char terminator);
125 
126  /*
127  * @brief Returns the location of the searched character
128  * @param c Character to search for
129  * @retval int -1 if not found 0 or positive number otherwise
130  */
131  virtual int indexOf(char c)
132  {
133  return -1;
134  }
135 
136 protected:
137  int timedRead();
138  int timedPeek();
139 
143  int peekNextDigit();
144 
150  long parseInt(char skipChar);
151 
155  float parseFloat(char skipChar);
156 
158 };
size_t readBytesUntil(char terminator, char *buffer, size_t length)
As readBytes() with terminator character.
virtual int available()=0
virtual size_t readBytes(char *buffer, size_t length)
Read chars from stream into buffer.
virtual int indexOf(char c)
Definition: Stream.h:131
int peekNextDigit()
returns the next numeric digit in the stream or -1 if timeout
bool find(const char *target)
Read data from the stream until the target string is found.
Definition: Stream.h:59
long parseInt()
Returns the first valid (long) integer value from the current position.
int timedRead()
The String class.
Definition: WString.h:136
Provides formatted output to stream.
Definition: Print.h:36
bool find(const char *target, size_t length)
Read data from the stream until the target string of given length is found.
Definition: Stream.h:70
virtual int read()=0
void setTimeout(unsigned long timeout)
Set maximum milliseconds to wait for stream data, default is 1 second.
Definition: Stream.h:49
uint16_t receiveTimeout
number of milliseconds to wait for the next char before aborting timed read
Definition: Stream.h:157
String readStringUntil(char terminator)
int timedPeek()
float parseFloat()
float version of parseInt
Stream()
Definition: Stream.h:40
virtual int peek()=0
virtual void flush()=0
bool findUntil(const char *target, const char *terminator)
As find() but search ends if the terminator string is found.
virtual String readString(size_t maxLen)
Like readBytes but place content into a String
Base Stream class.
Definition: Stream.h:32