Vaucanson 1.4
io.hxx
00001 // io.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2008 The
00006 // Vaucanson Group.
00007 //
00008 // This program is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU General Public License
00010 // as published by the Free Software Foundation; either version 2
00011 // of the License, or (at your option) any later version.
00012 //
00013 // The complete GNU General Public Licence Notice can be found as the
00014 // `COPYING' file in the root directory.
00015 //
00016 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
00017 //
00018 #ifndef VCSN_TOOLS_IO_HXX
00019 # define VCSN_TOOLS_IO_HXX
00020 
00021 # include <vaucanson/tools/io.hh>
00022 # include <sstream>
00023 
00024 namespace vcsn
00025 {
00026 
00027   namespace tools
00028   {
00029 
00030     /*-------.
00031     | Output |
00032     `-------*/
00033 
00034     template<typename Auto, typename TransitionConverter, typename Format>
00035     Auto& automaton_saver_<Auto, TransitionConverter, Format>::automaton()
00036     {
00037       return a_;
00038     }
00039 
00040     template<typename Auto, typename TransitionConverter, typename Format>
00041     const Auto& automaton_saver_<Auto, TransitionConverter, Format>::
00042     automaton() const
00043     {
00044       return a_;
00045     }
00046 
00047     template<typename Auto, typename TransitionConverter, typename Format>
00048     automaton_saver_<Auto, TransitionConverter, Format>::
00049     automaton_saver_(const Auto& a,
00050                      const TransitionConverter& c,
00051                      const Format& f)
00052       : a_(a), conv_(c), format_(f)
00053     {}
00054 
00055     template<typename Auto, typename TransitionConverter, typename Format>
00056     std::ostream&
00057     operator<<(std::ostream& o,
00058                const automaton_saver_<Auto, TransitionConverter, Format>& s)
00059     {
00060       BENCH_TASK_SCOPED("automaton output");
00061       s.format_(o, s, s.conv_);
00062       return o;
00063     }
00064 
00065     template<typename S, typename T>
00066     std::string string_out::operator()(const AutomataBase<S>&, const T& t) const
00067     {
00068       std::ostringstream os;
00069       os << t;
00070       return os.str();
00071     }
00072 
00073     template<typename S, typename T>
00074     std::string string_out::operator()(const TransducerBase<S>&, const T& t) const
00075     {
00076       std::ostringstream os;
00077       os << t;
00078       return os.str();
00079     }
00080 
00081   } // ! tools
00082 
00083   template<typename Auto, typename TransitionConverter, typename Format>
00084   tools::automaton_saver_<Auto, TransitionConverter, Format>
00085   automaton_saver(const Auto& a,
00086                   const TransitionConverter& e,
00087                   const Format& f)
00088   {
00089     return tools::automaton_saver_<Auto, TransitionConverter, Format>(a, e, f);
00090   }
00091 
00092   namespace tools
00093   {
00094     /*-------.
00095     | Output |
00096     `-------*/
00097 
00098     template<typename RE, typename TransitionConverter, typename Format>
00099     RE& regexp_saver_<RE, TransitionConverter, Format>::rat_exp()
00100     {
00101       return r_;
00102     }
00103 
00104     template<typename RE, typename TransitionConverter, typename Format>
00105     const RE& regexp_saver_<RE, TransitionConverter, Format>::
00106     rat_exp() const
00107     {
00108       return r_;
00109     }
00110 
00111     template<typename RE, typename TransitionConverter, typename Format>
00112     regexp_saver_<RE, TransitionConverter, Format>::
00113     regexp_saver_(const RE& r,
00114                   const TransitionConverter& c,
00115                   const Format& f)
00116       : r_(r), conv_(c), format_(f)
00117     {}
00118 
00119     template<typename RE, typename TransitionConverter, typename Format>
00120     std::ostream&
00121     operator<<(std::ostream& o,
00122                const regexp_saver_<RE, TransitionConverter, Format>& s)
00123     {
00124       BENCH_TASK_SCOPED("regexp output");
00125       s.format_(o, s, s.conv_);
00126       return o;
00127     }
00128 
00129   } // ! tools
00130 
00131   template<typename RE, typename TransitionConverter, typename Format>
00132   tools::regexp_saver_<RE, TransitionConverter, Format>
00133   regexp_saver(const RE& r,
00134                const TransitionConverter& e,
00135                const Format& f)
00136   {
00137     return tools::regexp_saver_<RE, TransitionConverter, Format>(r, e, f);
00138   }
00139 
00140   namespace tools
00141   {
00142 
00143     /*------.
00144     | Input |
00145     `------*/
00146 
00147     template<typename Auto, typename TransitionConverter, typename Format>
00148     Auto& automaton_loader_<Auto, TransitionConverter, Format>::automaton()
00149     {
00150       return a_;
00151     }
00152 
00153     template<typename Auto, typename TransitionConverter, typename Format>
00154     const Auto&
00155     automaton_loader_<Auto, TransitionConverter, Format>::automaton() const
00156     {
00157       return a_;
00158     }
00159 
00160     template<typename Auto, typename TransitionConverter, typename Format>
00161     automaton_loader_<Auto, TransitionConverter, Format>::
00162     automaton_loader_(Auto& a,
00163                       const TransitionConverter& conv,
00164                       const Format& format,
00165                       bool merge_states)
00166       : a_(a),
00167         conv_(conv),
00168         format_(format),
00169         scount_(0),
00170         smap_(),
00171         merge_states_(merge_states)
00172     {}
00173 
00174     template<typename Auto, typename TransitionConverter, typename Format>
00175     typename Auto::hstate_t
00176     automaton_loader_<Auto, TransitionConverter, Format>::
00177     add_state(unsigned s)
00178     {
00179       if (smap_.find(s) == smap_.end())
00180       {
00181         if (a_.has_state(s) && merge_states_)
00182           smap_[s] = hstate_t(s);
00183         else
00184           smap_[s] = a_.add_state();
00185       }
00186       return smap_[s];
00187     }
00188 
00189     template<typename Auto, typename TransitionConverter, typename Format>
00190     void automaton_loader_<Auto, TransitionConverter, Format>::
00191     set_initial(unsigned s, const std::string& lbl)
00192     {
00193       a_.set_initial(add_state(s), conv_(a_, lbl));
00194     }
00195 
00196     template<typename Auto, typename TransitionConverter, typename Format>
00197     void automaton_loader_<Auto, TransitionConverter, Format>::
00198     set_final(unsigned s, const std::string& lbl)
00199     {
00200       a_.set_final(add_state(s), conv_(a_, lbl));
00201     }
00202 
00203     template<typename Auto, typename TransitionConverter, typename Format>
00204     void automaton_loader_<Auto, TransitionConverter, Format>::
00205     add_spontaneous(unsigned from, unsigned to)
00206     {
00207       a_.add_spontaneous(add_state(from), add_state(to));
00208     }
00209 
00210     template<typename Auto, typename TransitionConverter, typename Format>
00211     void automaton_loader_<Auto, TransitionConverter, Format>::
00212     add_transition(unsigned from, unsigned to, const std::string& lbl)
00213     {
00214       a_.add_series_transition(add_state(from), add_state(to), conv_(a_, lbl));
00215     }
00216 
00217     template<typename Auto, typename TransitionConverter, typename Format>
00218     std::istream&
00219     operator>>(std::istream& in,
00220                automaton_loader_<Auto, TransitionConverter, Format> l)
00221     {
00222       BENCH_TASK_SCOPED("automaton input");
00223       l.format_(in, l);
00224       return in;
00225     }
00226 
00227   } // ! tools
00228 
00229   template<typename Auto, typename TransitionConverter, typename Format>
00230   tools::automaton_loader_<Auto, TransitionConverter, Format>
00231   automaton_loader(Auto& a,
00232                    const TransitionConverter& e,
00233                    const Format& f,
00234                    bool merge_states)
00235   {
00236     return tools::automaton_loader_<Auto, TransitionConverter, Format>
00237       (a, e, f, merge_states);
00238   }
00239 
00240   namespace tools
00241   {
00242 
00243     /*------.
00244     | Input |
00245     `------*/
00246 
00247     template<typename RE, typename TransitionConverter, typename Format>
00248     RE& regexp_loader_<RE, TransitionConverter, Format>::rat_exp()
00249     {
00250       return r_;
00251     }
00252 
00253     template<typename RE, typename TransitionConverter, typename Format>
00254     const RE&
00255     regexp_loader_<RE, TransitionConverter, Format>::rat_exp() const
00256     {
00257       return r_;
00258     }
00259 
00260     template<typename RE, typename TransitionConverter, typename Format>
00261     regexp_loader_<RE, TransitionConverter, Format>::
00262     regexp_loader_(RE& r,
00263                    const TransitionConverter& conv,
00264                    const Format& format)
00265       : r_(r),
00266         conv_(conv),
00267         format_(format)
00268     {}
00269 
00270     template<typename RE, typename TransitionConverter, typename Format>
00271     std::istream&
00272     operator>>(std::istream& in,
00273                regexp_loader_<RE, TransitionConverter, Format> l)
00274     {
00275       BENCH_TASK_SCOPED("regexp input");
00276       l.format_(in, l);
00277       return in;
00278     }
00279 
00280   } // ! tools
00281 
00282   template<typename RE, typename TransitionConverter, typename Format>
00283   tools::regexp_loader_<RE, TransitionConverter, Format>
00284   regexp_loader(RE& r,
00285                 const TransitionConverter& e,
00286                 const Format& f)
00287   {
00288     return tools::regexp_loader_<RE, TransitionConverter, Format>
00289       (r, e, f);
00290   }
00291 
00292 
00293 } // ! vcsn
00294 
00295 #endif // ! VCSN_TOOLS_IO_HXX