1 /* Copyright (C) 2012  Olga Yakovleva <yakovleva.o.v@gmail.com> */
2 
3 /* This program is free software: you can redistribute it and/or modify */
4 /* it under the terms of the GNU Lesser General Public License as published by */
5 /* the Free Software Foundation, either version 2.1 of the License, or */
6 /* (at your option) any later version. */
7 
8 /* This program is distributed in the hope that it will be useful, */
9 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
10 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the */
11 /* GNU Lesser General Public License for more details. */
12 
13 /* You should have received a copy of the GNU Lesser General Public License */
14 /* along with this program.  If not, see <http://www.gnu.org/licenses/>. */
15 
16 #ifndef RHVOICE_SAPI_TEXT_ITERATOR_HPP
17 #define RHVOICE_SAPI_TEXT_ITERATOR_HPP
18 
19 #include <iterator>
20 #include <sapi.h>
21 #include <sapiddk.h>
22 
23 #include "core/utf.hpp"
24 
25 namespace RHVoice
26 {
27   namespace sapi
28   {
29     class text_iterator: public std::iterator<std::forward_iterator_tag,const utf8::uint32_t>
30     {
31     public:
text_iterator()32       text_iterator():
33         text_frag(0),
34         range_end(0),
35         start(0),
36         end(0),
37         code_point('\0')
38       {
39       }
40 
text_iterator(const SPVTEXTFRAG * frag,std::size_t pos)41       text_iterator(const SPVTEXTFRAG* frag,std::size_t pos):
42         text_frag(frag),
43         range_end(frag->pTextStart+frag->ulTextLen),
44         start(frag->pTextStart+pos),
45         end(start),
46         code_point('\0')
47       {
48         ++(*this);
49       }
50 
operator *() const51       const utf8::uint32_t& operator*() const
52       {
53         return code_point;
54       }
55 
operator ==(const text_iterator & other) const56       bool operator==(const text_iterator& other) const
57       {
58         return ((text_frag==other.text_frag)&&(start==other.start));
59       }
60 
operator !=(const text_iterator & other) const61       bool operator!=(const text_iterator& other) const
62       {
63         return !(*this==other);
64       }
65 
operator ++()66       text_iterator& operator++()
67       {
68         if(end==range_end)
69           start=end;
70         else
71           {
72             const wchar_t* tmp=end;
73             code_point=utf::next(tmp,range_end);
74             start=end;
75             end=tmp;
76           }
77         return *this;
78       }
79 
operator ++(int)80       text_iterator operator++(int)
81       {
82         text_iterator tmp=*this;
83         ++(*this);
84         return tmp;
85       }
86 
offset() const87       std::size_t offset() const
88       {
89         return (text_frag->ulTextSrcOffset+(start-text_frag->pTextStart));
90       }
91 
92     private:
93       const SPVTEXTFRAG* text_frag;
94       const wchar_t *range_end,*start,*end;
95       utf8::uint32_t code_point;
96     };
97   }
98 }
99 #endif
100