29 #define YUILogComponent "ncurses"
30 #include <yui/YUILog.h>
37 std::string NCstring::termEncoding(
"UTF-8" );
46 , hotp( std::wstring::npos )
54 NCstring::NCstring(
const NCstring & nstr )
63 NCstring::NCstring(
const std::wstring & widestr )
65 , hotp( std::wstring::npos )
72 NCstring::NCstring(
const std::string & str )
74 , hotp( std::wstring::npos )
76 bool ok = RecodeToWchar( str,
"UTF-8", &wstr );
80 yuiError() <<
"ERROR: RecodeToWchar() failed" << std::endl;
86 NCstring::NCstring(
const char * cstr )
88 , hotp( std::wstring::npos )
90 bool ok = RecodeToWchar( cstr,
"UTF-8", &wstr );
94 yuiError() <<
"ERROR: RecodeToWchar() failed" << std::endl;
100 std::ostream & operator<<( std::ostream & STREAM,
const NCstring & OBJ )
102 return STREAM << OBJ.Str() ;
123 wstr.append( nstr.wstr );
127 static iconv_t fromwchar_cd = ( iconv_t )( -1 );
128 static std::string to_name =
"";
132 bool NCstring::RecodeFromWchar(
const std::wstring & in,
const std::string & to_encoding, std::string* out )
134 iconv_t cd = ( iconv_t )( -1 );
135 static bool complained =
false;
138 if ( in.length() == 0 )
142 if ( fromwchar_cd == ( iconv_t )( -1 )
143 || to_name != to_encoding )
145 if ( fromwchar_cd != ( iconv_t )( -1 ) )
147 iconv_close( fromwchar_cd );
150 fromwchar_cd = iconv_open( to_encoding.c_str(),
"WCHAR_T" );
152 yuiDebug() <<
"iconv_open( " << to_encoding.c_str() <<
", \"WCHAR_T\" )" << std::endl;
154 if ( fromwchar_cd == ( iconv_t )( -1 ) )
158 yuiError() <<
"ERROR: iconv_open failed" << std::endl;
166 to_name = to_encoding;
172 size_t in_len = in.length() *
sizeof( std::wstring::value_type );
173 char* in_ptr = (
char * )in.data();
175 size_t tmp_size = ( in_len *
sizeof( char ) ) * 2;
179 char* tmp = (
char* ) malloc( tmp_size +
sizeof(
char ) );
185 size_t tmp_len = tmp_size;
186 *((
char* ) tmp_ptr ) =
'\0';
188 size_t iconv_ret = iconv( cd, &in_ptr, &in_len, &tmp_ptr, &tmp_len );
190 *((
char* ) tmp_ptr ) =
'\0';
191 *out += std::string( tmp );
193 if ( iconv_ret == (
size_t )( -1 ) )
197 yuiError() <<
"ERROR iconv: " << errno << std::endl;
201 if ( errno == EINVAL || errno == EILSEQ )
206 in_ptr +=
sizeof( std::wstring::value_type );
208 in_len -=
sizeof( std::wstring::value_type );
212 while ( in_len != 0 );
219 static iconv_t towchar_cd = ( iconv_t )( -1 );
220 static std::string from_name =
"";
224 bool NCstring::RecodeToWchar(
const std::string& in,
const std::string &from_encoding, std::wstring* out )
226 iconv_t cd = ( iconv_t )( -1 );
227 static bool complained =
false;
230 if ( in.length() == 0 )
234 if ( towchar_cd == ( iconv_t )( -1 )
235 || from_name != from_encoding )
237 if ( towchar_cd != ( iconv_t )( -1 ) )
239 iconv_close( towchar_cd );
242 towchar_cd = iconv_open(
"WCHAR_T", from_encoding.c_str() );
244 yuiDebug() <<
"iconv_open( \"WCHAR_T\", " << from_encoding.c_str() <<
" )" << std::endl;
246 if ( towchar_cd == ( iconv_t )( -1 ) )
250 yuiError() <<
"Error: RecodeToWchar iconv_open() failed" << std::endl;
258 from_name = from_encoding;
264 size_t in_len = in.length();
265 char* in_ptr = const_cast <
char*>( in.c_str() );
267 size_t tmp_size = in_len *
sizeof( wchar_t );
268 char* tmp = (
char* ) malloc( tmp_size +
sizeof(
wchar_t ) );
273 size_t tmp_len = tmp_size;
276 size_t iconv_ret = iconv( cd, &in_ptr, &in_len, &tmp_ptr, &tmp_len );
278 *((
wchar_t* ) tmp_ptr ) = L
'\0';
280 *out += std::wstring((
wchar_t* ) tmp );
282 if ( iconv_ret == (
size_t )( -1 ) )
289 yuiError() <<
"ERROR iconv: " << errno << std::endl;
293 if ( errno == EINVAL || errno == EILSEQ )
304 while ( in_len != 0 );
313 std::string NCstring::Str()
const
316 RecodeFromWchar( wstr,
"UTF-8", &utf8str );
323 void NCstring::getHotkey( )
const
326 hotp = std::wstring::npos;
327 const wchar_t shortcutMarker = L
'&';
328 const wchar_t replacementShortcutMarker = L
'_';
338 bool have_shortcut =
false;
339 std::wstring::size_type len = wstr.length();
341 newstr.reserve( len );
343 for (std::wstring::iterator it = wstr.begin(); it != wstr.end(); it++) {
344 if ( *it == shortcutMarker &&
345 (it + 1 != wstr.end()) ) {
349 if ( *(it+1) == shortcutMarker) {
350 newstr += shortcutMarker;
357 if ( !have_shortcut) {
358 newstr += replacementShortcutMarker;
359 have_shortcut =
true;
369 std::wstring::size_type tpos = wstr.find_first_of( replacementShortcutMarker );
371 if ( tpos != std::wstring::npos && tpos != wstr.size() - 1 )
373 size_t realpos = 0, t;
375 for ( t = 0; t < tpos; t++ )
376 realpos += wcwidth( wstr[t] );
378 wstr.erase( tpos, 1 );
389 bool NCstring::setTerminalEncoding(
const std::string & encoding )
391 if ( termEncoding != encoding )
393 yuiMilestone() <<
"Terminal encoding SET to: " << encoding << std::endl;
394 termEncoding = encoding;