Vcsn  2.1
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  require(s == expect,
59  "unexpected: ", str_escape(s), ": expected ", str_escape(expect));
60  return expect;
61  }
62 
63  // http://stackoverflow.com/questions/2602013.
64  std::string
65  get_file_contents(const std::string& file)
66  {
67  std::ifstream in(file.c_str(), std::ios::in | std::ios::binary);
68  require(in.good(), "cannot read file: ", file, ": ", strerror(errno));
69 
70  std::string res;
71  in.seekg(0, std::ios::end);
72  res.resize(in.tellg());
73  in.seekg(0, std::ios::beg);
74  in.read(&res[0], res.size());
75  in.close();
76  return res;
77  }
78 
79  std::shared_ptr<std::istream>
80  open_input_file(const std::string& file)
81  {
82  std::shared_ptr<std::istream> res;
83  if (file.empty() || file == "-")
84  res.reset(&std::cin, [](...){});
85  else
86  {
87  res.reset(new std::ifstream(file.c_str()));
88  require(res->good(),
89  "cannot open ", file, " for reading: ", strerror(errno));
90  }
91  return res;
92  }
93 
94  std::shared_ptr<std::ostream>
95  open_output_file(const std::string& file)
96  {
97  std::shared_ptr<std::ostream> res;
98  if (file.empty() || file == "-")
99  res.reset(&std::cout, [](...){});
100  else
101  {
102  res.reset(new std::ofstream(file.c_str()));
103  require(res->good(),
104  "cannot open ", file, " for writing: ", strerror(errno));
105  }
106  return res;
107  }
108 }
std::ostream & str_escape(std::ostream &os, const std::string &str)
Output a string, escaping special characters.
Definition: escape.cc:43
std::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:372
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:37
std::shared_ptr< std::istream > open_input_file(const std::string &file)
Open file for reading and return its autoclosing stream.
Definition: stream.cc:80
void require(bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:75
std::shared_ptr< std::ostream > open_output_file(const std::string &file)
Open file for writing and return its autoclosing stream.
Definition: stream.cc:95
std::wostream wcnull
An wide-char stream that discards the output.
Definition: stream.cc:14
std::string get_file_contents(const std::string &file)
Return the contents of file.
Definition: stream.cc:65
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 bracketed(std::istream &i, char lbracket, 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