17 bracketed(std::istream& i,
const char lbracket,
const char rbracket)
19 assert(i.peek() == lbracket);
22 auto&& o = std::ostringstream{};
24 while ((c = i.get()) != -1)
28 else if (c == rbracket
33 raise(
"missing ",
str_quote(rbracket),
" after ",
41 auto&& f1 = std::ifstream(fn1, std::ifstream::binary|std::ifstream::ate);
42 auto&&
f2 = std::ifstream(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 get_line(std::istream& is)
83 auto res = std::string{};
84 std::getline(is,
res,
'\n');
96 switch (
int c = i.get())
99 #define CASE(Key, Value) \ 100 case Key: res = Value; i.ignore(); break 116 "get_char: unexpected end-of-file" 119 "get_char: invalid escape: \\x",
char(c1));
122 "get_char: unexpected end-of-file" 123 " after: \\x",
char(c1));
125 "get_char: invalid escape: \\x",
127 res = std::stoi(std::string{
char(c1),
char(c2)},
nullptr, 16);
135 "get_char: invalid escape: ",
143 "get_char: unexpected end-of-file");
147 char eat(std::istream& is,
char c)
154 else if (is.peek() == EOF)
160 const std::string&
eat(std::istream& is,
const std::string& expect)
164 size_t cnt = expect.size();
165 while (cnt && is >> c)
180 auto&&
in = std::ifstream(file.c_str(),
std::ios::in | std::ios::binary);
181 VCSN_REQUIRE(
in.good(),
"cannot read file: ", file,
": ", strerror(errno));
184 in.seekg(0, std::ios::end);
185 res.resize(
in.tellg());
186 in.seekg(0, std::ios::beg);
187 in.read(&res[0], res.size());
192 std::shared_ptr<std::istream>
195 auto res = std::shared_ptr<std::istream>{};
196 if (file.empty() || file ==
"-")
197 res.reset(&std::cin, [](...){});
200 res.reset(
new std::ifstream(file.c_str()));
202 "cannot open ", file,
" for reading: ", strerror(errno));
207 std::shared_ptr<std::ostream>
210 auto res = std::shared_ptr<std::ostream>{};
211 if (file.empty() || file ==
"-")
212 res.reset(&std::cout, [](...){});
215 res.reset(
new std::ofstream(file.c_str()));
217 "cannot open ", file,
" for writing: ", strerror(errno));
224 while (isspace(is.peek()))
229 xgetenv(
const std::string& var,
const std::string& val)
231 const char* cp = getenv(var.c_str());
232 return cp ? cp : val;
std::string xgetenv(const std::string &var, const std::string &val="")
getenv(var) if defined, otherwise val.
char get_char(std::istream &i)
Read a single char, with possible -escape support.
char eat(std::istream &is, char c)
Check lookahead character and advance.
void skip_space(std::istream &is)
Ignore spaces.
std::string get_file_contents(const std::string &file)
Return the contents of file.
ATTRIBUTE_NORETURN void fail_reading(std::istream &is, Args &&... args)
Throw an exception after failing to read from is.
Provide a variadic mul on top of a binary mul(), and one().
std::string expand_tilda(const std::string &res)
Expand initial "~" in res.
bool equal_files(const std::string &fn1, const std::string &fn2)
Whether two files have exactly equal contents.
std::ostream cnull
An narrow-char stream that discards the output.
std::shared_ptr< std::istream > open_input_file(const std::string &file)
Open file for reading and return its autoclosing stream.
auto in(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions arriving to state s.
std::shared_ptr< std::ostream > open_output_file(const std::string &file)
Open file for writing and return its autoclosing stream.
std::string bracketed(std::istream &i, char lbracket, char rbracket)
Extract the string which is here between lbracket and rbracket.
std::string str_quote(Args &&... args)
Convert to a string, in quotes.
void require(Bool b, Args &&... args)
If b is not verified, raise an error with args as message.
std::wostream wcnull
An wide-char stream that discards the output.
#define VCSN_REQUIRE(Cond,...)
A macro similar to require.