Stream.cpp 7.1 KB

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