Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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  require(is.peek() == c,
40  "unexpected: ", str_escape(is.peek()),
41  ": expected ", str_escape(c));
42  is.ignore();
43  return c;
44  }
45 
46  const std::string& eat(std::istream& is, const std::string& expect)
47  {
48  std::string s;
49  char c;
50  size_t cnt = expect.size();
51  while (cnt && is >> c)
52  {
53  s.append(1, c);
54  --cnt;
55  }
56  require(s == expect,
57  "unexpected: ", str_escape(s), ": expected ", str_escape(expect));
58  return expect;
59  }
60 
61  // http://stackoverflow.com/questions/2602013.
62  std::string
63  get_file_contents(const std::string& file)
64  {
65  std::ifstream in(file.c_str(), std::ios::in | std::ios::binary);
66  require(in.good(), "cannot read file: ", file, ": ", strerror(errno));
67 
68  std::string res;
69  in.seekg(0, std::ios::end);
70  res.resize(in.tellg());
71  in.seekg(0, std::ios::beg);
72  in.read(&res[0], res.size());
73  in.close();
74  return res;
75  }
76 
77  std::shared_ptr<std::istream>
78  open_input_file(const std::string& file)
79  {
80  std::shared_ptr<std::istream> res;
81  if (file.empty() || file == "-")
82  res.reset(&std::cin, [](...){});
83  else
84  {
85  res.reset(new std::ifstream(file.c_str()));
86  require(res->good(),
87  "cannot open ", file, " for reading: ", strerror(errno));
88  }
89  return res;
90  }
91 
92  std::shared_ptr<std::ostream>
93  open_output_file(const std::string& file)
94  {
95  std::shared_ptr<std::ostream> res;
96  if (file.empty() || file == "-")
97  res.reset(&std::cout, [](...){});
98  else
99  {
100  res.reset(new std::ofstream(file.c_str()));
101  require(res->good(),
102  "cannot open ", file, " for writing: ", strerror(errno));
103  }
104  return res;
105  }
106 
107  void fail_reading(std::istream& is, std::string explanation)
108  {
109  is.clear();
110  std::string buf;
111  std::getline(is, buf, '\n');
112  if (!is.good())
113  // This shouldn't really happen; however it's best to fail cleanly.
114  is.clear();
115  if (!buf.empty())
116  explanation += ": " + str_escape(buf);
117  raise(explanation);
118  }
119 }
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
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
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