123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581 |
- //! wchar implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/wchar.h.html
- use core::{mem, ptr, usize};
- use va_list::VaList as va_list;
- use header::stdio::*;
- use header::stdlib::MB_CUR_MAX;
- use header::string;
- use header::time::*;
- use platform;
- use platform::types::*;
- mod utf8;
- const WEOF: wint_t = 0xFFFF_FFFFu32;
- #[repr(C)]
- #[derive(Clone, Copy)]
- pub struct mbstate_t;
- #[no_mangle]
- pub unsafe extern "C" fn btowc(c: c_int) -> wint_t {
- //Check for EOF
- if c == EOF {
- return WEOF;
- }
- let uc = c as u8;
- let c = uc as c_char;
- let mut ps: mbstate_t = mbstate_t;
- let mut wc: wchar_t = 0;
- let saved_errno = platform::errno;
- let status = mbrtowc(&mut wc, &c as (*const c_char), 1, &mut ps);
- if status == usize::max_value() || status == usize::max_value() - 1 {
- platform::errno = saved_errno;
- return WEOF;
- }
- wc as wint_t
- }
- #[no_mangle]
- pub unsafe extern "C" fn fgetwc(stream: *mut FILE) -> wint_t {
- //TODO: Real multibyte
- btowc(fgetc(stream))
- }
- #[no_mangle]
- pub unsafe extern "C" fn fgetws(ws: *mut wchar_t, n: c_int, stream: *mut FILE) -> *mut wchar_t {
- //TODO: lock
- let mut i = 0;
- while ((i + 1) as c_int) < n {
- let wc = fgetwc(stream);
- if wc == WEOF {
- return ptr::null_mut();
- }
- *ws.add(i) = wc as wchar_t;
- i += 1;
- }
- while (i as c_int) < n {
- *ws.add(i) = 0;
- i += 1;
- }
- ws
- }
- #[no_mangle]
- pub unsafe extern "C" fn fputwc(wc: wchar_t, stream: *mut FILE) -> wint_t {
- //Convert wchar_t to multibytes first
- static mut INTERNAL: mbstate_t = mbstate_t;
- let mut bytes: [c_char; MB_CUR_MAX as usize] = [0; MB_CUR_MAX as usize];
- let amount = wcrtomb(bytes.as_mut_ptr(), wc, &mut INTERNAL);
- for i in 0..amount {
- fputc(bytes[i] as c_int, &mut *stream);
- }
- wc as wint_t
- }
- #[no_mangle]
- pub unsafe extern "C" fn fputws(ws: *const wchar_t, stream: *mut FILE) -> c_int {
- let mut i = 0;
- loop {
- let wc = *ws.add(i);
- if wc == 0 {
- return 0;
- }
- if fputwc(wc, stream) == WEOF {
- return -1;
- }
- i += 1;
- }
- }
- // #[no_mangle]
- pub extern "C" fn fwide(stream: *mut FILE, mode: c_int) -> c_int {
- unimplemented!();
- }
- #[no_mangle]
- pub unsafe extern "C" fn getwc(stream: *mut FILE) -> wint_t {
- fgetwc(stream)
- }
- #[no_mangle]
- pub unsafe extern "C" fn getwchar() -> wint_t {
- fgetwc(stdin)
- }
- #[no_mangle]
- pub unsafe extern "C" fn mbsinit(ps: *const mbstate_t) -> c_int {
- //Add a check for the state maybe
- if ps.is_null() {
- 1
- } else {
- 0
- }
- }
- #[no_mangle]
- pub unsafe extern "C" fn mbrlen(s: *const c_char, n: size_t, ps: *mut mbstate_t) -> size_t {
- static mut INTERNAL: mbstate_t = mbstate_t;
- mbrtowc(ptr::null_mut(), s, n, &mut INTERNAL)
- }
- //Only works for UTF8 at the moment
- #[no_mangle]
- pub unsafe extern "C" fn mbrtowc(
- pwc: *mut wchar_t,
- s: *const c_char,
- n: size_t,
- ps: *mut mbstate_t,
- ) -> size_t {
- static mut INTERNAL: mbstate_t = mbstate_t;
- if ps.is_null() {
- let ps = &mut INTERNAL;
- }
- if s.is_null() {
- let xs: [c_char; 1] = [0];
- utf8::mbrtowc(pwc, &xs[0] as *const c_char, 1, ps)
- } else {
- utf8::mbrtowc(pwc, s, n, ps)
- }
- }
- //Convert a multibyte string to a wide string with a limited amount of bytes
- //Required for in POSIX.1-2008
- #[no_mangle]
- pub unsafe extern "C" fn mbsnrtowcs(
- dst_ptr: *mut wchar_t,
- src_ptr: *mut *const c_char,
- src_len: size_t,
- dst_len: size_t,
- ps: *mut mbstate_t,
- ) -> size_t {
- static mut INTERNAL: mbstate_t = mbstate_t;
- if ps.is_null() {
- let ps = &mut INTERNAL;
- }
- let mut src = *src_ptr;
- let mut dst_offset: usize = 0;
- let mut src_offset: usize = 0;
- while (dst_ptr.is_null() || dst_offset < dst_len) && src_offset < src_len {
- let ps_copy = *ps;
- let mut wc: wchar_t = 0;
- let amount = mbrtowc(&mut wc, src.add(src_offset), src_len - src_offset, ps);
- // Stop in the event a decoding error occured.
- if amount == -1isize as usize {
- *src_ptr = src.add(src_offset);
- return 1isize as usize;
- }
- // Stop decoding early in the event we encountered a partial character.
- if amount == -2isize as usize {
- *ps = ps_copy;
- break;
- }
- // Store the decoded wide character in the destination buffer.
- if !dst_ptr.is_null() {
- *dst_ptr.add(dst_offset) = wc;
- }
- // Stop decoding after decoding a null character and return a NULL
- // source pointer to the caller, not including the null character in the
- // number of characters stored in the destination buffer.
- if wc == 0 {
- src = ptr::null();
- src_offset = 0;
- break;
- }
- dst_offset += 1;
- src_offset += amount;
- }
- *src_ptr = src.add(src_offset);
- dst_offset
- }
- //Convert a multibyte string to a wide string
- #[no_mangle]
- pub unsafe extern "C" fn mbsrtowcs(
- dst: *mut wchar_t,
- src: *mut *const c_char,
- len: size_t,
- ps: *mut mbstate_t,
- ) -> size_t {
- mbsnrtowcs(dst, src, size_t::max_value(), len, ps)
- }
- #[no_mangle]
- pub unsafe extern "C" fn putwc(wc: wchar_t, stream: *mut FILE) -> wint_t {
- fputwc(wc, &mut *stream)
- }
- #[no_mangle]
- pub unsafe extern "C" fn putwchar(wc: wchar_t) -> wint_t {
- fputwc(wc, &mut *stdout)
- }
- // #[no_mangle]
- pub extern "C" fn swprintf(
- s: *mut wchar_t,
- n: size_t,
- format: *const wchar_t,
- ap: va_list,
- ) -> c_int {
- unimplemented!();
- }
- // #[no_mangle]
- pub extern "C" fn swscanf(s: *const wchar_t, format: *const wchar_t, ap: va_list) -> c_int {
- unimplemented!();
- }
- // #[no_mangle]
- pub extern "C" fn towlower(wc: wint_t) -> wint_t {
- unimplemented!();
- }
- // #[no_mangle]
- pub extern "C" fn towupper(wc: wint_t) -> wint_t {
- unimplemented!();
- }
- // #[no_mangle]
- pub extern "C" fn ungetwc(wc: wint_t, stream: *mut FILE) -> wint_t {
- unimplemented!();
- }
- // #[no_mangle]
- pub extern "C" fn vfwprintf(stream: *mut FILE, format: *const wchar_t, arg: va_list) -> c_int {
- unimplemented!();
- }
- // #[no_mangle]
- pub extern "C" fn vwprintf(format: *const wchar_t, arg: va_list) -> c_int {
- unimplemented!();
- }
- // #[no_mangle]
- pub extern "C" fn vswprintf(
- s: *mut wchar_t,
- n: size_t,
- format: *const wchar_t,
- arg: va_list,
- ) -> c_int {
- unimplemented!();
- }
- //widechar to multibyte
- #[no_mangle]
- pub unsafe extern "C" fn wcrtomb(s: *mut c_char, wc: wchar_t, ps: *mut mbstate_t) -> size_t {
- let mut buffer: [c_char; MB_CUR_MAX as usize] = [0; MB_CUR_MAX as usize];
- let (s_cpy, wc_cpy) = if s.is_null() {
- (buffer.as_mut_ptr(), 0)
- } else {
- (s, wc)
- };
- utf8::wcrtomb(s_cpy, wc_cpy, ps)
- }
- #[no_mangle]
- pub unsafe extern "C" fn wcscat(ws1: *mut wchar_t, ws2: *const wchar_t) -> *mut wchar_t {
- wcsncat(ws1, ws2, usize::MAX)
- }
- #[no_mangle]
- pub unsafe extern "C" fn wcschr(ws: *const wchar_t, wc: wchar_t) -> *mut wchar_t {
- let mut i = 0;
- loop {
- if *ws.add(i) == wc {
- return ws.add(i) as *mut wchar_t;
- } else if *ws.add(i) == 0 {
- return 0 as *mut wchar_t;
- }
- i += 1;
- }
- }
- #[no_mangle]
- pub unsafe extern "C" fn wcscmp(ws1: *const wchar_t, ws2: *const wchar_t) -> c_int {
- wcsncmp(ws1, ws2, usize::MAX)
- }
- #[no_mangle]
- pub unsafe extern "C" fn wcscoll(ws1: *const wchar_t, ws2: *const wchar_t) -> c_int {
- //TODO: locale comparison
- wcscmp(ws1, ws2)
- }
- #[no_mangle]
- pub unsafe extern "C" fn wcscpy(ws1: *mut wchar_t, ws2: *const wchar_t) -> *mut wchar_t {
- let mut i = 0;
- loop {
- let wc = *ws2.add(i);
- *ws1.add(i) = wc;
- i += 1;
- if wc == 0 {
- return ws1;
- }
- }
- }
- #[no_mangle]
- pub unsafe extern "C" fn wcscspn(ws1: *const wchar_t, ws2: *const wchar_t) -> size_t {
- let mut i = 0;
- loop {
- let wc = *ws1.add(i);
- if wc == 0 || wcschr(ws2, wc) != 0 as *mut wchar_t {
- return i;
- }
- i += 1;
- }
- }
- // #[no_mangle]
- pub extern "C" fn wcsftime(
- wcs: *mut wchar_t,
- maxsize: size_t,
- format: *const wchar_t,
- timptr: *mut tm,
- ) -> size_t {
- unimplemented!();
- }
- #[no_mangle]
- pub unsafe extern "C" fn wcslen(ws: *const wchar_t) -> c_ulong {
- let mut i = 0;
- loop {
- if *ws.add(i) == 0 {
- return i as c_ulong;
- }
- i += 1;
- }
- }
- #[no_mangle]
- pub unsafe extern "C" fn wcsncat(
- ws1: *mut wchar_t,
- ws2: *const wchar_t,
- n: size_t,
- ) -> *mut wchar_t {
- let len = wcslen(ws1);
- let dest = ws1.add(len as usize);
- let mut i = 0;
- while i < n {
- let wc = *ws2.add(i);
- if wc == 0 {
- break;
- }
- *dest.add(i) = wc;
- i += 1;
- }
- *dest.add(i) = 0;
- ws1
- }
- #[no_mangle]
- pub unsafe extern "C" fn wcsncmp(ws1: *const wchar_t, ws2: *const wchar_t, n: size_t) -> c_int {
- let mut i = 0;
- while i < n {
- let wc1 = *ws1.add(i);
- let wc2 = *ws2.add(i);
- if wc1 != wc2 {
- return wc1 - wc2;
- } else if wc1 == 0 {
- break;
- }
- i += 1;
- }
- 0
- }
- #[no_mangle]
- pub unsafe extern "C" fn wcsncpy(
- ws1: *mut wchar_t,
- ws2: *const wchar_t,
- n: size_t,
- ) -> *mut wchar_t {
- let mut i = 0;
- while i < n {
- let wc = *ws2.add(i);
- *ws1.add(i) = wc;
- i += 1;
- if wc == 0 {
- break;
- }
- }
- while i < n {
- *ws1.add(i) = 0;
- i += 1;
- }
- ws1
- }
- // #[no_mangle]
- pub extern "C" fn wcspbrk(ws1: *const wchar_t, ws2: *const wchar_t) -> *mut wchar_t {
- unimplemented!();
- }
- // #[no_mangle]
- pub extern "C" fn wcsrchr(ws1: *const wchar_t, ws2: wchar_t) -> *mut wchar_t {
- unimplemented!();
- }
- // #[no_mangle]
- pub extern "C" fn wcsrtombs(
- dst: *mut c_char,
- src: *mut *const wchar_t,
- len: size_t,
- ps: *mut mbstate_t,
- ) -> size_t {
- unimplemented!();
- }
- // #[no_mangle]
- pub extern "C" fn wcsspn(ws1: *const wchar_t, ws2: *const wchar_t) -> size_t {
- unimplemented!();
- }
- // #[no_mangle]
- pub extern "C" fn wcsstr(ws1: *const wchar_t, ws2: *const wchar_t) -> *mut wchar_t {
- unimplemented!();
- }
- // #[no_mangle]
- pub extern "C" fn wcstod(nptr: *const wchar_t, endptr: *mut *mut wchar_t) -> f64 {
- unimplemented!();
- }
- // #[no_mangle]
- pub extern "C" fn wcstok(
- ws1: *mut wchar_t,
- ws2: *const wchar_t,
- ptr: *mut *mut wchar_t,
- ) -> *mut wchar_t {
- unimplemented!();
- }
- // #[no_mangle]
- pub extern "C" fn wcstol(nptr: *const wchar_t, endptr: *mut *mut wchar_t, base: c_int) -> c_long {
- unimplemented!();
- }
- // #[no_mangle]
- pub extern "C" fn wcstoul(nptr: *const wchar_t, endptr: *mut *mut wchar_t, base: c_int) -> c_ulong {
- unimplemented!();
- }
- // #[no_mangle]
- pub extern "C" fn wcswcs(ws1: *const wchar_t, ws2: *const wchar_t) -> *mut wchar_t {
- unimplemented!();
- }
- // #[no_mangle]
- pub extern "C" fn wcswidth(pwcs: *const wchar_t, n: size_t) -> c_int {
- unimplemented!();
- }
- // #[no_mangle]
- pub extern "C" fn wcsxfrm(ws1: *mut wchar_t, ws2: *const wchar_t, n: size_t) -> size_t {
- unimplemented!();
- }
- #[no_mangle]
- pub extern "C" fn wctob(c: wint_t) -> c_int {
- if c <= 0x7F {
- c as c_int
- } else {
- EOF
- }
- }
- // #[no_mangle]
- pub extern "C" fn wcwidth(wc: wchar_t) -> c_int {
- unimplemented!();
- }
- #[no_mangle]
- pub unsafe extern "C" fn wmemchr(ws: *const wchar_t, wc: wchar_t, n: size_t) -> *mut wchar_t {
- let mut i = 0;
- while i < n {
- if *ws.add(i) == wc {
- return ws.add(i) as *mut wchar_t;
- }
- i += 1;
- }
- 0 as *mut wchar_t
- }
- #[no_mangle]
- pub unsafe extern "C" fn wmemcmp(ws1: *const wchar_t, ws2: *const wchar_t, n: size_t) -> c_int {
- let mut i = 0;
- while i < n {
- let wc1 = *ws1.add(i);
- let wc2 = *ws2.add(i);
- if wc1 != wc2 {
- return wc1 - wc2;
- }
- i += 1;
- }
- 0
- }
- #[no_mangle]
- pub unsafe extern "C" fn wmemcpy(
- ws1: *mut wchar_t,
- ws2: *const wchar_t,
- n: size_t,
- ) -> *mut wchar_t {
- string::memcpy(
- ws1 as *mut c_void,
- ws2 as *const c_void,
- n * mem::size_of::<wchar_t>(),
- ) as *mut wchar_t
- }
- #[no_mangle]
- pub unsafe extern "C" fn wmemmove(
- ws1: *mut wchar_t,
- ws2: *const wchar_t,
- n: size_t,
- ) -> *mut wchar_t {
- string::memmove(
- ws1 as *mut c_void,
- ws2 as *const c_void,
- n * mem::size_of::<wchar_t>(),
- ) as *mut wchar_t
- }
- #[no_mangle]
- pub unsafe extern "C" fn wmemset(ws: *mut wchar_t, wc: wchar_t, n: size_t) -> *mut wchar_t {
- let mut i = 0;
- while i < n {
- *ws.add(i) = wc;
- i += 1;
- }
- ws
- }
- // #[no_mangle]
- pub extern "C" fn wprintf(format: *const wchar_t, ap: va_list) -> c_int {
- unimplemented!();
- }
- // #[no_mangle]
- pub extern "C" fn wscanf(format: *const wchar_t, ap: va_list) -> c_int {
- unimplemented!();
- }
|