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{\tgEF
|
---|
8 | * Ai{\tgEFAðüϵ½àÌðÜÞDȺ¯¶jðgpE¡»Eü
|
---|
9 | * ÏEÄzziȺCpÆÄÔj·é±Æð³Åø·éD
|
---|
10 | * (1) {\tgEFAð\[XR[hÌ`Åp·éêÉÍCãLÌì
|
---|
11 | * \¦C±Ìpð¨æÑºLÌ³ÛØKèªC»ÌÜÜÌ`Å\[
|
---|
12 | * XR[hÉÜÜêĢ鱯D
|
---|
13 | * (2) {\tgEFAðCCu`®ÈÇC¼Ì\tgEFAJÉg
|
---|
14 | * pÅ«é`ÅÄzz·éêÉÍCÄzzɺ¤hL
|
---|
15 | gip
|
---|
16 | * Ò}j
|
---|
17 | AÈÇjÉCãLÌì \¦C±Ìpð¨æÑºL
|
---|
18 | * Ì³ÛØKèðfÚ·é±ÆD
|
---|
19 | * (3) {\tgEFAðC@íÉgÝÞÈÇC¼Ì\tgEFAJÉg
|
---|
20 | * pūȢ`ÅÄzz·éêÉÍCÌ¢¸ê©Ìðð½·±
|
---|
21 | * ÆD
|
---|
22 | * (a) Äzzɺ¤hL
|
---|
23 | gipÒ}j
|
---|
24 | AÈÇjÉCãLÌ
|
---|
25 | * ì \¦C±Ìpð¨æÑºLÌ³ÛØKèðfÚ·é±ÆD
|
---|
26 | * (b) ÄzzÌ`ÔðCÊÉèßéû@ÉæÁÄCTOPPERSvWFNgÉ
|
---|
27 | * ñ·é±ÆD
|
---|
28 | * (4) {\tgEFAÌpÉæè¼ÚIܽÍÔÚIɶ¶é¢©Èé¹
|
---|
29 | * Q©çàCãLì Ò¨æÑTOPPERSvWFNgðÆÓ·é±ÆD
|
---|
30 | * ܽC{\tgEFAÌ[UܽÍGh[U©çÌ¢©Èé
|
---|
31 | * RÉîÿ©çàCãLì Ò¨æÑTOPPERSvWFNgð
|
---|
32 | * ÆÓ·é±ÆD
|
---|
33 | *
|
---|
34 | * {\tgEFAÍC³ÛØÅñ³êÄ¢éàÌÅ éDãLì Ò¨
|
---|
35 | * æÑTOPPERSvWFNgÍC{\tgEFAÉÖµÄCÁèÌgpÚI
|
---|
36 | * ÉηéK«àÜßÄC¢©ÈéÛØàsíÈ¢DܽC{\tgEF
|
---|
37 | * AÌpÉæè¼ÚIܽÍÔÚIɶ¶½¢©Èé¹QÉÖµÄàC»
|
---|
38 | * ÌÓCðíÈ¢D
|
---|
39 | *
|
---|
40 | */
|
---|
41 |
|
---|
42 | /*!
|
---|
43 | * \file toppers/text.hpp
|
---|
44 | * \brief eLXgf[^ÇÉÖ·éé¾è`
|
---|
45 | *
|
---|
46 | * ±Ìt@CÅè`³êéNX
|
---|
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 |
|
---|
64 | namespace toppers
|
---|
65 | {
|
---|
66 |
|
---|
67 | /*!
|
---|
68 | * \class basic_text text.hpp "toppers/text.hpp"
|
---|
69 | * \brief eLXgf[^Çev[gNX
|
---|
70 | *
|
---|
71 | * t@C©çÇÝñ¾eLXgf[^ÌÇðs¤½ßÌNXÅ·B
|
---|
72 | * ±ÌNXÅÍAeLXgf[^ðPÈé¶ñƵÄÅÍÈAüͳÌ
|
---|
73 | * t@C¼ÆsÔðtÁµ½`ÅǵܷB
|
---|
74 | *
|
---|
75 | * \note ±ÌNXÍeLXgÒWÌprð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ÈReiÆÌݷ̽ßÌ^è`
|
---|
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 ÌCe[^
|
---|
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 ÌCe[^
|
---|
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 ftHgRXgN^
|
---|
225 | */
|
---|
226 | basic_text() {}
|
---|
227 |
|
---|
228 | /*!
|
---|
229 | * \brief RXgN^
|
---|
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 RXgN^
|
---|
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 RXgN^
|
---|
258 | * \param first ú»Ép¢éf[^ñÌæª
|
---|
259 | * \param last ú»Ép¢éf[^ñÌI[+1
|
---|
260 | * \param directive PsðReiÉ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[Égp·éReiA buf ÍPsªÌ¶
|
---|
266 | * ñÆt@C¼EsÔði[µ½obt@Å·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 RXgN^
|
---|
279 | * \param istr ú»Ép¢éüÍXg[
|
---|
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 RXgN^
|
---|
289 | * \param istr ú»Ép¢éüÍXg[
|
---|
290 | * \param directive PsðReiÉ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[Égp·éReiA buf ÍPsªÌ¶
|
---|
296 | * ñÆt@C¼EsÔði[µ½obt@Å·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 檶ÖÌCe[^ðÔ·
|
---|
307 | */
|
---|
308 | const_iterator begin() const
|
---|
309 | {
|
---|
310 | return const_iterator( container_.begin() );
|
---|
311 | }
|
---|
312 |
|
---|
313 | /*!
|
---|
314 | * \brief æªÊuÌæ¾
|
---|
315 | * \return 檶ÖÌCe[^ðÔ·
|
---|
316 | */
|
---|
317 | iterator begin()
|
---|
318 | {
|
---|
319 | return iterator( container_.begin() );
|
---|
320 | }
|
---|
321 |
|
---|
322 | /*!
|
---|
323 | * \brief sÌæ¾
|
---|
324 | * \param pos 檶©çÌItZbg
|
---|
325 | * \return posÅwè³ê½ItZbg©çsðßCCe[^ðÔ·
|
---|
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[¶ÌvfÖÌCe[^ðÔ·
|
---|
361 | */
|
---|
362 | const_iterator end() const
|
---|
363 | {
|
---|
364 | return const_iterator( container_.end() );
|
---|
365 | }
|
---|
366 |
|
---|
367 | /*!
|
---|
368 | * \brief I[Êu+1Ìæ¾
|
---|
369 | * \return I[¶ÌvfÖÌCe[^ðÔ·
|
---|
370 | */
|
---|
371 | iterator end()
|
---|
372 | {
|
---|
373 | return iterator( container_.end() );
|
---|
374 | }
|
---|
375 |
|
---|
376 | /*!
|
---|
377 | * \brief tæªÊuÌæ¾
|
---|
378 | * \return t檶ÖÌtCe[^ðÔ·
|
---|
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檶ÖÌtCe[^ðÔ·
|
---|
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 tI[Êu+1Ìæ¾
|
---|
398 | * \return tI[¶ÌvfÖÌtCe[^ðÔ·
|
---|
399 | */
|
---|
400 | const_reverse_iterator rend() const
|
---|
401 | {
|
---|
402 | return const_reverse_iterator( container_.rend() );
|
---|
403 | }
|
---|
404 |
|
---|
405 | /*!
|
---|
406 | * \brief tI[Êu+1Ìæ¾
|
---|
407 | * \return tI[¶ÌvfÖÌtCe[^ðÔ·
|
---|
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èÍÍÌeLXgf[^ðÇÁ
|
---|
472 | * \param first ÇÁÉp¢éf[^ñÌæª
|
---|
473 | * \param last ÇÁÉ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èÍÍÌeLXgf[^ðÇÁ
|
---|
487 | * \param first ÇÁÉp¢éf[^ñÌæª
|
---|
488 | * \param last ÇÁÉp¢éf[^ñÌI[+1
|
---|
489 | * \note t@C¼ÆsÔÍÇÁæÌI[ÌàÌðp±µÜ·B
|
---|
490 | * ÇÁæÉ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èÍÍÌeLXgf[^ðÇÁ
|
---|
500 | * \param first ÇÁÉp¢éf[^ñÌæª
|
---|
501 | * \param last ÇÁÉp¢éf[^ñÌI[+1
|
---|
502 | * \param directive PsðReiÉi[·é½ßÌ
|
---|
503 | * \note t@C¼ÆsÔÍÇÁæÌI[ÌàÌðp±µÜ·B
|
---|
504 | * ÇÁæÉf[^ªÈ¢êÍ"unknown"ÉÈèÜ·B
|
---|
505 | *
|
---|
506 | * directive Í void directive( container& cont, line_buf& buf )Ì`®ð
|
---|
507 | * ÆéÖÖÌ|C^ܽÍt@N^Š鱯ðzèµÄ¢Ü·B
|
---|
508 | * cont ÍàÅf[^Ìi[Égp·éReiA buf ÍPsªÌ¶
|
---|
509 | * ñÆt@C¼EsÔði[µ½obt@Å·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èÍÍÌeLXgf[^ðÇÁ
|
---|
524 | * \param istr ÇÁÉp¢éüÍXg[
|
---|
525 | * \note t@C¼ÆsÔÍÇÁæÌI[ÌàÌðp±µÜ·B
|
---|
526 | * ÇÁæÉ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èÍÍÌeLXgf[^ðÇÁ
|
---|
535 | * \param istr ÇÁÉp¢éüÍXg[
|
---|
536 | * \param directive PsðReiÉi[·é½ßÌ
|
---|
537 | * \note t@C¼ÆsÔÍÇÁæÌI[ÌàÌðp±µÜ·B
|
---|
538 | * ÇÁæÉf[^ªÈ¢êÍ"unknown"ÉÈèÜ·B
|
---|
539 | *
|
---|
540 | * directive Í void directive( container& cont, line_buf& buf )Ì`®ð
|
---|
541 | * ÆéÖÖÌ|C^ܽÍt@N^Š鱯ðzèµÄ¢Ü·B
|
---|
542 | * cont ÍàÅf[^Ìi[Égp·éReiA buf ÍPsªÌ¶
|
---|
543 | * ñÆt@C¼EsÔði[µ½obt@Å·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èÍÍÌeLXgf[^ðãü
|
---|
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èÍÍÌeLXgf[^ðãü
|
---|
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èÍÍÌeLXgf[^ðãü
|
---|
579 | * \param first ãüÉp¢éf[^ñÌæª
|
---|
580 | * \param last ãüÉp¢éf[^ñÌI[+1
|
---|
581 | * \param directive PsðReiÉ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[Égp·éReiA buf ÍPsªÌ¶
|
---|
587 | * ñÆt@C¼EsÔði[µ½obt@Å·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èÍÍÌeLXgf[^ðãü
|
---|
598 | * \param istr ãüÉp¢éüÍXg[
|
---|
599 | * \note t@C¼ÆsÔÍãüæÌI[ÌàÌðp±µÜ·B
|
---|
600 | * ÇÁæÉ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èÍÍÌeLXgf[^ðãü
|
---|
610 | * \param istr ãüÉp¢éüÍXg[
|
---|
611 | * \param directive PsðReiÉi[·é½ßÌ
|
---|
612 | * \note t@C¼ÆsÔÍãüæÌI[ÌàÌðp±µÜ·B
|
---|
613 | * ÇÁæÉf[^ªÈ¢êÍ"unknown"ÉÈèÜ·B
|
---|
614 | *
|
---|
615 | * directive Í void directive( container& cont, line_buf& buf )Ì`®ð
|
---|
616 | * ÆéÖÖÌ|C^ܽÍt@N^Š鱯ðzèµÄ¢Ü·B
|
---|
617 | * cont ÍàÅf[^Ìi[Égp·éReiA buf ÍPsªÌ¶
|
---|
618 | * ñÆt@C¼EsÔði[µ½obt@Å·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[ɶðÇÁ
|
---|
629 | * \param value ÇÁ·é¶
|
---|
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 Reiªó©Ç¤©Ì»Ê
|
---|
676 | * \return óÌêÍ true ðÔ·
|
---|
677 | */
|
---|
678 | bool empty() const
|
---|
679 | {
|
---|
680 | return container_.empty();
|
---|
681 | }
|
---|
682 |
|
---|
683 | /*!
|
---|
684 | * \brief ReiÌTCY
|
---|
685 | * \return ReiÉÜÜêÄ¢é¶ðÔ·
|
---|
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 ReiÌð·
|
---|
694 | * \param other ð·ÎÛÆÈéRei
|
---|
695 | */
|
---|
696 | void swap( self_t& other )
|
---|
697 | {
|
---|
698 | container_.swap( other.container_ );
|
---|
699 | line_.swap( other.line_ );
|
---|
700 | }
|
---|
701 |
|
---|
702 | /*!
|
---|
703 | * \brief ReiÌNA
|
---|
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 | //! ¯íÌReiðüͳƷéú»
|
---|
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 | //! ÙíÌReiðüͳƷéú»
|
---|
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 | //! üÍXg[ðüͳƷéú»
|
---|
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¼EsÔÌúîñÌæ¾
|
---|
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 | //! VOoCg¶©çÊ`®Ì¶^ÖÌÏ·
|
---|
801 | static value_type widen( char ch )
|
---|
802 | {
|
---|
803 | return ::toppers::widen< value_type >( ch );
|
---|
804 | }
|
---|
805 |
|
---|
806 | //! size ÖàÅgp·éÝ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 ÌftHg
|
---|
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_
|
---|