Vaucanson  1.4.1
io.hxx
1 // io.hxx: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2008 The
6 // Vaucanson Group.
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License
10 // as published by the Free Software Foundation; either version 2
11 // of the License, or (at your option) any later version.
12 //
13 // The complete GNU General Public Licence Notice can be found as the
14 // `COPYING' file in the root directory.
15 //
16 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
17 //
18 #ifndef VCSN_TOOLS_IO_HXX
19 # define VCSN_TOOLS_IO_HXX
20 
21 # include <vaucanson/tools/io.hh>
22 # include <sstream>
23 
24 namespace vcsn
25 {
26 
27  namespace tools
28  {
29 
30  /*-------.
31  | Output |
32  `-------*/
33 
34  template<typename Auto, typename TransitionConverter, typename Format>
35  Auto& automaton_saver_<Auto, TransitionConverter, Format>::automaton()
36  {
37  return a_;
38  }
39 
40  template<typename Auto, typename TransitionConverter, typename Format>
41  const Auto& automaton_saver_<Auto, TransitionConverter, Format>::
42  automaton() const
43  {
44  return a_;
45  }
46 
47  template<typename Auto, typename TransitionConverter, typename Format>
48  automaton_saver_<Auto, TransitionConverter, Format>::
49  automaton_saver_(const Auto& a,
50  const TransitionConverter& c,
51  const Format& f)
52  : a_(a), conv_(c), format_(f)
53  {}
54 
55  template<typename Auto, typename TransitionConverter, typename Format>
56  std::ostream&
57  operator<<(std::ostream& o,
58  const automaton_saver_<Auto, TransitionConverter, Format>& s)
59  {
60  BENCH_TASK_SCOPED("automaton output");
61  s.format_(o, s, s.conv_);
62  return o;
63  }
64 
65  template<typename S, typename T>
66  std::string string_out::operator()(const AutomataBase<S>&, const T& t) const
67  {
68  std::ostringstream os;
69  os << t;
70  return os.str();
71  }
72 
73  template<typename S, typename T>
74  std::string string_out::operator()(const TransducerBase<S>&, const T& t) const
75  {
76  std::ostringstream os;
77  os << t;
78  return os.str();
79  }
80 
81  } // ! tools
82 
83  template<typename Auto, typename TransitionConverter, typename Format>
84  tools::automaton_saver_<Auto, TransitionConverter, Format>
85  automaton_saver(const Auto& a,
86  const TransitionConverter& e,
87  const Format& f)
88  {
89  return tools::automaton_saver_<Auto, TransitionConverter, Format>(a, e, f);
90  }
91 
92  namespace tools
93  {
94  /*-------.
95  | Output |
96  `-------*/
97 
98  template<typename RE, typename TransitionConverter, typename Format>
99  RE& regexp_saver_<RE, TransitionConverter, Format>::rat_exp()
100  {
101  return r_;
102  }
103 
104  template<typename RE, typename TransitionConverter, typename Format>
105  const RE& regexp_saver_<RE, TransitionConverter, Format>::
106  rat_exp() const
107  {
108  return r_;
109  }
110 
111  template<typename RE, typename TransitionConverter, typename Format>
112  regexp_saver_<RE, TransitionConverter, Format>::
113  regexp_saver_(const RE& r,
114  const TransitionConverter& c,
115  const Format& f)
116  : r_(r), conv_(c), format_(f)
117  {}
118 
119  template<typename RE, typename TransitionConverter, typename Format>
120  std::ostream&
121  operator<<(std::ostream& o,
122  const regexp_saver_<RE, TransitionConverter, Format>& s)
123  {
124  BENCH_TASK_SCOPED("regexp output");
125  s.format_(o, s, s.conv_);
126  return o;
127  }
128 
129  } // ! tools
130 
131  template<typename RE, typename TransitionConverter, typename Format>
132  tools::regexp_saver_<RE, TransitionConverter, Format>
133  regexp_saver(const RE& r,
134  const TransitionConverter& e,
135  const Format& f)
136  {
137  return tools::regexp_saver_<RE, TransitionConverter, Format>(r, e, f);
138  }
139 
140  namespace tools
141  {
142 
143  /*------.
144  | Input |
145  `------*/
146 
147  template<typename Auto, typename TransitionConverter, typename Format>
148  Auto& automaton_loader_<Auto, TransitionConverter, Format>::automaton()
149  {
150  return a_;
151  }
152 
153  template<typename Auto, typename TransitionConverter, typename Format>
154  const Auto&
155  automaton_loader_<Auto, TransitionConverter, Format>::automaton() const
156  {
157  return a_;
158  }
159 
160  template<typename Auto, typename TransitionConverter, typename Format>
161  automaton_loader_<Auto, TransitionConverter, Format>::
162  automaton_loader_(Auto& a,
163  const TransitionConverter& conv,
164  const Format& format,
165  bool merge_states)
166  : a_(a),
167  conv_(conv),
168  format_(format),
169  scount_(0),
170  smap_(),
171  merge_states_(merge_states)
172  {}
173 
174  template<typename Auto, typename TransitionConverter, typename Format>
175  typename Auto::hstate_t
176  automaton_loader_<Auto, TransitionConverter, Format>::
177  add_state(unsigned s)
178  {
179  if (smap_.find(s) == smap_.end())
180  {
181  if (a_.has_state(s) && merge_states_)
182  smap_[s] = hstate_t(s);
183  else
184  smap_[s] = a_.add_state();
185  }
186  return smap_[s];
187  }
188 
189  template<typename Auto, typename TransitionConverter, typename Format>
190  void automaton_loader_<Auto, TransitionConverter, Format>::
191  set_initial(unsigned s, const std::string& lbl)
192  {
193  a_.set_initial(add_state(s), conv_(a_, lbl));
194  }
195 
196  template<typename Auto, typename TransitionConverter, typename Format>
197  void automaton_loader_<Auto, TransitionConverter, Format>::
198  set_final(unsigned s, const std::string& lbl)
199  {
200  a_.set_final(add_state(s), conv_(a_, lbl));
201  }
202 
203  template<typename Auto, typename TransitionConverter, typename Format>
204  void automaton_loader_<Auto, TransitionConverter, Format>::
205  add_spontaneous(unsigned from, unsigned to)
206  {
207  a_.add_spontaneous(add_state(from), add_state(to));
208  }
209 
210  template<typename Auto, typename TransitionConverter, typename Format>
211  void automaton_loader_<Auto, TransitionConverter, Format>::
212  add_transition(unsigned from, unsigned to, const std::string& lbl)
213  {
214  a_.add_series_transition(add_state(from), add_state(to), conv_(a_, lbl));
215  }
216 
217  template<typename Auto, typename TransitionConverter, typename Format>
218  std::istream&
219  operator>>(std::istream& in,
220  automaton_loader_<Auto, TransitionConverter, Format> l)
221  {
222  BENCH_TASK_SCOPED("automaton input");
223  l.format_(in, l);
224  return in;
225  }
226 
227  } // ! tools
228 
229  template<typename Auto, typename TransitionConverter, typename Format>
230  tools::automaton_loader_<Auto, TransitionConverter, Format>
231  automaton_loader(Auto& a,
232  const TransitionConverter& e,
233  const Format& f,
234  bool merge_states)
235  {
236  return tools::automaton_loader_<Auto, TransitionConverter, Format>
237  (a, e, f, merge_states);
238  }
239 
240  namespace tools
241  {
242 
243  /*------.
244  | Input |
245  `------*/
246 
247  template<typename RE, typename TransitionConverter, typename Format>
248  RE& regexp_loader_<RE, TransitionConverter, Format>::rat_exp()
249  {
250  return r_;
251  }
252 
253  template<typename RE, typename TransitionConverter, typename Format>
254  const RE&
255  regexp_loader_<RE, TransitionConverter, Format>::rat_exp() const
256  {
257  return r_;
258  }
259 
260  template<typename RE, typename TransitionConverter, typename Format>
261  regexp_loader_<RE, TransitionConverter, Format>::
262  regexp_loader_(RE& r,
263  const TransitionConverter& conv,
264  const Format& format)
265  : r_(r),
266  conv_(conv),
267  format_(format)
268  {}
269 
270  template<typename RE, typename TransitionConverter, typename Format>
271  std::istream&
272  operator>>(std::istream& in,
273  regexp_loader_<RE, TransitionConverter, Format> l)
274  {
275  BENCH_TASK_SCOPED("regexp input");
276  l.format_(in, l);
277  return in;
278  }
279 
280  } // ! tools
281 
282  template<typename RE, typename TransitionConverter, typename Format>
283  tools::regexp_loader_<RE, TransitionConverter, Format>
284  regexp_loader(RE& r,
285  const TransitionConverter& e,
286  const Format& f)
287  {
288  return tools::regexp_loader_<RE, TransitionConverter, Format>
289  (r, e, f);
290  }
291 
292 
293 } // ! vcsn
294 
295 #endif // ! VCSN_TOOLS_IO_HXX