Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

293 lines
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 == nullptr) return true;
  79. size_t tlen = (terminator==nullptr)?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 == nullptr) return true;
  91. if( *target == 0) return true; // return true if target is a null string
  92. if (terminator == nullptr) 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. if (buffer == nullptr) return 0;
  193. size_t count = 0;
  194. while (count < length) {
  195. int c = timedRead();
  196. if (c < 0) {
  197. setReadError();
  198. break;
  199. }
  200. *buffer++ = (char)c;
  201. count++;
  202. }
  203. return count;
  204. }
  205. // as readBytes with terminator character
  206. // terminates if length characters have been read, timeout, or if the terminator character detected
  207. // returns the number of characters placed in the buffer (0 means no valid data found)
  208. size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length)
  209. {
  210. if (buffer == nullptr) return 0;
  211. if (length < 1) return 0;
  212. length--;
  213. size_t index = 0;
  214. while (index < length) {
  215. int c = timedRead();
  216. if (c == terminator) break;
  217. if (c < 0) {
  218. setReadError();
  219. break;
  220. }
  221. *buffer++ = (char)c;
  222. index++;
  223. }
  224. *buffer = 0;
  225. return index; // return number of characters, not including null terminator
  226. }
  227. String Stream::readString(size_t max)
  228. {
  229. String str;
  230. size_t length = 0;
  231. while (length < max) {
  232. int c = timedRead();
  233. if (c < 0) {
  234. setReadError();
  235. break; // timeout
  236. }
  237. if (c == 0) break;
  238. str += (char)c;
  239. length++;
  240. }
  241. return str;
  242. }
  243. String Stream::readStringUntil(char terminator, size_t max)
  244. {
  245. String str;
  246. size_t length = 0;
  247. while (length < max) {
  248. int c = timedRead();
  249. if (c < 0) {
  250. setReadError();
  251. break; // timeout
  252. }
  253. if (c == 0 || c == terminator) break;
  254. str += (char)c;
  255. length++;
  256. }
  257. return str;
  258. }