00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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 tools
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
00068 in.ignore();
00069
00070 std::getline(in, s, ']');
00071 for (i = 0; i < s.size() && s[s.size() - i - 1] == '\\' ; ++i)
00072 ;
00073
00074
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':
00100 from = to;
00101 in >> to;
00102 l.add_state(to);
00103 break;
00104 case 'i':
00105 get_delimited_exp(in, str);
00106 l.set_initial(to, str);
00107 break;
00108 case 'f':
00109 get_delimited_exp(in, str);
00110 l.set_final(to, str);
00111 break;
00112 case 'l':
00113 get_delimited_exp(in, str);
00114 l.add_transition(from, to, str);
00115 break;
00116 case 'S':
00117 l.add_spontaneous(from, to);
00118 break;
00119 case '.':
00120 done = true;
00121 break;
00122 case '#':
00123 std::getline(in, str);
00124 break;
00125 default:
00126 break;
00127 }
00128 }
00129 }
00130 }
00131 }
00132
00133 #endif // ! VCSN_TOOLS_SIMPLE_FORMAT_HXX