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 std::ifstream f1(fn1, std::ifstream::binary|std::ifstream::ate);
42 std::ifstream
f2(fn2, std::ifstream::binary|std::ifstream::ate);
44 if (f1.fail() || f2.fail())
47 if (f1.tellg() != f2.tellg())
50 f1.seekg(0, std::ifstream::beg);
51 f2.seekg(0, std::ifstream::beg);
52 return std::equal(std::istreambuf_iterator<char>(f1.rdbuf()),
53 std::istreambuf_iterator<char>(),
54 std::istreambuf_iterator<char>(f2.rdbuf()));
61 if (!
res.empty() &&
res[0] ==
'~')
63 assert(
res.size() == 1 ||
res[1] ==
'/');
65 char const *hdrive = getenv(
"HOMEDRIVE");
66 char const *hres = getenv(
"HOMERES");
68 res.replace(0, 1, home);
69 else if (hdrive && hres)
70 res.replace(0, 1, std::string(hdrive) + hres);
72 res.replace(0, 1,
xgetenv(
"VCSN_TMPDIR",
"/tmp"));
81 switch (
int c = i.get())
84 #define CASE(Key, Value) \ 85 case Key: res = Value; i.ignore(); break 101 "get_char: unexpected end-of-file" 104 "get_char: invalid escape: \\x",
char(c1));
107 "get_char: unexpected end-of-file" 108 " after: \\x",
char(c1));
110 "get_char: invalid escape: \\x",
112 res = std::stoi(std::string{
char(c1),
char(c2)},
nullptr, 16);
120 "get_char: invalid escape: \\",
char(c),
" in \\",
126 "get_char: unexpected end-of-file");
130 char eat(std::istream& is,
char c)
141 const std::string&
eat(std::istream& is,
const std::string& expect)
145 size_t cnt = expect.size();
146 while (cnt && is >> c)
162 VCSN_REQUIRE(
in.good(),
"cannot read file: ", file,
": ", strerror(errno));
165 in.seekg(0, std::ios::end);
166 res.resize(
in.tellg());
167 in.seekg(0, std::ios::beg);
168 in.read(&res[0], res.size());
173 std::shared_ptr<std::istream>
176 std::shared_ptr<std::istream>
res;
177 if (file.empty() || file ==
"-")
178 res.reset(&std::cin, [](...){});
181 res.reset(
new std::ifstream(file.c_str()));
183 "cannot open ", file,
" for reading: ", strerror(errno));
188 std::shared_ptr<std::ostream>
191 std::shared_ptr<std::ostream>
res;
192 if (file.empty() || file ==
"-")
193 res.reset(&std::cout, [](...){});
196 res.reset(
new std::ofstream(file.c_str()));
198 "cannot open ", file,
" for writing: ", strerror(errno));
205 while (isspace(is.peek()))
210 xgetenv(
const std::string& var,
const std::string& val)
212 const char* cp = getenv(var.c_str());
213 return cp ? cp : val;
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.
std::shared_ptr< std::istream > open_input_file(const std::string &file)
Open file for reading and return its autoclosing stream.
bool equal_files(const std::string &fn1, const std::string &fn2)
Whether two files have exactly equal contents.
char get_char(std::istream &i)
Read a single char, with possible -escape support.
#define VCSN_REQUIRE(Cond,...)
A macro similar to require.
auto in(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions arriving to state s.
weightset_mixin< detail::f2_impl > f2
char eat(std::istream &is, char c)
Check lookahead character and advance.
std::string expand_tilda(const std::string &res)
Expand initial "~" in res.
std::string xgetenv(const std::string &var, const std::string &val="")
getenv(var) if defined, otherwise val.
ATTRIBUTE_NORETURN void fail_reading(std::istream &is, Args &&... args)
Throw an exception after failing to read from is.
std::ostream cnull
An narrow-char stream that discards the output.
void require(Bool b, Args &&... args)
If b is not verified, raise an error with args as message.
void skip_space(std::istream &is)
Ignore spaces.
std::string bracketed(std::istream &i, char lbracket, char rbracket)
Extract the string which is here between lbracket and rbracket.
std::string get_file_contents(const std::string &file)
Return the contents of file.
std::ostream & str_escape(std::ostream &os, const std::string &str, const char *special=nullptr)
Output a string, escaping special characters.