bug-ncurses
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

ncursesw + non-utf8 locales


From: Felix Natter
Subject: ncursesw + non-utf8 locales
Date: Mon, 17 Oct 2011 22:41:29 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux)

hi,

Sorry for not trying to post to address@hidden, but there's no
chance of getting an answer there :-(

I am co-developer of the typing tutor gtypist, which saves its
lesson files in UTF-8, uses mbstowcs() to convert them to wchar_t*
and outputs each character with ncurses' add_wch().

This works fine with all utf-8 locales, but not with ISO-8859-1
locales (LC_ALL=de_DE for me, because this is a ISO-8859-1 locale).

I have attached a minimal example that includes the wideaddch() and
widen() helper functions I wrote. If you choose a ISO-8859-1 locale for
setlocale(), and switch gnome-terminal to Western (ISO-8859-15, which
should be compatible), then you see only garbage on the screen.
I guess this is to be expected when you try to output wchar_t (UTF-32?)
to a 8bit console.
But what can I do to fix this?

We'd like to have a codebase that supports both UTF-8 and 8bit-locales,
and avoid a special case for 8bit locales (except for changes to helper
functions like wideaddch() and widen() of course).

I also uploaded the file in case the utf-8 encoding gets messed up
during mail transfer:
  http://home.inf.fh-brs.de/~fnatte2s/testcase.c

Thanks a lot in advance!
-- 
Felix Natter
#include <wchar.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <locale.h>
#define _XOPEN_SOURCE_EXTENDED
#include <ncursesw/curses.h>

void wideaddch(wchar_t c)
{
  cchar_t c2;
  wchar_t wc[2];
  int result;

  wc[0] = c;
  wc[1] = L'\0';
  
  result = setcchar(&c2, wc, 0, 0, NULL);
  if (result != OK)
  {
      endwin();
      printf("error in setcchar()!\n");
  }
  add_wch(&c2);
}

int mbslen(const char* str)
{
  return mbstowcs(NULL, str, 0);
}

wchar_t* widen(const char* text)
{
  int numChars = mbslen(text);
  wchar_t* wideText = malloc((numChars+1) * sizeof(wchar_t));
  int convresult = mbstowcs(wideText, text, numChars+1);
  if (convresult != numChars)
  {
      endwin();
      printf("error in mbstowcs()\n");
  }

  return wideText;
}


int main()
{
    /*setlocale(LC_ALL,"de_DE.UTF-8");*/  /* replace this with an available 
utf-8 locale */
    setlocale(LC_ALL, "de_DE");   /* replace this with an available ISO-8859-1 
locale */
    
    const wchar_t* WCS2 = widen("aäaäß°αλ");
  
    initscr();
    clear();
    refresh();

    move(0,0);
    addwstr(WCS2);

    move(1,0);
    int i;
    for (i = 0; i < wcslen(WCS2); i++)
    {
        wideaddch(WCS2[i]);
    }

    getch();
    endwin();

    return 0;
}
/*
  Local Variables:
  compile-command: "gcc -Wall -O0 -g -lncursesw -o t testcase.c"
  End:
*/

reply via email to

[Prev in Thread] Current Thread [Next in Thread]