Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
stream.hh
Go to the documentation of this file.
1 #ifndef VCSN_MISC_STREAM_HH
2 # define VCSN_MISC_STREAM_HH
3 
4 # include <sstream>
5 # include <stdexcept>
6 # include <iostream> // cin
7 # include <memory> // shared_ptr
8 
9 # include <vcsn/misc/escape.hh>
10 # include <vcsn/misc/export.hh>
11 # include <vcsn/misc/raise.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, const char lbracket, const 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  auto res = vs.conv(i, std::forward<Args>(args)...);
35  require(i.peek() == EOF,
36  vs.sname(), ": invalid value: ", str,
37  ", unexpected ", str_escape(i.peek()));
38  return res;
39  }
40 
46  char eat(std::istream& is, char c);
47 
53  const std::string& eat(std::istream& is, const std::string& s);
54 
60  ATTRIBUTE_NORETURN
61  void fail_reading(std::istream& is, std::string explanation);
62 
64  template <typename ValueSet, typename... Args>
65  auto
66  to_string(const ValueSet& vs,
67  const typename ValueSet::value_t& v, Args&&... args)
68  -> std::string
69  {
70  std::ostringstream o;
71  vs.print(v, o, std::forward<Args>(args)...);
72  return o.str();
73  }
74 
76  std::string get_file_contents(const std::string& file);
77 
80  std::shared_ptr<std::istream> open_input_file(const std::string& file);
81 
84  std::shared_ptr<std::ostream> open_output_file(const std::string& file);
85 
86 }
87 
88 #endif // !VCSN_MISC_STREAM_HH
std::string bracketed(std::istream &i, const char lbracket, const char rbracket)
Extract the string which is here between lbracket and rbracket.
Definition: stream.cc:17
std::ostream cnull
An narrow-char stream that discards the output.
Definition: stream.cc:13
std::string get_file_contents(const std::string &file)
Return the contents of file.
Definition: stream.cc:63
std::shared_ptr< std::ostream > open_output_file(const std::string &file)
Open file for writing and return its autoclosing stream.
Definition: stream.cc:93
std::ostream & str_escape(std::ostream &os, const std::string &str)
Output a string, escaping special characters.
Definition: escape.cc:43
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::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:329
std::shared_ptr< std::istream > open_input_file(const std::string &file)
Open file for reading and return its autoclosing stream.
Definition: stream.cc:78
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:37
std::wostream wcnull
An wide-char stream that discards the output.
Definition: stream.cc:14
#define LIBVCSN_API
Definition: export.hh:9
ATTRIBUTE_NORETURN void fail_reading(std::istream &is, std::string explanation)
Throw an exception after failing to read from is.
Definition: stream.cc:107
void require(bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:39
std::string to_string(direction d)
Conversion to string.
Definition: direction.cc:7