1 // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
2 // (C) Copyright 2005-2007 Jonathan Turkanis
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
6 // See http://www.boost.org/libs/iostreams for documentation.
8 // Adapted from an example of James Kanze, with suggestions from Peter Dimov.
9 // See http://www.gabi-soft.fr/codebase-en.html.
11 #ifndef BOOST_IOSTREAMS_FINITE_STATE_FILTER_HPP_INCLUDED
12 #define BOOST_IOSTREAMS_FINITE_STATE_FILTER_HPP_INCLUDED
15 #include <cstdio> // EOF.
16 #include <iostream> // cin, cout.
19 #include <boost/config.hpp> // JOIN, member template friends.
20 #include <boost/detail/workaround.hpp>
21 #include <boost/iostreams/categories.hpp>
22 #include <boost/iostreams/char_traits.hpp>
23 #include <boost/iostreams/checked_operations.hpp> // put_if.
24 #include <boost/iostreams/concepts.hpp>
25 #include <boost/iostreams/detail/ios.hpp> // openmode.
26 #include <boost/iostreams/filter/stdio.hpp>
27 #include <boost/iostreams/operations.hpp>
28 #include <boost/mpl/begin_end.hpp>
29 #include <boost/mpl/deref.hpp>
30 #include <boost/preprocessor/control/expr_if.hpp>
31 #include <boost/static_assert.hpp>
32 #include <boost/type_traits/is_base_and_derived.hpp>
34 namespace boost { namespace iostreams {
36 //------------------Definition of basic character classes---------------------//
38 struct finite_state_machine_base {
40 static const int initial_state = 0;
42 // All-inclusive character class.
46 static bool test(Ch, const std::locale&) { return true; }
49 // Locale-sensitive character classes.
51 #define BOOST_IOSTREAMS_CHARACTER_CLASS(class) \
52 struct BOOST_JOIN(is_, class) { \
53 template<typename Ch> \
54 static bool test(Ch event, const std::locale& loc) \
55 { return std::BOOST_JOIN(is, class)(event, loc); } \
59 BOOST_IOSTREAMS_CHARACTER_CLASS(alnum)
60 BOOST_IOSTREAMS_CHARACTER_CLASS(alpha)
61 BOOST_IOSTREAMS_CHARACTER_CLASS(cntrl)
62 BOOST_IOSTREAMS_CHARACTER_CLASS(digit)
63 BOOST_IOSTREAMS_CHARACTER_CLASS(graph)
64 BOOST_IOSTREAMS_CHARACTER_CLASS(lower)
65 BOOST_IOSTREAMS_CHARACTER_CLASS(print)
66 BOOST_IOSTREAMS_CHARACTER_CLASS(punct)
67 BOOST_IOSTREAMS_CHARACTER_CLASS(space)
68 BOOST_IOSTREAMS_CHARACTER_CLASS(upper)
69 BOOST_IOSTREAMS_CHARACTER_CLASS(xdigit)
71 #undef BOOST_IOSTREAMS_CHARACTER_CLASS
75 struct finite_state_machine_base_ex : finite_state_machine_base {
78 static bool test(Ch event, const std::locale&)
85 //------------------Definition of base class for finite state filters---------//
89 template<typename FiniteStateMachine>
90 class finite_state_filter_impl;
92 } // End namespace detail.
94 template<typename Derived, typename Ch = char>
95 class finite_state_machine : public finite_state_machine_base_ex<Ch> {
98 typedef typename char_traits<Ch>::int_type int_type;
99 void imbue(const std::locale& loc) { loc_ = loc; }
100 const std::locale& getloc() const { return loc_; }
102 finite_state_machine() : off_(0) { }
104 // Template whose instantiations make up transition table.
107 typename CharacterClass,
109 void (Derived::*Action)(char_type) >
111 typedef CharacterClass character_class;
112 static const int state = State;
113 static const int next_state = NextState;
114 static void execute(Derived& d, char_type event)
124 return off_ == buf_.size();
126 void push(char c) { buf_ += c; }
129 char_type result = buf_[off_++];
130 if (off_ == buf_.size())
134 char_type& top() { return buf_[off_]; }
141 // Default event handlers.
144 void skip(char_type) { }
146 #if BOOST_WORKAROUND(__MWERKS__, <= 0x3206)
147 template<typename Ch>
148 void _push_impl(Ch c) { push(c); }
151 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
152 template<typename FiniteStateFilter>
153 friend class detail::finite_state_filter_impl;
157 void on_any(char_type) { }
159 typedef std::basic_string<char_type> string_type;
160 typedef typename string_type::size_type size_type;
166 #if !BOOST_WORKAROUND(__MWERKS__, <= 0x3206)
167 # define BOOST_IOSTREAMS_FSM(fsm) \
168 template<typename Ch> \
170 { ::boost::iostreams::finite_state_machine<fsm, Ch>::push(c); } \
171 template<typename Ch> \
172 void skip(Ch c) { (void) c; } \
174 #else // #ifndef __MWERKS__
175 # define BOOST_IOSTREAMS_FSM(fsm) \
176 void push(char c) { this->_push_impl(c); } \
177 void push(wchar_t c) { this->_push_impl(c); } \
178 void skip(char c) { (void) c; } \
179 void skip(wchar_t c) { (void) c; } \
183 //------------------Definition of finite_state_filter_impl--------------------//
187 template<typename FiniteStateMachine>
188 class finite_state_filter_impl : protected FiniteStateMachine
191 template<typename First, typename Last>
192 struct process_event_impl;
194 typedef typename char_type_of<FiniteStateMachine>::type char_type;
196 finite_state_filter_impl() : state_(FiniteStateMachine::initial_state) { }
198 template<typename T0>
199 explicit finite_state_filter_impl(const T0& t0)
200 : FiniteStateMachine(t0), state_(FiniteStateMachine::initial_state)
203 template<typename T0, typename T1>
204 finite_state_filter_impl(const T0& t0, const T1& t1)
205 : FiniteStateMachine(t0, t1), state_(FiniteStateMachine::initial_state)
208 template<typename T0, typename T1, typename T2>
209 finite_state_filter_impl(const T0& t0, const T1& t1, const T2& t2)
210 : FiniteStateMachine(t0, t1, t2),
211 state_(FiniteStateMachine::initial_state)
214 void process_event(char_type c)
216 typedef typename FiniteStateMachine::transition_table transitions;
217 typedef typename mpl::begin<transitions>::type first;
218 typedef typename mpl::end<transitions>::type last;
219 state_ = process_event_impl<first, last>::execute(*this, state_, c);
221 int& state() { return state_; }
224 state_ = FiniteStateMachine::initial_state;
228 template<typename First, typename Last>
229 struct process_event_impl {
230 static int execute(FiniteStateMachine& fsm, int state, char_type event)
232 typedef typename mpl::deref<First>::type rule;
233 typedef typename mpl::next<First>::type next;
234 typedef typename rule::character_class character_class;
236 if ( state == rule::state &&
237 character_class::test(event, fsm.getloc()) )
240 rule::execute(fsm, event);
241 return rule::next_state;
244 // Rule is inapplicable: try next rule.
245 return process_event_impl<next, Last>::execute(fsm, state, event);
249 template<typename Last>
250 struct process_event_impl<Last, Last> {
251 static int execute(FiniteStateMachine& fsm, int state, char_type c)
257 #if BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(60590042)) /* Tru64 */ \
258 || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3205)) /* CW9.4 */
261 template<typename FSM>
262 static void on_any(FSM& fsm, char_type c) { fsm.on_any(c); }
267 } // End namespace detail.
269 //------------------Definition of finite_state_filter-------------------------//
271 template<typename FiniteStateMachine>
272 class finite_state_filter
273 : public detail::finite_state_filter_impl<FiniteStateMachine>
276 typedef detail::finite_state_filter_impl<FiniteStateMachine> base_type;
278 typedef typename base_type::char_type char_type;
279 typedef char_traits<char_type> traits_type;
280 typedef typename base_type::int_type int_type;
282 : dual_use, filter_tag, closable_tag, localizable_tag
285 finite_state_filter() : flags_(0) { }
287 template<typename T0>
288 finite_state_filter(const T0& t0)
289 : base_type(t0), flags_(0)
292 template<typename T0, typename T1>
293 finite_state_filter(const T0& t0, const T1& t1)
294 : base_type(t0, t1), flags_(0)
297 template<typename T0, typename T1, typename T2>
298 finite_state_filter(const T0& t0, const T1& t1, const T2& t2)
299 : base_type(t0, t1, t2), flags_(0)
302 template<typename Source>
303 int_type get(Source& src)
305 assert((flags_ & f_write) == 0);
309 if ((flags_ & f_eof) == 0) {
311 // Read a character and process it.
313 if (traits_type::is_eof(c = iostreams::get(src))) {
316 } else if (!traits_type::would_block(c)) {
317 this->process_event(c);
321 // Return a character, if available.
324 else if ((flags_ & f_eof) != 0)
325 return traits_type::eof();
329 template<typename Sink>
330 bool put(Sink& dest, char_type c)
332 assert((flags_ & f_read) == 0);
335 this->process_event(c);
336 while (!this->empty() && iostreams::put(dest, this->top()))
342 template<typename Device>
343 void close(Device& dev, BOOST_IOS::openmode which)
345 if (which == BOOST_IOS::out) {
346 if (flags_ & f_write)
347 while (!this->empty())
348 iostreams::put_if(dev, this->pop());
356 f_write = f_read << 1,
363 } } // End namespaces iostreams, boost.
365 #endif // #ifndef BOOST_IOSTREAMS_FINITE_STATE_FILTER_HPP_INCLUDED