[54] | 1 | /*
|
---|
| 2 | * TOPPERS Software
|
---|
| 3 | * Toyohashi Open Platform for Embedded Real-Time Systems
|
---|
| 4 | *
|
---|
| 5 | * Copyright (C) 2005-2008 by TAKAGI Nobuhisa
|
---|
| 6 | *
|
---|
| 7 | * 上記著作権者は,以下の(1)〜(4)の条件を満たす場合に限り,本ソフトウェ
|
---|
| 8 | * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
|
---|
| 9 | * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
|
---|
| 10 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
| 11 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
| 12 | * スコード中に含まれていること.
|
---|
| 13 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
| 14 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
| 15 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
| 16 | * の無保証規定を掲載すること.
|
---|
| 17 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
| 18 | * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
|
---|
| 19 | * と.
|
---|
| 20 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
| 21 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
| 22 | * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
|
---|
| 23 | * 報告すること.
|
---|
| 24 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
| 25 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
| 26 | * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
|
---|
| 27 | * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
|
---|
| 28 | * 免責すること.
|
---|
| 29 | *
|
---|
| 30 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
| 31 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
|
---|
| 32 | * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
|
---|
| 33 | * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
|
---|
| 34 | * の責任を負わない.
|
---|
| 35 | *
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | /*!
|
---|
| 39 | * \file toppers/misc.hpp
|
---|
| 40 | * \brief 雑多なライブラリのための宣言定義
|
---|
| 41 | */
|
---|
| 42 | #ifndef TOPPERS_MISC_HPP_
|
---|
| 43 | #define TOPPERS_MISC_HPP_
|
---|
| 44 |
|
---|
| 45 | #include <stdexcept>
|
---|
| 46 | #include <locale>
|
---|
| 47 | #include <iosfwd>
|
---|
| 48 | #include <string>
|
---|
| 49 | #include <algorithm>
|
---|
| 50 | #include <cctype>
|
---|
| 51 | #include <cwchar>
|
---|
| 52 | #include <cwctype>
|
---|
| 53 | #include <cstdlib>
|
---|
| 54 | #include "toppers/codeset.hpp"
|
---|
| 55 | #include <boost/scoped_array.hpp>
|
---|
| 56 |
|
---|
| 57 | #if defined(_MSC_VER) || defined(__MINGW32__)
|
---|
| 58 | #include <mbstring.h>
|
---|
| 59 | #endif
|
---|
| 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 変換エラー例外クラス
|
---|
| 74 | */
|
---|
| 75 | class conversion_error : public std::runtime_error
|
---|
| 76 | {
|
---|
| 77 | public:
|
---|
| 78 | /*!
|
---|
| 79 | * \brief コンストラクタ
|
---|
| 80 | * \param what 例外原因文字列
|
---|
| 81 | */
|
---|
| 82 | explicit conversion_error( std::string const& what ) : std::runtime_error( what ) {}
|
---|
| 83 | };
|
---|
| 84 |
|
---|
| 85 | /*!
|
---|
| 86 | * \brief シングルバイト文字から CharT 型(多くは wchar_t 型)文字への変換
|
---|
| 87 | * \param ch シングルバイト文字
|
---|
| 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 シングルバイト文字列から CharT 型文字列への変換
|
---|
| 127 | * \param str シングルバイト文字列
|
---|
| 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 が大文字であれば対応する小文字を、それ以外は 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 ワイド文字列を小文字に変換
|
---|
| 177 | * \param str 変換対象の文字
|
---|
| 178 | * \return str に含まれる大文字を小文字に変換したワイド文字列を返す
|
---|
| 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 が小文字であれば対応する大文字を、それ以外は 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 ワイド文字列を大文字に変換
|
---|
| 214 | * \param str 変換対象の文字
|
---|
| 215 | * \return str に含まれる小文字を大文字に変換したワイド文字列を返す
|
---|
| 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 指定文字で区切られたリスト出力
|
---|
| 238 | * \param first 出力する先頭要素位置
|
---|
| 239 | * \param last 出力する終端要素位置+1
|
---|
| 240 | * \param ostr 出力ストリーム
|
---|
| 241 | * \param pred 各要素を受け取り出力値を返す述語
|
---|
| 242 | * \param delim 区切文字
|
---|
| 243 | *
|
---|
| 244 | * この関数は区間 [first, last) の各要素を pred に渡して得られる値を delim
|
---|
| 245 | * で区切って ostr に出力します。\n
|
---|
| 246 | * 終端の要素の後には delim は出力されず、要素と要素の間にのみ delim が
|
---|
| 247 | * 出力されます。
|
---|
| 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 | inline std::string dir_delimiter_to_slash( std::string const& str )
|
---|
| 288 | {
|
---|
| 289 | #if defined(_MSC_VER) || defined(__MINGW32__)
|
---|
| 290 | std::string result;
|
---|
| 291 | result.reserve( str.size() );
|
---|
| 292 |
|
---|
| 293 | unsigned char const* s1 = reinterpret_cast< unsigned char const* >( str.c_str() );
|
---|
| 294 | while ( unsigned char const* s2 = _mbschr( s1, '\\' ) )
|
---|
| 295 | {
|
---|
| 296 | result.append( s1, s2 );
|
---|
| 297 | result.push_back( '/' );
|
---|
| 298 | s1 = s2 + 1;
|
---|
| 299 | }
|
---|
| 300 | return result + reinterpret_cast< char const* >( s1 );
|
---|
| 301 | #else
|
---|
| 302 | return str;
|
---|
| 303 | #endif
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | #endif // ! TOPPERS_MISC_HPP_
|
---|