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/misc.hpp
|
---|
44 | * \brief G½ÈCu̽ßÌé¾è`
|
---|
45 | */
|
---|
46 | #ifndef TOPPERS_MISC_HPP_
|
---|
47 | #define TOPPERS_MISC_HPP_
|
---|
48 |
|
---|
49 | #include <stdexcept>
|
---|
50 | #include <locale>
|
---|
51 | #include <iosfwd>
|
---|
52 | #include <string>
|
---|
53 | #include <algorithm>
|
---|
54 | #include <cctype>
|
---|
55 | #include <cwchar>
|
---|
56 | #include <cwctype>
|
---|
57 | #include <cstdlib>
|
---|
58 | #include "toppers/codeset.hpp"
|
---|
59 | #include <boost/scoped_array.hpp>
|
---|
60 |
|
---|
61 | // workaround
|
---|
62 | #include <ctype.h>
|
---|
63 | #include <wctype.h>
|
---|
64 |
|
---|
65 | #define TOPPERS_STRINGIFY( s ) TOPPERS_STRINGIFY_( s )
|
---|
66 | #define TOPPERS_STRINGIFY_( s ) # s
|
---|
67 |
|
---|
68 | namespace toppers
|
---|
69 | {
|
---|
70 |
|
---|
71 | /*!
|
---|
72 | * \class conversion_error misc.hpp "toppers/misc.hpp"
|
---|
73 | * \brief Ï·G[áONX
|
---|
74 | */
|
---|
75 | class conversion_error : public std::runtime_error
|
---|
76 | {
|
---|
77 | public:
|
---|
78 | /*!
|
---|
79 | * \brief RXgN^
|
---|
80 | * \param what áO´ö¶ñ
|
---|
81 | */
|
---|
82 | explicit conversion_error( std::string const& what ) : std::runtime_error( what ) {}
|
---|
83 | };
|
---|
84 |
|
---|
85 | /*!
|
---|
86 | * \brief VOoCg¶©ç CharT ^i½Í wchar_t ^j¶ÖÌÏ·
|
---|
87 | * \param ch VOoCg¶
|
---|
88 | * \return CharT ^¶ðÔ·
|
---|
89 | */
|
---|
90 | template < typename CharT >
|
---|
91 | inline CharT widen( char ch )
|
---|
92 | {
|
---|
93 | return std::use_facet< std::ctype< CharT > >( std::locale() ).widen( ch );
|
---|
94 | }
|
---|
95 |
|
---|
96 | template <>
|
---|
97 | inline char widen< char >( char ch )
|
---|
98 | {
|
---|
99 | return ch;
|
---|
100 | }
|
---|
101 |
|
---|
102 | template <>
|
---|
103 | inline unsigned char widen< unsigned char >( char ch )
|
---|
104 | {
|
---|
105 | return static_cast< unsigned char >( ch );
|
---|
106 | }
|
---|
107 |
|
---|
108 | template <>
|
---|
109 | inline wchar_t widen< wchar_t >( char ch )
|
---|
110 | {
|
---|
111 | #if defined( __MINGW32__ )
|
---|
112 | wchar_t wc = wchar_t( -1 );
|
---|
113 | if ( std::mbtowc( &wc, &ch, 1 ) < 0 )
|
---|
114 | {
|
---|
115 | return WEOF;
|
---|
116 | }
|
---|
117 | return wc;
|
---|
118 | #elif defined( __CYGWIN__ )
|
---|
119 | return static_cast< wchar_t >( static_cast< unsigned char >( ch ) );
|
---|
120 | #else
|
---|
121 | return static_cast< wchar_t >( std::btowc( static_cast< unsigned char >( ch ) ) );
|
---|
122 | #endif
|
---|
123 | }
|
---|
124 |
|
---|
125 | /*!
|
---|
126 | * \brief VOoCg¶ñ©ç CharT ^¶ñÖÌÏ·
|
---|
127 | * \param str VOoCg¶ñ
|
---|
128 | * \return CharT ^¶ñðÔ·
|
---|
129 | */
|
---|
130 | template < typename CharT >
|
---|
131 | std::basic_string< CharT > const widen( std::string const& str );
|
---|
132 |
|
---|
133 | template <>
|
---|
134 | inline std::basic_string< char > const widen( std::string const& str )
|
---|
135 | {
|
---|
136 | return str;
|
---|
137 | }
|
---|
138 |
|
---|
139 | template <>
|
---|
140 | inline std::basic_string< wchar_t > const widen( std::string const& str )
|
---|
141 | {
|
---|
142 | boost::scoped_array< wchar_t > t( new wchar_t[str.size()+1] );
|
---|
143 | if ( std::mbstowcs( t.get(), str.c_str(), str.size() ) == size_t( -1 ) )
|
---|
144 | {
|
---|
145 | static conversion_error x( "in function widen" );
|
---|
146 | throw x;
|
---|
147 | }
|
---|
148 | return t.get();
|
---|
149 | }
|
---|
150 |
|
---|
151 | #undef tolower
|
---|
152 |
|
---|
153 | /*!
|
---|
154 | * \brief ¬¶ÖÌÏ·
|
---|
155 | * \param ch Ï·ÎÛ̶
|
---|
156 | * \return ch ªå¶Å êÎη鬶ðA»êÈOÍ ch ðÔ·
|
---|
157 | */
|
---|
158 | inline char tolower( char ch )
|
---|
159 | {
|
---|
160 | return static_cast< char >( std::tolower( static_cast< unsigned char >( ch ) ) );
|
---|
161 | }
|
---|
162 |
|
---|
163 | /*!
|
---|
164 | * \brief ¶ñð¬¶ÉÏ·
|
---|
165 | * \param str Ï·ÎÛ̶
|
---|
166 | * \return str ÉÜÜêéå¶ð¬¶ÉÏ·µ½¶ñðÔ·
|
---|
167 | */
|
---|
168 | inline std::string const tolower( std::string str )
|
---|
169 | {
|
---|
170 | char ( *f )( char ) = &tolower;
|
---|
171 | std::transform( str.begin(), str.end(), str.begin(), f );
|
---|
172 | return str;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /*!
|
---|
176 | * \brief Ch¶ñð¬¶ÉÏ·
|
---|
177 | * \param str Ï·ÎÛ̶
|
---|
178 | * \return str ÉÜÜêéå¶ð¬¶ÉÏ·µ½Ch¶ñðÔ·
|
---|
179 | */
|
---|
180 | template < class Traits, class Allocator >
|
---|
181 | std::basic_string< wchar_t, Traits, Allocator > const tolower( std::basic_string< wchar_t, Traits, Allocator > str )
|
---|
182 | {
|
---|
183 | wint_t ( *f )( wint_t ) = &towlower;
|
---|
184 | std::transform( str.begin(), str.end(), str.begin(), f );
|
---|
185 | return str;
|
---|
186 | }
|
---|
187 |
|
---|
188 | #undef toupper
|
---|
189 |
|
---|
190 | /*!
|
---|
191 | * \brief å¶ÖÌÏ·
|
---|
192 | * \param ch Ï·ÎÛ̶
|
---|
193 | * \return ch ª¬¶Å êÎηéå¶ðA»êÈOÍ ch ðÔ·
|
---|
194 | */
|
---|
195 | inline char toupper( char ch )
|
---|
196 | {
|
---|
197 | return static_cast< char >( std::toupper( static_cast< unsigned char >( ch ) ) );
|
---|
198 | }
|
---|
199 |
|
---|
200 | /*!
|
---|
201 | * \brief ¶ñðå¶ÉÏ·
|
---|
202 | * \param str Ï·ÎÛ̶
|
---|
203 | * \return str ÉÜÜê鬶ðå¶ÉÏ·µ½¶ñðÔ·
|
---|
204 | */
|
---|
205 | inline std::string const toupper( std::string str )
|
---|
206 | {
|
---|
207 | char ( *f )( char ) = &toupper;
|
---|
208 | std::transform( str.begin(), str.end(), str.begin(), f );
|
---|
209 | return str;
|
---|
210 | }
|
---|
211 |
|
---|
212 | /*!
|
---|
213 | * \brief Ch¶ñðå¶ÉÏ·
|
---|
214 | * \param str Ï·ÎÛ̶
|
---|
215 | * \return str ÉÜÜê鬶ðå¶ÉÏ·µ½Ch¶ñðÔ·
|
---|
216 | */
|
---|
217 | template < class Traits, class Allocator >
|
---|
218 | std::basic_string< wchar_t, Traits, Allocator > const toupper( std::basic_string< wchar_t, Traits, Allocator > str )
|
---|
219 | {
|
---|
220 | wint_t ( *f )( wint_t ) = &towupper;
|
---|
221 | std::transform( str.begin(), str.end(), str.begin(), f );
|
---|
222 | return str;
|
---|
223 | }
|
---|
224 |
|
---|
225 | #undef isspace
|
---|
226 |
|
---|
227 | /*!
|
---|
228 | * \brief óÞÌ»Ê
|
---|
229 | *
|
---|
230 | */
|
---|
231 | inline bool isspace( char ch )
|
---|
232 | {
|
---|
233 | return std::isspace( static_cast< unsigned char >( ch ) ) != 0;
|
---|
234 | }
|
---|
235 |
|
---|
236 | /*!
|
---|
237 | * \brief wè¶ÅæØçê½XgoÍ
|
---|
238 | * \param first oÍ·éæªvfÊu
|
---|
239 | * \param last oÍ·éI[vfÊu+1
|
---|
240 | * \param ostr oÍXg[
|
---|
241 | * \param pred evfðó¯æèoÍlðÔ·qê
|
---|
242 | * \param delim æØ¶
|
---|
243 | *
|
---|
244 | * ±ÌÖÍæÔ [first, last) Ìevfð pred ÉnµÄ¾çêélð delim
|
---|
245 | * ÅæØÁÄ ostr Éo͵ܷB\n
|
---|
246 | * I[ÌvfÌãÉÍ delim Íoͳê¸AvfÆvfÌÔÉÌÝ delim ª
|
---|
247 | * oͳêÜ·B
|
---|
248 | */
|
---|
249 | template < class InputIterator, typename CharT, class Traits, class Pred >
|
---|
250 | void output_list( InputIterator first, InputIterator last, std::basic_ostream< CharT, Traits >& ostr, Pred pred, CharT const* delim = 0 )
|
---|
251 | {
|
---|
252 | if ( delim == 0 )
|
---|
253 | {
|
---|
254 | static CharT const null_delim[] = { 0 };
|
---|
255 | delim = null_delim;
|
---|
256 | }
|
---|
257 |
|
---|
258 | for ( bool f = false; first != last; ++first )
|
---|
259 | {
|
---|
260 | if ( f )
|
---|
261 | {
|
---|
262 | ostr << delim;
|
---|
263 | }
|
---|
264 | ostr << pred( *first );
|
---|
265 | f = true;
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 | template < typename CharT, class Traits, class Allocator >
|
---|
270 | std::basic_string< CharT, Traits, Allocator > trim( std::basic_string< CharT, Traits, Allocator > const& str, std::basic_string< CharT, Traits, Allocator > const& ws )
|
---|
271 | {
|
---|
272 | if ( str.empty() || ws.empty() )
|
---|
273 | {
|
---|
274 | return str;
|
---|
275 | }
|
---|
276 | typename std::basic_string< CharT, Traits, Allocator >::size_type first = str.find_first_not_of( ws, 0 );
|
---|
277 | for ( typename std::basic_string< CharT, Traits, Allocator >::size_type i = str.size() - 1; i >= 0; i-- )
|
---|
278 | {
|
---|
279 | if ( ws.find( str[ i ] ) == ws.npos )
|
---|
280 | {
|
---|
281 | return str.substr( first, i );
|
---|
282 | }
|
---|
283 | }
|
---|
284 | return str.substr( first );
|
---|
285 | }
|
---|
286 |
|
---|
287 | }
|
---|
288 |
|
---|
289 | #endif // ! TOPPERS_MISC_HPP_
|
---|