w11 - cpp 0.794
Backend server for Rlink and w11
Loading...
Searching...
No Matches
RiosState.cpp
Go to the documentation of this file.
1// $Id: RiosState.cpp 1186 2019-07-12 17:49:59Z mueller $
2// SPDX-License-Identifier: GPL-3.0-or-later
3// Copyright 2006-2018 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4//
5// Revision History:
6// Date Rev Version Comment
7// 2018-12-21 1090 1.0.1 use constructor delegation
8// 2011-01-30 357 1.0 Adopted from CTBioState
9// 2006-04-16 - - Last change on CTBioState
10// ---------------------------------------------------------------------------
11
16#include "RiosState.hpp"
17
18using namespace std;
19
25//------------------------------------------+-----------------------------------
26// all method definitions in namespace Retro
27namespace Retro {
28
30
32 : fStream(stream),
33 fOldFlags(fStream.flags()),
34 fOldPrecision(-1),
35 fOldFill(0),
36 fCtype(0)
37{}
38
39//------------------------------------------+-----------------------------------
41
42RiosState::RiosState(ios& stream, const char* form, int prec)
43 : RiosState(stream)
44{
45 SetFormat(form, prec);
46}
47
48//------------------------------------------+-----------------------------------
50
52{
53 fStream.flags(fOldFlags);
54 if (fOldPrecision >= 0) fStream.precision(fOldPrecision);
55 if (fOldFill != 0) fStream.fill(fOldFill);
56}
57
58//------------------------------------------+-----------------------------------
60
61void RiosState::SetFormat(const char* form, int prec)
62{
63 bool b_plus = false;
64 bool b_minus = false;
65 bool b_point = false;
66 bool b_dollar = false;
67 bool b_internal = false;
68 char c_ctype = 0;
69 char c_fill = 0;
70 char c;
71
72 if (form == nullptr) form = ""; // allow null as format
73
74 for (c = *form++; ; c = *form++) {
75 if (c == '+') { b_plus = true; continue;}
76 if (c == '-') { b_minus = true; continue;}
77 if (c == '.') { b_point = true; continue;}
78 if (c == '$') { b_dollar = true; continue;}
79 break;
80 }
81
82 if (c != 0 && isalpha(c)) { c_ctype = c; c = *form++; }
83 if (c != 0) c_fill = c;
84
85 if (prec >= 0) {
86 int i_old_precision = fStream.precision(prec);
87 if (fOldPrecision < 0) fOldPrecision = i_old_precision;
88 }
89 if (c_fill != 0) {
90 char c_old_fill = fStream.fill(c_fill);
91 if (fOldFill == 0) fOldFill = c_old_fill;
92 }
93
94 fCtype = c_ctype;
95
96 switch(c_ctype) {
97 case 'd':
98 b_internal = !b_minus & (c_fill == '0');
99 fStream.setf(ios::dec,ios::basefield);
100 break;
101 case 'o':
102 b_internal = !b_minus & (c_fill == '0');
103 fStream.setf(ios::oct,ios::basefield);
104 break;
105 case 'x':
106 case 'X':
107 b_internal = !b_minus & (c_fill == '0');
108 fStream.setf(ios::hex,ios::basefield);
109 if (isupper(c_ctype)) fStream.setf(ios::uppercase);
110 break;
111 case 'g':
112 case 'G':
113 b_internal = !b_minus & (c_fill == '0');
114 fStream.setf(ios_base::fmtflags(0),ios::floatfield);
115 if (isupper(c_ctype)) fStream.setf(ios::uppercase);
116 break;
117 case 'f':
118 b_internal = !b_minus & (c_fill == '0');
119 fStream.setf(ios::fixed,ios::floatfield);
120 break;
121 case 'e':
122 case 'E':
123 b_internal = !b_minus & (c_fill == '0');
124 fStream.setf(ios::scientific,ios::floatfield);
125 if (isupper(c_ctype)) fStream.setf(ios::uppercase);
126 break;
127 case 's':
128 case 'p':
129 case 'c':
130 break;
131 }
132
133 {
134 ios_base::fmtflags l_flags = ios_base::fmtflags(0);
135 if (b_plus) l_flags |= ios::showpos;
136 if (b_point) l_flags |= ios::showpoint;
137 if (b_dollar) l_flags |= ios::showbase;
138 fStream.setf(l_flags);
139 fStream.setf(b_internal ? ios::internal :
140 (b_minus ? ios::left : ios::right), ios::adjustfield);
141 }
142}
143
144} // end namespace Retro
Stack object for ostream state. **.
Definition: RiosState.hpp:23
std::ios & fStream
Definition: RiosState.hpp:33
~RiosState()
Destructor.
Definition: RiosState.cpp:51
void SetFormat(const char *form, int prec=-1)
Setup format.
Definition: RiosState.cpp:61
std::ios_base::fmtflags fOldFlags
Definition: RiosState.hpp:34
Declaration of class ReventLoop.
Definition: ReventLoop.cpp:47