诅咒中的非空格字符

问题描述 投票:0回答:2

我正在尝试编写一个基本程序,以使用cursesnon-spacing character在C中打印ā(带上划线的a)。我已将语言环境设置为en_US.UTF-8,并且能够使用该语言环境打印国际语言字符。此代码仅打印不带上划线的。我也与ncurses得到类似的结果。要在屏幕上显示ā,我还需要做什么?

#include <curses.h>
#include <locale.h>
#include <wchar.h>
#include <assert.h>

int main() {  
    setlocale(LC_ALL, "");
    initscr();
    int s = 0x41;     // represents 'a'
    int ns = 0x0305; // represents COMBINING OVERLINE (a non-spacing character)

    assert(wcwidth(ns) == 0);

    wchar_t wstr[] = { s, ns, L'\0'};
    cchar_t *cc;
    int x = setcchar(cc, wstr, 0x00, 0, NULL);
    assert(x == 0);

    add_wch(cc);

    refresh();
    getch();
    endwin();
    return 0;
}
c locale ncurses curses
2个回答
0
投票

这里有多个问题。首先,将setcchar的结果存储到未初始化指针cc的随机存储器中。每当函数使用指针进行输出时,您都需要传递对象的地址将要存储结果的位置,而不是未初始化的指针变量。输出必须是足够长的数组,以存储输入中的字符数。我不确定null终止约定是什么,为了安全起见,我会使用:

cchar_t cc[3];
int x = setcchar(cc, wstr, 0x00, 0, NULL);

然后,add_wch函数仅需要添加一个字符,并根据它是空格字符还是非间距字符来替换或追加。因此,您需要为每个字符调用一次。


0
投票

curses调用需要一个指向data的指针,而不仅仅是一个指针。

这里是该程序的修复程序:

add_wch

在xterm上产生带有横线的“ A”:

> diff -u foo.c.orig foo.c --- foo.c.orig 2020-05-21 19:50:48.000000000 -0400 +++ foo.c 2020-05-21 19:51:46.799849136 -0400 @@ -3,7 +3,7 @@ #include <wchar.h> #include <assert.h> -int main() { +int main(void) { setlocale(LC_ALL, ""); initscr(); int s = 0x41; // represents 'a' @@ -12,11 +12,11 @@ assert(wcwidth(ns) == 0); wchar_t wstr[] = { s, ns, L'\0'}; - cchar_t *cc; - int x = setcchar(cc, wstr, 0x00, 0, NULL); + cchar_t cc; + int x = setcchar(&cc, wstr, 0x00, 0, NULL); assert(x == 0); - add_wch(cc); + add_wch(&cc); refresh(); getch();

© www.soinside.com 2019 - 2024. All rights reserved.