Vaucanson  1.4.1
simple_format.hxx
1 // simple_format.hxx: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 The Vaucanson Group.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // The complete GNU General Public Licence Notice can be found as the
13 // `COPYING' file in the root directory.
14 //
15 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
16 //
17 #ifndef VCSN_TOOLS_SIMPLE_FORMAT_HXX
18 # define VCSN_TOOLS_SIMPLE_FORMAT_HXX
19 
20 # include <vaucanson/tools/simple_format.hh>
21 # include <string>
22 
23 namespace vcsn
24 {
25  namespace tools
26  {
27  template<typename Saver, typename Conv>
28  void simple::operator()(std::ostream& o, const Saver& s, const Conv& conv) const
29  {
30  typedef typename Saver::automaton_t auto_t;
31  const auto_t& a = s.automaton();
32  unsigned count = 1;
33 
34  o << "# States list" << std::endl;
35  for (typename auto_t::state_iterator i = a.states().begin();
36  i != a.states().end();
37  ++i, ++count)
38  {
39  o << 's' << *i << ' ';
40  if (a.is_initial(*i))
41  o << "i[" << conv(a, a.get_initial(*i)) << "] ";
42  if (a.is_final(*i))
43  o << "f[" << conv(a, a.get_final(*i)) << "]";
44  o << std::endl;
45  }
46 
47  o << std::endl;
48  o << "# Transitions list" << std::endl;
49  for (typename auto_t::transition_iterator i = a.transitions().begin();
50  i != a.transitions().end();
51  ++i)
52  {
53  o << 's' << a.src_of(*i) << ' '
54  << 's' << a.dst_of(*i) << ' ';
55  if (a.is_spontaneous(*i))
56  o << 'S';
57  else
58  o << "l[" << conv(a, a.series_of(*i)) << "]" << std::endl;
59  }
60  o << '.' << std::endl;
61  }
62 
63  void get_delimited_exp(std::istream& in, std::string& s)
64  {
65  std::string::size_type i = 1;
66 
67  // Ignore the first '['
68  in.ignore();
69  // Readline
70  std::getline(in, s, ']');
71  for (i = 0; i < s.size() && s[s.size() - i - 1] == '\\' ; ++i)
72  ;
73 
74  // While the final ']' is escaped, read again and concat
75  while (i % 2 == 1)
76  {
77  std::string tmp;
78 
79  s = s + "]";
80  std::getline(in, tmp, ']');
81  s = s + tmp;
82  for (i = 0; i < tmp.size() && tmp[tmp.size() - i - 1] == '\\' ; ++i)
83  ;
84  }
85  }
86 
87  template<typename Loader>
88  void simple::operator()(std::istream& in, Loader& l) const
89  {
90  bool done = false;
91  unsigned from, to;
92  char cmd;
93  std::string str;
94  while (in && !done)
95  {
96  in >> cmd;
97  switch(cmd)
98  {
99  case 's': // Definition of a state
100  from = to;
101  in >> to;
102  l.add_state(to);
103  break;
104  case 'i': // The previous state is an initial one
105  get_delimited_exp(in, str);
106  l.set_initial(to, str);
107  break;
108  case 'f': // The previous state is a final one
109  get_delimited_exp(in, str);
110  l.set_final(to, str);
111  break;
112  case 'l': // The label of transition between the 2 previous states
113  get_delimited_exp(in, str);
114  l.add_transition(from, to, str);
115  break;
116  case 'S': // A spontaneous transions between the 2 previous states
117  l.add_spontaneous(from, to);
118  break;
119  case '.':
120  done = true;
121  break;
122  case '#': // The start of a comment
123  std::getline(in, str);
124  break;
125  default: // Ignore other caracters
126  break;
127  }
128  }
129  }
130  }
131 }
132 
133 #endif // ! VCSN_TOOLS_SIMPLE_FORMAT_HXX