Evo C++ Library v0.5.1
Stream Formatting

Evo supports extensive stream formatting.

Stream Formatting

Text streams can be written with formatting using the stream operator (<<).

The following examples write format to stdout, but also apply to any Stream type (File, Socket, PipeOut, etc).

#include <evo/io.h>
using namespace evo;
int main() {
// Get console streams, use stdout
Console& c = con();
// Write a line with a string and number
c.out << "Testing " << 123 << NL;
// Write another with another string and number
c.out << "Foobar " << 1.23 << NL;
return 0;
}

Streams also support explicit formatting to customize the output:

#include <evo/io.h>
using namespace evo;
int main() {
// Get console streams, use stdout
Console& c = con();
// Format line as: Testing aaaa...
c.out << "Testing " << FmtChar('a', 4) << "..." << NL;
// Format line as: Testing 0123...
c.out << "Testing " << FmtInt(123, fDEC).width(4) << "..." << NL;
// Format line as: Testing 0x07B...
c.out << "Testing " << FmtUInt(0x7B, fHEX, fPREFIX2, 3) << "..." << NL;
// Format line as: Testing 1.230...
c.out << "Testing " << FmtFloat(1.23f, 3) << "..." << NL;
// Format line as: Testing 01.230...
c.out << "Testing " << FmtFloatD(1.23, 3, 6) << "..." << NL;
return 0;
}

Formatting attributes must be explicitly passed with each value – Stream itself doesn't keep any formatting state.

Formatting types used with operator<<():

Stream Newlines

Streams have a default newline value (see constructor) formatted with NL – default is determined by the stream itself.

#include <evo/io.h>
using namespace evo;
int main() {
{
// Write file with Windows newlines (nCRLF) by default (regardless of system)
File file("windows.txt", oWRITE_NEW, nCRLF);
file << "Windows" << NL;
// Add string with newline (converted to default newline)
file << "foo\n";
// You can also explicitly add other newline chars (not converted)
file << nLF;
// Or write system default newline (depends on system)
file << NL_SYS;
}
{
// Write file with Linux/Unix newlines (nLF) by default (regardless of system)
File file("linux.txt", oWRITE_NEW, nLF);
file << "Linux/Unix" << NL;
}
return 0;
}
Stream Formatting With State

Create a Stream::Format object (where Stream is the stream type used) to keep formatting state and make formatting "sticky" through that object, similar to String::Format:

#include <evo/io.h>
using namespace evo;
int main() {
// Get formatting object, use stdout
Console::Format out(con().out);
// Set int formatting to use hex with prefix, padded to 2 digits
out << FmtSetInt(fHEX, fPREFIX2, 2);
// Format line with some numbers as: 0x01,0x02,0x03
out << 1 << ',' << 2 << ',' << 3 << NL;
// Set int formatting to use octal with no prefix
out << fOCT << fPREFIX0;
// Format line with some numbers in octal as: 7,10,11
out << 7 << ',' << 8 << ',' << 9 << NL;
// Set int formatting to use decimal and pad to 3 digits, set field width to 5 and align-right
out << FmtSetInt(fDEC, 3) << FmtSetField(fRIGHT, 5, '.');
// Format line with some numbers in decimal as: ..012,..034
out << 12 << ',' << 34 << NL;
}

This can also be done as a one-liner using a temporary formatter:

#include <evo/io.h>
using namespace evo;
int main() {
// Open new file for writing
File file("file.txt", oWRITE_NEW);
// Format line with some numbers as hex with prefix: 0x1,0x2,0x3
File::Format(file) << fHEX << fPREFIX2 << 1 << ',' << 2 << ',' << 3 << NL;
return 0;
}

Sticky formatting is useful for applying the same formatting to many fields. You may still use explicit stream formatting (see previous section) to override sticky formatting.

Formatting types for setting attributes with operator<<()

Smart Quoting

See also: Smart Quoting