w11 - cpp 0.794
Backend server for Rlink and w11
Loading...
Searching...
No Matches
RosPrintBvi.cpp
Go to the documentation of this file.
1// $Id: RosPrintBvi.cpp 1186 2019-07-12 17:49:59Z mueller $
2// SPDX-License-Identifier: GPL-3.0-or-later
3// Copyright 2011-2018 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4//
5// Revision History:
6// Date Rev Version Comment
7// 2018-12-18 1089 1.0.3 use c++ style casts
8// 2013-02-03 481 1.0.2 use Rexception
9// 2011-03-12 368 1.0.1 allow base=0, will print in hex,oct and bin
10// 2011-03-05 366 1.0 Initial version
11// ---------------------------------------------------------------------------
12
17#include "RosPrintBvi.hpp"
18
19#include "Rexception.hpp"
20
21using namespace std;
22
28// all method definitions in namespace Retro
29namespace Retro {
30
31//------------------------------------------+-----------------------------------
33
34RosPrintBvi::RosPrintBvi(uint8_t val, size_t base, size_t nbit)
35 : fVal(val),
36 fBase(base),
37 fNbit(nbit)
38{
39 if (base!=0 && base!=2 && base!=8 && base!=16)
40 throw Rexception("RosPrintBvi::<ctor>",
41 "Bad args: base must be 0,2,8, or 16");
42 if (nbit<1 || nbit>8)
43 throw Rexception("RosPrintBvi::<ctor>",
44 "Bad args: nbit must be in 1,..,8");
45}
46
47//------------------------------------------+-----------------------------------
49
50RosPrintBvi::RosPrintBvi(uint16_t val, size_t base, size_t nbit)
51 : fVal(val),
52 fBase(base),
53 fNbit(nbit)
54{
55 if (base!=0 && base!=2 && base!=8 && base!=16)
56 throw Rexception("RosPrintBvi::<ctor>",
57 "Bad args: base must be 0,2,8, or 16");
58 if (nbit<1 || nbit>16)
59 throw Rexception("RosPrintBvi::<ctor>",
60 "Bad args: nbit must be in 1,..,16");
61}
62
63//------------------------------------------+-----------------------------------
65
66RosPrintBvi::RosPrintBvi(uint32_t val, size_t base, size_t nbit)
67 : fVal(val),
68 fBase(base),
69 fNbit(nbit)
70{
71 if (base!=0 && base!=2 && base!=8 && base!=16)
72 throw Rexception("RosPrintBvi::<ctor>",
73 "Bad args: base must be 0,2,8, or 16");
74 if (nbit<1 || nbit>32)
75 throw Rexception("RosPrintBvi::<ctor>",
76 "Bad args: nbit must be in 1,..,32");
77}
78
79//------------------------------------------+-----------------------------------
81
82void RosPrintBvi::Print(std::ostream& os) const
83{
84 if (fBase == 0) {
85 os << RosPrintBvi(fVal, 16, fNbit) << " "
86 << RosPrintBvi(fVal, 8, fNbit) << " "
87 << RosPrintBvi(fVal, 2, fNbit);
88 return;
89 }
90
91 char buf[33];
92 Convert(buf);
93 os << buf;
94 return;
95}
96
97//------------------------------------------+-----------------------------------
99
100void RosPrintBvi::Print(std::string& os) const
101{
102 if (fBase == 0) {
103 os << RosPrintBvi(fVal, 16, fNbit);
104 os += " ";
105 os << RosPrintBvi(fVal, 8, fNbit);
106 os += " ";
107 os << RosPrintBvi(fVal, 2, fNbit);
108 return;
109 }
110
111 char buf[33];
112 Convert(buf);
113 os += buf;
114 return;
115}
116
117//------------------------------------------+-----------------------------------
119
120void RosPrintBvi::Convert(char* pbuf) const
121{
122
123 size_t nwidth = 1;
124 if (fBase == 8) nwidth = 3;
125 if (fBase == 16) nwidth = 4;
126 uint32_t nmask = (1<<nwidth)-1;
127
128 size_t ndig = (fNbit+nwidth-1)/nwidth;
129
130 for (size_t i=ndig; i>0; i--) {
131 uint32_t nibble = ((fVal)>>((i-1)*nwidth)) & nmask;
132 nibble += (nibble <= 9) ? '0' : ('a'-10);
133 *pbuf++ = char(nibble);
134 }
135
136 *pbuf++ = '\0';
137
138 return;
139}
140
141} // end namespace Retro
142
FIXME_docs.
Definition: Rexception.hpp:29
void Convert(char *pbuf) const
FIXME_docs.
void Print(std::ostream &os) const
FIXME_docs.
Definition: RosPrintBvi.cpp:82
uint32_t fVal
value to be printed
Definition: RosPrintBvi.hpp:36
size_t fBase
base: 2,8, or 16
Definition: RosPrintBvi.hpp:37
RosPrintBvi(uint8_t val, size_t base=2, size_t nbit=8)
Constructor. FIXME_docs.
Definition: RosPrintBvi.cpp:34
size_t fNbit
number of bits to print
Definition: RosPrintBvi.hpp:38
Declaration of class ReventLoop.
Definition: ReventLoop.cpp:47