4 * \brief Basic IO stream
7 * (c) 2009 Alexander Warg <warg@os.inf.tu-dresden.de>
8 * economic rights: Technische Universität Dresden (Germany)
10 * This file is part of TUD:OS and distributed under the terms of the
11 * GNU General Public License 2.
12 * Please see the COPYING-GPL-2 file for details.
14 * As a special exception, you may use this file as part of a free software
15 * library without restriction. Specifically, if other files instantiate
16 * templates or use macros or inline functions from this file, or you compile
17 * this file and link it with other files to produce an executable, this
18 * file does not by itself cause the resulting executable to be covered by
19 * the GNU General Public License. This exception does not however
20 * invalidate any other reasons why the executable file might be covered by
21 * the GNU General Public License.
28 * \brief Modifier class for the IO stream.
30 * An IO Modifier can be used to change properties of an IO stream
31 * for example the number format.
36 IOModifier( int x ) : mod(x) {}
37 bool operator == (IOModifier o) { return mod == o.mod; }
38 bool operator != (IOModifier o) { return mod != o.mod; }
44 * \brief Backend to write or read stream data.
52 friend class BasicOStream;
58 virtual ~IOBackend() {}
60 virtual void write(char const *str, unsigned len) = 0;
63 void write(IOModifier m);
64 void write(long long int c, int len);
65 void write(long long unsigned c, int len);
66 void write(long long unsigned c, unsigned char base = 10,
67 unsigned char len = 0, char pad = ' ');
80 * \brief Write-only backend for stream data.
85 BasicOStream(IOBackend *b)
89 void write(char const *str, unsigned len)
90 { if(iob) iob->write(str,len); }
92 void write(long long int c, int len)
93 { if(iob) iob->write(c,len); }
95 void write(long long unsigned c, unsigned char base = 10,
96 unsigned char len = 0, char pad = ' ')
97 { if(iob) iob->write(c, base, len, pad); }
99 void write(long long unsigned c, int len)
100 { if(iob) iob->write(c,len); }
102 void write(IOModifier m)
103 { if(iob) iob->write(m); }
105 IOBackend::Mode be_mode() const
106 { if(iob) return iob->mode(); return 0; }
108 void be_mode(IOBackend::Mode m)
109 { if(iob) iob->mode(m); }
118 * \brief Container class describing a the number format.
123 IONumFmt(unsigned long long n, unsigned char base = 10,
124 unsigned char len = 0, char pad = ' ')
125 : n(n), base(base), len(len), pad(pad)
128 BasicOStream &print(BasicOStream &o) const;
131 unsigned long long n;
132 unsigned char base, len;
136 inline IONumFmt n_hex(unsigned long long n) { return IONumFmt(n, 16); }
139 * \brief Modifies the stream to print numbers as hexadecimal values.
141 extern IOModifier const hex;
144 * \brief Modifies the stream to print numbers as decimal values.
146 extern IOModifier const dec;
149 BasicOStream &IONumFmt::print(BasicOStream &o) const
151 o.write(n, base, len, pad);
162 operator << (L4::BasicOStream &s, char const * const str)
166 s.write("(NULL)", 6);
171 for(; str[l]!=0; l++)
179 operator << (L4::BasicOStream &s, signed short u)
181 s.write((long long signed)u,-1);
187 operator << (L4::BasicOStream &s, signed u)
189 s.write((long long signed)u,-1);
195 operator << (L4::BasicOStream &s, signed long u)
197 s.write((long long signed)u,-1);
203 operator << (L4::BasicOStream &s, signed long long u)
211 operator << (L4::BasicOStream &s, unsigned short u)
213 s.write((long long unsigned)u,-1);
219 operator << (L4::BasicOStream &s, unsigned u)
221 s.write((long long unsigned)u,-1);
227 operator << (L4::BasicOStream &s, unsigned long u)
229 s.write((long long unsigned)u,-1);
235 operator << (L4::BasicOStream &s, unsigned long long u)
243 operator << (L4::BasicOStream &s, void const *u)
245 long unsigned x = (long unsigned)u;
246 L4::IOBackend::Mode mode = s.be_mode();
248 s.write((long long unsigned)x,-1);
255 operator << (L4::BasicOStream &s, L4::IOModifier m)
263 operator << (L4::BasicOStream &s, char c)
273 operator << (L4::BasicOStream &o, L4::IONumFmt const &n)
274 { return n.print(o); }