]> www.ginac.de Git - cln.git/blob - src/base/output/cl_output_hex.cc
Finalize CLN 1.3.7 release.
[cln.git] / src / base / output / cl_output_hex.cc
1 // fprinthexadecimal().
2
3 // General includes.
4 #include "base/cl_sysdep.h"
5
6 // Specification.
7 #include "cln/io.h"
8
9
10 // Implementation.
11
12 namespace cln {
13
14 static void fprinthexadecimal_impl (std::ostream& stream, uintptr_t x)
15 {
16         #define bufsize (sizeof(uintptr_t)*2)
17         var char buf[bufsize+1];
18         var char* bufptr = &buf[bufsize];
19         *bufptr = '\0';
20         do {
21                 uintptr_t q = x / 16;
22                 uintptr_t r = x % 16;
23                 *--bufptr = (r<10 ? '0'+r : 'A'-10+r);
24                 x = q;
25         } while (x > 0);
26         fprint(stream,bufptr);
27         #undef bufsize
28 }
29
30 static void fprinthexadecimal_impl (std::ostream& stream, intptr_t x)
31 {
32         if (x >= 0)
33                 fprinthexadecimal(stream,(uintptr_t)x);
34         else {
35                 fprintchar(stream,'-');
36                 fprinthexadecimal(stream,(uintptr_t)(-1-x)+1);
37         }
38 }
39
40 void fprinthexadecimal (std::ostream& stream, unsigned int x)
41 {
42         fprinthexadecimal_impl(stream,(uintptr_t)x);
43 }
44 void fprinthexadecimal (std::ostream& stream, int x)
45 {
46         fprinthexadecimal_impl(stream,(intptr_t)x);
47 }
48
49 void fprinthexadecimal (std::ostream& stream, unsigned long x)
50 {
51         fprinthexadecimal_impl(stream,(uintptr_t)x);
52 }
53 void fprinthexadecimal (std::ostream& stream, long x)
54 {
55         fprinthexadecimal_impl(stream,(intptr_t)x);
56 }
57
58 void fprinthexadecimal (std::ostream& stream, unsigned long long x)
59 {
60 #if long_long_bitsize <= pointer_bitsize
61         fprinthexadecimal_impl(stream,(uintptr_t)x);
62 #else
63         #define bufsize (sizeof(unsigned long long)*2)
64         var char buf[bufsize+1];
65         var char* bufptr = &buf[bufsize];
66         *bufptr = '\0';
67         do {
68                 unsigned long long q = x / 16;
69                 unsigned long long r = x % 16;
70                 *--bufptr = (r<10 ? '0'+r : 'A'-10+r);
71                 x = q;
72         } while (x > 0);
73         fprint(stream,bufptr);
74         #undef bufsize
75 #endif
76 }
77
78 void fprinthexadecimal (std::ostream& stream, long long x)
79 {
80 #if long_long_bitsize <= pointer_bitsize
81         fprinthexadecimal_impl(stream,(intptr_t)x);
82 #else
83         if (x >= 0)
84                 fprinthexadecimal(stream,(unsigned long long)x);
85         else {
86                 fprintchar(stream,'-');
87                 fprinthexadecimal(stream,(unsigned long long)(-1-x)+1);
88         }
89 #endif
90 }
91
92 }  // namespace cln