scnlib  0.1.2
FormattedinputformodernC++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
scan.h
Go to the documentation of this file.
1 // Copyright 2017-2019 Elias Kosunen
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // This file is a part of scnlib:
16 // https://github.com/eliaskosunen/scnlib
17 
18 #ifndef SCN_DETAIL_SCAN_H
19 #define SCN_DETAIL_SCAN_H
20 
21 #include "vscan.h"
22 
23 namespace scn {
25 
26  template <typename Stream, typename... Args>
27  scan_result scan(Stream& s,
29  Args&... a)
30  {
31  static_assert(sizeof...(Args) > 0,
32  "Have to scan at least a single argument");
33 
34  using context_type = basic_context<Stream>;
35 
36  auto args = make_args<context_type>(a...);
37  auto ctx = context_type(s, f, args);
38  return vscan(ctx);
39  }
40  template <typename Stream, typename... Args>
42  Stream& s,
44  Args&... a)
45  {
46  static_assert(sizeof...(Args) > 0,
47  "Have to scan at least a single argument");
48 
49  using context_type =
51  options>;
52 
53  auto args = make_args<context_type>(a...);
54  auto ctx = context_type(s, f, args, opt);
55  return vscan(ctx);
56  }
57 
58  template <typename Stream, typename... Args>
59  scan_result scanf(Stream& s,
61  Args&... a)
62  {
63  static_assert(sizeof...(Args) > 0,
64  "Have to scan at least a single argument");
65 
66  using context_type = basic_scanf_context<Stream>;
67 
68  auto args = make_args<context_type>(a...);
69  auto ctx = context_type(s, f, args);
70  return vscan(ctx);
71  }
72  template <typename Stream, typename... Args>
74  Stream& s,
76  Args&... a)
77  {
78  static_assert(sizeof...(Args) > 0,
79  "Have to scan at least a single argument");
80 
81  using context_type = basic_scanf_context<
83 
84  auto args = make_args<context_type>(a...);
85  auto ctx = context_type(s, f, args, opt);
86  return vscan(ctx);
87  }
88 
89  template <typename Stream, typename... Args>
90  scan_result scan(Stream& s, detail::default_t, Args&... a)
91  {
92  static_assert(sizeof...(Args) > 0,
93  "Have to scan at least a single argument");
94 
95  using context_type = basic_empty_context<Stream>;
96 
97  auto args = make_args<context_type>(a...);
98  auto ctx = context_type(s, static_cast<int>(sizeof...(Args)), args);
99  return vscan(ctx);
100  }
101  template <typename Stream, typename... Args>
102  scan_result scan(options opt, Stream& s, detail::default_t, Args&... a)
103  {
104  static_assert(sizeof...(Args) > 0,
105  "Have to scan at least a single argument");
106 
107  using context_type = basic_empty_context<
109 
110  auto args = make_args<context_type>(a...);
111  auto ctx =
112  context_type(s, static_cast<int>(sizeof...(Args)), args, opt);
113  return vscan(ctx);
114  }
115 
117  SCN_CLANG_IGNORE("-Wexit-time-destructors")
118 
119  // Reference to global stdin stream.
120  // Not safe to use during static construction or destruction.
121  template <typename CharT>
123  {
124  static erased_stream<CharT> stream{basic_cstdio_stream<CharT>(stdin)};
125  return stream;
126  }
128  {
129  return stdin_stream<char>();
130  }
132  {
133  return stdin_stream<wchar_t>();
134  }
135 
137 
138  // Read from stdin
139  template <typename... Args>
141  {
142  static_assert(sizeof...(Args) > 0,
143  "Have to scan at least a single argument");
144 
145  auto& stream = stdin_stream<char>();
146 
147  using stream_type =
149  using context_type =
151 
152  auto args = make_args<context_type>(a...);
153  auto ctx = context_type(stream, f, args);
154  return vscan(ctx);
155  }
156  template <typename... Args>
158  {
159  static_assert(sizeof...(Args) > 0,
160  "Have to scan at least a single argument");
161 
162  auto& stream = stdin_stream<wchar_t>();
163 
164  using stream_type =
166  using context_type =
168 
169  auto args = make_args<context_type>(a...);
170  auto ctx = context_type(stream, f, args);
171  return vscan(ctx);
172  }
173 
174  // Read from stdin with prompt
175  // Like Python's input()
176  template <typename... Args>
177  scan_result prompt(const char* p, string_view f, Args&... a)
178  {
179  static_assert(sizeof...(Args) > 0,
180  "Have to scan at least a single argument");
181  SCN_EXPECT(p != nullptr);
182 
183  std::printf("%s", p);
184 
185  auto& stream = stdin_stream<char>();
186  using stream_type =
188  using context_type =
190 
191  auto args = make_args<context_type>(a...);
192  auto ctx = context_type(stream, f, args);
193  return vscan(ctx);
194  }
195  template <typename... Args>
196  scan_result prompt(const wchar_t* p, wstring_view f, Args&... a)
197  {
198  static_assert(sizeof...(Args) > 0,
199  "Have to scan at least a single argument");
200  SCN_EXPECT(p != nullptr);
201 
202  std::wprintf(L"%ls", p);
203 
204  auto& stream = stdin_stream<wchar_t>();
205  using stream_type =
207  using context_type =
209 
210  auto args = make_args<context_type>(a...);
211  auto ctx = context_type(stream, f, args);
212  return vscan(ctx);
213  }
214 
216 } // namespace scn
217 
218 #endif // SCN_DETAIL_SCAN_H
#define SCN_END_NAMESPACE
Definition: config.h:376
erased_stream< char > & cstdin()
Definition: scan.h:127
SCN_CLANG_POP scan_result input(string_view f, Args &...a)
Definition: scan.h:140
scan_result vscan(Context &ctx)
Definition: vscan.h:38
SCN_CLANG_PUSH erased_stream< CharT > & stdin_stream()
Definition: scan.h:122
#define SCN_CLANG_IGNORE(x)
Definition: config.h:128
#define SCN_CLANG_POP
Definition: config.h:127
erased_stream< wchar_t > & wcstdin()
Definition: scan.h:131
scan_result scanf(Stream &s, basic_string_view< typename Stream::char_type > f, Args &...a)
Definition: scan.h:59
#define SCN_BEGIN_NAMESPACE
Definition: config.h:375
SCN_BEGIN_NAMESPACE scan_result scan(Stream &s, basic_string_view< typename Stream::char_type > f, Args &...a)
Definition: scan.h:27
A view over a (sub)string.
Definition: string_view.h:56
scan_result prompt(const char *p, string_view f, Args &...a)
Definition: scan.h:177
typename std::conditional< is_sized_stream< Stream >::value, basic_erased_sized_stream_context< char_type >, basic_erased_stream_context< char_type >>::type type
Definition: vscan.h:102
#define SCN_EXPECT(cond)
Definition: config.h:371
#define SCN_CLANG_PUSH
Definition: config.h:126