1 | /*
|
---|
2 | * TOPPERS Software
|
---|
3 | * Toyohashi Open Platform for Embedded Real-Time Systems
|
---|
4 | *
|
---|
5 | * Copyright (C) 2007-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/csv.hpp
|
---|
44 | * \brief CSVt@CüoÍÉÖ·éé¾è`
|
---|
45 | */
|
---|
46 | #ifndef TOPPERS_CSV_HPP_
|
---|
47 | #define TOPPERS_CSV_HPP_
|
---|
48 |
|
---|
49 | #include <stdexcept>
|
---|
50 | #include <ostream>
|
---|
51 | #include <iterator>
|
---|
52 | #include <string>
|
---|
53 | #include <vector>
|
---|
54 | #include <algorithm>
|
---|
55 | #include <boost/utility.hpp>
|
---|
56 | #include "toppers/misc.hpp"
|
---|
57 |
|
---|
58 | namespace toppers
|
---|
59 | {
|
---|
60 |
|
---|
61 | class csv_error : public std::runtime_error
|
---|
62 | {
|
---|
63 | public:
|
---|
64 | explicit csv_error( long line )
|
---|
65 | : std::runtime_error( "CSV parse error" ), line_( line )
|
---|
66 | {
|
---|
67 | }
|
---|
68 | long line() const
|
---|
69 | {
|
---|
70 | return line_;
|
---|
71 | }
|
---|
72 | private:
|
---|
73 | long line_;
|
---|
74 | };
|
---|
75 |
|
---|
76 | /*!
|
---|
77 | * \class basic_csv csv.hpp "toppers/csv.hpp"
|
---|
78 | * \brief CSV t@CÌðÍNX
|
---|
79 | */
|
---|
80 | template < typename CharT, class Traits = std::char_traits< CharT > >
|
---|
81 | class basic_csv
|
---|
82 | {
|
---|
83 | public:
|
---|
84 | typedef std::basic_string< CharT, Traits > string_type;
|
---|
85 | typedef typename std::vector< std::vector< string_type > >::const_iterator const_iterator;
|
---|
86 | typedef typename std::vector< std::vector< string_type > >::const_reference const_reference;
|
---|
87 | typedef typename std::vector< std::vector< string_type > >::value_type value_type;
|
---|
88 | typedef typename std::vector< std::vector< string_type > >::size_type size_type;
|
---|
89 |
|
---|
90 | basic_csv();
|
---|
91 | template < class InputIterator >
|
---|
92 | basic_csv( InputIterator first, InputIterator last )
|
---|
93 | {
|
---|
94 | read( first, last );
|
---|
95 | }
|
---|
96 |
|
---|
97 | template < class InputIterator >
|
---|
98 | void read( InputIterator first, InputIterator last )
|
---|
99 | {
|
---|
100 | const CharT dquo = widen< CharT >( '\"' );
|
---|
101 | const CharT comma = widen< CharT >( ',' );
|
---|
102 | const CharT lf = widen< CharT >( '\n' );
|
---|
103 | enum
|
---|
104 | {
|
---|
105 | none,
|
---|
106 | non_escaped,
|
---|
107 | escape_opened,
|
---|
108 | escape_closed
|
---|
109 | } state = none;
|
---|
110 | std::vector< std::vector< string_type > > records;
|
---|
111 | std::vector< string_type > record;
|
---|
112 | string_type field;
|
---|
113 | long line = 1;
|
---|
114 |
|
---|
115 | record.reserve( 16 );
|
---|
116 | field.reserve( 255 );
|
---|
117 |
|
---|
118 | while ( first != last )
|
---|
119 | {
|
---|
120 | CharT ch = *first;
|
---|
121 | if ( ch == dquo )
|
---|
122 | {
|
---|
123 | switch ( state )
|
---|
124 | {
|
---|
125 | case none:
|
---|
126 | state = escape_opened;
|
---|
127 | break;
|
---|
128 | case escape_opened:
|
---|
129 | ++first;
|
---|
130 | ch = *first;
|
---|
131 | if ( ch == dquo ) // "" ¾Á½ÌÅA" ðÇÁ
|
---|
132 | {
|
---|
133 | field.push_back( dquo );
|
---|
134 | }
|
---|
135 | else // ¶ "
|
---|
136 | {
|
---|
137 | state = escape_closed;
|
---|
138 | }
|
---|
139 | continue; // ++first ð±êÈãÀs³¹È¢
|
---|
140 | default:
|
---|
141 | throw csv_error( line );
|
---|
142 | break;
|
---|
143 | }
|
---|
144 | }
|
---|
145 | else // ch != dquo
|
---|
146 | {
|
---|
147 | if ( state == escape_opened )
|
---|
148 | {
|
---|
149 | field.push_back( ch );
|
---|
150 | }
|
---|
151 | else
|
---|
152 | {
|
---|
153 | if ( ch == comma || ch == lf )
|
---|
154 | {
|
---|
155 | record.push_back( field );
|
---|
156 | field.clear();
|
---|
157 | state = none;
|
---|
158 | if ( ch == lf ) // üsiRFC4180 Å͵§ÉÍ CR-LF ȯêÎÈçÈ¢ªAÆè ¦¸ '\n' Æ·éj
|
---|
159 | {
|
---|
160 | records.push_back( record );
|
---|
161 | record.clear();
|
---|
162 | ++line;
|
---|
163 | }
|
---|
164 | }
|
---|
165 | else
|
---|
166 | {
|
---|
167 | field.push_back( ch );
|
---|
168 | // CharT const* debug_string = field.c_str();
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|
172 | ++first;
|
---|
173 | }
|
---|
174 | records_.swap( records );
|
---|
175 | }
|
---|
176 | template < class OutputIterator >
|
---|
177 | void write( OutputIterator result ) const
|
---|
178 | {
|
---|
179 | const CharT dquo = widen< CharT >( '\"' );
|
---|
180 | const CharT comma = widen< CharT >( ',' );
|
---|
181 | const CharT lf = widen< CharT >( '\n' );
|
---|
182 |
|
---|
183 | for ( typename std::vector< std::vector< string_type > >::const_iterator r_iter( records_.begin() ), r_last( records_.end() );
|
---|
184 | r_iter != r_last;
|
---|
185 | ++r_iter )
|
---|
186 | {
|
---|
187 | for ( typename std::vector< string_type >::const_iterator f_iter( r_iter->begin() ), f_last( r_iter->end() );
|
---|
188 | f_iter != f_last;
|
---|
189 | ++f_iter )
|
---|
190 | {
|
---|
191 | std::string field;
|
---|
192 | bool need_escape = false;
|
---|
193 |
|
---|
194 | for ( typename string_type::const_iterator s_iter( f_iter->begin() ), s_last( f_iter->end() );
|
---|
195 | s_iter != s_last;
|
---|
196 | ++s_iter )
|
---|
197 | {
|
---|
198 | CharT ch = *s_iter;
|
---|
199 | if ( ch == dquo )
|
---|
200 | {
|
---|
201 | field.push_back( dquo );
|
---|
202 | need_escape = true;
|
---|
203 | }
|
---|
204 | else if ( ch == comma || ch == lf )
|
---|
205 | {
|
---|
206 | need_escape = true;
|
---|
207 | }
|
---|
208 | field.push_back( ch );
|
---|
209 | }
|
---|
210 |
|
---|
211 | if ( need_escape )
|
---|
212 | {
|
---|
213 | field = dquo + field + dquo;
|
---|
214 | }
|
---|
215 | result = std::copy( field.begin(), field.end(), result );
|
---|
216 | if ( boost::next( f_iter ) != f_last )
|
---|
217 | {
|
---|
218 | *result++ = comma;
|
---|
219 | }
|
---|
220 | }
|
---|
221 | *result++ = lf;
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | const_iterator begin() const
|
---|
226 | {
|
---|
227 | return records_.begin();
|
---|
228 | }
|
---|
229 | const_iterator end() const
|
---|
230 | {
|
---|
231 | return records_.end();
|
---|
232 | }
|
---|
233 | const_reference at( size_type pos ) const
|
---|
234 | {
|
---|
235 | return records_.at( pos );
|
---|
236 | }
|
---|
237 | const_reference operator[]( size_type pos ) const
|
---|
238 | {
|
---|
239 | return records_[ pos ];
|
---|
240 | }
|
---|
241 | bool empty() const
|
---|
242 | {
|
---|
243 | return records_.empty();
|
---|
244 | }
|
---|
245 | size_type size() const
|
---|
246 | {
|
---|
247 | return records_.size();
|
---|
248 | }
|
---|
249 | void swap( basic_csv& other )
|
---|
250 | {
|
---|
251 | records_.swap( other.records_ );
|
---|
252 | }
|
---|
253 | private:
|
---|
254 | std::vector< std::vector< string_type > > records_;
|
---|
255 | };
|
---|
256 |
|
---|
257 | typedef basic_csv< char > csv;
|
---|
258 | typedef basic_csv< wchar_t > wcsv;
|
---|
259 |
|
---|
260 | }
|
---|
261 |
|
---|
262 | #endif // TOPPERS_CSV_HPP_
|
---|