-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinput.hpp
417 lines (374 loc) · 9.06 KB
/
input.hpp
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#pragma once
//=====================================================================//
/*! @file
@brief input クラス @n
数値、文字列などの入力クラス @n
%b ---> 2進の数値 @n
%o ---> 8進の数値 @n
%d ---> 10進の数値 @n
%x ---> 16進の数値 @n
%f ---> 浮動小数点数(float、double) @n
%c ---> 1文字のキャラクター @n
%% ---> '%' のキャラクター
@author 平松邦仁 ([email protected])
@copyright Copyright (C) 2017 Kunihito Hiramatsu @n
Released under the MIT license @n
https://github.com/hirakuni45/RX/blob/master/LICENSE
*/
//=====================================================================//
#include <type_traits>
#include <unistd.h>
namespace utils {
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief 標準入力ファンクタ
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
class def_chainp {
const char* str_;
char last_;
bool unget_;
public:
//-----------------------------------------------------------------//
/*!
@brief コンストラクター
@param[in] str 入力文字列(省力された場合「sci_getch」を使う)
*/
//-----------------------------------------------------------------//
def_chainp(const char* str = nullptr) : str_(str), last_(0), unget_(false) { }
//-----------------------------------------------------------------//
/*!
@brief 1文字戻す
*/
//-----------------------------------------------------------------//
void unget() {
unget_ = true;
}
//-----------------------------------------------------------------//
/*!
@brief ファンクタ(文字取得)
@return 文字
*/
//-----------------------------------------------------------------//
char operator() () {
if(unget_) {
unget_ = false;
} else {
if(str_ == nullptr) {
char ch;
if(read(0, &ch, 1) == 1) {
if(ch == '\n') ch = 0;
last_ = ch;
} else {
last_ = 0;
}
} else {
last_ = *str_;
if(last_ != 0) { ++str_; }
}
}
return last_;
}
};
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief 汎用入力クラス
@param[in] INP 文字入力クラス
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <class INP>
class basic_input {
public:
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief エラー種別
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
enum class error : uint8_t {
none, ///< エラー無し
cha_sets, ///< 文字セットの不一致
partition, ///< 仕切りキャラクターの不一致
input_type, ///< 無効な入力タイプ
not_integer, ///< 整数の不一致
not_float, ///< 浮動小数点の不一致
terminate, ///< 終端文字の不一致
};
private:
const char* form_;
INP inp_;
enum class mode : uint8_t {
NONE,
BIN,
OCT,
DEC,
HEX,
REAL,
CHA,
};
mode mode_;
error error_;
int num_;
uint32_t bin_() {
uint32_t a = 0;
char ch;
while((ch = inp_()) != 0 && ch >= '0' && ch <= '1') {
a <<= 1;
a += ch - '0';
}
return a;
}
uint32_t oct_() {
uint32_t a = 0;
char ch;
while((ch = inp_()) != 0 && ch >= '0' && ch <= '7') {
a <<= 3;
a += ch - '0';
}
return a;
}
uint32_t dec_() {
uint32_t a = 0;
char ch;
while((ch = inp_()) != 0 && ch >= '0' && ch <= '9') {
a *= 10;
a += ch - '0';
}
return a;
}
uint32_t hex_() {
uint32_t a = 0;
char ch;
while((ch = inp_()) != 0) {
if(ch >= '0' && ch <= '9') {
a <<= 4;
a += ch - '0';
} else if(ch >= 'A' && ch <= 'F') {
a <<= 4;
a += ch - 'A' + 10;
} else if(ch >= 'a' && ch <= 'f') {
a <<= 4;
a += ch - 'a' + 10;
} else {
break;
}
}
return a;
}
template<typename T>
T real_() {
uint32_t a = 0;
uint32_t b = 0;
uint32_t c = 1;
char ch;
bool p = false;
while((ch = inp_()) != 0) {
if(ch >= '0' && ch <= '9') {
a *= 10;
a += ch - '0';
c *= 10;
} else if(!p && ch == '.') {
b = a;
a = 0;
c = 1;
p = true;
} else {
break;
}
}
if(p) {
return static_cast<T>(b) + (static_cast<T>(a) / static_cast<T>(c));
} else {
return static_cast<T>(a);
}
}
void next_()
{
enum class fmm : uint8_t {
none,
type,
};
fmm cm = fmm::none;
char ch;
while((ch = *form_++) != 0) {
switch(cm) {
case fmm::none:
if(ch == '[') {
auto a = inp_();
const char* p = form_;
bool ok = false;
while((ch = *p++) != 0 && ch != ']') {
if(ch == a) ok = true;
}
form_ = p;
if(!ok) {
error_ = error::cha_sets;
return;
}
} else if(ch == '%' && *form_ != '%') {
cm = fmm::type;
} else if(ch != inp_()) {
error_ = error::partition;
return;
}
break;
case fmm::type:
switch(ch) {
case 'b':
mode_ = mode::BIN;
break;
case 'o':
mode_ = mode::OCT;
break;
case 'd':
mode_ = mode::DEC;
break;
case 'x':
mode_ = mode::HEX;
break;
case 'f':
mode_ = mode::REAL;
break;
case 'c':
mode_ = mode::CHA;
break;
default:
error_ = error::input_type;
break;
}
return;
}
}
if(ch == 0 && inp_() == 0) ;
else {
error_ = error::terminate;
}
}
bool neg_() {
bool neg = false;
auto s = inp_();
if(s == '-') { neg = true; }
else if(s == '+') { neg = false; }
else inp_.unget();
return neg;
}
void skip_space_() {
while(1) {
char ch = inp_();
if(ch != ' ') {
inp_.unget();
return;
}
}
}
int32_t nb_int_(bool sign = true)
{
skip_space_();
auto neg = neg_();
uint32_t v = 0;
switch(mode_) {
case mode::BIN:
v = bin_();
break;
case mode::OCT:
v = oct_();
break;
case mode::DEC:
v = dec_();
break;
case mode::HEX:
v = hex_();
break;
default:
error_ = error::not_integer;
break;
}
if(error_ == error::none) {
inp_.unget();
next_();
if(error_ == error::none) ++num_;
}
if(sign && neg) return -static_cast<int32_t>(v);
else return static_cast<int32_t>(v);
}
template <typename T>
T nb_real_()
{
skip_space_();
bool neg = neg_();
T v = 0.0f;
switch(mode_) {
case mode::REAL:
v = real_<T>();
break;
default:
error_ = error::not_float;
break;
}
if(error_ == error::none) {
inp_.unget();
next_();
if(error_ == error::none) ++num_;
}
if(neg) return -v;
else return v;
}
public:
//-----------------------------------------------------------------//
/*!
@brief コンストラクター
@param[in] form 入力形式
@param[in] inp 変換文字列(nullptrの場合、sci_getch で取得)
*/
//-----------------------------------------------------------------//
basic_input(const char* form, const char* inp = nullptr) : form_(form), inp_(inp),
mode_(mode::NONE), error_(error::none), num_(0)
{
next_();
}
//-----------------------------------------------------------------//
/*!
@brief エラー種別を返す
@return エラー
*/
//-----------------------------------------------------------------//
error get_error() const { return error_; }
//-----------------------------------------------------------------//
/*!
@brief 変換ステータスを返す
@return 変換が全て正常なら「true」
*/
//-----------------------------------------------------------------//
bool status() const { return error_ == error::none; }
//-----------------------------------------------------------------//
/*!
@brief 正常変換数を取得
@return 正常変換数
*/
//-----------------------------------------------------------------//
int num() const { return num_; }
//-----------------------------------------------------------------//
/*!
@brief テンプレート・オペレーター「%」
@param[in] val 整数型
@return 自分の参照
*/
//-----------------------------------------------------------------//
template <typename T>
basic_input& operator % (T& val)
{
if(error_ != error::none) return *this;
if(std::is_floating_point<T>::value) {
val = nb_real_<T>();
} else {
if(mode_ == mode::CHA) {
val = inp_();
next_();
} else {
val = nb_int_(std::is_signed<T>::value);
}
}
return *this;
}
};
typedef basic_input<def_chainp> input;
}