17 bracketed(std::istream& i,
const char lbracket,
const char rbracket)
19 assert(i.peek() == lbracket);
24 while ((c = i.get()) != -1)
28 else if (c == rbracket
33 raise(
"missing ",
str_escape(rbracket),
" after ",
37 char eat(std::istream&
is,
char c)
46 const std::string&
eat(std::istream&
is,
const std::string& expect)
50 size_t cnt = expect.size();
51 while (cnt && is >> c)
65 std::ifstream in(file.c_str(), std::ios::in | std::ios::binary);
66 require(in.good(),
"cannot read file: ", file,
": ", strerror(errno));
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());
77 std::shared_ptr<std::istream>
80 std::shared_ptr<std::istream> res;
81 if (file.empty() || file ==
"-")
82 res.reset(&std::cin, [](...){});
85 res.reset(
new std::ifstream(file.c_str()));
87 "cannot open ", file,
" for reading: ", strerror(errno));
92 std::shared_ptr<std::ostream>
95 std::shared_ptr<std::ostream> res;
96 if (file.empty() || file ==
"-")
97 res.reset(&std::cout, [](...){});
100 res.reset(
new std::ofstream(file.c_str()));
102 "cannot open ", file,
" for writing: ", strerror(errno));
111 std::getline(is, buf,
'\n');
std::string bracketed(std::istream &i, const char lbracket, const char rbracket)
Extract the string which is here between lbracket and rbracket.
std::ostream cnull
An narrow-char stream that discards the output.
std::string get_file_contents(const std::string &file)
Return the contents of file.
std::shared_ptr< std::ostream > open_output_file(const std::string &file)
Open file for writing and return its autoclosing stream.
std::ostream & str_escape(std::ostream &os, const std::string &str)
Output a string, escaping special characters.
std::istringstream is
The input stream: the specification to translate.
std::shared_ptr< std::istream > open_input_file(const std::string &file)
Open file for reading and return its autoclosing stream.
char eat(std::istream &is, char c)
Check lookahead character and advance.
std::wostream wcnull
An wide-char stream that discards the output.
ATTRIBUTE_NORETURN void fail_reading(std::istream &is, std::string explanation)
Throw an exception after failing to read from is.
void require(bool b, Args &&...args)
If b is not verified, raise an error with args as message.