sanguino/cores/arduino/Stream.cpp

Thu, 07 Jul 2016 12:23:34 +0200

author
mbayer
date
Thu, 07 Jul 2016 12:23:34 +0200
changeset 2
b373b0288715
permissions
-rw-r--r--

added missing sanguino files

2
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1 /*
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
2 Stream.cpp - adds parsing methods to Stream class
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
3 Copyright (c) 2008 David A. Mellis. All right reserved.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
4
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
5 This library is free software; you can redistribute it and/or
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
6 modify it under the terms of the GNU Lesser General Public
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
7 License as published by the Free Software Foundation; either
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
8 version 2.1 of the License, or (at your option) any later version.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
9
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
13 Lesser General Public License for more details.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
14
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
15 You should have received a copy of the GNU Lesser General Public
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
16 License along with this library; if not, write to the Free Software
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
18
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
19 Created July 2011
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
20 parsing functions based on TextFinder library by Michael Margolis
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
21 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
22
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
23 #include "Arduino.h"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
24 #include "Stream.h"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
25
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
26 #define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
27 #define NO_SKIP_CHAR 1 // a magic char not found in a valid ASCII numeric field
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
28
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
29 // private method to read stream with timeout
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
30 int Stream::timedRead()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
31 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
32 int c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
33 _startMillis = millis();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
34 do {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
35 c = read();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
36 if (c >= 0) return c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
37 } while(millis() - _startMillis < _timeout);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
38 return -1; // -1 indicates timeout
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
39 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
40
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
41 // private method to peek stream with timeout
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
42 int Stream::timedPeek()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
43 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
44 int c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
45 _startMillis = millis();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
46 do {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
47 c = peek();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
48 if (c >= 0) return c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
49 } while(millis() - _startMillis < _timeout);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
50 return -1; // -1 indicates timeout
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
51 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
52
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
53 // returns peek of the next digit in the stream or -1 if timeout
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
54 // discards non-numeric characters
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
55 int Stream::peekNextDigit()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
56 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
57 int c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
58 while (1) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
59 c = timedPeek();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
60 if (c < 0) return c; // timeout
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
61 if (c == '-') return c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
62 if (c >= '0' && c <= '9') return c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
63 read(); // discard non-numeric
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
64 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
65 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
66
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
67 // Public Methods
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
68 //////////////////////////////////////////////////////////////
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
69
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
70 void Stream::setTimeout(unsigned long timeout) // sets the maximum number of milliseconds to wait
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
71 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
72 _timeout = timeout;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
73 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
74
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
75 // find returns true if the target string is found
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
76 bool Stream::find(char *target)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
77 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
78 return findUntil(target, NULL);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
79 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
80
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
81 // reads data from the stream until the target string of given length is found
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
82 // returns true if target string is found, false if timed out
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
83 bool Stream::find(char *target, size_t length)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
84 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
85 return findUntil(target, length, NULL, 0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
86 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
87
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
88 // as find but search ends if the terminator string is found
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
89 bool Stream::findUntil(char *target, char *terminator)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
90 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
91 return findUntil(target, strlen(target), terminator, strlen(terminator));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
92 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
93
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
94 // reads data from the stream until the target string of the given length is found
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
95 // search terminated if the terminator string is found
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
96 // returns true if target string is found, false if terminated or timed out
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
97 bool Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t termLen)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
98 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
99 size_t index = 0; // maximum target string length is 64k bytes!
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
100 size_t termIndex = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
101 int c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
102
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
103 if( *target == 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
104 return true; // return true if target is a null string
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
105 while( (c = timedRead()) > 0){
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
106
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
107 if(c != target[index])
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
108 index = 0; // reset index if any char does not match
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
109
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
110 if( c == target[index]){
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
111 //////Serial.print("found "); Serial.write(c); Serial.print("index now"); Serial.println(index+1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
112 if(++index >= targetLen){ // return true if all chars in the target match
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
113 return true;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
114 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
115 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
116
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
117 if(termLen > 0 && c == terminator[termIndex]){
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
118 if(++termIndex >= termLen)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
119 return false; // return false if terminate string found before target string
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
120 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
121 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
122 termIndex = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
123 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
124 return false;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
125 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
126
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
127
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
128 // returns the first valid (long) integer value from the current position.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
129 // initial characters that are not digits (or the minus sign) are skipped
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
130 // function is terminated by the first character that is not a digit.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
131 long Stream::parseInt()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
132 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
133 return parseInt(NO_SKIP_CHAR); // terminate on first non-digit character (or timeout)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
134 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
135
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
136 // as above but a given skipChar is ignored
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
137 // this allows format characters (typically commas) in values to be ignored
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
138 long Stream::parseInt(char skipChar)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
139 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
140 boolean isNegative = false;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
141 long value = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
142 int c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
143
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
144 c = peekNextDigit();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
145 // ignore non numeric leading characters
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
146 if(c < 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
147 return 0; // zero returned if timeout
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
148
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
149 do{
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
150 if(c == skipChar)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
151 ; // ignore this charactor
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
152 else if(c == '-')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
153 isNegative = true;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
154 else if(c >= '0' && c <= '9') // is c a digit?
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
155 value = value * 10 + c - '0';
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
156 read(); // consume the character we got with peek
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
157 c = timedPeek();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
158 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
159 while( (c >= '0' && c <= '9') || c == skipChar );
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
160
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
161 if(isNegative)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
162 value = -value;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
163 return value;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
164 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
165
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
166
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
167 // as parseInt but returns a floating point value
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
168 float Stream::parseFloat()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
169 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
170 return parseFloat(NO_SKIP_CHAR);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
171 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
172
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
173 // as above but the given skipChar is ignored
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
174 // this allows format characters (typically commas) in values to be ignored
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
175 float Stream::parseFloat(char skipChar){
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
176 boolean isNegative = false;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
177 boolean isFraction = false;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
178 long value = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
179 char c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
180 float fraction = 1.0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
181
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
182 c = peekNextDigit();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
183 // ignore non numeric leading characters
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
184 if(c < 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
185 return 0; // zero returned if timeout
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
186
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
187 do{
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
188 if(c == skipChar)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
189 ; // ignore
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
190 else if(c == '-')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
191 isNegative = true;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
192 else if (c == '.')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
193 isFraction = true;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
194 else if(c >= '0' && c <= '9') { // is c a digit?
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
195 value = value * 10 + c - '0';
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
196 if(isFraction)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
197 fraction *= 0.1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
198 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
199 read(); // consume the character we got with peek
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
200 c = timedPeek();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
201 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
202 while( (c >= '0' && c <= '9') || c == '.' || c == skipChar );
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
203
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
204 if(isNegative)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
205 value = -value;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
206 if(isFraction)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
207 return value * fraction;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
208 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
209 return value;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
210 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
211
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
212 // read characters from stream into buffer
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
213 // terminates if length characters have been read, or timeout (see setTimeout)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
214 // returns the number of characters placed in the buffer
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
215 // the buffer is NOT null terminated.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
216 //
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
217 size_t Stream::readBytes(char *buffer, size_t length)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
218 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
219 size_t count = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
220 while (count < length) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
221 int c = timedRead();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
222 if (c < 0) break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
223 *buffer++ = (char)c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
224 count++;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
225 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
226 return count;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
227 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
228
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
229
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
230 // as readBytes with terminator character
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
231 // terminates if length characters have been read, timeout, or if the terminator character detected
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
232 // returns the number of characters placed in the buffer (0 means no valid data found)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
233
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
234 size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
235 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
236 if (length < 1) return 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
237 size_t index = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
238 while (index < length) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
239 int c = timedRead();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
240 if (c < 0 || c == terminator) break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
241 *buffer++ = (char)c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
242 index++;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
243 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
244 return index; // return number of characters, not including null terminator
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
245 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
246
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
247 String Stream::readString()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
248 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
249 String ret;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
250 int c = timedRead();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
251 while (c >= 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
252 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
253 ret += (char)c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
254 c = timedRead();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
255 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
256 return ret;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
257 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
258
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
259 String Stream::readStringUntil(char terminator)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
260 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
261 String ret;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
262 int c = timedRead();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
263 while (c >= 0 && c != terminator)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
264 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
265 ret += (char)c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
266 c = timedRead();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
267 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
268 return ret;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
269 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
270

mercurial