Vcsn  2.4
Be Rational
stream.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream> // cin
4 #include <memory> // shared_ptr
5 #include <sstream>
6 #include <stdexcept>
7 
8 #include <vcsn/misc/escape.hh>
9 #include <vcsn/misc/export.hh>
10 #include <vcsn/misc/raise.hh>
11 #include <vcsn/misc/to-string.hh>
12 
13 namespace vcsn LIBVCSN_API
14 {
16  extern std::ostream cnull;
17 
19  extern std::wostream wcnull;
20 
23  std::string
24  bracketed(std::istream& i, char lbracket, char rbracket);
25 
27  template <typename ValueSet, typename... Args>
28  auto
29  conv(const ValueSet& vs, const std::string& str, Args&&... args)
30  -> decltype(vs.conv(std::declval<std::istream&>(),
31  std::forward<Args>(args)...))
32  {
33  std::istringstream i{str};
34  try
35  {
36  const auto res = vs.conv(i, std::forward<Args>(args)...);
37  VCSN_REQUIRE(i.peek() == EOF,
38  vs, ": unexpected ", str_escape(i.peek()));
39  return res;
40  }
41  catch (const std::runtime_error& e)
42  {
43  raise(e, " while reading: ", str_escape(str));
44  }
45  }
46 
52  char eat(std::istream& is, char c);
53 
59  const std::string& eat(std::istream& is, const std::string& s);
60 
66  template <typename... Args>
67  ATTRIBUTE_NORETURN
68  void fail_reading(std::istream& is, Args&&... args)
69  {
70  is.clear();
71  std::string buf;
72  std::getline(is, buf, '\n');
73  if (!is.good())
74  // This shouldn't really happen; however it's best to fail cleanly.
75  is.clear();
76  if (buf.empty())
77  raise(std::forward<Args>(args)...);
78  else
79  raise(std::forward<Args>(args)..., ": ", str_escape(buf));
80  }
81 
84  char get_char(std::istream& i);
85 
87  std::string get_file_contents(const std::string& file);
88 
91  std::shared_ptr<std::istream> open_input_file(const std::string& file);
92 
95  std::shared_ptr<std::ostream> open_output_file(const std::string& file);
96 
99  void skip_space(std::istream& is);
100 }
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:90
return res
Definition: multiply.hh:398
std::string bracketed(std::istream &i, char lbracket, char rbracket)
Extract the string which is here between lbracket and rbracket.
Definition: stream.cc:17
std::string get_file_contents(const std::string &file)
Return the contents of file.
Definition: stream.cc:119
std::ostream cnull
An narrow-char stream that discards the output.
Definition: stream.cc:13
#define LIBVCSN_API
Definition: export.hh:8
std::shared_ptr< std::istream > open_input_file(const std::string &file)
Open file for reading and return its autoclosing stream.
Definition: stream.cc:134
Definition: a-star.hh:8
#define VCSN_REQUIRE(Cond,...)
A macro similar to require.
Definition: raise.hh:111
void skip_space(std::istream &is)
Ignore spaces.
Definition: stream.cc:163
char get_char(std::istream &i)
Read a single char, with possible -escape support.
Definition: stream.cc:37
std::ostream & str_escape(std::ostream &os, const std::string &str, const char *special=nullptr)
Output a string, escaping special characters.
Definition: escape.cc:51
std::wostream wcnull
An wide-char stream that discards the output.
Definition: stream.cc:14
ATTRIBUTE_NORETURN void fail_reading(std::istream &is, Args &&...args)
Throw an exception after failing to read from is.
Definition: stream.hh:68
auto conv(const ValueSet &vs, const std::string &str, Args &&...args) -> decltype(vs.conv(std::declval< std::istream & >(), std::forward< Args >(args)...))
Parse str via vs.conv.
Definition: stream.hh:29
std::shared_ptr< std::ostream > open_output_file(const std::string &file)
Open file for writing and return its autoclosing stream.
Definition: stream.cc:149