Vcsn  2.2
Be Rational
stream.cc
Go to the documentation of this file.
1 #include <vcsn/misc/stream.hh>
2 
3 #include <cassert>
4 #include <cstring> // strerror
5 #include <istream>
6 #include <fstream>
7 
8 #include <vcsn/misc/raise.hh>
9 
10 namespace vcsn
11 {
12 
13  std::ostream cnull{nullptr};
14  std::wostream wcnull{nullptr};
15 
16  std::string
17  bracketed(std::istream& i, const char lbracket, const char rbracket)
18  {
19  assert(i.peek() == lbracket);
20  i.ignore();
21  size_t level = 1;
22  std::ostringstream o;
23  int c;
24  while ((c = i.get()) != -1)
25  {
26  if (c == lbracket)
27  ++level;
28  else if (c == rbracket
29  && !--level)
30  return o.str();
31  o << char(c);
32  }
33  raise("missing ", str_escape(rbracket), " after ",
34  str_escape(lbracket), o.str());
35  }
36 
37  char eat(std::istream& is, char c)
38  {
39  if(is.peek() == c)
40  {
41  is.ignore();
42  return c;
43  }
44  else
45  fail_reading(is, "expected ", str_escape(c), ", got");
46  }
47 
48  const std::string& eat(std::istream& is, const std::string& expect)
49  {
50  std::string s;
51  char c;
52  size_t cnt = expect.size();
53  while (cnt && is >> c)
54  {
55  s.append(1, c);
56  --cnt;
57  }
58  VCSN_REQUIRE(s == expect,
59  "unexpected: ", str_escape(s),
60  ": expected ", str_escape(expect));
61  return expect;
62  }
63 
64  // http://stackoverflow.com/questions/2602013.
65  std::string
66  get_file_contents(const std::string& file)
67  {
68  std::ifstream in(file.c_str(), std::ios::in | std::ios::binary);
69  VCSN_REQUIRE(in.good(), "cannot read file: ", file, ": ", strerror(errno));
70 
71  std::string res;
72  in.seekg(0, std::ios::end);
73  res.resize(in.tellg());
74  in.seekg(0, std::ios::beg);
75  in.read(&res[0], res.size());
76  in.close();
77  return res;
78  }
79 
80  std::shared_ptr<std::istream>
81  open_input_file(const std::string& file)
82  {
83  std::shared_ptr<std::istream> res;
84  if (file.empty() || file == "-")
85  res.reset(&std::cin, [](...){});
86  else
87  {
88  res.reset(new std::ifstream(file.c_str()));
89  VCSN_REQUIRE(res->good(),
90  "cannot open ", file, " for reading: ", strerror(errno));
91  }
92  return res;
93  }
94 
95  std::shared_ptr<std::ostream>
96  open_output_file(const std::string& file)
97  {
98  std::shared_ptr<std::ostream> res;
99  if (file.empty() || file == "-")
100  res.reset(&std::cout, [](...){});
101  else
102  {
103  res.reset(new std::ofstream(file.c_str()));
104  VCSN_REQUIRE(res->good(),
105  "cannot open ", file, " for writing: ", strerror(errno));
106  }
107  return res;
108  }
109 }
std::shared_ptr< std::istream > open_input_file(const std::string &file)
Open file for reading and return its autoclosing stream.
Definition: stream.cc:81
#define VCSN_REQUIRE(Cond,...)
A macro similar to require.
Definition: raise.hh:89
Definition: a-star.hh:8
std::ostream & str_escape(std::ostream &os, const std::string &str, const char *special=nullptr)
Output a string, escaping special characters.
Definition: escape.cc:54
std::shared_ptr< std::ostream > open_output_file(const std::string &file)
Open file for writing and return its autoclosing stream.
Definition: stream.cc:96
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:37
std::string bracketed(std::istream &i, char lbracket, char rbracket)
Extract the string which is here between lbracket and rbracket.
Definition: stream.cc:17
ATTRIBUTE_NORETURN void fail_reading(std::istream &is, Args &&...args)
Throw an exception after failing to read from is.
Definition: stream.hh:62
std::string get_file_contents(const std::string &file)
Return the contents of file.
Definition: stream.cc:66
std::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:380
std::ostream cnull
An narrow-char stream that discards the output.
Definition: stream.cc:13
std::wostream wcnull
An wide-char stream that discards the output.
Definition: stream.cc:14
auto in(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions arriving to state s.
Definition: automaton.hh:105