Qualia  0.2
Stream.h
Go to the documentation of this file.
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_Compat_h
23 #define Stream_Compat_h
24 
25 #include <qualia/core/common.h>
26 
27 #if !is_arduino()
28 
29 #include <inttypes.h>
30 #include "Print.h"
31 
32 // compatability macros for testing
33 /*
34 #define getInt() parseInt()
35 #define getInt(skipChar) parseInt(skipchar)
36 #define getFloat() parseFloat()
37 #define getFloat(skipChar) parseFloat(skipChar)
38 #define getString( pre_string, post_string, buffer, length)
39 readBytesBetween( pre_string, terminator, buffer, length)
40 */
41 
42 class Stream : public Print
43 {
44  private:
45  unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
46  unsigned long _startMillis; // used for timeout measurement
47  int timedRead(); // private method to read stream with timeout
48  int timedPeek(); // private method to peek stream with timeout
49  int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
50 
51  public:
52  virtual int available() = 0;
53  virtual int read() = 0;
54  virtual int peek() = 0;
55  virtual void flush() = 0;
56 
57  Stream() {_timeout=1000;}
58 
59 // parsing methods
60 
61  void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
62 
63  bool find(char *target); // reads data from the stream until the target string is found
64  // returns true if target string is found, false if timed out (see setTimeout)
65 
66  bool find(char *target, size_t length); // reads data from the stream until the target string of given length is found
67  // returns true if target string is found, false if timed out
68 
69  bool findUntil(char *target, char *terminator); // as find but search ends if the terminator string is found
70 
71  bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found
72 
73 
74  long parseInt(); // returns the first valid (long) integer value from the current position.
75  // initial characters that are not digits (or the minus sign) are skipped
76  // integer is terminated by the first character that is not a digit.
77 
78  float parseFloat(); // float version of parseInt
79 
80  size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer
81  // terminates if length characters have been read or timeout (see setTimeout)
82  // returns the number of characters placed in the buffer (0 means no valid data found)
83 
84  size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character
85  // terminates if length characters have been read, timeout, or if the terminator character detected
86  // returns the number of characters placed in the buffer (0 means no valid data found)
87 
88  // Arduino String functions to be added here
89  String readString();
90  String readStringUntil(char terminator);
91 
92  protected:
93  long parseInt(char skipChar); // as above but the given skipChar is ignored
94  // as above but the given skipChar is ignored
95  // this allows format characters (typically commas) in values to be ignored
96 
97  float parseFloat(char skipChar); // as above but the given skipChar is ignored
98 };
99 
100 #endif
101 
102 #endif