1 /*
2  * (c) 2004-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
3  *               Alexander Warg <warg@os.inf.tu-dresden.de>,
4  *               Frank Mehnert <fm3@os.inf.tu-dresden.de>
5  *     economic rights: Technische Universität Dresden (Germany)
6  *
7  * This file is part of TUD:OS and distributed under the terms of the
8  * GNU General Public License 2.
9  * Please see the COPYING-GPL-2 file for details.
10  *
11  * As a special exception, you may use this file as part of a free software
12  * library without restriction.  Specifically, if other files instantiate
13  * templates or use macros or inline functions from this file, or you compile
14  * this file and link it with other files to produce an executable, this
15  * file does not by itself cause the resulting executable to be covered by
16  * the GNU General Public License.  This exception does not however
17  * invalidate any other reasons why the executable file might be covered by
18  * the GNU General Public License.
19  */
20 
21 #include <l4/cxx/basic_ostream>
22 
23 namespace L4
24 {
25   IOModifier const hex(16);
26   IOModifier const dec(10);
27 
28   static char const hex_chars[] = "0123456789abcdef";
29 
write(IOModifier m)30   void IOBackend::write(IOModifier m)
31   {
32     if (m == dec)
33       int_mode = 10;
34     else if (m == hex)
35       int_mode = 16;
36   }
37 
write(long long int u,int)38   void IOBackend::write(long long int u, int /*len*/)
39   {
40     char buffer[20];
41     int  pos = 20;
42     bool sign = u < 0;
43     if (sign)
44       u = -u;
45     buffer[19] = '0';
46     while (u)
47       {
48         buffer[--pos] = hex_chars[u % int_mode];
49         u /= int_mode;
50       }
51     if (pos == 20)
52       pos = 19;
53     if (sign)
54       buffer[--pos] = '-';
55 
56     write(buffer + pos, 20 - pos);
57   }
58 
write(long long unsigned u,int)59   void IOBackend::write(long long unsigned u, int /*len*/)
60   {
61     char buffer[20];
62     int  pos = 20;
63     buffer[19] = '0';
64     while (u)
65       {
66         buffer[--pos] = hex_chars[u % int_mode];
67         u /= int_mode;
68       }
69     if (pos == 20)
70       pos = 19;
71 
72     write(buffer + pos, 20 - pos);
73   }
74 
write(long long unsigned u,unsigned char base,unsigned char len,char pad)75   void IOBackend::write(long long unsigned u, unsigned char base,
76                         unsigned char len, char pad)
77   {
78     char buffer[30];
79     unsigned pos = sizeof(buffer) - !u;
80     buffer[sizeof(buffer) - 1] = '0';
81     while (pos > 0 && u)
82       {
83         buffer[--pos] = hex_chars[u % base];
84         u /= base;
85       }
86 
87     if (len > sizeof(buffer))
88       len = sizeof(buffer);
89 
90     if (len && sizeof(buffer) - pos > len)
91       pos = sizeof(buffer) - len;
92 
93     while (sizeof(buffer) - pos < len)
94       buffer[--pos] = pad;
95 
96     write(buffer + pos, sizeof(buffer) - pos);
97   }
98 };
99