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 The Vaucanson Group.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // The complete GNU General Public Licence Notice can be found as the
00013 // `COPYING' file in the root directory.
00014 //
00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
00016 //
00017 #ifndef VCSN_TOOLS_IO_HXX
00018 # define VCSN_TOOLS_IO_HXX
00019 
00020 # include <vaucanson/tools/io.hh>
00021 # include <sstream>
00022 
00023 namespace vcsn
00024 {
00025 
00026   namespace io
00027   {
00028 
00029     /*-------.
00030     | Output |
00031     `-------*/
00032 
00033     template<typename Auto, typename TransitionConverter, typename Format>
00034     Auto& automaton_saver_<Auto, TransitionConverter, Format>::automaton()
00035     {
00036       return a_;
00037     }
00038 
00039     template<typename Auto, typename TransitionConverter, typename Format>
00040     const Auto& automaton_saver_<Auto, TransitionConverter, Format>::
00041     automaton() const
00042     {
00043       return a_;
00044     }
00045 
00046     template<typename Auto, typename TransitionConverter, typename Format>
00047     automaton_saver_<Auto, TransitionConverter, Format>::
00048     automaton_saver_(const Auto& a,
00049                      const TransitionConverter& c,
00050                      const Format& f)
00051       : a_(a), conv_(c), format_(f)
00052     {}
00053 
00054     template<typename Auto, typename TransitionConverter, typename Format>
00055     std::ostream&
00056     operator<<(std::ostream& o,
00057                const automaton_saver_<Auto, TransitionConverter, Format>& s)
00058     {
00059       s.format_(o, s, s.conv_);
00060       return o;
00061     }
00062 
00063     inline
00064     std::string string_out::check_empty_word(const std::string& str) const
00065     {
00066       if (str.empty())
00067         return std::string("1");
00068       return str;
00069     }
00070 
00071     template<typename S, typename T>
00072     std::string string_out::operator()(const AutomataBase<S>&, const T& t) const
00073     {
00074       std::ostringstream os;
00075       os << t;
00076       return os.str();
00077     }
00078 
00079     template<typename S, typename T>
00080     std::string string_out::operator()(const TransducerBase<S>&, const T& t) const
00081     {
00082       std::ostringstream os;
00083       os << t;
00084       return check_empty_word(os.str());
00085     }
00086 
00087   } // io
00088 
00089   template<typename Auto, typename TransitionConverter, typename Format>
00090   io::automaton_saver_<Auto, TransitionConverter, Format>
00091   automaton_saver(const Auto& a,
00092                   const TransitionConverter& e = TransitionConverter(),
00093                   const Format& f = Format())
00094   {
00095     return io::automaton_saver_<Auto, TransitionConverter, Format>(a, e, f);
00096   }
00097 
00098   namespace io
00099   {
00100 
00101     /*------.
00102     | Input |
00103     `------*/
00104 
00105 
00106     template<typename Auto, typename TransitionConverter, typename Format>
00107     Auto& automaton_loader_<Auto, TransitionConverter, Format>::automaton()
00108     {
00109       return a_;
00110     }
00111 
00112     template<typename Auto, typename TransitionConverter, typename Format>
00113     const Auto&
00114     automaton_loader_<Auto, TransitionConverter, Format>::automaton() const
00115     {
00116       return a_;
00117     }
00118 
00119     template<typename Auto, typename TransitionConverter, typename Format>
00120     automaton_loader_<Auto, TransitionConverter, Format>::
00121     automaton_loader_(Auto& a,
00122                       const TransitionConverter& conv,
00123                       const Format& format,
00124                       bool merge_states)
00125       : a_(a),
00126         conv_(conv),
00127         format_(format),
00128         scount_(0),
00129         smap_(),
00130         merge_states_(merge_states)
00131     {}
00132 
00133     template<typename Auto, typename TransitionConverter, typename Format>
00134     hstate_t automaton_loader_<Auto, TransitionConverter, Format>::
00135     add_state(unsigned s)
00136     {
00137       if (smap_.find(s) == smap_.end())
00138       {
00139         if (a_.has_state(s) && merge_states_)
00140           smap_[s] = s;
00141         else
00142           smap_[s] = a_.add_state();
00143       }
00144       return smap_[s];
00145     }
00146 
00147     template<typename Auto, typename TransitionConverter, typename Format>
00148     void automaton_loader_<Auto, TransitionConverter, Format>::
00149     set_initial(unsigned s, const std::string& lbl)
00150     {
00151       a_.set_initial(add_state(s), conv_(a_, lbl));
00152     }
00153 
00154     template<typename Auto, typename TransitionConverter, typename Format>
00155     void automaton_loader_<Auto, TransitionConverter, Format>::
00156     set_final(unsigned s, const std::string& lbl)
00157     {
00158       a_.set_final(add_state(s), conv_(a_, lbl));
00159     }
00160 
00161     template<typename Auto, typename TransitionConverter, typename Format>
00162     void automaton_loader_<Auto, TransitionConverter, Format>::
00163     add_spontaneous(unsigned from, unsigned to)
00164     {
00165       a_.add_spontaneous(add_state(from), add_state(to));
00166     }
00167 
00168     template<typename Auto, typename TransitionConverter, typename Format>
00169     void automaton_loader_<Auto, TransitionConverter, Format>::
00170     add_transition(unsigned from, unsigned to, const std::string& lbl)
00171     {
00172       a_.add_series_transition(add_state(from), add_state(to), conv_(a_, lbl));
00173     }
00174 
00175     template<typename Auto, typename TransitionConverter, typename Format>
00176     std::istream&
00177     operator>>(std::istream& in,
00178                automaton_loader_<Auto, TransitionConverter, Format> l)
00179     {
00180       l.format_(in, l);
00181       return in;
00182     }
00183 
00184 
00185   }
00186 
00187   template<typename Auto, typename TransitionConverter, typename Format>
00188   io::automaton_loader_<Auto, TransitionConverter, Format>
00189   automaton_loader(Auto& a,
00190                    const TransitionConverter& e = TransitionConverter(),
00191                    const Format& f = Format(),
00192                    bool merge_states = false)
00193   {
00194     return io::
00195       automaton_loader_<Auto, TransitionConverter, Format>(a, e, f, merge_states);
00196   }
00197 
00198 } // vcsn
00199 
00200 
00201 #endif // ! VCSN_TOOLS_IO_HXX

Generated on Fri Jul 28 12:18:34 2006 for Vaucanson by  doxygen 1.4.6