-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow.h
178 lines (150 loc) · 3.27 KB
/
show.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
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
177
178
// vim: set ts=2 sw=2 expandtab:
// Copyright (c) 2014 Luchang Jin
// All rights reserved.
// This program 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.
//
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
#pragma once
#include <sstream>
#include <string>
#include <cstdarg>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <sstream>
#include <cassert>
namespace qshow {
inline std::string vssprintf(const char* fmt, va_list args)
{
std::string str;
char* cstr;
int ret = vasprintf(&cstr, fmt, args);
assert(ret >= 0);
str += std::string(cstr);
std::free(cstr);
return str;
}
inline std::string ssprintf(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
return vssprintf(fmt, args);
}
inline std::string show()
{
return "";
}
inline std::string show(const int& x)
{
return ssprintf("%d", x);
}
inline std::string show(const unsigned int& x)
{
return ssprintf("%u", x);
}
inline std::string show(const long& x)
{
return ssprintf("%ld", x);
}
inline std::string show(const unsigned long& x)
{
return ssprintf("%lu", x);
}
inline std::string show(const double& x)
{
return ssprintf("%24.17E", x);
}
inline std::string show(const bool& x)
{
return x ? "true" : "false";
}
inline std::string show(const std::string& x)
{
std::ostringstream out;
out << x;
return out.str();
}
template <class T>
std::string shows(const T& x)
{
std::ostringstream out;
out << x;
return out.str();
}
template <class T>
T& reads(T& x, const std::string& str)
{
std::istringstream in(str);
in >> x;
return x;
}
inline long read_long(const std::string& str)
{
long ret = 0;
reads(ret, str);
return ret;
}
inline double read_double(const std::string& str)
{
double ret = 0.0;
reads(ret, str);
return ret;
}
inline FILE*& get_output_file()
{
static FILE* out = stdout;
return out;
}
inline FILE*& get_monitor_file()
{
static FILE* out = NULL;
return out;
}
inline void display(const std::string& str, FILE* fp = NULL)
{
if (NULL == fp) {
fp = get_monitor_file();
if (NULL != fp) {
fprintf(fp, "%s", str.c_str());
}
fp = get_output_file();
}
if (NULL != fp) {
fprintf(fp, "%s", str.c_str());
}
}
inline void displayln(const std::string& str, FILE* fp = NULL)
{
if (NULL == fp) {
fp = get_monitor_file();
if (NULL != fp) {
fprintf(fp, "%s\n", str.c_str());
}
fp = get_output_file();
}
if (NULL != fp) {
fprintf(fp, "%s\n", str.c_str());
}
}
//////////////////////////////////////////////////////////////////
inline void fdisplay(FILE* fp, const std::string& str)
{
fprintf(fp, "%s", str.c_str());
}
inline void fdisplayln(FILE* fp, const std::string& str)
{
fprintf(fp, "%s\n", str.c_str());
}
}
#ifndef USE_NAMESPACE
using namespace qshow;
#endif