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.h
142 lines (114 loc) · 3.82 KB
/
FormatStream.h
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
/*
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 "Stream.h"
#include "StringStream.h"
#pragma once
namespace Duck {
class FormatParams {
public:
explicit FormatParams(InputStream& stream);
enum {
CHARACTER,
DECIMAL,
HEX,
HEX_UPPER
} number_format = DECIMAL;
int precision = -1;
bool alternate_form = false;
bool literal_brace = false;
};
class FormatStream {
public:
FormatStream(OutputStream& stream, std::string format);
~FormatStream();
template<typename T>
FormatStream& operator%(const T& arg) {
feed(arg);
return *this;
}
private:
void output_until_format();
FormatParams read_format_params();
template<typename T>
void feed(const T& arg) {
feed_start:
output_until_format();
if(m_fmt_stream.eof())
return; //If we reached EOF, there's no argument to print
auto fmt_params = read_format_params();
//If the format parameter is '{', that means just print '{' and look for the next argument
if(fmt_params.literal_brace) {
m_stream << "{";
goto feed_start;
}
//Write the argument
write_arg(fmt_params, arg);
}
//Base formatters
void write_hex(FormatParams params, unsigned long long arg);
//Integers
void write_arg(FormatParams params, long long arg);
void write_arg (FormatParams params, long arg) { write_arg(params, (long long) arg); }
void write_arg (FormatParams params, int arg) { write_arg(params, (long long) arg); }
void write_arg (FormatParams params, char arg) { write_arg(params, (long long) arg); }
void write_arg(FormatParams params, unsigned long long arg);
void write_arg(FormatParams params, unsigned long arg) { write_arg(params, (unsigned long long) arg); }
void write_arg(FormatParams params, unsigned int arg) { write_arg(params, (unsigned long long) arg); }
//Floats and Doubles
void write_arg(FormatParams params, long double arg);
void write_arg(FormatParams params, double arg) { write_arg(params, (long double) arg); }
void write_arg(FormatParams params, float arg) { write_arg(params, (long double) arg); }
//All other types of args
template<typename T>
void write_arg(FormatParams params, const T& arg) {
m_stream << arg;
}
OutputStream& m_stream;
std::string m_format;
StringInputStream m_fmt_stream;
};
FormatStream operator%(OutputStream& stream, const char* fmt);
template<typename... T>
void sprint(OutputStream& stream, const char* fmt, T... args) {
(stream % fmt % ... % args);
}
template<typename... T>
void sprintln(OutputStream& stream, const char* fmt, T... args) {
sprint(stream, fmt, args...);
stream << "\n";
}
template<typename... T>
void print(const char* fmt, T... args) {
sprint(Stream::std_out, fmt, args...);
}
template<typename... T>
void println(const char* fmt, T... args) {
sprintln(Stream::std_out, fmt, args...);
}
template<typename... T>
void printerr(const char* fmt, T... args) {
sprint(Stream::std_err, fmt, args...);
}
template<typename... T>
void printerrln(const char* fmt, T... args) {
sprintln(Stream::std_err, fmt, args...);
}
template<typename... T>
std::string format(const char* fmt, T... args) {
StringOutputStream out;
sprint(out, fmt, args...);
return out.string();
}
}