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 ",
41 switch (
int c = i.get())
44 #define CASE(Key, Value) \
45 case Key: res = Value; i.ignore(); break
61 "get_char: unexpected end-of-file"
64 "get_char: invalid escape: \\x",
char(c1));
67 "get_char: unexpected end-of-file"
68 " after: \\x",
char(c1));
70 "get_char: invalid escape: \\x",
72 res = std::stoi(std::string{
char(c1),
char(c2)},
nullptr, 16);
80 "get_char: invalid escape: \\",
char(c),
" in \\",
86 "get_char: unexpected end-of-file");
90 char eat(std::istream& is,
char c)
101 const std::string&
eat(std::istream& is,
const std::string& expect)
105 size_t cnt = expect.size();
106 while (cnt && is >> c)
122 VCSN_REQUIRE(
in.good(),
"cannot read file: ", file,
": ", strerror(errno));
125 in.seekg(0, std::ios::end);
126 res.resize(
in.tellg());
127 in.seekg(0, std::ios::beg);
128 in.read(&res[0], res.size());
133 std::shared_ptr<std::istream>
136 std::shared_ptr<std::istream>
res;
137 if (file.empty() || file ==
"-")
138 res.reset(&std::cin, [](...){});
141 res.reset(
new std::ifstream(file.c_str()));
143 "cannot open ", file,
" for reading: ", strerror(errno));
148 std::shared_ptr<std::ostream>
151 std::shared_ptr<std::ostream>
res;
152 if (file.empty() || file ==
"-")
153 res.reset(&std::cout, [](...){});
156 res.reset(
new std::ofstream(file.c_str()));
158 "cannot open ", file,
" for writing: ", strerror(errno));
ATTRIBUTE_NORETURN void fail_reading(std::istream &is, Args &&...args)
Throw an exception after failing to read from is.
std::ostream & str_escape(std::ostream &os, const std::string &str, const char *special=nullptr)
Output a string, escaping special characters.
std::string get_file_contents(const std::string &file)
Return the contents of file.
auto in(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions arriving to state s.
char get_char(std::istream &i)
Read a single char, with possible -escape support.
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
std::string bracketed(std::istream &i, char lbracket, char rbracket)
Extract the string which is here between lbracket and rbracket.
std::ostream cnull
An narrow-char stream that discards the output.
#define VCSN_REQUIRE(Cond,...)
A macro similar to require.
std::shared_ptr< std::istream > open_input_file(const std::string &file)
Open file for reading and return its autoclosing stream.
std::wostream wcnull
An wide-char stream that discards the output.
std::shared_ptr< std::ostream > open_output_file(const std::string &file)
Open file for writing and return its autoclosing stream.
char eat(std::istream &is, char c)
Check lookahead character and advance.