/
cppinput.hpp
164 lines (134 loc) · 3.55 KB
/
cppinput.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
#ifndef __CPPINPUT_HPP__
#define __CPPINPUT_HPP__
#include <iostream>
#include <string>
#include <sstream>
#include <assert.h>
namespace cppinput
{
namespace ci_private
{
// Split the format string into a test string before the specifier, the
// specifier, and the remainder of the string.
inline bool nextSpecifier(std::string fmt, std::string &pre_match,
std::string &specifier, std::string &post_match)
{
unsigned i = 0, j = 0;
bool found_start = false;
bool found_end = false;
for(; i < fmt.size(); ++i)
{
if(fmt[i] == '{')
{
if(i < fmt.size()-1)
{
if(fmt[i+1] != '{')
{
found_start = true;
break;
}
}
else
{
// TODO: raise proper exception
assert(!"Invalid format string. TODO - raise proper exception");
}
}
}
if(!found_start)
{
pre_match = fmt;
return false;
}
pre_match = fmt.substr(0, i);
for(j = i; j < fmt.size(); ++j)
{
if(fmt[j] == '}')
{
found_end = true;
break;
}
}
if(!found_end)
{
// TODO: raise proper exception
assert(!"Invalid format string. TODO - raise proper exception");
}
specifier = fmt.substr(i, j-i+1);
post_match = fmt.substr(j+1);
return true;
}
}; // namespace private
inline bool input(std::istream &in, std::string fmt)
{
std::string pre_match, specifier, post_match;
bool have_specifier = ci_private::nextSpecifier(fmt, pre_match, specifier, post_match);
if(have_specifier)
{
// Error, we have no types left!
return false;
}
for(unsigned i = 0; i < fmt.size(); ++i)
{
char c;
in.get(c);
if(c != fmt[i])
{
in.setstate(std::ios::failbit);
return false;
}
}
return true;
}
// Input from the given stream, as specified by the format string fmt, storing
// the values in the params provided.
template<typename V, typename... Values>
bool input(std::istream &in, std::string fmt, V &value, Values &... params)
{
std::string pre_match, specifier, post_match;
bool have_specifier;
have_specifier = ci_private::nextSpecifier(fmt, pre_match, specifier, post_match);
// First, match prematch
for(unsigned i = 0; i < pre_match.size(); ++i)
{
char c;
// in >> c;
in.get(c);
if(c != pre_match[i])
{
in.setstate(std::ios::failbit);
return false;
}
}
if(have_specifier)
{
// Then, input as per the specifier
in >> value;
// Finally, recurse with the rest of the string
return input(in, post_match, params...);
}
return true;
}
template<typename V, typename... Values>
bool input(std::istream &in, std::string fmt)
{
// Just match fmt
for(unsigned i = 0; i < fmt.size(); ++i)
{
char c;
in.get(c);
if(c != fmt[i])
{
in.setstate(std::ios::failbit);
return false;
}
}
}
template<typename... Values>
bool input(std::string &in, std::string fmt, Values &... params)
{
std::stringstream ss(in);
return input(ss, fmt, params...);
}
}; // namespace cppinput
#endif