source: cfg_oil/trunk/toppers/misc.hpp@ 8

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

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

File size: 8.4 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/misc.hpp
44 * \brief ŽG‘½‚ȃ‰ƒCƒuƒ‰ƒŠ‚Ì‚½‚߂̐錾’è‹`
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
68namespace toppers
69{
70
71 /*!
72 * \class conversion_error misc.hpp "toppers/misc.hpp"
73 * \brief •ÏŠ·ƒGƒ‰[—áŠOƒNƒ‰ƒX
74 */
75 class conversion_error : public std::runtime_error
76 {
77 public:
78 /*!
79 * \brief ƒRƒ“ƒXƒgƒ‰ƒNƒ^
80 * \param what —áŠOŒ´ˆö•¶Žš—ñ
81 */
82 explicit conversion_error( std::string const& what ) : std::runtime_error( what ) {}
83 };
84
85 /*!
86 * \brief ƒVƒ“ƒOƒ‹ƒoƒCƒg•¶Žš‚©‚ç CharT Œ^i‘½‚­‚Í wchar_t Œ^j•¶Žš‚Ö‚Ì•ÏŠ·
87 * \param ch ƒVƒ“ƒOƒ‹ƒoƒCƒg•¶Žš
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 ƒVƒ“ƒOƒ‹ƒoƒCƒg•¶Žš—ñ‚©‚ç CharT Œ^•¶Žš—ñ‚Ö‚Ì•ÏŠ·
127 * \param str ƒVƒ“ƒOƒ‹ƒoƒCƒg•¶Žš—ñ
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 ƒƒCƒh•¶Žš—ñ‚ð¬•¶Žš‚É•ÏŠ·
177 * \param str •ÏŠ·‘Ώۂ̕¶Žš
178 * \return str ‚ÉŠÜ‚Ü‚ê‚é‘啶Žš‚ð¬•¶Žš‚É•ÏŠ·‚µ‚½ƒƒCƒh•¶Žš—ñ‚ð•Ô‚·
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 ƒƒCƒh•¶Žš—ñ‚ð‘啶Žš‚É•ÏŠ·
214 * \param str •ÏŠ·‘Ώۂ̕¶Žš
215 * \return str ‚ÉŠÜ‚Ü‚ê‚鏬•¶Žš‚ð‘啶Žš‚É•ÏŠ·‚µ‚½ƒƒCƒh•¶Žš—ñ‚ð•Ô‚·
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’蕶Žš‚Å‹æØ‚ç‚ꂽƒŠƒXƒgo—Í
238 * \param first o—Í‚·‚éæ“ª—v‘fˆÊ’u
239 * \param last o—Í‚·‚éI’[—v‘fˆÊ’u+1
240 * \param ostr o—̓XƒgƒŠ[ƒ€
241 * \param pred Še—v‘f‚ðŽó‚¯Žæ‚èo—Í’l‚ð•Ô‚·qŒê
242 * \param delim ‹æØ•¶Žš
243 *
244 * ‚±‚̊֐”‚Í‹æŠÔ [first, last) ‚ÌŠe—v‘f‚ð pred ‚É“n‚µ‚Ä“¾‚ç‚ê‚é’l‚ð delim
245 * ‚Å‹æØ‚Á‚Ä ostr ‚ɏo—Í‚µ‚Ü‚·B\n
246 * I’[‚Ì—v‘f‚ÌŒã‚É‚Í delim ‚͏o—Í‚³‚ꂸA—v‘f‚Æ—v‘f‚ÌŠÔ‚É‚Ì‚Ý 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_
Note: See TracBrowser for help on using the repository browser.