Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Unicode/UTF8 chars to a ncurses display in C

Tags:

c

utf-8

ncurses

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

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:

add_wch(characters[0]);

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

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:

#include <ncurses.h> 

Compiling with :

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

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 addch which 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.

Any thoughts?

I am using 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.

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

like image 875
ocodo Avatar asked Jan 16 '11 01:01

ocodo


3 Answers

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

Try using -lncursesw instead of -lncurses.

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

like image 187
mattinm Avatar answered Nov 12 '22 02:11

mattinm


This is not 2 characters:

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 characters[]={L'\uE030', L'\uE029'};

or

wchar_t characters[]=L"\uE030\uE029";
like image 42
R.. GitHub STOP HELPING ICE Avatar answered Nov 12 '22 03:11

R.. GitHub STOP HELPING ICE


cchar_t is defined as:

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.

like image 1
David X Avatar answered Nov 12 '22 04:11

David X