Skip to content

Commit f426344

Browse files
authored
Update ostream
allow use of F() macro for Strings: cout << F("Hello") << endl;
1 parent 007aa19 commit f426344

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/ostream

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
#include "cstdio"
2828
#include "ostream_helpers"
2929

30+
#ifdef ARDUINO_ARCH_AVR
31+
#include <avr/pgmspace.h> // typedef PGM_P, pgm_read_byte()
32+
#include "WString.h" // class __FlashStringHelper
33+
#endif
34+
3035
#pragma GCC visibility push(default)
3136

3237
namespace std {
@@ -407,6 +412,24 @@ namespace std {
407412
return out;
408413
}
409414

415+
#ifdef ARDUINO_ARCH_AVR
416+
//support strings from flash memory [PI]
417+
//stolen from Arduino Print.cpp
418+
//sample use: cout << F("Hello") << endl;
419+
template<class charT, class traits> _UCXXEXPORT basic_ostream<charT,traits>&
420+
operator<<(basic_ostream<charT,traits>& out, const __FlashStringHelper* c)
421+
{
422+
typename basic_ostream<charT,traits>::sentry s(out);
423+
PGM_P p = reinterpret_cast<PGM_P>(c);
424+
while (true) {
425+
charT x = pgm_read_byte(p++);
426+
if (x == 0) break;
427+
out.put(x);
428+
}
429+
return out;
430+
}
431+
#endif
432+
410433
// partial specializations
411434
template<class traits> _UCXXEXPORT basic_ostream<char,traits>&
412435
operator<<(basic_ostream<char,traits>& out, const char* c)
@@ -416,6 +439,24 @@ namespace std {
416439
return out;
417440
}
418441

442+
#ifdef ARDUINO_ARCH_AVR
443+
//support strings from flash memory [PI]
444+
//stolen from Arduino Print.cpp
445+
//sample use: cout << F("Hello") << endl;
446+
template<class traits> _UCXXEXPORT basic_ostream<char,traits>&
447+
operator<<(basic_ostream<char,traits>& out, const __FlashStringHelper* c) //[PI: New] stolen from Arduino Print.cpp
448+
{
449+
typename basic_ostream<char,traits>::sentry s(out);
450+
PGM_P p = reinterpret_cast<PGM_P>(c);
451+
while (true) {
452+
char x = pgm_read_byte(p++);
453+
if (x == 0) break;
454+
out.put(x);
455+
}
456+
return out;
457+
}
458+
#endif
459+
419460
#ifdef __UCLIBCXX_HAS_WCHAR__
420461
template<class traits> _UCXXEXPORT basic_ostream<wchar_t,traits>&
421462
operator<<(basic_ostream<wchar_t,traits>& out, const char* c)

0 commit comments

Comments
 (0)