ncurses.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Wed, 26 May 2010 14:56:44 +0200
changeset 126 52722ac7693f
parent 118 17dff2c6fc88
permissions -rw-r--r--
mysql_db: get not more than num_fields fields from a row I haven't tested the old approach, because i have never installed or used mysql. But it's nice to see that it actually works with this small patch. committer: Markus Bröker <mbroeker@largo.homelinux.org>

/**
 * ncurses.c
 * Copyright (C) 2008 Markus Broeker
 *
 * Unicode != UTF-8
 * unicode with ncursesw ( wide char)
 * gcc $< -lncursesw -o $@
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <ncurses.h>
#include <wchar.h>
#include <locale.h>

#ifndef u_char
typedef unsigned char u_char;
#endif

#define ESCAPE 0x1B

#ifndef get_wch
extern int get_wch ();
#endif

int check_locale ()
{
    if (!setlocale (LC_CTYPE, "")) {
        perror ("SETLOCALE");
        exit (EXIT_FAILURE);
    }
    return EXIT_SUCCESS;
}

wchar_t func (WINDOW * win)
{
    wchar_t guess = '\0';

    char dest[6];

    int maxx;
    int maxy;

    while (guess != ESCAPE) {
        while ((get_wch (&guess)) != OK)
            /*
             * waiting for multibyte character
             */
            ;

        maxx = getmaxx (win);
        maxy = getmaxy (win);

        memset (&dest, 0, sizeof (dest));
        if (wctomb (dest, guess) == -1) {
            perror ("WCTOMB");
            return -1;
        }

        mvprintw (maxy / 2, maxx / 2 - 12,
                  "UTF-8: (%2X:%2X:%2X:%2X)\t\t", (u_char) dest[0],
                  (u_char) dest[1], (u_char) dest[2], (u_char) dest[3]);

        mvprintw (maxy - 1, 0, "KEY: %2lc\t", guess);
        mvprintw (maxy - 1, maxx - 14, "UNICODE: %4X", guess);
        mvprintw (maxy - 1, maxx / 2 - 10, "Press ESC to quit");
    }
    return guess;
}

int main ()
{
    wchar_t x;

    WINDOW *win;

    check_locale ();
    win = initscr ();
    keypad (win, 1);
    noecho ();

    x = func (win);

    endwin ();
    printf ("\n");

    return EXIT_SUCCESS;
}