Stream.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. findMulti/findUntil routines written by Jim Leonard/Xuth
  18. */
  19. #include "Arduino.h"
  20. #include "Stream.h"
  21. #define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait
  22. #define NO_SKIP_CHAR 1 // a magic char not found in a valid ASCII numeric field
  23. // private method to read stream with timeout
  24. int Stream::timedRead()
  25. {
  26. int c;
  27. _startMillis = millis();
  28. do {
  29. c = read();
  30. if (c >= 0) return c;
  31. } while(millis() - _startMillis < _timeout);
  32. return -1; // -1 indicates timeout
  33. }
  34. // private method to peek stream with timeout
  35. int Stream::timedPeek()
  36. {
  37. int c;
  38. _startMillis = millis();
  39. do {
  40. c = peek();
  41. if (c >= 0) return c;
  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(char *target)
  66. {
  67. return findUntil(target, strlen(target), NULL, 0);
  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(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(char *target, char *terminator)
  77. {
  78. return findUntil(target, strlen(target), terminator, strlen(terminator));
  79. }
  80. // reads data from the stream until the target string of the given length is found
  81. // search terminated if the terminator string is found
  82. // returns true if target string is found, false if terminated or timed out
  83. bool Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t termLen)
  84. {
  85. if (terminator == NULL) {
  86. MultiTarget t[1] = {{target, targetLen, 0}};
  87. return findMulti(t, 1) == 0 ? true : false;
  88. } else {
  89. MultiTarget t[2] = {{target, targetLen, 0}, {terminator, termLen, 0}};
  90. return findMulti(t, 2) == 0 ? true : false;
  91. }
  92. }
  93. // returns the first valid (long) integer value from the current position.
  94. // initial characters that are not digits (or the minus sign) are skipped
  95. // function is terminated by the first character that is not a digit.
  96. long Stream::parseInt()
  97. {
  98. return parseInt(NO_SKIP_CHAR); // terminate on first non-digit character (or timeout)
  99. }
  100. // as above but a given skipChar is ignored
  101. // this allows format characters (typically commas) in values to be ignored
  102. long Stream::parseInt(char skipChar)
  103. {
  104. bool isNegative = false;
  105. long value = 0;
  106. int c;
  107. c = peekNextDigit();
  108. // ignore non numeric leading characters
  109. if(c < 0)
  110. return 0; // zero returned if timeout
  111. do{
  112. if(c == skipChar)
  113. ; // ignore this charactor
  114. else if(c == '-')
  115. isNegative = true;
  116. else if(c >= '0' && c <= '9') // is c a digit?
  117. value = value * 10 + c - '0';
  118. read(); // consume the character we got with peek
  119. c = timedPeek();
  120. }
  121. while( (c >= '0' && c <= '9') || c == skipChar );
  122. if(isNegative)
  123. value = -value;
  124. return value;
  125. }
  126. // as parseInt but returns a floating point value
  127. float Stream::parseFloat()
  128. {
  129. return parseFloat(NO_SKIP_CHAR);
  130. }
  131. // as above but the given skipChar is ignored
  132. // this allows format characters (typically commas) in values to be ignored
  133. float Stream::parseFloat(char skipChar){
  134. bool isNegative = false;
  135. bool isFraction = false;
  136. long value = 0;
  137. char c;
  138. float fraction = 1.0;
  139. c = peekNextDigit();
  140. // ignore non numeric leading characters
  141. if(c < 0)
  142. return 0; // zero returned if timeout
  143. do{
  144. if(c == skipChar)
  145. ; // ignore
  146. else if(c == '-')
  147. isNegative = true;
  148. else if (c == '.')
  149. isFraction = true;
  150. else if(c >= '0' && c <= '9') { // is c a digit?
  151. value = value * 10 + c - '0';
  152. if(isFraction)
  153. fraction *= 0.1;
  154. }
  155. read(); // consume the character we got with peek
  156. c = timedPeek();
  157. }
  158. while( (c >= '0' && c <= '9') || c == '.' || c == skipChar );
  159. if(isNegative)
  160. value = -value;
  161. if(isFraction)
  162. return value * fraction;
  163. else
  164. return value;
  165. }
  166. // read characters from stream into buffer
  167. // terminates if length characters have been read, or timeout (see setTimeout)
  168. // returns the number of characters placed in the buffer
  169. // the buffer is NOT null terminated.
  170. //
  171. size_t Stream::readBytes(char *buffer, size_t length)
  172. {
  173. size_t count = 0;
  174. while (count < length) {
  175. int c = timedRead();
  176. if (c < 0) break;
  177. *buffer++ = (char)c;
  178. count++;
  179. }
  180. return count;
  181. }
  182. // as readBytes with terminator character
  183. // terminates if length characters have been read, timeout, or if the terminator character detected
  184. // returns the number of characters placed in the buffer (0 means no valid data found)
  185. size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length)
  186. {
  187. if (length < 1) return 0;
  188. size_t index = 0;
  189. while (index < length) {
  190. int c = timedRead();
  191. if (c < 0 || c == terminator) break;
  192. *buffer++ = (char)c;
  193. index++;
  194. }
  195. return index; // return number of characters, not including null terminator
  196. }
  197. String Stream::readString()
  198. {
  199. String ret;
  200. int c = timedRead();
  201. while (c >= 0)
  202. {
  203. ret += (char)c;
  204. c = timedRead();
  205. }
  206. return ret;
  207. }
  208. String Stream::readStringUntil(char terminator)
  209. {
  210. String ret;
  211. int c = timedRead();
  212. while (c >= 0 && c != terminator)
  213. {
  214. ret += (char)c;
  215. c = timedRead();
  216. }
  217. return ret;
  218. }
  219. int Stream::findMulti( struct Stream::MultiTarget *targets, int tCount) {
  220. // any zero length target string automatically matches and would make
  221. // a mess of the rest of the algorithm.
  222. for (struct MultiTarget *t = targets; t < targets+tCount; ++t) {
  223. if (t->len <= 0)
  224. return t - targets;
  225. }
  226. while (1) {
  227. int c = timedRead();
  228. if (c < 0)
  229. return -1;
  230. for (struct MultiTarget *t = targets; t < targets+tCount; ++t) {
  231. // the simple case is if we match, deal with that first.
  232. if (c == t->str[t->index]) {
  233. if (++t->index == t->len)
  234. return t - targets;
  235. else
  236. continue;
  237. }
  238. // if not we need to walk back and see if we could have matched further
  239. // down the stream (ie '1112' doesn't match the first position in '11112'
  240. // but it will match the second position so we can't just reset the current
  241. // index to 0 when we find a mismatch.
  242. if (t->index == 0)
  243. continue;
  244. int origIndex = t->index;
  245. do {
  246. --t->index;
  247. // first check if current char works against the new current index
  248. if (c != t->str[t->index])
  249. continue;
  250. // if it's the only char then we're good, nothing more to check
  251. if (t->index == 0) {
  252. t->index++;
  253. break;
  254. }
  255. // otherwise we need to check the rest of the found string
  256. int diff = origIndex - t->index;
  257. size_t i;
  258. for (i = 0; i < t->index; ++i) {
  259. if (t->str[i] != t->str[i + diff])
  260. break;
  261. }
  262. // if we successfully got through the previous loop then our current
  263. // index is good.
  264. if (i == t->index) {
  265. t->index++;
  266. break;
  267. }
  268. // otherwise we just try the next index
  269. } while (t->index);
  270. }
  271. }
  272. // unreachable
  273. return -1;
  274. }