You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

291 line
7.6KB

  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. #define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait
  20. #define NO_SKIP_CHAR 1 // a magic char not found in a valid ASCII numeric field
  21. // private method to read stream with timeout
  22. int Stream::timedRead()
  23. {
  24. int c;
  25. unsigned long startMillis = millis();
  26. do {
  27. c = read();
  28. if (c >= 0) return c;
  29. yield();
  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. unsigned long startMillis = millis();
  38. do {
  39. c = peek();
  40. if (c >= 0) return c;
  41. yield();
  42. } while(millis() - startMillis < _timeout);
  43. return -1; // -1 indicates timeout
  44. }
  45. // returns peek of the next digit in the stream or -1 if timeout
  46. // discards non-numeric characters
  47. int Stream::peekNextDigit()
  48. {
  49. int c;
  50. while (1) {
  51. c = timedPeek();
  52. if (c < 0) return c; // timeout
  53. if (c == '-') return c;
  54. if (c >= '0' && c <= '9') return c;
  55. read(); // discard non-numeric
  56. }
  57. }
  58. // Public Methods
  59. //////////////////////////////////////////////////////////////
  60. void Stream::setTimeout(unsigned long timeout) // sets the maximum number of milliseconds to wait
  61. {
  62. _timeout = timeout;
  63. }
  64. // find returns true if the target string is found
  65. bool Stream::find(const char *target)
  66. {
  67. return findUntil(target, NULL);
  68. }
  69. // reads data from the stream until the target string of given length is found
  70. // returns true if target string is found, false if timed out
  71. bool Stream::find(const char *target, size_t length)
  72. {
  73. return findUntil(target, length, NULL, 0);
  74. }
  75. // as find but search ends if the terminator string is found
  76. bool Stream::findUntil(const char *target, const char *terminator)
  77. {
  78. if(target == NULL) return true;
  79. size_t tlen = (terminator==NULL)?0:strlen(terminator);
  80. return findUntil(target, strlen(target), terminator, strlen(terminator));
  81. }
  82. // reads data from the stream until the target string of the given length is found
  83. // search terminated if the terminator string is found
  84. // returns true if target string is found, false if terminated or timed out
  85. bool Stream::findUntil(const char *target, size_t targetLen, const char *terminator, size_t termLen)
  86. {
  87. size_t index = 0; // maximum target string length is 64k bytes!
  88. size_t termIndex = 0;
  89. int c;
  90. if( target == NULL) return true;
  91. if( *target == 0) return true; // return true if target is a null string
  92. if (terminator == NULL) termLen = 0;
  93. while( (c = timedRead()) > 0){
  94. if( c == target[index]){
  95. //////Serial.print("found "); Serial.write(c); Serial.print("index now"); Serial.println(index+1);
  96. if(++index >= targetLen){ // return true if all chars in the target match
  97. return true;
  98. }
  99. }
  100. else{
  101. index = 0; // reset index if any char does not match
  102. }
  103. if(termLen > 0 && c == terminator[termIndex]){
  104. if(++termIndex >= termLen)
  105. return false; // return false if terminate string found before target string
  106. }
  107. else
  108. termIndex = 0;
  109. }
  110. return false;
  111. }
  112. // returns the first valid (long) integer value from the current position.
  113. // initial characters that are not digits (or the minus sign) are skipped
  114. // function is terminated by the first character that is not a digit.
  115. long Stream::parseInt()
  116. {
  117. return parseInt(NO_SKIP_CHAR); // terminate on first non-digit character (or timeout)
  118. }
  119. // as above but a given skipChar is ignored
  120. // this allows format characters (typically commas) in values to be ignored
  121. long Stream::parseInt(char skipChar)
  122. {
  123. boolean isNegative = false;
  124. long value = 0;
  125. int c;
  126. c = peekNextDigit();
  127. // ignore non numeric leading characters
  128. if(c < 0)
  129. return 0; // zero returned if timeout
  130. do{
  131. if(c == skipChar)
  132. ; // ignore this charactor
  133. else if(c == '-')
  134. isNegative = true;
  135. else if(c >= '0' && c <= '9') // is c a digit?
  136. value = value * 10 + c - '0';
  137. read(); // consume the character we got with peek
  138. c = timedPeek();
  139. }
  140. while( (c >= '0' && c <= '9') || c == skipChar );
  141. if(isNegative)
  142. value = -value;
  143. return value;
  144. }
  145. // as parseInt but returns a floating point value
  146. float Stream::parseFloat()
  147. {
  148. return parseFloat(NO_SKIP_CHAR);
  149. }
  150. // as above but the given skipChar is ignored
  151. // this allows format characters (typically commas) in values to be ignored
  152. float Stream::parseFloat(char skipChar){
  153. boolean isNegative = false;
  154. boolean isFraction = false;
  155. long value = 0;
  156. int c;
  157. float fraction = 1.0;
  158. c = peekNextDigit();
  159. // ignore non numeric leading characters
  160. if(c < 0)
  161. return 0; // zero returned if timeout
  162. do{
  163. if(c == skipChar)
  164. ; // ignore
  165. else if(c == '-')
  166. isNegative = true;
  167. else if (c == '.')
  168. isFraction = true;
  169. else if(c >= '0' && c <= '9') { // is c a digit?
  170. value = value * 10 + c - '0';
  171. if(isFraction)
  172. fraction *= 0.1;
  173. }
  174. read(); // consume the character we got with peek
  175. c = timedPeek();
  176. }
  177. while( (c >= '0' && c <= '9') || c == '.' || c == skipChar );
  178. if(isNegative)
  179. value = -value;
  180. if(isFraction)
  181. return value * fraction;
  182. else
  183. return value;
  184. }
  185. // read characters from stream into buffer
  186. // terminates if length characters have been read, or timeout (see setTimeout)
  187. // returns the number of characters placed in the buffer
  188. // the buffer is NOT null terminated.
  189. //
  190. size_t Stream::readBytes(char *buffer, size_t length)
  191. {
  192. size_t count = 0;
  193. while (count < length) {
  194. int c = timedRead();
  195. if (c < 0) {
  196. setReadError();
  197. break;
  198. }
  199. *buffer++ = (char)c;
  200. count++;
  201. }
  202. return count;
  203. }
  204. // as readBytes with terminator character
  205. // terminates if length characters have been read, timeout, or if the terminator character detected
  206. // returns the number of characters placed in the buffer (0 means no valid data found)
  207. size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length)
  208. {
  209. if (length < 1) return 0;
  210. length--;
  211. size_t index = 0;
  212. while (index < length) {
  213. int c = timedRead();
  214. if (c == terminator) break;
  215. if (c < 0) {
  216. setReadError();
  217. break;
  218. }
  219. *buffer++ = (char)c;
  220. index++;
  221. }
  222. *buffer = 0;
  223. return index; // return number of characters, not including null terminator
  224. }
  225. String Stream::readString(size_t max)
  226. {
  227. String str;
  228. size_t length = 0;
  229. while (length < max) {
  230. int c = timedRead();
  231. if (c < 0) {
  232. setReadError();
  233. break; // timeout
  234. }
  235. if (c == 0) break;
  236. str += (char)c;
  237. length++;
  238. }
  239. return str;
  240. }
  241. String Stream::readStringUntil(char terminator, size_t max)
  242. {
  243. String str;
  244. size_t length = 0;
  245. while (length < max) {
  246. int c = timedRead();
  247. if (c < 0) {
  248. setReadError();
  249. break; // timeout
  250. }
  251. if (c == 0 || c == terminator) break;
  252. str += (char)c;
  253. length++;
  254. }
  255. return str;
  256. }