Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
print.cc
Go to the documentation of this file.
1 #include <vcsn/algos/print.hh>
2 
4 #include <vcsn/dyn/algos.hh>
5 #include <vcsn/algos/print.hh>
6 #include <vcsn/dyn/automaton.hh>
7 #include <vcsn/misc/escape.hh>
8 #include <vcsn/misc/raise.hh>
9 #include <vcsn/misc/xalloc.hh>
10 
11 namespace vcsn
12 {
13  namespace dyn
14  {
15 
16  /*---------------------------.
17  | print(automaton, stream). |
18  `---------------------------*/
19 
20  std::ostream&
21  print(const dyn::automaton& aut, std::ostream& out,
22  const std::string& format)
23  {
24  if (format == "dot" || format == "default" || format == "")
25  dot(aut, out, false);
26  else if (format == "dot2tex")
27  dot(aut, out, true);
28  else if (format == "efsm")
29  efsm(aut, out);
30  else if (format == "fado")
31  fado(aut, out);
32  else if (format == "grail")
33  grail(aut, out);
34  else if (format == "info")
35  info(aut, out);
36  else if (format == "info,detailed")
37  info(aut, out, true);
38  else if (format == "null")
39  {}
40  else if (format == "tikz")
41  tikz(aut, out);
42  else
43  raise("invalid output format for automaton: ", str_escape(format));
44  return out;
45  }
46 
47  /*-------------------------.
48  | print(context, stream). |
49  `-------------------------*/
50 
51  REGISTER_DEFINE(print_ctx);
52  std::ostream&
53  print(const dyn::context& ctx, std::ostream& out, const std::string& format)
54  {
55  return detail::print_ctx_registry().call(ctx, out, format);
56  }
57 
58  /*---------------------------.
59  | print(expansion, stream). |
60  `---------------------------*/
61 
62  REGISTER_DEFINE(print_expansion);
63 
64  std::ostream&
65  print(const dyn::expansion& w, std::ostream& out, const std::string& format)
66  {
67  if (format == "null")
68  {}
69  else if (format == "latex")
70  detail::print_expansion_registry().call(w, out, format);
71  else if (format == "text" || format == "default" || format == "")
72  {
73  // FIXME: problem with rvalue if we pass
74  // 'std::string("text")'.
75  // FIXME: We _need_ the const, see name.hh.
76  const std::string format = "text";
77  detail::print_expansion_registry().call(w, out, format);
78  }
79  else
80  raise("invalid output format for expansion: ", str_escape(format));
81  return out;
82  }
83 
84  /*-----------------------.
85  | print(label, stream). |
86  `-----------------------*/
87 
88  REGISTER_DEFINE(print_label);
89 
90  std::ostream&
91  print(const dyn::label& w, std::ostream& out, const std::string& format)
92  {
93  if (format == "null")
94  {}
95  else if (format == "latex")
96  detail::print_label_registry().call(w, out, format);
97  else if (format == "text" || format == "default" || format == "")
98  {
99  // FIXME: problem with rvalue if we pass
100  // 'std::string("text")'.
101  // FIXME: We _need_ the const, see name.hh.
102  const std::string format = "text";
103  detail::print_label_registry().call(w, out, format);
104  }
105  else
106  raise("invalid output format for label: ", str_escape(format));
107  return out;
108  }
109 
110  /*----------------------------.
111  | print(polynomial, stream). |
112  `----------------------------*/
113 
114  REGISTER_DEFINE(list_polynomial);
115  REGISTER_DEFINE(print_polynomial);
116 
117  std::ostream&
118  print(const polynomial& p, std::ostream& out, const std::string& format)
119  {
120  if (format == "list")
121  detail::list_polynomial_registry().call(p, out);
122  else if (format == "null")
123  {}
124  else if (format == "latex")
125  detail::print_polynomial_registry().call(p, out, format);
126  else if (format == "text" || format == "default" || format == "")
127  {
128  // FIXME: problem with rvalue if we pass
129  // 'std::string("text")'.
130  // FIXME: We _need_ the const, see name.hh.
131  const std::string format = "text";
132  detail::print_polynomial_registry().call(p, out, format);
133  }
134  else
135  raise("invalid output format for polynomial: ", str_escape(format));
136  return out;
137  }
138 
139 
140  /*------------------------.
141  | print(ratexp, stream). |
142  `------------------------*/
143 
144  REGISTER_DEFINE(print_ratexp);
145 
146  std::ostream&
147  print(const ratexp& exp, std::ostream& out, const std::string& format)
148  {
149  if (format == "info")
150  info(exp, out);
151  else if (format == "null")
152  {}
153  else if (format == "latex")
154  detail::print_ratexp_registry().call(exp, out, format);
155  else if (format == "text" || format == "default" || format == "")
156  {
157  // FIXME: problem with rvalue if we pass
158  // 'std::string("text")'.
159  // FIXME: We _need_ the const, see name.hh.
160  const std::string format = "text";
161  detail::print_ratexp_registry().call(exp, out, format);
162  }
163  else
164  raise("invalid output format for ratexp: ", str_escape(format));
165  return out;
166  }
167 
168 
169  /*------------------------.
170  | print(weight, stream). |
171  `------------------------*/
172 
173  REGISTER_DEFINE(print_weight);
174 
175  std::ostream&
176  print(const dyn::weight& w, std::ostream& out, const std::string& format)
177  {
178  if (format == "null")
179  {}
180  else if (format == "latex")
181  detail::print_weight_registry().call(w, out, format);
182  else if (format == "text" || format == "default" || format == "")
183  {
184  // FIXME: problem with rvalue if we pass
185  // 'std::string("text")'.
186  // FIXME: We _need_ the const, see name.hh.
187  const std::string format = "text";
188  detail::print_weight_registry().call(w, out, format);
189  }
190  else
191  raise("invalid output format for weight: ", str_escape(format));
192  return out;
193  }
194 
195 
196  /*-----------------.
197  | ostream format. |
198  `-----------------*/
199 
201 
202  void
203  set_format(std::ostream& o, const std::string& format)
204  {
205  if (!format_flag(o))
206  format_flag(o) = new std::string;
207  *format_flag(o) = format;
208  }
209 
210  std::string
211  get_format(std::ostream& o)
212  {
213  if (!format_flag(o))
214  format_flag(o) = new std::string;
215  return *format_flag(o);
216  }
217 
218  }
219 }
220 
221 namespace std
222 {
223  std::ostream&
224  operator<<(std::ostream& o, const vcsn::dyn::automaton& a)
225  {
226  return vcsn::dyn::print(a, o, vcsn::dyn::get_format(o));
227  }
228 
229  std::ostream&
230  operator<<(std::ostream& o, const vcsn::dyn::context& c)
231  {
232  return vcsn::dyn::print(c, o, vcsn::dyn::get_format(o));
233  }
234 
235  std::ostream&
236  operator<<(std::ostream& o, const vcsn::dyn::expansion& e)
237  {
238  return vcsn::dyn::print(e, o, vcsn::dyn::get_format(o));
239  }
240 
241  std::ostream&
242  operator<<(std::ostream& o, const vcsn::dyn::label& l)
243  {
244  return vcsn::dyn::print(l, o, vcsn::dyn::get_format(o));
245  }
246 
247  std::ostream&
248  operator<<(std::ostream& o, const vcsn::dyn::polynomial& p)
249  {
250  return vcsn::dyn::print(p, o, vcsn::dyn::get_format(o));
251  }
252 
253  std::ostream&
254  operator<<(std::ostream& o, const vcsn::dyn::ratexp& r)
255  {
256  return vcsn::dyn::print(r, o, vcsn::dyn::get_format(o));
257  }
258 
259  std::ostream&
260  operator<<(std::ostream& o, const vcsn::dyn::weight& w)
261  {
262  return vcsn::dyn::print(w, o, vcsn::dyn::get_format(o));
263  }
264 
265 }
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:71
std::string get_format(std::ostream &o)
Get the output format for o.
Definition: print.cc:211
std::ostream & list_polynomial(const polynomial &polynomial, std::ostream &o)
Bridge.
Definition: print.hh:126
std::ostream & operator<<(std::ostream &o, type_t t)
Definition: printer.hxx:16
std::ostream & print_polynomial(const polynomial &polynomial, std::ostream &o, const std::string &format)
Bridge.
Definition: print.hh:148
std::ostream & print_ctx(const context &ctx, std::ostream &o, const std::string &format)
Bridge.
Definition: print.hh:29
std::ostream & print(const automaton &a, std::ostream &o, const std::string &format="default")
Print automaton a on o using format format.
Definition: print.cc:21
std::ostream & print_weight(const weight &weight, std::ostream &o, const std::string &format)
Bridge.
Definition: print.hh:219
std::shared_ptr< const detail::weight_base > weight
Definition: fwd.hh:82
std::ostream & str_escape(std::ostream &os, const std::string &str)
Output a string, escaping special characters.
Definition: escape.cc:43
std::shared_ptr< detail::ratexp_base > ratexp
Definition: fwd.hh:64
std::ostream & print_expansion(const expansion &expansion, std::ostream &o, const std::string &format)
Bridge.
Definition: print.hh:63
std::shared_ptr< const detail::expansion_base > expansion
Definition: expansion.hh:74
Declaration of vcsn::xalloc.
std::ostream & print_ratexp(const ratexp &exp, std::ostream &o, const std::string &format)
Bridge.
Definition: print.hh:184
std::ostream & grail(const automaton &aut, std::ostream &out)
Output in Grail format.
Definition: grail.cc:17
std::ostream & fado(const automaton &aut, std::ostream &out)
Output in FAdo format.
Definition: fado.cc:109
Allocate slots in std::ostreams.
Definition: xalloc.hh:39
std::ostream & print_label(const label &label, std::ostream &o, const std::string &format)
Bridge.
Definition: print.hh:86
std::shared_ptr< const detail::label_base > label
Definition: fwd.hh:46
std::shared_ptr< const detail::context_base > context
Definition: context.hh:71
std::ostream & tikz(const automaton &aut, std::ostream &out)
Output aut in LaTeX's TikZ format.
Definition: tikz.cc:17
std::ostream & dot(const automaton &aut, std::ostream &out, bool dot2tex=false)
Output aut in GraphViz' Dot format.
Definition: dot.cc:17
std::ostream & info(const automaton &aut, std::ostream &out, bool detailed=false)
Output various facts about an automaton.
Definition: info.cc:17
xalloc< std::string * > format_flag
Definition: print.cc:200
void set_format(std::ostream &o, const std::string &format)
Specify the output format for o.
Definition: print.cc:203
std::ostream & efsm(const automaton &aut, std::ostream &out)
Output in Extended FSM format.
Definition: efsm.cc:157
std::shared_ptr< const detail::polynomial_base > polynomial
Definition: fwd.hh:55
variadic_mul_mixin< detail::r_impl > r
Definition: fwd.hh:42