Awali
Another Weighted Automata library
stream.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_MISC_STREAM_HH
18 # define AWALI_MISC_STREAM_HH
19 
20 # include <sstream>
21 # include <stdexcept>
22 # include <iostream> // cin
23 # include <memory> // shared_ptr
24 
26 //#include <awali/sttc/misc/export.hh>
27 #include <awali/sttc/misc/raise.hh>
28 
29 namespace awali { namespace sttc {
30  //deprecated
31  //LIBAWALI_API
33  //extern std::ostream cnull;
35  //extern std::wostream wcnull;
36 
39  std::string
40  bracketed(std::istream& i, const char lbracket, const char rbracket);
41 
43  template <typename ValueSet, typename... Args>
44  auto
45  conv(const ValueSet& vs, const std::string& str, Args&&... args)
46  -> decltype(vs.conv(std::declval<std::istream&>(),
47  std::forward<Args>(args)...))
48  {
49  std::istringstream i{str};
50  auto res = vs.conv(i, std::forward<Args>(args)...);
51  if (i.peek() != -1)
52  throw std::domain_error(vs.sname() + ": invalid value: " + str
53  + ", unexpected "
54  + str_escape(i.peek()));
55  return res;
56  }
57 
62  void eat(std::istream& is, char c){
63  require(is.peek() == c,
64  "unexpected: ", str_escape(is.peek()),
65  ": expected ", str_escape(c));
66  is.ignore();
67  }
68 
73  void eat(std::istream& is, const std::string& expect){
74  std::string s;
75  char c;
76  size_t cnt = expect.size();
77  while (cnt && is >> c)
78  {
79  s.append(1, c);
80  --cnt;
81  }
82  require(s == expect,
83  "unexpected: ", str_escape(s), ": expected ", str_escape(expect));
84  }
85 
92  ATTRIBUTE_NORETURN
93  void fail_reading(std::istream& is, std::string explanation)
94  {
95  is.clear();
96  std::string buf;
97  std::getline(is, buf, '\n');
98  if (!is.good())
99  // This shouldn't really happen; however it's best to fail cleanly.
100  is.clear();
101  if (!buf.empty())
102  explanation += ": " + str_escape(buf);
103  raise(explanation);
104  }
105 
107  template <typename ValueSet, typename... Args>
108  auto
109  format(const ValueSet& vs,
110  const typename ValueSet::value_t& v, Args&&... args)
111  -> std::string
112  {
113  std::ostringstream o;
114  vs.print(v, o, std::forward<Args>(args)...);
115  return o.str();
116  }
117 
119  std::string get_file_contents(const std::string& file);
120 
123  std::shared_ptr<std::istream> open_input_file(const std::string& file);
124 
127  std::shared_ptr<std::ostream> open_output_file(const std::string& file);
128 
129 }}//end of ns awali::stc
130 
131 #endif // !AWALI_MISC_STREAM_HH
The semiring of complex numbers.
Definition: c.hh:44
void eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.hh:62
ATTRIBUTE_NORETURN void fail_reading(std::istream &is, std::string explanation)
Throw an exception after failing to read from is.
Definition: stream.hh:93
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 int c)
Definition: escape.hh:30
std::string bracketed(std::istream &i, const char lbracket, const char rbracket)
An narrow-char stream that discards the output.
auto format(const ValueSet &vs, const typename ValueSet::value_t &v, Args &&... args) -> std::string
Format v via vs.print.
Definition: stream.hh:109
std::string get_file_contents(const std::string &file)
Return the contents of file.
std::shared_ptr< std::istream > open_input_file(const std::string &file)
Open file for reading and return its autoclosing stream.
void require(bool b, Args &&... args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:55
auto conv(const ValueSet &vs, const std::string &str, Args &&... args) -> decltype(vs.conv(std::declval< std::istream & >(), std::forward< Args >(args)...))
Parse str via vs.conv.
Definition: stream.hh:45
Main namespace of Awali.
Definition: ato.hh:22