2014-12-26 23 views
1

我正在使用以下代碼來執行大寫轉換。 我的軟件中的字符串是UTF8編碼的。以下代碼可在Windows,AIX和Linux(全部64位)上正常工作,但在Solaris上無效(SunOS 5.10 Generic_147440-01 sun4 sparc SUNW,SPARC-Enterprise)。C++ Solaris特殊字符的大寫轉換不起作用

在Solaris上,「ä」等特殊字符未轉換爲大寫字母。但是,諸如'a','b'等的ASCII字符正在被正確地轉換爲大寫字母。

void String::MakeUpperUTF8() 
{ 
    WCHAR *pwstr = GetUnicode(); // Decode UTF8 encoded string to wide char string using iconv 

    if (!pwstr) 
    { 
     return; // return if decode fails. 
    } 

    std::locale::global(std::locale("")); 
    const std::ctype<WCHAR>& f = std::use_facet< std::ctype<WCHAR> >(std::locale()); // using std //ctype facet and std locale convert string to uppercase 

    f.toupper(pwstr, pwstr + wcslen(pwstr)); 
    char *strPos = SetUnicode(pwstr, -1); // encode string back to UTF8 

} 

您能否幫我解決。我想知道爲什麼這個代碼不適用於Solaris。

+1

什麼是默認的語言環境? –

+0

你能舉例說明你得到的結果嗎?例如,如果你輸入「ä」(c3 a4),你得到的是哪個字符(和數值)而不是「Ä」(c3 84)? – SHR

+0

我的默認語言環境是en_US.UTF-8。以下是在Solaris上了把語言環境的命令:LANG =的en_US.UTF-8 LC_CTYPE = 「是en_US.UTF-8」 LC_NUMERIC = 「是en_US.UTF-8」 LC_TIME = 「是en_US.UTF-8」 LC_COLLATE =「en_US.UTF-8」 LC_MONETARY =「en_US.UTF-8」 LC_MESSAGES =「en_US.UTF-8」 LC_ALL = en_US.UTF-8 – user1565291

回答

0

謝謝你們的幫助。

我使用下面的代碼解決了我的問題。標準C++方面在Solaris上不起作用。因此我使用了towupper()Solaris API。

#if defined (SUN) || (__sun) 
     for (long i=0; i < nWLength; ++i) 
     { 
      pwstr[i] = towupper(pwstr[i]); 
     } 
    #else 
     const std::ctype<WCHAR>& f = std::use_facet< std::ctype<WCHAR> >(std::locale()); // using std ctype facet and std locale convert string to uppercase 
     f.toupper(pwstr, pwstr + nWLength); 
    #endif 

感謝, 薩米特

0

下面的代碼失敗,我已經試過除了C任何區域:

#include <locale> 
int main() { 
    std::locale::global(std::locale("")); 
    return 0; 
} 

建有(-std=c++0x似乎沒有任何效果,還試圖-ansi具有相同的結果):

g++ -ggdb -Wall -std=c++0x solaris_locale.cc 
g++ -ggdb -Wall solaris_locale.cc 

失敗:

$ locale 
LANG=en_US.UTF-8 
LC_CTYPE="en_US.UTF-8" 
LC_NUMERIC="en_US.UTF-8" 
LC_TIME="en_US.UTF-8" 
LC_COLLATE="en_US.UTF-8" 
LC_MONETARY="en_US.UTF-8" 
LC_MESSAGES="en_US.UTF-8" 
LC_ALL= 
$ ./a.out 
terminate called after throwing an instance of 'std::runtime_error' 
    what(): locale::facet::_S_create_c_locale name not valid 
Abort (core dumped) 

Wo RKS:

$ LANG=C ./a.out 

GDB回溯給出:

(gdb) bt 
#0 0xfe579265 in _lwp_kill() from /lib/libc.so.1 
#1 0xfe57218a in thr_kill() from /lib/libc.so.1 
#2 0xfe520fed in raise() from /lib/libc.so.1 
#3 0xfe4f875d in abort() from /lib/libc.so.1 
#4 0xfe7343d5 in __gnu_cxx::__verbose_terminate_handler() 
    at /builds/hudson/workspace/nightly-update/build/i386/components/gcc45/gcc-4.5.2/libstdc++-v3/libsupc++/vterminate.cc:93 
#5 0xfe7313c5 in __cxxabiv1::__terminate (
    handler=0xfe734280 <__gnu_cxx::__verbose_terminate_handler()>) 
    at /builds/hudson/workspace/nightly-update/build/i386/components/gcc45/gcc-4.5.2/libstdc++-v3/libsupc++/eh_terminate.cc:39 
#6 0xfe731422 in std::terminate() 
    at /builds/hudson/workspace/nightly-update/build/i386/components/gcc45/gcc-4.5.2/libstdc++-v3/libsupc++/eh_terminate.cc:49 
#7 0xfe731591 in __cxa_throw (obj=0x8061af0, tinfo=0xfe7652ec, 
    dest=0xfe725bb0 <~runtime_error>) 
    at /builds/hudson/workspace/nightly-update/build/i386/components/gcc45/gcc-4.5.2/libstdc++-v3/libsupc++/eh_throw.cc:83 
#8 0xfe71e927 in std::__throw_runtime_error (
    __s=0xfe735e18 "locale::facet::_S_create_c_locale name not valid") 
    at /builds/hudson/workspace/nightly-update/build/i386/components/gcc45/build/i86/i386-pc-solaris2.11/libstdc++-v3/include/bits/basic_string.h:233 
#9 0xfe72e790 in std::locale::facet::_S_create_c_locale ([email protected], 
    __s=0x8061254 "en_US.UTF-8") at c++locale.cc:66 
#10 0xfe735e18 in .LC11() from /usr/lib/libstdc++.so.6 
#11 0x00000000 in ??() 

機信息:

$ uname -a 
SunOS os 5.11 11.1 i86pc i386 i86pc 
$ g++ -v 
Using built-in specs. 
COLLECT_GCC=g++ 
COLLECT_LTO_WRAPPER=/usr/gcc/4.5/lib/gcc/i386-pc-solaris2.11/4.5.2/lto-wrapper 
Target: i386-pc-solaris2.11 
Configured with: /builds/hudson/workspace/nightly-update/build/i386/components/gcc45/gcc-4.5.2/configure CC=/ws/on11update-tools/SUNWspro/sunstudio12.1/bin/cc CXX=/ws/on11update-tools/SUNWspro/sunstudio12.1/bin/CC --prefix=/usr/gcc/4.5 --mandir=/usr/gcc/4.5/share/man --bindir=/usr/gcc/4.5/bin --libdir=/usr/gcc/4.5/lib --sbindir=/usr/gcc/4.5/sbin --infodir=/usr/gcc/4.5/share/info --libexecdir=/usr/gcc/4.5/lib --enable-languages=c,c++,fortran,objc --enable-shared --with-gmp-include=/usr/include/gmp --with-mpfr-include=/usr/include/mpfr --without-gnu-ld --with-ld=/usr/bin/ld --with-gnu-as --with-as=/usr/gnu/bin/as CFLAGS='-g -O2 ' 
Thread model: posix 
gcc version 4.5.2 (GCC) 

我會承擔的Solaris C++語言環境的支持是不完整/斷。

+0

我懷疑glibc locales!=原生locales,而libstdC++只能和glibc很好地搭配。也許你可以嘗試鏈接到glibc的相同測試,也可以使用Sun的本地編譯器。 –

+0

@ n.m。我使用OS自帶的'g ++'版本,它是機器上唯一的C/C++編譯器。我在同一臺機器上測試了'setlocale(LC_ALL,「」);'在C程序中(而不是C++),它似乎可以工作(至少''mbstowcs()'可以和UTF8一起工作)。我認爲從源代碼安裝glibc將與這個問題無關。 – kestasx

+0

從源代碼安裝glibc將浪費時間,因爲glibc無法在Solaris上構建/使用 - 它不支持Solaris系統調用,僅支持Linux系統調用。 – alanc