chicken-users
[Top][All Lists]
Advanced

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

[Chicken-users] Mapping wchar_t strings


From: Tobia Conforto
Subject: [Chicken-users] Mapping wchar_t strings
Date: Mon, 21 Feb 2011 01:58:31 +0100

Hi fellow Chicken users

I'm write an extension for ncursesw, which uses wchar_t* strings for
unicode string operations. It will hopefully become a ncursesw egg,
with a revised API, sometime in the future!

This is what i have so far (relevant code only). It works on my setup,
but before I proceed any further, I'd like to get a confirmation that
this is indeed the best way to pass a chicken utf8 string as a
wchar_t*.

Also, is that the best way to require utf8 in an extension?

#>
#include <wchar.h>
#include <locale.h>
#include <ncursesw/ncurses.h>
<#

;; I need utf8-aware string-length
(require-extension utf8)

(require-library srfi-4)

(module ncursesw
...

(import srfi-4)

;; setup locale from environment variables--nothing works otherwise
(foreign-code "setlocale(LC_ALL, \"\");")

;; fill a pre-allocated wchar-string (u32vector) with the contents of
an utf8 c-string
(define fill-wchar-string
  (foreign-lambda* void ((u32vector vec) (c-string str) (int len))
    "mbsrtowcs((wchar_t *) vec, (const char **) &str, len, NULL);"))

;; convert an utf8 c-string into a wchar-string (zero-terminated u32vector)
(define (string->wchar-string str)
  (let* ((len (+ 1 (string-length str)))
         (vec (make-u32vector len)))
    (fill-wchar-string vec str len)
    vec))

(define-foreign-type wchar-string u32vector string->wchar-string)

(define addwstr (foreign-lambda int "addwstr" wchar-string))

...
)

Tobia



reply via email to

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