1 // ==========================================================================
2 //                 SeqAn - The Library for Sequence Analysis
3 // ==========================================================================
4 // Copyright (c) 2006-2010, Knut Reinert, FU Berlin
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are met:
9 //
10 //     * Redistributions of source code must retain the above copyright
11 //       notice, this list of conditions and the following disclaimer.
12 //     * Redistributions in binary form must reproduce the above copyright
13 //       notice, this list of conditions and the following disclaimer in the
14 //       documentation and/or other materials provided with the distribution.
15 //     * Neither the name of Knut Reinert or the FU Berlin nor the names of
16 //       its contributors may be used to endorse or promote products derived
17 //       from this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
23 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29 // DAMAGE.
30 //
31 // ==========================================================================
32 // Author: David Weese <david.weese@fu-berlin.de>
33 // ==========================================================================
34 // Code that allows strings to be treated like streams.
35 //
36 // TODO(holtgrew): This could be called ${module}_adapt_sequence.h, where $module is the name of a future streaming/I/O module.
37 // ==========================================================================
38 
39 #ifndef SEQAN_HEADER_SEQUENCE_STREAM_H
40 #define SEQAN_HEADER_SEQUENCE_STREAM_H
41 
42 namespace SEQAN_NAMESPACE_MAIN
43 {
44 
45 
46 //////////////////////////////////////////////////////////////////////////////
47 
48 template <typename TContainer, typename TSpec>
49 inline bool
_streamEOF(Iter<TContainer,TSpec> const & iter)50 _streamEOF(Iter<TContainer, TSpec> const & iter)
51 {
52 SEQAN_CHECKPOINT
53     return atEnd(iter);
54 }
55 
56 //////////////////////////////////////////////////////////////////////////////
57 
58 template <typename TValue, typename TContainer, typename TSpec>
59 inline ::std::streamsize
_streamRead(TValue * target,Iter<TContainer,TSpec> & source,::std::streamsize limit)60 _streamRead(TValue * target,
61             Iter<TContainer, TSpec> & source,
62             ::std::streamsize limit)
63 {
64 SEQAN_CHECKPOINT
65     if (position(target) + limit > length(container(target)))
66         limit = length(container(target)) - position(target);
67     Iter<TContainer, TSpec> sourceEnd = source + limit;
68     for (; source != sourceEnd; ++source, ++target)
69         *target = *source;
70     return limit;
71 }
72 
73 //////////////////////////////////////////////////////////////////////////////
74 
75 template <typename TContainer, typename TSpec>
76 inline typename Value<Iter<TContainer, TSpec> >::Type
_streamGet(Iter<TContainer,TSpec> & source)77 _streamGet(Iter<TContainer, TSpec> & source)
78 {
79 SEQAN_CHECKPOINT
80     typename Value<Iter<TContainer, TSpec> >::Type _val = getValue(source);
81     goNext(source);
82     return _val;
83 }
84 
85 //////////////////////////////////////////////////////////////////////////////
86 
87 template <typename TContainer, typename TSpec>
88 inline typename Value<Iter<TContainer, TSpec> >::Type
_streamPeek(Iter<TContainer,TSpec> & source)89 _streamPeek(Iter<TContainer, TSpec> & source)
90 {
91 SEQAN_CHECKPOINT
92     return getValue(source);
93 }
94 
95 //////////////////////////////////////////////////////////////////////////////
96 
97 template <typename TContainer, typename TSpec, typename TChar>
98 inline void
_streamPut(Iter<TContainer,TSpec> & target,TChar character)99 _streamPut(Iter<TContainer, TSpec> & target,
100            TChar character)
101 {
102 SEQAN_CHECKPOINT
103     if (atEnd(target))
104     {
105         typename Container<Iter<TContainer, TSpec> >::Type & container_ = container(target);
106         appendValue(container_, character);
107         target = begin(container_) + (length(container_) - 1);
108     } else
109         *target = character;
110 }
111 
112 //////////////////////////////////////////////////////////////////////////////
113 
114 template <typename TContainer, typename TSpec>
115 inline typename Position<Iter<TContainer, TSpec> >::Type
_streamTellG(Iter<TContainer,TSpec> & me)116 _streamTellG(Iter<TContainer, TSpec> & me)
117 {
118 SEQAN_CHECKPOINT
119     return position(me);
120 }
121 
122 //////////////////////////////////////////////////////////////////////////////
123 
124 template <typename TContainer, typename TSpec>
125 inline typename Position<Iter<TContainer, TSpec> >::Type
_streamTellP(Iter<TContainer,TSpec> & me)126 _streamTellP(Iter<TContainer, TSpec> & me)
127 {
128 SEQAN_CHECKPOINT
129     return position(me);
130 }
131 
132 //////////////////////////////////////////////////////////////////////////////
133 
134 template <typename TContainer, typename TSpec>
135 inline void
_streamSeekG(Iter<TContainer,TSpec> & me,typename Position<Iter<TContainer,TSpec>>::Type pos)136 _streamSeekG(Iter<TContainer, TSpec> & me,
137      typename Position<Iter<TContainer, TSpec> >::Type pos)
138 {
139 SEQAN_CHECKPOINT
140     me = begin(container(me)) + pos;
141 }
142 
143 //////////////////////////////////////////////////////////////////////////////
144 
145 template <typename TContainer, typename TSpec>
146 inline void
_streamSeekP(Iter<TContainer,TSpec> & me,typename Position<Iter<TContainer,TSpec>>::Type pos)147 _streamSeekP(Iter<TContainer, TSpec> & me,
148      typename Position<Iter<TContainer, TSpec> >::Type pos)
149 {
150 SEQAN_CHECKPOINT
151     me = begin(container(me)) + pos;
152 }
153 
154 //////////////////////////////////////////////////////////////////////////////
155 
156 template <typename TContainer, typename TSpec>
157 inline void
_streamSeek2G(Iter<TContainer,TSpec> & me,int off)158 _streamSeek2G(Iter<TContainer, TSpec> & me,
159      int off)
160 {
161 SEQAN_CHECKPOINT
162     me = begin(container(me)) + (position(me) + off);
163 }
164 
165 
166 //////////////////////////////////////////////////////////////////////////////
167 
168 } //namespace SEQAN_NAMESPACE_MAIN
169 
170 #endif //#ifndef SEQAN_HEADER_...
171