Skip to content

Instantly share code, notes, and snippets.

@cmaglie cmaglie/StringStream.h
Last active Mar 7, 2019

Embed
What would you like to do?
StringStream class for Arduino
#ifndef _STRING_STREAM_H_INCLUDED_
#define _STRING_STREAM_H_INCLUDED_
#include <Stream.h>
class StringStream : public Stream
{
public:
StringStream(const String &s) : string(s), position(0) { }
// Stream methods
virtual int available() { return string.length() - position; }
virtual int read() { return position < string.length() ? string[position++] : -1; }
virtual int peek() { return position < string.length() ? string[position] : -1; }
virtual void flush() { };
// Print methods
virtual size_t write(uint8_t c) { string += (char)c; };
private:
String &string;
int length;
int position;
};
#endif // _STRING_STREAM_H_INCLUDED_
@jindrichsirucek

This comment has been minimized.

Copy link

jindrichsirucek commented Sep 25, 2015

Hi, it looks good this library..

do You know why is not working for me?

StringStream.h:9:58: error: invalid initialization of reference of type 'String&' from expression of type 'const String'
StringStream(const String &s) : string(s), position(0) { }
^
Error compiling.

@pingdynasty

This comment has been minimized.

Copy link

pingdynasty commented Nov 6, 2015

Remove const on line 9.
Also change line 17 to virtual size_t write(uint8_t c) { string += (char)c; return 1; };
And, to fix the compiler warnings, lines 21/22 to:
unsigned int length;
unsigned int position;

@sanfx

This comment has been minimized.

Copy link

sanfx commented Jan 27, 2017

would it be possible to achieve some thing like this:

StringStream sqlQuery;
    sqlQuery  << "INSERT INTO arduinoSensorData.sensorLog (out_temperature,  " 
        << "out_humidity,  drwngRoom_temperature, drwngRoom_humidity, "
        << "pot1_soilMoisture, pot1_avg_SoilMoisture, wateringPot1) VALUES ('" 
        << outdoorTempInC << "', '" << outoorHumidity << "', '" << indorTempinC
        << "', '" << humidity << "', '" << val << "', '" << avgVal << "', '"
        << statusMsg << "');";

tried it gives an error,

error: no matching function for call to 'StringStream::StringStream()'
     StringStream sqlQuery;

how do I achieve the above ?

@arnavb

This comment has been minimized.

Copy link

arnavb commented Feb 18, 2018

@sanfx, this is really late, but you would have to implement a operator<< for the class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.