Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Input / Output System

The input/output of ELM is a merge of the C++ standard library system and of the Java standard library. From the former, it uses the "<<" and ">>" operators overload. From the latter, it implements the two IO access: low-level classes manage streams of bytes and the high-level provides formatting facilities. More...

Classes

class  ANSIManager
 
class  BlockInStream
 
class  BlockOutStream
 
class  InFileStream
 
class  Input
 
class  StringInput
 
class  FileInput
 
class  InStream
 
class  IOException
 
class  Monitor
 
class  OutFileStreal
 
class  Output
 
class  IntFormat
 
class  StringFormat
 
class  Tag< P >
 
class  FloatFormat
 
class  Printable< T, M >
 
class  StringOutput
 
class  FileOutput
 
class  OutStream
 
class  StreamPipe
 
class  TeeOutStream
 

Functions

IntFormat pointer (const void *p)
 
IntFormat byte (t::uint8 b)
 
io::Output cout (buf_out)
 
io::Output cerr (buf_err)
 
StringInput read (const char *s)
 
FileInput read (sys::Path path)
 
FileOutput write (sys::Path path)
 
FileOutput append (sys::Path path)
 
IntFormat base (int base, IntFormat fmt)
 
IntFormat bin (IntFormat fmt)
 
IntFormat hex (IntFormat fmt)
 
IntFormat width (int width, IntFormat fmt)
 
IntFormat align (alignment_t align, IntFormat fmt)
 
IntFormat left (IntFormat fmt)
 
IntFormat right (IntFormat fmt)
 
IntFormat center (IntFormat fmt)
 
IntFormat pad (char pad, IntFormat fmt)
 
IntFormat uppercase (IntFormat fmt)
 
IntFormat lowercase (IntFormat fmt)
 
IntFormat fmt (t::int8 i)
 
IntFormat fmt (t::uint8 i)
 
IntFormat fmt (t::int16 i)
 
IntFormat fmt (t::uint16 i)
 
IntFormat fmt (t::int32 i)
 
IntFormat fmt (t::uint32 i)
 
IntFormat fmt (t::int64 i)
 
IntFormat fmt (t::uint64 i)
 
template<class T , class M >
Printable< T, M > p (const T &data, const M &man)
 

Variables

io::Input cin
 
const EOL endl
 
FloatFormat percent = FloatFormat().width(5, 2).decimal().right()
 
io::Output cout
 
io::Output cerr
 
sys::SystemInStream & in = sys::stdin_object
 
sys::SystemOutStream & out = sys::stdout_object
 
sys::SystemOutStream & err = sys::stderr_object
 

Detailed Description

The input/output of ELM is a merge of the C++ standard library system and of the Java standard library. From the former, it uses the "<<" and ">>" operators overload. From the latter, it implements the two IO access: low-level classes manage streams of bytes and the high-level provides formatting facilities.

High-Level Input / Output

This level provides formatted input/output as in C++ standard library, that is, through the "<<" and ">>" operators. The formatted output class is Output and the formatted input class is Input.

Three global variables give access to the standard input/output:

Most basic types are supported (including String and CString). Some constants are provided for special output:

Unlike the C++ standard library, the output format is managed through special formatting objects. For integer, the work is performed by IntFormat that provides display features like representation base, field width, alignment, uppercase letters for base greater than 10, signess, padding. For float values, it exists the class FloatFormat and for strings, StringFormat can be used.

This format objects may easily be built using io::fmt() primitives and using internal constructors as in the example below:

cout << fmt(x).width(8).hex().right().width(8).pad('0');

A format can be built and used latter:

IntFormat address_fmt = IntFormat().width(8).hex().right().width(8).pad('0');
cout << address_fmt(x);

The errors are managed here using exception objects derived from the IOException class.

As with streams of the STL, the operators "<<" and ">>" can be overload to be adapted to custom types:

class T { ... };
io::Output& operator<<(io::Output& out, const T& v) { ...; return out; }
io::Input& operator>>(io::Input& out, T& v) { ...; return in; }

To group together input, output and error stream, the Monitor class provides the base of a configurable monitoring system with error and warning display support.

Formatting

The class IntFormat embeds the value of an integer and its format configuration. These objects are usually passed to Output object and is built from a list of usual inline functions (described below):

void *p;

The basic to display a formatted integer is to use a constructor inline function as io::fmt(). This builds an IntFormat object to display the passed integer. The format can then be modified by calling specific functions as listed below:

One way to implement the pointer of the above example is to define an inline function as this:

inline IntFormat pointer(void *p) {
return io::f(t::uint64(p)).width(16).pad('0').right().hex();
}

Notice the function io::f() that is a shortcut to create an IntFormat object.

Another way to define this format is to declare an IntFormat variable and to benefit from the overlaod of operator() of this class to pass the actual value to the format:

IntFormat pointer = IntFormat().width(16).pad('0').right().hex();

A last facility to specialize the way a data is formatted is the use of Tag class. This template class takes a class as parameter that will be called to perform the actual display. Using tags, it is possible to specialize the display of particular object inside the usual flow of "<<" operators. The example below displays a string escaping the special characters "<", ">" and "&" in order, for example, to output HTML text:

class Escape {
public:
typedef string t;
static void print(io::Output& out, t s) {
for(int i = 0; i < s.length(); i++)
switch(s[i]) {
case '>': out << "&gt;"; break;
case '<': out << "&lt;"; break;
case '&': out << "&amp;"; break;
default: out << s[i]; break;
}
}
};
string my_string;
cout << Tag<Escape>(my_string) << io::endl;

ELM proposes some predefined formats:

ANSI Codes

On OS's supporting them (Linux and MacOSX), ANSI codes allows to change the look of displayed text. ANSI codes are supported by io::Output class and can be used if the header file <elm/io/ansi.h> is include. The look change is implemented by sending special values to the output.

The text color can be changed using io::BLACK, io::RED, io::GREEN, io::YELLOW, io::BLUE, io::MAGENTA, io::CYAN or io::WHITE. Bright colors equivalent colors exists and are name by prefixing the color with "BRIGHT_": io::BRIGHT_XXX. Background color can also be customize by prefixing the color name with "BACK_": io::BACK_XXX.

Style of the characters can also be changed by displaying special values as io::BOLD, io::ITALIC, io::UNDERLINE, io::BLINK, ... To reset the display of the characters, one can use the value io::PLAIN.

Byte Streams

The low-level IO system is only responsible for exchanging streams of bytes.

All input streams must inherit from the InStream class and defines the following functions:

The errors are returned by the called functions. Either a positive value, or a value of InStream::FAILED (error on the media), or InStream::ENDED (end of the media reached). Information about the errors may be obtained by the InStream::lastErrorMessage() method.

All output streams must inherit from the OutStream class and defines the following functions:

The errors are returned as negative value by this functions. Information about the errors may be obtained by the OutStream::lastErrorMessage() method.

Byte-stream level of IO provides also some convenient classes:

Helper Classes and Functions

Some classes provides shortcut to perform input / output on files or on strings:

Tag Class

The class io::Tag can be used to display an object in a different as usual (that is not using the default overload of operator <<). This class takes a class P as generic parameter that must implement a static print function. It will be called with, as parameters, the output to use and the object to display.

The example below display integers in a special way, surrounded by "!". The display is handled by the class SpecInt. A tag for SpecInt is defined using a typedef and the integer is display in this special way using the tag type SPEC_INT.

class SpecInt {
public:
typedef int t;
static void print(io::Output& out, int i)
{ out << "!" << i << "!"; }
};
typedef io::Tag<SpecInt> SPEC_INT;
cout << SPEC_INT(666);

Printable Class

The class io::Printable and the function io::p() allows to perform a display requiring a third-party object (often called a manager). This means that the third-party object must define a function named print() and taking as parameter the object to display and the output.

In the example below, there is a special manager named IntManager that provides some integers as text. The print() function of this class is invoked using the function io::p().

class IntManager {
public:
static cstring t[4];
void print(int i, io::Output& out) const {
if(i >= 4)
out << "more";
else
out << t[i];
}
};
cstring IntManager::t[4] = { "zero", "one", "two", "three" };
IntManager man;
buf << io::p(2, man);

List Printer

The list printer can be used to display easily and conveniently a list of values inside an output << operator sequence. The class ListPrinter records the list and the separator. The display of the resulting object can be displayed directly using operator <<. A faster way to do this is to use the function io::list() as in the example below:

names.add("Albert");
names.add("Ben");
names.add("Cindy");
cout << "Names: " << io::list(names, ", ") << io::endl;

The output will be:

names: Albert, Ben, Cindy

Function Documentation

◆ align()

IntFormat align ( alignment_t  align,
IntFormat  fmt 
)
inline

#include <include/elm/io/Output.h>

Used the given alignment to display the integer in its field.

Parameters
alignAlignment.
fmtDisplayed integer.
Deprecated:

References IntFormat::align(), and elm::io::fmt().

Referenced by Debug::debugPrefixWrapped().

◆ append()

FileOutput append ( sys::Path  p)
inline

#include <include/elm/io/io.h>

Shortcut to append an output to a file.

Parameters
pPath to the file to append to. @raise io::IOException If the file cannot be opened.

Referenced by FileOutput::FileOutput().

◆ base()

IntFormat base ( int  base,
IntFormat  fmt 
)
inline

#include <include/elm/io/Output.h>

Format an integer with the given base.

Parameters
baseNumeric base.
fmtDisplayed integer.
Deprecated:

References IntFormat::base(), and elm::io::fmt().

Referenced by make::extend(), elm::serial2::operator&(), elm::serial2::operator<<(), elm::serial2::operator>>(), Path::relativeTo(), Input::scanLLong(), Input::scanLong(), Input::scanULLong(), Input::scanULong(), and elm::io::test_base().

◆ bin()

IntFormat bin ( IntFormat  fmt)
inline

#include <include/elm/io/Output.h>

Used a binary base to display an integer.

Parameters
fmtDisplayed integer.
Deprecated:

References IntFormat::bin(), and elm::io::fmt().

◆ byte()

IntFormat byte ( t::uint8  b)

#include <include/elm/io/Output.h>

Format an integer to display it as an hexadecimal byte.

Parameters
bByte to format.
Returns
Formatted byte.

References elm::io::fmt(), IntFormat::hex(), IntFormat::pad(), IntFormat::right(), and IntFormat::width().

Referenced by OutStream::write(), VarExpander::write(), and BufferedOutStream::write().

◆ center()

IntFormat center ( IntFormat  fmt)
inline

#include <include/elm/io/Output.h>

Center the integer in its field.

Parameters
fmtDisplayed integer.
Deprecated:

References IntFormat::center(), and elm::io::fmt().

◆ cerr()

io::Output elm::cerr ( buf_err  )

#include <src/system_SystemIO.cpp>

Standard error output.

◆ cout()

io::Output elm::cout ( buf_out  )

#include <src/system_SystemIO.cpp>

Standard output.

◆ fmt() [1/8]

IntFormat fmt ( t::int16  i)
inline

#include <include/elm/io/Output.h>

Build a formatted signed 16-bit integer.

◆ fmt() [2/8]

IntFormat fmt ( t::int32  i)
inline

#include <include/elm/io/Output.h>

Build a formatted signed 32-bit integer.

◆ fmt() [3/8]

IntFormat fmt ( t::int64  i)
inline

#include <include/elm/io/Output.h>

Build a formatted signed 64-bit integer.

◆ fmt() [4/8]

◆ fmt() [5/8]

IntFormat fmt ( t::uint16  i)
inline

#include <include/elm/io/Output.h>

Build a formatted unsigned 16-bit integer.

◆ fmt() [6/8]

IntFormat fmt ( t::uint32  i)
inline

#include <include/elm/io/Output.h>

Build a formatted unsigned 32-bit integer.

◆ fmt() [7/8]

IntFormat fmt ( t::uint64  i)
inline

#include <include/elm/io/Output.h>

Build a formatted unsigned 64-bit integer.

◆ fmt() [8/8]

IntFormat fmt ( t::uint8  i)
inline

#include <include/elm/io/Output.h>

Build a formatted unsigned 8-bit integer.

◆ hex()

IntFormat hex ( IntFormat  fmt)
inline

#include <include/elm/io/Output.h>

Used an hexadecimal base to display an integer.

Parameters
fmtDisplayed integer.
Deprecated:

References elm::io::fmt(), and IntFormat::hex().

Referenced by WAHVector::__dump(), GroupedGC::mark(), SimpleGC::mark(), and MD5::print().

◆ left()

IntFormat left ( IntFormat  fmt)
inline

#include <include/elm/io/Output.h>

Align the integer to the left in its field.

Parameters
fmtDisplayed integer.
Deprecated:

References elm::io::fmt(), and IntFormat::left().

Referenced by Tree::Node::_left(), GenTree< T, K, C, A >::VisitStack::copyLeft(), and TreeBag< value_t, AssocComparator< K, T, Comparator< K > > >::remove().

◆ lowercase()

IntFormat lowercase ( IntFormat  fmt)
inline

#include <include/elm/io/Output.h>

Select lowercase characters for digits bigger than 10.

Parameters
fmtDisplayed integer.
Deprecated:

References elm::io::fmt(), and IntFormat::lower().

◆ p()

Printable< T, M > p ( const T &  data,
const M &  man 
)
inline

#include <include/elm/io/Output.h>

Fast building of a managed printable. Look to Printable for more details.

Parameters
dataData to print.
manManager of the data.

Referenced by inst< T >::_free(), CleanList::add(), ListGC::allocate(), ListGC::block_t::block(), ListGC::clean(), HashTable< Pair< const void *, bool >, AssocHashKey< const void *, bool, HashKey< const void * > >, DefaultAlloc >::copy(), Buffer::copyFrom(), Buffer::copyTo(), elm::count(), BlockAllocatorWithGC< node_t >::destroy(), Path::PathIter::ended(), Path::PathIter::equals(), String::escape(), elm::sys::evaluate(), GenTree< T, IdAdapter< T >, elm::Comparator< T > >::exchange(), elm::exists(), elm::find(), Vector< elm::dtd::AbstractAttribute * >::find(), GenTree< T, IdAdapter< T >, elm::Comparator< T > >::find(), elm::forall(), DefaultAllocatorDelegate::free(), AllocatorDelegate< A >::free(), EquivManager< T >::free(), BlockAllocator< T >::free(), CompareManager< T, C, E, A >::free(), HashManager< K, H, A >::free(), BinomialQueue< T, C, A >::get(), HashMap< elm::CString, void * >::get(), Map< Pair< K, K >, T, segment_comparator_t >::get(), Section::getList(), HashKey< Pair< T1, T2 > >::hash(), elm::hash_jenkins(), elm::hash_ptr(), Map< Pair< K, K >, T, segment_comparator_t >::hasKey(), CString::indexOf(), String::indexOf(), Vector< elm::dtd::AbstractAttribute * >::indexOf(), Plugger::isPlugged(), Path::PathIter::item(), CString::lastIndexOf(), String::lastIndexOf(), Vector< elm::dtd::AbstractAttribute * >::lastIndexOf(), File::load(), GenTree< T, IdAdapter< T >, elm::Comparator< T > >::lookup(), Builder< K, T, Comparator< K > >::make(), SegmentBuilder< K, T, C >::make(), GroupedGC::mark(), SimpleGC::mark(), elm::mismatch(), Path::PathIter::next(), Bag< T >::operator delete(), SharedPtr< T >::operator!=(), elm::operator<<(), elm::io::operator<<(), LockPtr< elm::sys::FileItem >::operator=(), SharedPtr< T >::operator=(), RefPair< T1, T2 >::operator=(), SharedPtr< T >::operator==(), elm::operator>>(), IntFormat::pad(), FloatFormat::pad(), StringFormat::pad(), Manager::parse(), elm::io::pointer(), Output::print(), elm::product(), HashMap< elm::CString, void * >::putAll(), Parser::recordID(), Parser::recordPatch(), AbstractTree::remove(), HashTable< Pair< const void *, bool >, AssocHashKey< const void *, bool, HashKey< const void * > >, DefaultAlloc >::remove(), GenTree< T, IdAdapter< T >, elm::Comparator< T > >::remove(), String::replace(), ListGC::runGC(), elm::select(), elm::select_iter(), InstanceType::typeFor(), elm::unique(), and VarExpander::write().

◆ pad()

IntFormat pad ( char  pad,
IntFormat  fmt 
)
inline

#include <include/elm/io/Output.h>

Select the padding character.

Parameters
padPadding character.
fmtDisplayed integer.
Deprecated:

References elm::io::fmt(), and IntFormat::pad().

Referenced by MD5::print().

◆ pointer()

IntFormat pointer ( const void p)

◆ read() [1/2]

StringInput read ( const char *  s)
inline

#include <include/elm/io/io.h>

Shortcut to perform an input from a string.

Parameters
sString to use as input.

◆ read() [2/2]

FileInput read ( sys::Path  p)
inline

#include <include/elm/io/io.h>

Shortcut to perform an input from a file.

Parameters
pPath to the file to read from. @raise io::IOException If the file cannot be opened.

Referenced by UnixInStream::read(), elm::xom::read_callback(), Saver::setReadable(), and Input::swallow().

◆ right()

IntFormat right ( IntFormat  fmt)
inline

#include <include/elm/io/Output.h>

Align the integer to the right in its field.

Parameters
fmtDisplayed integer.
Deprecated:

References elm::io::fmt(), and IntFormat::right().

Referenced by Tree::Node::_right(), GenTree< T, K, C, A >::VisitStack::copyRight(), Tree< K, T, C >::find(), MD5::print(), and TreeBag< value_t, AssocComparator< K, T, Comparator< K > > >::remove().

◆ uppercase()

IntFormat uppercase ( IntFormat  fmt)
inline

#include <include/elm/io/Output.h>

Select uppercase characters for digits bigger than 10.

Parameters
fmtDisplayed integer.
Deprecated:

References elm::io::fmt(), and IntFormat::upper().

◆ width()

IntFormat width ( int  width,
IntFormat  fmt 
)
inline

#include <include/elm/io/Output.h>

Select the width of field where the integer will be displayed to.

Parameters
widthField width.
fmtDisplayed integer.
Deprecated:

References elm::io::fmt(), and IntFormat::width().

Referenced by Debug::debugPrefixWrapped(), and MD5::print().

◆ write()

FileOutput write ( sys::Path  p)
inline

#include <include/elm/io/io.h>

Shortcut to perform an output to a file.

Parameters
pPath to the file to write to. @raise io::IOException If the file cannot be created.

Referenced by UnixOutStream::write().

Variable Documentation

◆ cerr

◆ cin

io::Input cin

#include <include/elm/io/io.h>

Formatted input.

◆ cout

◆ endl

◆ err

sys::SystemOutStream & err = sys::stderr_object

#include <include/elm/sys/SystemIO.h>

Standard error stream.

Referenced by SystemException::error(), Plugger::getLastError(), Plugger::lastError(), Plugger::plugFile(), ProcessBuilder::run(), and WinOutStream::write().

◆ in

sys::SystemInStream & in = sys::stdin_object

#include <include/elm/sys/SystemIO.h>

Standard input stream.

Referenced by ProcessBuilder::run(), Input::def_scanner< T >::scan(), and Input::enum_scanner< T >::scan().

◆ out

sys::SystemOutStream & out = sys::stdout_object

◆ percent

FloatFormat percent = FloatFormat().width(5, 2).decimal().right()

#include <include/elm/io/Output.h>

Float format to display a percentage.

elm::io::p
Printable< T, M > p(const T &data, const M &man)
Definition: Output.h:302
elm::io::out
sys::SystemOutStream & out
Definition: system_SystemIO.cpp:122
elm::io::IntFormat::hex
IntFormat & hex(void)
Definition: Output.h:85
elm::cstring
CString cstring
Definition: CString.h:62
elm::io::pointer
IntFormat pointer(const void *p)
Definition: io_Output.cpp:871
elm::cout
io::Output cout
elm::io::IntFormat::width
IntFormat & width(int w)
Definition: Output.h:86
elm::io::operator<<
Output & operator<<(Output &out, const T &v)
Definition: Output.h:216
elm::io::list
ListPrinter< T > list(const T &l, cstring s="", typename ListPrinter< T >::fun_t f=ListPrinter< T >::asis)
Definition: Output.h:321
elm::t::uint64
unsigned long uint64
Definition: arch.h:33
elm::io::endl
const EOL endl
Definition: io_Output.cpp:880
elm::io::fmt
IntFormat fmt(t::int8 i)
Definition: Output.h:271
elm::t::uint32
unsigned int uint32
Definition: arch.h:31
elm::io::IntFormat::pad
IntFormat & pad(char p)
Definition: Output.h:94
elm::io::IntFormat::right
IntFormat & right(void)
Definition: Output.h:90
elm::operator>>
io::StringInput operator>>(const string &str, T &val)
Definition: AutoString.h:82
elm::io::in
sys::SystemInStream & in
Definition: system_SystemIO.cpp:116
Vector