C语言 将 Unicode/UTF8 字符添加到 C 中的 ncurses 显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4703168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:32:15  来源:igfitidea点击:

Adding Unicode/UTF8 chars to a ncurses display in C

cutf-8ncurses

提问by ocodo

I'm attempting to add wchar_t Unicode characters to an ncurses display in C.

我正在尝试将 wchar_t Unicode 字符添加到 C 中的 ncurses 显示中。

I have an array:

我有一个数组:

wchar_t characters[]={L'\uE030', L'\uE029'}; // containing 2 thai letters, for example 

And I later try to add a wchar_t from the array to the ncurses display with:

然后我尝试从数组中添加一个 wchar_t 到 ncurses 显示:

add_wch(characters[0]);

To provide a bit more info, doing this with ASCII works ok, using:

为了提供更多信息,使用 ASCII 执行此操作可以正常工作,使用:

char characters[]={'A', 'B'};

// and later...

addch(characters[0]);

To setup the locale, I add the include...

要设置语言环境,我添加了包含...

#include <locale.h>

// in main()
setlocale(LC_CTYPE,"C-UTF-8");

The ncurses include is:

ncurses 包括:

#include <ncurses.h> 

Compiling with :

编译:

(edit: added c99 standard, for universal char name support.)

(编辑:添加了 c99 标准,用于通用字符名称支持。)

gcc -o ncursesutf8 ncursesutf8.c -lm -lncurses -Wall -std=c99

I get the following compilation warning (of course the executable will fail):

我收到以下编译警告(当然可执行文件会失败):

ncursesutf8.c:48: warning: implicit declaration of function ‘add_wch'

I've tried just using addchwhich appears to be macro'ed to work with wchar_t but when I do that the Unicode chars do not show up, instead they show as ASCII chars instead.

我试过只使用addch似乎被宏处理过的 wchar_t 但是当我这样做时,Unicode 字符没有显示出来,而是显示为 ASCII 字符。

Any thoughts?

有什么想法吗?

I am using OS X Snow Leopard, 10.6.6

我使用的是 OS X Snow Leopard,10.6.6

Edit: removed error on wchar_t []assignment to use L'\u0E30'instead of L"\u0E30"etc. I've also updated the compiler settings to use C99 (to add universal char name support). both changes do not fix the problem.

编辑:删除wchar_t []了使用L'\u0E30'而不是L"\u0E30"等的分配错误。我还更新了编译器设置以使用 C99(添加通用字符名称支持)。两种更改都不能解决问题。

Still no answers on this, does anyone know how to do Unicode ncurses addchar(add_wchar?) ?! Help!

仍然没有答案,有谁知道如何做 Unicode ncurses addchar(add_wchar?) ?!帮助!

回答by mattinm

The wide character support is handled by ncursesw. Depending on your distro, ncurses may or may not point there (seemingly not in yours).

宽字符支持由 ncursesw 处理。根据您的发行版,ncurses 可能会也可能不会指向那里(似乎不在您的发行版中)。

Try using -lncurseswinstead of -lncurses.

尝试使用-lncursesw代替-lncurses.

Also, for the locale, try calling setlocale(LC_ALL, "")

另外,对于语言环境,请尝试调用 setlocale(LC_ALL, "")

回答by R.. GitHub STOP HELPING ICE

This is not2 characters:

不是2 个字符:

wchar_t characters[]={L"\uE030", L"\uE029"};

You're trying to initialize wchar_t(integer) values with pointers, which should result in an error from the compiler. Either use:

您正在尝试wchar_t使用指针初始化(整数)值,这应该会导致编译器出错。要么使用:

wchar_t characters[]={L'\uE030', L'\uE029'};

or

或者

wchar_t characters[]=L"\uE030\uE029";

回答by David X

cchar_tis defined as:

cchar_t定义为:

typedef struct {
    attr_t  attr;
    wchar_t chars[CCHARW_MAX];
} cchar_t;

so you might try:

所以你可以尝试:

int add_wchar(int c)
{
    cchar_t t = {
        0, // .attr
        {c, 0} // not sure how .chars works, so best guess
    };
    return add_wch(t);
}

not at all tested, but should work.

根本没有经过测试,但应该可以工作。

回答by joshbodily

Did you define _XOPEN_SOURCE_EXTENDEDbefore including the ncurses header?

_XOPEN_SOURCE_EXTENDED在包含 ncurses 头文件之前定义了吗?