simple_format.hxx

00001 // simple_format.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_SIMPLE_FORMAT_HXX
00018 # define VCSN_TOOLS_SIMPLE_FORMAT_HXX
00019 
00020 # include <vaucanson/tools/simple_format.hh>
00021 # include <string>
00022 
00023 namespace vcsn
00024 {
00025   namespace io
00026   {
00027     template<typename Saver, typename Conv>
00028     void simple::operator()(std::ostream& o, const Saver& s, const Conv& conv) const
00029     {
00030       typedef typename Saver::automaton_t auto_t;
00031       const auto_t& a = s.automaton();
00032       unsigned count = 1;
00033 
00034       o << "# States list" << std::endl;
00035       for (typename auto_t::state_iterator i = a.states().begin();
00036            i != a.states().end();
00037            ++i, ++count)
00038       {
00039         o << 's' << *i << ' ';
00040         if (a.is_initial(*i))
00041           o << "i[" << conv(a, a.get_initial(*i)) << "] ";
00042         if (a.is_final(*i))
00043           o << "f[" << conv(a, a.get_final(*i)) << "]";
00044         o << std::endl;
00045       }
00046 
00047       o << std::endl;
00048       o << "# Transitions list" << std::endl;
00049       for (typename auto_t::transition_iterator i = a.transitions().begin();
00050            i != a.transitions().end();
00051            ++i)
00052       {
00053         o << 's' << a.src_of(*i) << ' '
00054           << 's' << a.dst_of(*i) << ' ';
00055         if (a.is_spontaneous(*i))
00056           o << 'S';
00057         else
00058           o << "l[" << conv(a, a.series_of(*i)) << "]" << std::endl;
00059       }
00060       o << '.' << std::endl;
00061     }
00062 
00063     void get_delimited_exp(std::istream& in, std::string& s)
00064     {
00065       std::string::size_type    i = 1;
00066 
00067       // Ignore the first '['
00068       in.ignore();
00069       // Readline
00070       std::getline(in, s, ']');
00071       for (i = 0; i < s.size() && s[s.size() - i - 1] == '\\' ; ++i)
00072         ;
00073 
00074       // While the final ']' is escaped, read again and concat
00075       while (i % 2 == 1)
00076       {
00077         std::string     tmp;
00078 
00079         s = s + "]";
00080         std::getline(in, tmp, ']');
00081         s = s + tmp;
00082         for (i = 0; i < tmp.size() && tmp[tmp.size() - i - 1] == '\\' ; ++i)
00083           ;
00084       }
00085     }
00086 
00087     template<typename Loader>
00088     void simple::operator()(std::istream& in, Loader& l) const
00089     {
00090       bool done = false;
00091       unsigned from, to;
00092       char cmd;
00093       std::string str;
00094       while (in && !done)
00095       {
00096         in >> cmd;
00097         switch(cmd)
00098         {
00099           case 's': // Definition of a state
00100             from = to;
00101             in >> to;
00102             l.add_state(to);
00103             break;
00104           case 'i': // The previous state is an initial one
00105             get_delimited_exp(in, str);
00106             l.set_initial(to, str);
00107             break;
00108           case 'f': // The previous state is a final one
00109             get_delimited_exp(in, str);
00110             l.set_final(to, str);
00111             break;
00112           case 'l': // The label of transition between the 2 previous states
00113             get_delimited_exp(in, str);
00114             l.add_transition(from, to, str);
00115             break;
00116           case 'S': // A spontaneous transions between the 2 previous states
00117             l.add_spontaneous(from, to);
00118             break;
00119           case '.':
00120             done = true;
00121             break;
00122           case '#': // The start of a comment
00123             std::getline(in, str);
00124             break;
00125           default: // Ignore other caracters
00126             break;
00127         }
00128       }
00129     }
00130   }
00131 }
00132 
00133 #endif // ! VCSN_TOOLS_SIMPLE_FORMAT_HXX

Generated on Sat Jul 29 17:13:11 2006 for Vaucanson by  doxygen 1.4.6