source: cfg_oil/trunk/toppers/text.hpp@ 23

Last change on this file since 23 was 8, checked in by msugi, 14 years ago

ディレクトリ構造の作成,ソース一式とReleaseビルドのコミット.

File size: 25.2 KB
Line 
1/*
2 * TOPPERS Software
3 * Toyohashi Open Platform for Embedded Real-Time Systems
4 *
5 * Copyright (C) 2005-2008 by TAKAGI Nobuhisa
6 *
7 * ã‹L’˜ìŒ ŽÒ‚́CˆÈ‰º‚Ì(1)`(4)‚ÌðŒ‚ð–ž‚½‚·ê‡‚ÉŒÀ‚èC–{ƒ\ƒtƒgƒEƒF
8 * ƒAi–{ƒ\ƒtƒgƒEƒFƒA‚ð‰ü•Ï‚µ‚½‚à‚Ì‚ðŠÜ‚ށDˆÈ‰º“¯‚¶j‚ðŽg—pE•¡»E‰ü
9 * •ÏEÄ”z•ziˆÈ‰ºC—˜—p‚ƌĂԁj‚·‚邱‚Ƃ𖳏ž‚Å‹–‘ø‚·‚éD
10 * (1) –{ƒ\ƒtƒgƒEƒFƒA‚ðƒ\[ƒXƒR[ƒh‚ÌŒ`‚Å—˜—p‚·‚éê‡‚ɂ́Cã‹L‚Ì’˜ì
11 * Œ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’肪C‚»‚Ì‚Ü‚Ü‚ÌŒ`‚Ń\[
12 * ƒXƒR[ƒh’†‚ÉŠÜ‚Ü‚ê‚Ä‚¢‚邱‚ƁD
13 * (2) –{ƒ\ƒtƒgƒEƒFƒA‚ðCƒ‰ƒCƒuƒ‰ƒŠŒ`Ž®‚ȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
14 * —p‚Å‚«‚éŒ`‚ōĔz•z‚·‚éê‡‚ɂ́CÄ”z•z‚É”º‚¤ƒhƒLƒ…
15ƒƒ“ƒgi—˜—p
16 * ŽÒƒ}ƒjƒ…
17ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L
18 * ‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
19 * (3) –{ƒ\ƒtƒgƒEƒFƒA‚ðC‹@Ší‚É‘g‚ݍž‚ނȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
20 * —p‚Å‚«‚È‚¢Œ`‚ōĔz•z‚·‚éê‡‚ɂ́CŽŸ‚Ì‚¢‚¸‚ê‚©‚ÌðŒ‚ð–ž‚½‚·‚±
21 * ‚ƁD
22 * (a) Ä”z•z‚É”º‚¤ƒhƒLƒ…
23ƒƒ“ƒgi—˜—pŽÒƒ}ƒjƒ…
24ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜
25 * ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
26 * (b) Ä”z•z‚ÌŒ`‘Ô‚ðC•Ê‚É’è‚ß‚é•û–@‚É‚æ‚Á‚āCTOPPERSƒvƒƒWƒFƒNƒg‚É
27 * •ñ‚·‚邱‚ƁD
28 * (4) –{ƒ\ƒtƒgƒEƒFƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚é‚¢‚©‚Ȃ鑹
29 * ŠQ‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð–Ɛӂ·‚邱‚ƁD
30 * ‚Ü‚½C–{ƒ\ƒtƒgƒEƒFƒA‚̃†[ƒU‚Ü‚½‚̓Gƒ“ƒhƒ†[ƒU‚©‚ç‚Ì‚¢‚©‚Ȃ闝
31 * —R‚ÉŠî‚­¿‹‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð
32 * –Ɛӂ·‚邱‚ƁD
33 *
34 * –{ƒ\ƒtƒgƒEƒFƒA‚́C–³•ÛØ‚Å’ñ‹Ÿ‚³‚ê‚Ä‚¢‚é‚à‚Ì‚Å‚ ‚éDã‹L’˜ìŒ ŽÒ‚¨
35 * ‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚́C–{ƒ\ƒtƒgƒEƒFƒA‚ÉŠÖ‚µ‚āC“Á’è‚ÌŽg—p–Ú“I
36 * ‚ɑ΂·‚é“K‡«‚àŠÜ‚߂āC‚¢‚©‚È‚é•ÛØ‚às‚í‚È‚¢D‚Ü‚½C–{ƒ\ƒtƒgƒEƒF
37 * ƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚½‚¢‚©‚Ȃ鑹ŠQ‚ÉŠÖ‚µ‚Ä‚àC‚»
38 * ‚̐ӔC‚𕉂í‚È‚¢D
39 *
40 */
41
42/*!
43 * \file toppers/text.hpp
44 * \brief ƒeƒLƒXƒgƒf[ƒ^ŠÇ—‚ÉŠÖ‚·‚ééŒ¾’è‹`
45 *
46 * ‚±‚̃tƒ@ƒCƒ‹‚Å’è‹`‚³‚ê‚éƒNƒ‰ƒX
47 * \code
48 * class basic_text< Container, CharT, Traits, Allocator >;
49 * \endcode
50 */
51#ifndef TOPPERS_TEXT_HPP_
52#define TOPPERS_TEXT_HPP_
53
54#include <algorithm>
55#include <iterator>
56#include <vector>
57#include <numeric>
58#include "toppers/text_line.hpp"
59#include "toppers/misc.hpp"
60#include <boost/utility.hpp>
61#include <boost/iterator/iterator_facade.hpp>
62#include <boost/format.hpp>
63
64namespace toppers
65{
66
67 /*!
68 * \class basic_text text.hpp "toppers/text.hpp"
69 * \brief ƒeƒLƒXƒgƒf[ƒ^ŠÇ—ƒeƒ“ƒvƒŒ[ƒgƒNƒ‰ƒX
70 *
71 * ƒtƒ@ƒCƒ‹‚©‚ç“ǂݍž‚ñ‚¾ƒeƒLƒXƒgƒf[ƒ^‚ÌŠÇ—‚ðs‚¤‚½‚߂̃Nƒ‰ƒX‚Å‚·B
72 * ‚±‚̃Nƒ‰ƒX‚ł́AƒeƒLƒXƒgƒf[ƒ^‚ð’P‚Ȃ镶Žš—ñ‚Æ‚µ‚Ä‚Å‚Í‚È‚­A“ü—ÍŒ³‚Ì
73 * ƒtƒ@ƒCƒ‹–¼‚ƍs”ԍ†‚ð•t‰Á‚µ‚½Œ`‚ÅŠÇ—‚µ‚Ü‚·B
74 *
75 * \note ‚±‚̃Nƒ‰ƒX‚̓eƒLƒXƒg•ÒW‚Ì—p“r‚ð‘z’肵‚½‚à‚Ì‚Å‚Í‚ ‚è‚Ü‚¹‚ñB
76 */
77 template
78 <
79 template < typename, class > class Container,
80 typename CharT,
81 class Traits = std::char_traits< CharT >,
82 class Allocator = std::allocator< CharT >
83 >
84 class basic_text
85 {
86 public:
87 typedef basic_text< Container, CharT, Traits, Allocator > self_t;
88 typedef std::basic_string< CharT, Traits, Allocator > string_type;
89 typedef ::toppers::text_line line_type;
90 typedef basic_line_buf< CharT, Traits, Allocator > line_buf;
91
92 // ˆÈ‰º‚͈ê”Ê“I‚ȃRƒ“ƒeƒi‚Ƃ̌݊·‚Ì‚½‚ß‚ÌŒ^’è‹`
93 ////////////////////////////////////////////////////////////////////////////////
94 typedef CharT value_type;
95 typedef Container< line_buf, std::allocator< line_buf > > container;
96 typedef value_type& reference;
97 typedef value_type const& const_reference;
98 typedef value_type* pointer;
99 typedef value_type const* const_pointer;
100 typedef Allocator allocator_type;
101 typedef typename Allocator::difference_type difference_type;
102 typedef typename Allocator::size_type size_type;
103
104 /*!
105 * \class const_iterator text.hpp "toppers/text.hpp"
106 * \brief toppers::basic_text ‚̃CƒeƒŒ[ƒ^
107 */
108 class const_iterator : public boost::iterator_facade< const_iterator, CharT, boost::random_access_traversal_tag, CharT const& >
109 {
110 friend class boost::iterator_core_access;
111 public:
112 typedef boost::iterator_facade< const_iterator, CharT, boost::random_access_traversal_tag, CharT const& > base_t;
113 typedef typename container::const_iterator row_iterator;
114 typedef typename base_t::value_type value_type;
115 typedef typename base_t::reference reference;
116 typedef typename base_t::pointer pointer;
117 typedef typename base_t::difference_type difference_type;
118
119 const_iterator() : row_(), col_( 0 ) {}
120 const_iterator( row_iterator row, size_type col = 0 ) : row_( row ), col_( col ) {}
121 line_type const& line() const { return row_->line; }
122 row_iterator const& get_row() const { return row_; }
123 size_type get_col() const { return col_; }
124 protected:
125 reference dereference() const { return row_->buf[col_]; }
126 bool equal( const_iterator const& rhs ) const { return row_ == rhs.row_ && col_ == rhs.col_; }
127 void increment() { if ( ++col_ == row_->buf.size() ) { col_ = 0; ++row_; } }
128 void decrement() { if ( col_ == 0 ) col_ = ( --row_ )->buf.size(); --col_; }
129 void advance( difference_type n )
130 {
131 typename container::const_iterator row = row_;
132 difference_type col = static_cast< difference_type >( col_ );
133 if ( n >= 0 )
134 {
135 while ( static_cast< size_type>( col + n ) > row->buf.size() )
136 {
137 n -= row->buf.size() - col;
138 ++row;
139 col = 0;
140 }
141 col += n;
142 if ( col == row->buf.size() )
143 {
144 col = 0;
145 ++row;
146 }
147 }
148 else // n < 0
149 {
150 while ( col + n < -1 )
151 {
152 n += col;
153 --row;
154 col = row->buf.size() - 1;
155 }
156 col += n;
157 if ( col == -1 )
158 {
159 --row;
160 col = row->buf.size() - 1;
161 }
162 }
163 row_ = row;
164 col_ = col;
165 }
166 difference_type distance_to( const_iterator const& rhs ) const
167 {
168 difference_type n;
169 typename container::const_iterator row = row_;
170 if ( rhs.row_ < row_ )
171 {
172 n = -static_cast< difference_type >( col_ + ( rhs.row_->buf.size() - rhs.col_ ) );
173 while ( --row != rhs.row_ )
174 {
175 n -= row->buf.size();
176 }
177 }
178 else if ( row_ < rhs.row_ )
179 {
180 n = col_ + rhs.col_;
181 while ( ++row != rhs.row_ )
182 {
183 n += row->buf.size();
184 }
185 }
186 else
187 {
188 n = static_cast< difference_type >( rhs.col_ ) - static_cast< difference_type >( col_ );
189 }
190 return n;
191 }
192 private:
193 typename container::const_iterator row_;
194 size_type col_;
195 };
196
197 /*!
198 * \class iterator text.hpp "toppers/text.hpp"
199 * \brief toppers::basic_text ‚̃CƒeƒŒ[ƒ^
200 */
201 class iterator : public const_iterator
202 {
203 public:
204 typedef typename container::iterator row_iterator;
205 typedef typename const_iterator::value_type value_type;
206 typedef typename const_iterator::reference reference;
207 typedef typename const_iterator::pointer pointer;
208 typedef typename const_iterator::difference_type difference_type;
209
210 iterator() {}
211 explicit iterator( row_iterator row, size_type col = 0 ) : const_iterator( row, col ) {}
212 value_type& operator*() const { return const_cast< value_type& >( const_iterator::dereference() ); }
213 iterator& operator++() { const_iterator::increment(); return *this; }
214 iterator operator++( int ) { iterator t( *this ); const_iterator::increment(); return t; }
215 iterator& operator--() { const_iterator::decrement(); return *this; }
216 iterator operator--( int ) { iterator t( *this ); const_iterator::decrement(); return t; }
217 };
218
219 typedef std::reverse_iterator< const_iterator > const_reverse_iterator;
220 typedef std::reverse_iterator< iterator > reverse_iterator;
221
222 public:
223 /*!
224 * \brief ƒfƒtƒHƒ‹ƒgƒRƒ“ƒXƒgƒ‰ƒNƒ^
225 */
226 basic_text() {}
227
228 /*!
229 * \brief ƒRƒ“ƒXƒgƒ‰ƒNƒ^
230 * \param first ‰Šú‰»‚É—p‚¢‚éƒf[ƒ^—ñ‚̐擪
231 * \param last ‰Šú‰»‚É—p‚¢‚éƒf[ƒ^—ñ‚̏I’[+1
232 */
233 basic_text( const_iterator const& first, const_iterator const& last )
234 {
235 if ( first != last )
236 {
237 init( first, last );
238 }
239 }
240
241 /*!
242 * \brief ƒRƒ“ƒXƒgƒ‰ƒNƒ^
243 * \param first ‰Šú‰»‚É—p‚¢‚éƒf[ƒ^—ñ‚̐擪
244 * \param last ‰Šú‰»‚É—p‚¢‚éƒf[ƒ^—ñ‚̏I’[+1
245 * \note ƒtƒ@ƒCƒ‹–¼‚Í"unknown"‚É‚È‚è‚Ü‚·B
246 */
247 template < class InputIterator >
248 basic_text( InputIterator first, InputIterator last )
249 {
250 if ( first != last )
251 {
252 init( first, last, append_directive );
253 }
254 }
255
256 /*!
257 * \brief ƒRƒ“ƒXƒgƒ‰ƒNƒ^
258 * \param first ‰Šú‰»‚É—p‚¢‚éƒf[ƒ^—ñ‚̐擪
259 * \param last ‰Šú‰»‚É—p‚¢‚éƒf[ƒ^—ñ‚̏I’[+1
260 * \param directive ‚Ps‚ðƒRƒ“ƒeƒi‚ÉŠi”[‚·‚邽‚߂̏ˆ—
261 * \note ƒtƒ@ƒCƒ‹–¼‚Í"unknown"‚É‚È‚è‚Ü‚·B
262 *
263 * directive ‚Í void directive( container& cont, line_buf& buf )‚ÌŒ`Ž®‚ð
264 * ‚Æ‚éŠÖ”‚ւ̃|ƒCƒ“ƒ^‚Ü‚½‚̓tƒ@ƒ“ƒNƒ^‚Å‚ ‚邱‚Æ‚ð‘z’肵‚Ä‚¢‚Ü‚·B
265 * cont ‚Í“à•”‚Ńf[ƒ^‚ÌŠi”[‚ÉŽg—p‚·‚éƒRƒ“ƒeƒiA buf ‚Í‚Ps•ª‚Ì•¶Žš
266 * —ñ‚ƃtƒ@ƒCƒ‹–¼Es”ԍ†‚ðŠi”[‚µ‚½ƒoƒbƒtƒ@‚Å‚·B
267 */
268 template < class InputIterator, class Directive >
269 basic_text( InputIterator first, InputIterator last, Directive directive )
270 {
271 if ( first != last )
272 {
273 init( first, last, directive );
274 }
275 }
276
277 /*!
278 * \brief ƒRƒ“ƒXƒgƒ‰ƒNƒ^
279 * \param istr ‰Šú‰»‚É—p‚¢‚é“ü—̓XƒgƒŠ[ƒ€
280 * \note ƒtƒ@ƒCƒ‹–¼‚Í"unknown"‚É‚È‚è‚Ü‚·B
281 */
282 basic_text( std::basic_istream< CharT, Traits >& istr )
283 {
284 init( istr, append_directive );
285 }
286
287 /*!
288 * \brief ƒRƒ“ƒXƒgƒ‰ƒNƒ^
289 * \param istr ‰Šú‰»‚É—p‚¢‚é“ü—̓XƒgƒŠ[ƒ€
290 * \param directive ‚Ps‚ðƒRƒ“ƒeƒi‚ÉŠi”[‚·‚邽‚߂̏ˆ—
291 * \note ƒtƒ@ƒCƒ‹–¼‚Í"unknown"‚É‚È‚è‚Ü‚·B
292 *
293 * directive ‚Í void directive( container& cont, line_buf& buf )‚ÌŒ`Ž®‚ð
294 * ‚Æ‚éŠÖ”‚ւ̃|ƒCƒ“ƒ^‚Ü‚½‚̓tƒ@ƒ“ƒNƒ^‚Å‚ ‚邱‚Æ‚ð‘z’肵‚Ä‚¢‚Ü‚·B
295 * cont ‚Í“à•”‚Ńf[ƒ^‚ÌŠi”[‚ÉŽg—p‚·‚éƒRƒ“ƒeƒiA buf ‚Í‚Ps•ª‚Ì•¶Žš
296 * —ñ‚ƃtƒ@ƒCƒ‹–¼Es”ԍ†‚ðŠi”[‚µ‚½ƒoƒbƒtƒ@‚Å‚·B
297 */
298 template < class Directive >
299 basic_text( std::basic_istream< CharT, Traits >& istr, Directive directive )
300 {
301 init( istr, directive );
302 }
303
304 /*!
305 * \brief æ“ªˆÊ’u‚̎擾
306 * \return æ“ª•¶Žš‚ւ̃CƒeƒŒ[ƒ^‚ð•Ô‚·
307 */
308 const_iterator begin() const
309 {
310 return const_iterator( container_.begin() );
311 }
312
313 /*!
314 * \brief æ“ªˆÊ’u‚̎擾
315 * \return æ“ª•¶Žš‚ւ̃CƒeƒŒ[ƒ^‚ð•Ô‚·
316 */
317 iterator begin()
318 {
319 return iterator( container_.begin() );
320 }
321
322 /*!
323 * \brief s”‚̎擾
324 * \param pos æ“ª•¶Žš‚©‚ç‚̃IƒtƒZƒbƒg
325 * \return pos‚ÅŽw’肳‚ꂽƒIƒtƒZƒbƒg‚©‚çs”‚ð‹‚ßCƒCƒeƒŒ[ƒ^‚ð•Ô‚·
326 */
327 const_iterator line_at(size_type pos) const
328 {
329 typedef text::container::const_iterator const_row_iterator;
330 const_iterator first( container_.begin() ), last( container_.end() );
331 int count = 0 , buf_size , i;
332 size_type sum = 0;
333
334 while((sum < pos) && (first != last))
335 {
336 const_row_iterator current( first.get_row() );
337 buf_size = current->buf.size();
338 sum += buf_size;
339 i = 0;
340 while(i < buf_size)
341 {
342 i++;
343 first++;
344 }
345 count += 1;
346 }
347
348 if(first < last)
349 {
350 first -= buf_size;
351 return const_iterator(first);
352 }
353 else
354 {
355 return const_iterator( --last );
356 }
357 }
358 /*!
359 * \brief I’[ˆÊ’u+1‚̎擾
360 * \return I’[•¶Žš‚ÌŽŸ—v‘f‚ւ̃CƒeƒŒ[ƒ^‚ð•Ô‚·
361 */
362 const_iterator end() const
363 {
364 return const_iterator( container_.end() );
365 }
366
367 /*!
368 * \brief I’[ˆÊ’u+1‚̎擾
369 * \return I’[•¶Žš‚ÌŽŸ—v‘f‚ւ̃CƒeƒŒ[ƒ^‚ð•Ô‚·
370 */
371 iterator end()
372 {
373 return iterator( container_.end() );
374 }
375
376 /*!
377 * \brief ‹t‡æ“ªˆÊ’u‚̎擾
378 * \return ‹t‡æ“ª•¶Žš‚Ö‚Ì‹tƒCƒeƒŒ[ƒ^‚ð•Ô‚·
379 */
380 const_reverse_iterator rbegin() const
381 {
382 string_type const* pbuf = &container_.back().buf;
383 return const_reverse_iterator( container_.rbegin() + pbuf->empty() ? 0 : pbuf->size()-1 );
384 }
385
386 /*!
387 * \brief ‹t‡æ“ªˆÊ’u‚̎擾
388 * \return ‹t‡æ“ª•¶Žš‚Ö‚Ì‹tƒCƒeƒŒ[ƒ^‚ð•Ô‚·
389 */
390 reverse_iterator rbegin()
391 {
392 string_type* pbuf = &container_.back().buf;
393 return reverse_iterator( container_.rbegin() + pbuf->empty() ? 0 : pbuf->size()-1 );
394 }
395
396 /*!
397 * \brief ‹t‡I’[ˆÊ’u+1‚̎擾
398 * \return ‹t‡I’[•¶Žš‚ÌŽŸ—v‘f‚Ö‚Ì‹tƒCƒeƒŒ[ƒ^‚ð•Ô‚·
399 */
400 const_reverse_iterator rend() const
401 {
402 return const_reverse_iterator( container_.rend() );
403 }
404
405 /*!
406 * \brief ‹t‡I’[ˆÊ’u+1‚̎擾
407 * \return ‹t‡I’[•¶Žš‚ÌŽŸ—v‘f‚Ö‚Ì‹tƒCƒeƒŒ[ƒ^‚ð•Ô‚·
408 */
409 reverse_iterator rend()
410 {
411 return reverse_iterator( container_.rend() );
412 }
413
414 /*!
415 * \brief æ“ª•¶Žš‚ÌŽQÆ
416 * \return æ“ª•¶Žš‚Ö‚ÌŽQÆ‚ð•Ô‚·
417 */
418 const_reference front() const
419 {
420 return container_.front().buf[0];
421 }
422
423 /*!
424 * \brief æ“ª•¶Žš‚ÌŽQÆ
425 * \return æ“ª•¶Žš‚Ö‚ÌŽQÆ‚ð•Ô‚·
426 */
427 reference front()
428 {
429 return container_.front().buf[0];
430 }
431
432 /*!
433 * \brief I’[•¶Žš‚ÌŽQÆ
434 * \return I’[•¶Žš‚Ö‚ÌŽQÆ‚ð•Ô‚·
435 */
436 const_reference back() const
437 {
438 string_type const* pbuf = &container_.back().buf;
439 return pbuf->at( pbuf->size()-1 );
440 }
441
442 /*!
443 * \brief I’[•¶Žš‚ÌŽQÆ
444 * \return I’[•¶Žš‚Ö‚ÌŽQÆ‚ð•Ô‚·
445 */
446 reference back()
447 {
448 string_type* pbuf = &container_.back().buf;
449 return pbuf->at( pbuf->size()-1 );
450 }
451
452 /*!
453 * \brief Žw’èˆÊ’u‚Ì•¶Žš‚ÌŽQÆ
454 * \return Žw’èˆÊ’u‚Ì•¶Žš‚Ö‚ÌŽQÆ‚ð•Ô‚·
455 */
456 const_reference at( size_type pos ) const
457 {
458 return container_.front().buf[pos];
459 }
460
461 /*!
462 * \brief Žw’èˆÊ’u‚Ì•¶Žš‚ÌŽQÆ
463 * \return Žw’èˆÊ’u‚Ì•¶Žš‚Ö‚ÌŽQÆ‚ð•Ô‚·
464 */
465 reference at( size_type pos )
466 {
467 return container_.front().buf[pos];
468 }
469
470 /*!
471 * \brief Žw’è”͈͂̃eƒLƒXƒgƒf[ƒ^‚ð’ljÁ
472 * \param first ’ljÁ‚É—p‚¢‚éƒf[ƒ^—ñ‚̐擪
473 * \param last ’ljÁ‚É—p‚¢‚éƒf[ƒ^—ñ‚̏I’[+1
474 */
475 void append( const_iterator const& first, const_iterator const& last )
476 {
477 if ( first != last )
478 {
479 self_t t( *this );
480 t.init( first, last );
481 swap( t );
482 }
483 }
484
485 /*!
486 * \brief Žw’è”͈͂̃eƒLƒXƒgƒf[ƒ^‚ð’ljÁ
487 * \param first ’ljÁ‚É—p‚¢‚éƒf[ƒ^—ñ‚̐擪
488 * \param last ’ljÁ‚É—p‚¢‚éƒf[ƒ^—ñ‚̏I’[+1
489 * \note ƒtƒ@ƒCƒ‹–¼‚ƍs”ԍ†‚͒ljÁæ‚̏I’[‚Ì‚à‚Ì‚ðŒp‘±‚µ‚Ü‚·B
490 * ’ljÁæ‚Ƀf[ƒ^‚ª‚È‚¢ê‡‚Í"unknown"‚É‚È‚è‚Ü‚·B
491 */
492 template < class InputIterator >
493 void append( InputIterator first, InputIterator last )
494 {
495 append( first, last, append_directive );
496 }
497
498 /*!
499 * \brief Žw’è”͈͂̃eƒLƒXƒgƒf[ƒ^‚ð’ljÁ
500 * \param first ’ljÁ‚É—p‚¢‚éƒf[ƒ^—ñ‚̐擪
501 * \param last ’ljÁ‚É—p‚¢‚éƒf[ƒ^—ñ‚̏I’[+1
502 * \param directive ‚Ps‚ðƒRƒ“ƒeƒi‚ÉŠi”[‚·‚邽‚߂̏ˆ—
503 * \note ƒtƒ@ƒCƒ‹–¼‚ƍs”ԍ†‚͒ljÁæ‚̏I’[‚Ì‚à‚Ì‚ðŒp‘±‚µ‚Ü‚·B
504 * ’ljÁæ‚Ƀf[ƒ^‚ª‚È‚¢ê‡‚Í"unknown"‚É‚È‚è‚Ü‚·B
505 *
506 * directive ‚Í void directive( container& cont, line_buf& buf )‚ÌŒ`Ž®‚ð
507 * ‚Æ‚éŠÖ”‚ւ̃|ƒCƒ“ƒ^‚Ü‚½‚̓tƒ@ƒ“ƒNƒ^‚Å‚ ‚邱‚Æ‚ð‘z’肵‚Ä‚¢‚Ü‚·B
508 * cont ‚Í“à•”‚Ńf[ƒ^‚ÌŠi”[‚ÉŽg—p‚·‚éƒRƒ“ƒeƒiA buf ‚Í‚Ps•ª‚Ì•¶Žš
509 * —ñ‚ƃtƒ@ƒCƒ‹–¼Es”ԍ†‚ðŠi”[‚µ‚½ƒoƒbƒtƒ@‚Å‚·B
510 */
511 template < class InputIterator, class Directive >
512 void append( InputIterator first, InputIterator last, Directive directive )
513 {
514 if ( first != last )
515 {
516 self_t t( *this );
517 t.init( first, last, directive );
518 swap( t );
519 }
520 }
521
522 /*!
523 * \brief Žw’è”͈͂̃eƒLƒXƒgƒf[ƒ^‚ð’ljÁ
524 * \param istr ’ljÁ‚É—p‚¢‚é“ü—̓XƒgƒŠ[ƒ€
525 * \note ƒtƒ@ƒCƒ‹–¼‚ƍs”ԍ†‚͒ljÁæ‚̏I’[‚Ì‚à‚Ì‚ðŒp‘±‚µ‚Ü‚·B
526 * ’ljÁæ‚Ƀf[ƒ^‚ª‚È‚¢ê‡‚Í"unknown"‚É‚È‚è‚Ü‚·B
527 */
528 void append( std::basic_istream< CharT, Traits >& istr )
529 {
530 append( istr, append_directive );
531 }
532
533 /*!
534 * \brief Žw’è”͈͂̃eƒLƒXƒgƒf[ƒ^‚ð’ljÁ
535 * \param istr ’ljÁ‚É—p‚¢‚é“ü—̓XƒgƒŠ[ƒ€
536 * \param directive ‚Ps‚ðƒRƒ“ƒeƒi‚ÉŠi”[‚·‚邽‚߂̏ˆ—
537 * \note ƒtƒ@ƒCƒ‹–¼‚ƍs”ԍ†‚͒ljÁæ‚̏I’[‚Ì‚à‚Ì‚ðŒp‘±‚µ‚Ü‚·B
538 * ’ljÁæ‚Ƀf[ƒ^‚ª‚È‚¢ê‡‚Í"unknown"‚É‚È‚è‚Ü‚·B
539 *
540 * directive ‚Í void directive( container& cont, line_buf& buf )‚ÌŒ`Ž®‚ð
541 * ‚Æ‚éŠÖ”‚ւ̃|ƒCƒ“ƒ^‚Ü‚½‚̓tƒ@ƒ“ƒNƒ^‚Å‚ ‚邱‚Æ‚ð‘z’肵‚Ä‚¢‚Ü‚·B
542 * cont ‚Í“à•”‚Ńf[ƒ^‚ÌŠi”[‚ÉŽg—p‚·‚éƒRƒ“ƒeƒiA buf ‚Í‚Ps•ª‚Ì•¶Žš
543 * —ñ‚ƃtƒ@ƒCƒ‹–¼Es”ԍ†‚ðŠi”[‚µ‚½ƒoƒbƒtƒ@‚Å‚·B
544 */
545 template < class Directive >
546 void append( std::basic_istream< CharT, Traits >& istr, Directive directive )
547 {
548 self_t t( *this );
549 t.init( istr, directive );
550 swap( t );
551 }
552
553 /*!
554 * \brief Žw’è”͈͂̃eƒLƒXƒgƒf[ƒ^‚ð‘ã“ü
555 * \param first ‘ã“ü‚É—p‚¢‚éƒf[ƒ^—ñ‚̐擪
556 * \param last ‘ã“ü‚É—p‚¢‚éƒf[ƒ^—ñ‚̏I’[+1
557 */
558 void assign( const_iterator const& first, const_iterator const& last )
559 {
560 self_t t( first, last );
561 swap( t );
562 }
563
564 /*!
565 * \brief Žw’è”͈͂̃eƒLƒXƒgƒf[ƒ^‚ð‘ã“ü
566 * \param first ‘ã“ü‚É—p‚¢‚éƒf[ƒ^—ñ‚̐擪
567 * \param last ‘ã“ü‚É—p‚¢‚éƒf[ƒ^—ñ‚̏I’[+1
568 * \note ƒtƒ@ƒCƒ‹–¼‚Í"unknown"‚É‚È‚è‚Ü‚·B
569 */
570 template < class InputIterator >
571 void assign( InputIterator first, InputIterator last )
572 {
573 self_t t( first, last, append_directive );
574 swap( t );
575 }
576
577 /*!
578 * \brief Žw’è”͈͂̃eƒLƒXƒgƒf[ƒ^‚ð‘ã“ü
579 * \param first ‘ã“ü‚É—p‚¢‚éƒf[ƒ^—ñ‚̐擪
580 * \param last ‘ã“ü‚É—p‚¢‚éƒf[ƒ^—ñ‚̏I’[+1
581 * \param directive ‚Ps‚ðƒRƒ“ƒeƒi‚ÉŠi”[‚·‚邽‚߂̏ˆ—
582 * \note ƒtƒ@ƒCƒ‹–¼‚Í"unknown"‚É‚È‚è‚Ü‚·B
583 *
584 * directive ‚Í void directive( container& cont, line_buf& buf )‚ÌŒ`Ž®‚ð
585 * ‚Æ‚éŠÖ”‚ւ̃|ƒCƒ“ƒ^‚Ü‚½‚̓tƒ@ƒ“ƒNƒ^‚Å‚ ‚邱‚Æ‚ð‘z’肵‚Ä‚¢‚Ü‚·B
586 * cont ‚Í“à•”‚Ńf[ƒ^‚ÌŠi”[‚ÉŽg—p‚·‚éƒRƒ“ƒeƒiA buf ‚Í‚Ps•ª‚Ì•¶Žš
587 * —ñ‚ƃtƒ@ƒCƒ‹–¼Es”ԍ†‚ðŠi”[‚µ‚½ƒoƒbƒtƒ@‚Å‚·B
588 */
589 template < class InputIterator, class Directive >
590 void assign( InputIterator first, InputIterator last, Directive directive )
591 {
592 self_t t( first, last, directive );
593 swap( t );
594 }
595
596 /*!
597 * \brief Žw’è”͈͂̃eƒLƒXƒgƒf[ƒ^‚ð‘ã“ü
598 * \param istr ‘ã“ü‚É—p‚¢‚é“ü—̓XƒgƒŠ[ƒ€
599 * \note ƒtƒ@ƒCƒ‹–¼‚ƍs”ԍ†‚Í‘ã“üæ‚̏I’[‚Ì‚à‚Ì‚ðŒp‘±‚µ‚Ü‚·B
600 * ’ljÁæ‚Ƀf[ƒ^‚ª‚È‚¢ê‡‚Í"unknown"‚É‚È‚è‚Ü‚·B
601 */
602 void assign( std::basic_istream< CharT, Traits >& istr )
603 {
604 self_t t( istr, append_directive );
605 swap( t );
606 }
607
608 /*!
609 * \brief Žw’è”͈͂̃eƒLƒXƒgƒf[ƒ^‚ð‘ã“ü
610 * \param istr ‘ã“ü‚É—p‚¢‚é“ü—̓XƒgƒŠ[ƒ€
611 * \param directive ‚Ps‚ðƒRƒ“ƒeƒi‚ÉŠi”[‚·‚邽‚߂̏ˆ—
612 * \note ƒtƒ@ƒCƒ‹–¼‚ƍs”ԍ†‚Í‘ã“üæ‚̏I’[‚Ì‚à‚Ì‚ðŒp‘±‚µ‚Ü‚·B
613 * ’ljÁæ‚Ƀf[ƒ^‚ª‚È‚¢ê‡‚Í"unknown"‚É‚È‚è‚Ü‚·B
614 *
615 * directive ‚Í void directive( container& cont, line_buf& buf )‚ÌŒ`Ž®‚ð
616 * ‚Æ‚éŠÖ”‚ւ̃|ƒCƒ“ƒ^‚Ü‚½‚̓tƒ@ƒ“ƒNƒ^‚Å‚ ‚邱‚Æ‚ð‘z’肵‚Ä‚¢‚Ü‚·B
617 * cont ‚Í“à•”‚Ńf[ƒ^‚ÌŠi”[‚ÉŽg—p‚·‚éƒRƒ“ƒeƒiA buf ‚Í‚Ps•ª‚Ì•¶Žš
618 * —ñ‚ƃtƒ@ƒCƒ‹–¼Es”ԍ†‚ðŠi”[‚µ‚½ƒoƒbƒtƒ@‚Å‚·B
619 */
620 template < class Directive >
621 void assign( std::basic_istream< CharT, Traits >& istr, Directive directive )
622 {
623 self_t t( istr, directive );
624 swap( t );
625 }
626
627 /*!
628 * \brief I’[‚É•¶Žš‚ð’ljÁ
629 * \param value ’ljÁ‚·‚镶Žš
630 */
631 void push_back( value_type value )
632 {
633 if ( container_.empty() || container_.back().line.file != line_.file )
634 {
635 line_buf buf( initial_line(), string_type( 1, value ) );
636 container_.push_back( buf );
637 }
638 else
639 {
640 string_type const* pbuf = &container_.back().buf;
641 if ( pbuf->at( pbuf->size()-1 ) == widen( '\n' ) )
642 {
643 ++line_.line;
644 line_buf buf( initial_line(), string_type( 1, value ) );
645 container_.push_back( buf );
646 }
647 else
648 {
649 container_.back().buf += value;
650 }
651 }
652 }
653
654 /*!
655 * \brief I’[‚©‚當Žš‚ðíœ
656 */
657 void pop_back()
658 {
659 if ( container_.empty() )
660 {
661 container_.pop_back();
662 }
663 else
664 {
665 string_type const* pbuf = &container_.back().buf;
666 pbuf->resize( pbuf->size()-1 );
667 if ( container_.back().buf.empty() )
668 {
669 container_.pop_back();
670 }
671 }
672 }
673
674 /*!
675 * \brief ƒRƒ“ƒeƒi‚ª‹ó‚©‚Ç‚¤‚©‚Ì”»•Ê
676 * \return ‹ó‚̏ꍇ‚Í true ‚ð•Ô‚·
677 */
678 bool empty() const
679 {
680 return container_.empty();
681 }
682
683 /*!
684 * \brief ƒRƒ“ƒeƒi‚̃TƒCƒY
685 * \return ƒRƒ“ƒeƒi‚ÉŠÜ‚Ü‚ê‚Ä‚¢‚镶Žš”‚ð•Ô‚·
686 */
687 size_type size() const
688 {
689 return std::accumulate( container_.begin(), container_.end(), size_type( 0 ), add_line_size );
690 }
691
692 /*!
693 * \brief ƒRƒ“ƒeƒi‚ÌŒðŠ·
694 * \param other ŒðŠ·‘ΏۂƂȂéƒRƒ“ƒeƒi
695 */
696 void swap( self_t& other )
697 {
698 container_.swap( other.container_ );
699 line_.swap( other.line_ );
700 }
701
702 /*!
703 * \brief ƒRƒ“ƒeƒi‚̃NƒŠƒA
704 */
705 void clear()
706 {
707 container_.clear();
708 line_.file.clear();
709 line_.line = 0;
710 }
711
712 void set_line( std::string const& file, long line )
713 {
714 line_.file = file;
715 line_.line = line;
716 }
717
718 line_type get_line() const
719 {
720 return line_;
721 }
722 private:
723 //! “¯Ží‚̃Rƒ“ƒeƒi‚ð“ü—ÍŒ³‚Æ‚·‚鏉Šú‰»ˆ—
724 void init( const_iterator const& first, const_iterator const& last )
725 {
726 line_buf buf( *first.get_row() );
727 if ( first.get_col() > 0 )
728 {
729 buf.buf = buf.buf.substr( first.get_col() );
730 }
731 container_.push_back( buf );
732 std::copy( boost::next( first ).get_row(), last.get_row(), std::back_inserter( container_ ) );
733 buf = *last.get_row();
734 buf.buf = buf.buf.substr( 0, last.get_col() );
735 container_.push_back( buf );
736 line_ = buf.line;
737 }
738
739 //! ˆÙŽí‚̃Rƒ“ƒeƒi‚ð“ü—ÍŒ³‚Æ‚·‚鏉Šú‰»ˆ—
740 template < class InputIterator, class Directive >
741 void init( InputIterator first, InputIterator last, Directive directive )
742 {
743 line_buf buf( initial_line() );
744 value_type const nl = widen( '\n' );
745 for ( InputIterator iter = first; iter != last; ++iter )
746 {
747 value_type ch = *iter;
748 buf.buf += ch;
749 if ( ch == nl )
750 {
751 directive( container_, buf );
752 line_ = buf.line;
753 }
754 }
755 if ( !buf.buf.empty() )
756 {
757 container_.push_back( buf );
758 }
759 line_ = buf.line;
760 }
761
762 //! “ü—̓XƒgƒŠ[ƒ€‚ð“ü—ÍŒ³‚Æ‚·‚鏉Šú‰»ˆ—
763 template < class Directive >
764 void init( std::basic_istream< CharT, Traits >& istr, Directive directive )
765 {
766 line_buf buf( initial_line() );
767 value_type const nl = widen( '\n' );
768 while ( istr )
769 {
770 value_type ch;
771 istr.get( ch );
772 buf.buf += ch;
773 if ( ch == nl )
774 {
775 directive( container_, buf );
776 line_ = buf.line;
777 }
778 }
779 if ( !buf.buf.empty() )
780 {
781 container_.push_back( buf );
782 }
783 line_ = buf.line;
784 }
785
786 //! ƒtƒ@ƒCƒ‹–¼Es”ԍ†‚̏‰Šúî•ñ‚̎擾
787 line_type const initial_line() const
788 {
789 if ( line_.line > 0 )
790 {
791 return line_;
792 }
793 if ( container_.empty() )
794 {
795 return line_type( "unknown", 1 );
796 }
797 return container_.back().line;
798 }
799
800 //! ƒVƒ“ƒOƒ‹ƒoƒCƒg•¶Žš‚©‚ç•ÊŒ`Ž®‚Ì•¶ŽšŒ^‚Ö‚Ì•ÏŠ·
801 static value_type widen( char ch )
802 {
803 return ::toppers::widen< value_type >( ch );
804 }
805
806 //! size ŠÖ”“à‚ÅŽg—p‚·‚é—ÝŽZˆ—
807 static size_type add_line_size( size_type value, line_buf const& buf )
808 {
809 return value + buf.buf.size();
810 }
811
812 //! Directive ‚̃fƒtƒHƒ‹ƒgˆ—
813 static void append_directive( container& cont, line_buf& buf )
814 {
815 cont.push_back( buf );
816 ++buf.line.line;
817 buf.buf.clear();
818 }
819 private:
820 container container_;
821 line_type line_;
822 };
823
824 typedef basic_text< std::vector, char > text;
825
826 inline text_line const& get_text_line( text::const_iterator iter )
827 {
828 return iter.line();
829 }
830
831 inline text_line const& get_text_line( text::iterator iter )
832 {
833 return iter.line();
834 }
835
836}
837
838#endif // ! TOPPERS_TEXT_HPP_
Note: See TracBrowser for help on using the repository browser.