StringStream.h 757 B

1234567891011121314151617181920212223242526272829
  1. /*
  2. * Adapated from https://gist.github.com/cmaglie/5883185
  3. */
  4. #ifndef _STRING_STREAM_H_INCLUDED_
  5. #define _STRING_STREAM_H_INCLUDED_
  6. #include <Stream.h>
  7. class StringStream : public Stream
  8. {
  9. public:
  10. StringStream(String &s) : string(s), position(0) { }
  11. // Stream methods
  12. virtual int available() { return string.length() - position; }
  13. virtual int read() { return position < string.length() ? string[position++] : -1; }
  14. virtual int peek() { return position < string.length() ? string[position] : -1; }
  15. virtual void flush() { };
  16. // Print methods
  17. virtual size_t write(uint8_t c) { string += (char)c; };
  18. private:
  19. String &string;
  20. unsigned int length;
  21. unsigned int position;
  22. };
  23. #endif // _STRING_STREAM_H_INCLUDED_