This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
FormatStream.cpp
176 lines (153 loc) · 4.05 KB
/
FormatStream.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
This file is part of duckOS.
duckOS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
duckOS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with duckOS. If not, see <https://www.gnu.org/licenses/>.
Copyright (c) Byteduck 2016-2021. All rights reserved.
*/
#include "FormatStream.h"
using namespace Duck;
FormatParams::FormatParams(InputStream& stream) {
enum {
REGULAR,
PRECISION
} state = REGULAR;
std::string precision_string;
while(!stream.eof()) {
char ch = stream.getchar();
switch(state) {
case REGULAR: {
regstate:
switch(ch) {
case '{':
literal_brace = true;
return;
case 'c':
number_format = CHARACTER;
break;
case 'x':
number_format = HEX;
break;
case 'X':
number_format = HEX_UPPER;
break;
case 'd':
number_format = DECIMAL;
break;
case '#':
alternate_form = true;
break;
case '.':
state = PRECISION;
precision_string = "";
break;
case '}':
return;
default:
break;
}
break;
}
case PRECISION: {
if(ch >= '0' && ch <= '9') {
precision_string += ch;
} else {
precision = atoi(precision_string.c_str());
state = REGULAR;
goto regstate;
}
}
}
}
}
FormatStream::FormatStream(OutputStream& stream, std::string format):
m_stream(stream), m_format(std::move(format)), m_fmt_stream(m_format) {}
FormatStream::~FormatStream() {
//Flush the rest of the format string
while(!m_fmt_stream.eof()) {
output_until_format();
if(read_format_params().literal_brace)
m_stream << "{";
}
}
void FormatStream::output_until_format() {
m_fmt_stream.set_delimeter('{');
std::string buf;
m_fmt_stream >> buf;
m_stream << buf;
}
FormatParams FormatStream::read_format_params() {
return FormatParams(m_fmt_stream);
}
void FormatStream::write_hex(FormatParams params, unsigned long long num) {
//Figure out the width of the resulting string
size_t num_chars = 0;
unsigned int numchar_calc = num;
do {
num_chars++;
numchar_calc /= 0x10;
} while(numchar_calc);
//If we have to print the prefix, do so
if(params.alternate_form)
m_stream << "0x";
//Output the hex
const char* hex = params.number_format == FormatParams::HEX_UPPER ? "0123456789ABCDEF" : "0123456789abcdef";
for(size_t i = 1; i <= num_chars; i++)
m_stream.putchar(hex[(num >> ((num_chars - i) * 4)) & 0xFu]);
}
void FormatStream::write_arg(FormatParams params, long long arg) {
switch(params.number_format) {
case FormatParams::CHARACTER:
m_stream.putchar((char) arg);
break;
case FormatParams::HEX:
case FormatParams::HEX_UPPER:
write_hex(params, arg);
break;
case FormatParams::DECIMAL:
m_stream << arg;
break;
}
}
void FormatStream::write_arg(FormatParams params, unsigned long long int arg) {
switch(params.number_format) {
case FormatParams::CHARACTER:
m_stream.putchar((char) arg);
break;
case FormatParams::HEX:
case FormatParams::HEX_UPPER:
write_hex(params, arg);
break;
case FormatParams::DECIMAL:
m_stream << arg;
break;
}
}
void FormatStream::write_arg(FormatParams params, long double arg) {
//Write the part before the decimal
m_stream << ((long) arg);
if(params.precision != 0)
m_stream.putchar('.');
else
return;
//Print decimal places
int num_places = (params.precision > -1 && params.precision < 8) ? params.precision : 8;
for (int d = 0; d < num_places; d++) {
if ((int) (arg * 100000.0) % 100000 == 0 && d != 0)
break;
arg *= 10.0;
m_stream.putchar("0123456789"[((int) arg) % 10]);
}
}
namespace Duck {
FormatStream operator%(OutputStream& stream, const char* fmt) {
return {stream, fmt};
}
}