Stream.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. Stream.h - base class for character-based streams.
  3. Copyright (c) 2010 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. parsing functions based on TextFinder library by Michael Margolis
  16. */
  17. #ifndef Stream_h
  18. #define Stream_h
  19. #include <inttypes.h>
  20. #include "Print.h"
  21. // compatability macros for testing
  22. /*
  23. #define getInt() parseInt()
  24. #define getInt(skipChar) parseInt(skipchar)
  25. #define getFloat() parseFloat()
  26. #define getFloat(skipChar) parseFloat(skipChar)
  27. #define getString( pre_string, post_string, buffer, length)
  28. readBytesBetween( pre_string, terminator, buffer, length)
  29. */
  30. class Stream : public Print
  31. {
  32. private:
  33. unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
  34. unsigned long _startMillis; // used for timeout measurement
  35. int timedRead(); // private method to read stream with timeout
  36. int timedPeek(); // private method to peek stream with timeout
  37. int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
  38. public:
  39. virtual int available() = 0;
  40. virtual int read() = 0;
  41. virtual int peek() = 0;
  42. virtual void flush() = 0;
  43. Stream() {_timeout=1000;}
  44. // parsing methods
  45. void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
  46. bool find(char *target); // reads data from the stream until the target string is found
  47. // returns true if target string is found, false if timed out (see setTimeout)
  48. bool find(char *target, size_t length); // reads data from the stream until the target string of given length is found
  49. // returns true if target string is found, false if timed out
  50. bool findUntil(char *target, char *terminator); // as find but search ends if the terminator string is found
  51. bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found
  52. long parseInt(); // returns the first valid (long) integer value from the current position.
  53. // initial characters that are not digits (or the minus sign) are skipped
  54. // integer is terminated by the first character that is not a digit.
  55. float parseFloat(); // float version of parseInt
  56. size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer
  57. // terminates if length characters have been read or timeout (see setTimeout)
  58. // returns the number of characters placed in the buffer (0 means no valid data found)
  59. size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character
  60. // terminates if length characters have been read, timeout, or if the terminator character detected
  61. // returns the number of characters placed in the buffer (0 means no valid data found)
  62. // Arduino String functions to be added here
  63. String readString();
  64. String readStringUntil(char terminator);
  65. protected:
  66. long parseInt(char skipChar); // as above but the given skipChar is ignored
  67. // as above but the given skipChar is ignored
  68. // this allows format characters (typically commas) in values to be ignored
  69. float parseFloat(char skipChar); // as above but the given skipChar is ignored
  70. };
  71. #endif