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 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 tools
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       TIMER_SCOPED ("automaton output");
00060       s.format_(o, s, s.conv_);
00061       return o;
00062     }
00063 
00064     inline
00065     std::string string_out::check_empty_word(const std::string& str) const
00066     {
00067       if (str.empty())
00068         return std::string("1");
00069       return str;
00070     }
00071 
00072     template<typename S, typename T>
00073     std::string string_out::operator()(const AutomataBase<S>&, const T& t) const
00074     {
00075       std::ostringstream os;
00076       os << t;
00077       return os.str();
00078     }
00079 
00080     template<typename S, typename T>
00081     std::string string_out::operator()(const TransducerBase<S>&, const T& t) const
00082     {
00083       std::ostringstream os;
00084       os << t;
00085       return check_empty_word(os.str());
00086     }
00087 
00088   } // tools
00089 
00090   template<typename Auto, typename TransitionConverter, typename Format>
00091   tools::automaton_saver_<Auto, TransitionConverter, Format>
00092   automaton_saver(const Auto& a,
00093                   const TransitionConverter& e,
00094                   const Format& f)
00095   {
00096     return tools::automaton_saver_<Auto, TransitionConverter, Format>(a, e, f);
00097   }
00098 
00099   namespace tools
00100   {
00101 
00102     /*------.
00103     | Input |
00104     `------*/
00105 
00106 
00107     template<typename Auto, typename TransitionConverter, typename Format>
00108     Auto& automaton_loader_<Auto, TransitionConverter, Format>::automaton()
00109     {
00110       return a_;
00111     }
00112 
00113     template<typename Auto, typename TransitionConverter, typename Format>
00114     const Auto&
00115     automaton_loader_<Auto, TransitionConverter, Format>::automaton() const
00116     {
00117       return a_;
00118     }
00119 
00120     template<typename Auto, typename TransitionConverter, typename Format>
00121     automaton_loader_<Auto, TransitionConverter, Format>::
00122     automaton_loader_(Auto& a,
00123                       const TransitionConverter& conv,
00124                       const Format& format,
00125                       bool merge_states)
00126       : a_(a),
00127         conv_(conv),
00128         format_(format),
00129         scount_(0),
00130         smap_(),
00131         merge_states_(merge_states)
00132     {}
00133 
00134     template<typename Auto, typename TransitionConverter, typename Format>
00135     hstate_t automaton_loader_<Auto, TransitionConverter, Format>::
00136     add_state(unsigned s)
00137     {
00138       if (smap_.find(s) == smap_.end())
00139       {
00140         if (a_.has_state(s) && merge_states_)
00141           smap_[s] = s;
00142         else
00143           smap_[s] = a_.add_state();
00144       }
00145       return smap_[s];
00146     }
00147 
00148     template<typename Auto, typename TransitionConverter, typename Format>
00149     void automaton_loader_<Auto, TransitionConverter, Format>::
00150     set_initial(unsigned s, const std::string& lbl)
00151     {
00152       a_.set_initial(add_state(s), conv_(a_, lbl));
00153     }
00154 
00155     template<typename Auto, typename TransitionConverter, typename Format>
00156     void automaton_loader_<Auto, TransitionConverter, Format>::
00157     set_final(unsigned s, const std::string& lbl)
00158     {
00159       a_.set_final(add_state(s), conv_(a_, lbl));
00160     }
00161 
00162     template<typename Auto, typename TransitionConverter, typename Format>
00163     void automaton_loader_<Auto, TransitionConverter, Format>::
00164     add_spontaneous(unsigned from, unsigned to)
00165     {
00166       a_.add_spontaneous(add_state(from), add_state(to));
00167     }
00168 
00169     template<typename Auto, typename TransitionConverter, typename Format>
00170     void automaton_loader_<Auto, TransitionConverter, Format>::
00171     add_transition(unsigned from, unsigned to, const std::string& lbl)
00172     {
00173       a_.add_series_transition(add_state(from), add_state(to), conv_(a_, lbl));
00174     }
00175 
00176     template<typename Auto, typename TransitionConverter, typename Format>
00177     std::istream&
00178     operator>>(std::istream& in,
00179                automaton_loader_<Auto, TransitionConverter, Format> l)
00180     {
00181       TIMER_SCOPED ("automaton input");
00182       l.format_(in, l);
00183       return in;
00184     }
00185 
00186 
00187   } // tools
00188 
00189   template<typename Auto, typename TransitionConverter, typename Format>
00190   tools::automaton_loader_<Auto, TransitionConverter, Format>
00191   automaton_loader(Auto& a,
00192                    const TransitionConverter& e,
00193                    const Format& f,
00194                    bool merge_states)
00195   {
00196     return tools::automaton_loader_<Auto, TransitionConverter, Format>
00197       (a, e, f, merge_states);
00198   }
00199 
00200 } // vcsn
00201 
00202 
00203 #endif // ! VCSN_TOOLS_IO_HXX

Generated on Sun Jul 29 19:35:20 2007 for Vaucanson by  doxygen 1.5.2