FastArduino
v1.8
C++ library to build fast but small Arduino/AVR projects
|
Go to the documentation of this file.
173 explicit operator bool()
const
337 flags_ = (flags_ & ~mask) | (
flags & mask);
438 precision_ = rhs.precision_;
455 static constexpr uint8_t DOUBLE_BUFFER_SIZE =
MAX_PRECISION + 7 + 1;
465 void convert(
const char* token,
bool& value)
468 value = (strcmp(token,
"true") == 0);
470 value = (atol(token) != 0);
472 void convert(
const char* token,
double& value)
475 double val = strtod(token, &endptr);
481 const char* binary_token(
const char* token)
const
483 if ((base() == 2) && ((strncmp(token,
"0b0", 3) == 0) || (strncmp(token,
"0b1", 3) == 0)))
return token + 2;
486 bool convert(
const char* token,
long& value)
489 long val = strtol(binary_token(token), &endptr, base());
494 return (endptr != token);
496 bool convert(
const char* token,
unsigned long& value)
499 unsigned long val = strtoul(binary_token(token), &endptr, base());
504 return (endptr != token);
506 void convert(
const char* token,
int& value)
509 if (convert(token, val)) value = (int) val;
511 void convert(
const char* token,
unsigned int& value)
514 if (convert(token, val)) value = (
unsigned int) val;
518 void convert(ostreambuf& out,
int value)
const
521 char buffer[
sizeof(int) * 8 + 1];
522 format_number(out, itoa(value, buffer, base()));
524 void convert(ostreambuf& out,
unsigned int value)
const
527 char buffer[
sizeof(
unsigned int) * 8 + 1];
528 format_number(out, utoa(value, buffer, base()));
530 void convert(ostreambuf& out,
long value)
const
533 char buffer[
sizeof(long) * 8 + 1];
534 format_number(out, ltoa(value, buffer, base()));
536 void convert(ostreambuf& out,
unsigned long value)
const
539 char buffer[
sizeof(
unsigned long) * 8 + 1];
540 format_number(out, ultoa(value, buffer, base()));
542 static size_t double_digits(
double value)
544 int digits = int(log10(fabs(value)));
548 return size_t(digits + 1);
550 bool is_too_large(
double value)
const
553 return (1 + double_digits(value) + 1 +
precision() + 1) > DOUBLE_BUFFER_SIZE;
555 void convert(ostreambuf& out,
double value)
const
559 char buffer[DOUBLE_BUFFER_SIZE];
563 const uint8_t DTOSTRE_MAX_PRECISION = 7;
569 const uint8_t EXPONENT_SIZE = 1 + 1 + 2;
571 char* exponent = strchr(buffer,
'e');
572 uint8_t added_zeros =
precision() - DTOSTRE_MAX_PRECISION;
573 memmove(exponent + added_zeros, exponent, EXPONENT_SIZE + 1);
575 memset(exponent,
'0', added_zeros);
585 char* dp = strchr(buffer,
'.');
589 char* reverse = buffer + strlen(buffer) - 1;
590 while ((reverse != dp) && (*reverse ==
'0')) *reverse-- = 0;
592 if (reverse == dp) *reverse = 0;
596 justify(out, buffer, add_sign(buffer,
true),
nullptr);
598 void convert(ostreambuf& out,
char value)
const
603 justify(out, buffer,
false,
nullptr);
605 void convert(ostreambuf& out,
bool value)
const
608 justify(out, (value ?
F(
"true") :
F(
"false")));
610 convert(out, (value ? 1 : 0));
613 void upper(
char* input,
bool is_float =
false)
const
618 const char* prefix_base()
const
632 bool add_sign(
const char* input,
bool is_float =
false)
const
634 return (
flags() &
showpos) && ((
flags() &
dec) || is_float) && (input[0] !=
'+') && (input[0] !=
'-');
637 void format_number(ostreambuf& out,
char* input)
const
640 justify(out, input, add_sign(input), prefix_base());
643 void output_number(ostreambuf& out,
const char* input,
bool add_sign,
const char* prefix)
const
645 if (add_sign) out.put_(
'+',
false);
646 if (prefix) out.sputn(prefix);
650 void output_filler(ostreambuf& out,
char filler, uint8_t size)
const
652 while (size--) out.put_(filler,
false);
655 void justify(ostreambuf& out,
const char* input,
bool add_sign,
const char* prefix)
const
661 size_t len = strlen(input) + (prefix ? strlen(prefix) : 0) + (add_sign ? 1 : 0);
664 uint8_t add =
width() - len;
667 output_number(out, input, add_sign, prefix);
668 output_filler(out,
fill(), add);
673 output_filler(out,
fill(), add);
674 output_number(out, input, add_sign, prefix);
680 output_number(out, input, add_sign, prefix);
683 void justify(ostreambuf& out,
const flash::FlashStorage* input)
const
685 size_t len = strlen_P((
const char*) input);
688 uint8_t add =
width() - len;
692 output_filler(out,
fill(), add);
697 output_filler(out,
fill(), add);
718 uint8_t precision_ = 6;
729 template<
typename FSTREAM>
inline void skipws(FSTREAM& stream)
738 template<
typename FSTREAM>
inline void noskipws(FSTREAM& stream)
747 template<
typename FSTREAM>
inline void bin(FSTREAM& stream)
756 template<
typename FSTREAM>
inline void oct(FSTREAM& stream)
765 template<
typename FSTREAM>
inline void dec(FSTREAM& stream)
774 template<
typename FSTREAM>
inline void hex(FSTREAM& stream)
788 template<
typename FSTREAM>
inline void boolalpha(FSTREAM& stream)
801 template<
typename FSTREAM>
inline void noboolalpha(FSTREAM& stream)
821 template<
typename FSTREAM>
inline void showbase(FSTREAM& stream)
836 template<
typename FSTREAM>
inline void noshowbase(FSTREAM& stream)
849 template<
typename FSTREAM>
inline void showpos(FSTREAM& stream)
862 template<
typename FSTREAM>
inline void noshowpos(FSTREAM& stream)
877 template<
typename FSTREAM>
inline void uppercase(FSTREAM& stream)
892 template<
typename FSTREAM>
inline void nouppercase(FSTREAM& stream)
906 template<
typename FSTREAM>
inline void unitbuf(FSTREAM& stream)
922 template<
typename FSTREAM>
inline void nounitbuf(FSTREAM& stream)
935 template<
typename FSTREAM>
inline void left(FSTREAM& stream)
948 template<
typename FSTREAM>
inline void right(FSTREAM& stream)
982 template<
typename FSTREAM>
inline void fixed(FSTREAM& stream)
1000 template<
typename FSTREAM>
inline void scientific(FSTREAM& stream)
void showpos(FSTREAM &stream)
Set the ios::showpos format flag for stream.
uint8_t precision() const
Get the current precision (default = 6) used for formatted floating values output.
static constexpr fmtflags left
Pad all output to width() characters, with fill() character appended at the end so that the output ap...
static constexpr fmtflags showpos
Write non-negative numerical values preceded by +.
void setf(fmtflags flags)
Set this stream's format flags whose bits are set in flags, leaving unchanged the rest.
void fill(char fill)
Set fill as new fill character for this stream.
uint16_t fmtflags
Bitmask type to represent stream format flags.
void showbase(FSTREAM &stream)
Set the ios::showbase format flag for stream.
static constexpr fmtflags basefield
Bitmask constant used with setf(fmtflags, fmtflags) when changing the output base format.
void flags(fmtflags flags)
Set new format flags for this stream.
static constexpr fmtflags floatfield
Bitmask constant used with setf(fmtflags, fmtflags) when changing the floating point output represent...
void noshowbase(FSTREAM &stream)
Clear the ios::showbase format flag for stream.
bool bad() const
Return true if a non-recoverable error has occurred on the associated stream.
void oct(FSTREAM &stream)
Manipulator for an output or input stream, which will set the base, used to represent (output) or int...
ios_base & copyfmt(const ios_base &rhs)
Copy formatting information from rhs to this stream.
static constexpr fmtflags oct
Read or write integral values using octal (0..7) base format.
void fixed(FSTREAM &stream)
Set the ios::floatfield format flag for stream to ios::fixed.
char fill() const
Return the fill character.
void noshowpos(FSTREAM &stream)
Clear the ios::showpos format flag for stream.
void setstate(iostate state)
Set the stream error flags state in addition to currently set flags.
bool eof() const
Return true if the associated stream has reached end-of-file.
static constexpr iostate badbit
This bit is set when an irrecoverable stream error has occurred, e.g.
static constexpr fmtflags boolalpha
Read or write bool values as alphabetic string (true or false).
fmtflags flags() const
Return the format flags currently selected in this stream.
static constexpr fmtflags uppercase
Write uppercase letters instead of lowercase in certain insertion operations.
uint8_t width() const
Get the current minimum width value (default = 0) used for formatted output.
static void init()
Performs special initialization for the target MCU.
uint8_t iostate
Bitmask type to represent stream state flags.
void hex(FSTREAM &stream)
Manipulator for an output or input stream, which will set the base, used to represent (output) or int...
void scientific(FSTREAM &stream)
Set the ios::floatfield format flag for stream to ios::scientific.
void boolalpha(FSTREAM &stream)
Set the ios::boolalpha format flag for stream.
static constexpr iostate goodbit
No error; always 0.
iostate rdstate() const
Return the current stream error state.
Base class for formatted streams.
void nounitbuf(FSTREAM &stream)
Clear the ios::unitbuf format flag for stream.
void skipws(FSTREAM &stream)
Manipulator for an input stream, which will activate whitespace discarding before formatted input ope...
static constexpr fmtflags showbase
Write integral values prefixed by their base:
void right(FSTREAM &stream)
Set the ios::adjustfield format flag for stream to ios::right, thus adjusting next output to the righ...
static constexpr uint8_t MAX_PRECISION
The maximum allowed precision.
static constexpr fmtflags fixed
Write floating point values in scientific notation.
static constexpr fmtflags skipws
Skip leading spaces on certain extraction (read) operations.
static constexpr iostate failbit
This bit is set when an input or operation failed due to a formatting error during extraction.
static constexpr fmtflags dec
Read or write integral values using decimal (0..9) base format.
void dec(FSTREAM &stream)
Manipulator for an output or input stream, which will set the base, used to represent (output) or int...
void clear(iostate state=goodbit)
Set the stream error state flags by assigning them the value of state.
void noskipws(FSTREAM &stream)
Manipulator for an input stream, which will deactivate whitespace discarding before formatted input o...
static constexpr fmtflags unitbuf
Flush output after each insertion operation.
Defines C++-like streams API, based on circular buffers for input or output.
void precision(uint8_t precision)
Set precision (number of digits after decimal point) used for displaying floating values.
void bin(FSTREAM &stream)
Manipulator for an output or input stream, which will set the base, used to represent (output) or int...
void unsetf(fmtflags flags)
Clear this stream's format flags whose bits are set in flags.
void uppercase(FSTREAM &stream)
Set the ios::uppercase format flag for stream.
void noboolalpha(FSTREAM &stream)
Clear the ios::boolalpha format flag for stream.
bool operator!() const
Return true if an error has occurred on the associated stream, since last time state was reset (clear...
void left(FSTREAM &stream)
Set the ios::adjustfield format flag for stream to ios::left, thus adjusting next output to the left.
void nouppercase(FSTREAM &stream)
Clear the ios::uppercase format flag for stream.
void setf(fmtflags flags, fmtflags mask)
Set this stream's format flags whose bits are set in both flags and mask, and clears the format flags...
static constexpr fmtflags bin
Read or write integral values using binary (0,1) base format.
static constexpr iostate eofbit
This bit is set if the stream has unexpectedly reached its end during an extraction.
#define F(ptr)
Force string constant to be stored as flash storage.
static constexpr fmtflags scientific
Write floating point values in fixed-point notation.
void width(uint8_t width)
Set minimum width used for displaying values.
C++-like std::iostream facilities.
static constexpr fmtflags hex
Read or write integral values using hexadecimal (0..9,A..F) base format.
static constexpr fmtflags adjustfield
Bitmask constant used with setf(fmtflags, fmtflags) when changing the output adjustment.
bool fail() const
Return true if an error has occurred on the associated stream, since last time state was reset (clear...
void defaultfloat(FSTREAM &stream)
Set the ios::floatfield format flag for stream to ios::defaultfloat.
void unitbuf(FSTREAM &stream)
Set the ios::unitbuf format flag for stream.
static constexpr fmtflags right
Pad all output to width() characters, with fill() character added at the beginning so that the output...