8000 Stream: +helpers to stream regular String by d-a-v · Pull Request #9043 · esp8266/Arduino · GitHub
[go: up one dir, main page]

Skip to content

Stream: +helpers to stream regular String #9043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
String2Stream: +helpers to stream (temporary) streamed strings
    CS2Stream(sourceStringNotConsumed).sendAll(S2Stream(destString));
  • Loading branch information
d-a-v committed Dec 1, 2023
commit e25c4af1a5340d3d9228abee40f7e5e5775eb6a2
4 changes: 4 additions & 0 deletions cores/esp8266/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,21 +196,25 @@ class Stream: public Print {
// returns number of transferred bytes
size_t sendAvailable (Stream* to) { return sendGeneric(to, -1, -1, oneShotMs::alwaysExpired); }
size_t sendAvailable (Stream& to) { return sendAvailable(&to); }
size_t sendAvailable (Stream&& to) { return sendAvailable(&to); }

// transfers data until timeout
// returns number of transferred bytes
size_t sendAll (Stream* to, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendGeneric(to, -1, -1, timeoutMs); }
size_t sendAll (Stream& to, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendAll(&to, timeoutMs); }
size_t sendAll (Stream&& to, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendAll(&to, timeoutMs); }

// transfers data until a char is encountered (the char is swallowed but not transferred) with timeout
// returns number of transferred bytes
size_t sendUntil (Stream* to, const int readUntilChar, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendGeneric(to, -1, readUntilChar, timeoutMs); }
size_t sendUntil (Stream& to, const int readUntilChar, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendUntil(&to, readUntilChar, timeoutMs); }
size_t sendUntil (Stream&& to, const int readUntilChar, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendUntil(&to, readUntilChar, timeoutMs); }

// transfers data until requested size or timeout
// returns number of transferred bytes
size_t sendSize (Stream* to, const ssize_t maxLen, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendGeneric(to, maxLen, -1, timeoutMs); }
size_t sendSize (Stream& to, const ssize_t maxLen, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendSize(&to, maxLen, timeoutMs); }
size_t sendSize (Stream&& to, const ssize_t maxLen, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendSize(&to, maxLen, timeoutMs); }

// remaining size (-1 by default = unknown)
virtual ssize_t streamRemaining () { return -1; }
Expand Down
18 changes: 17 additions & 1 deletion cores/esp8266/StreamString.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

///////////////////////////////////////////////////////////////
// S2Stream points to a String and makes it a Stream
// ("String to Stream")
// (it is also the helper for StreamString)

class S2Stream: public Stream
Expand Down Expand Up @@ -192,7 +193,7 @@ class S2Stream: public Stream
}

// Reading this stream will mark the string as read without consuming
// (not enabled by default)
// (not enabled by default, see CS2Stream)
// Calling resetPointer() resets the read state and allows rereading.
void resetPointer(int pointer = 0)
{
Expand All @@ -204,6 +205,21 @@ class S2Stream: public Stream
int peekPointer; // -1:String is consumed / >=0:resettable pointer
};

///////////////////////////////////////////////////////////////
// CS2Stream is a S2Stream using an unmodifiable string by default
// ("Constant String to Stream")
// for equivalent with char* see StreamConstPtr(StreamDev.h)

class CS2Stream: public S2Stream
{
public:

CS2Stream(String& string) : S2Stream(string, 0) { }
CS2Stream(String* string) : S2Stream(string, 0) { }
void setConsume() = delete; // prevent from modifying the string
};

///////////////////////////////////////////////////////////////
// StreamString is a S2Stream holding the String

class StreamString: public String, public S2Stream
Expand Down
0