1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright (c) 1996-1998 by Sun Microsystems, Inc. 24 * All rights reserved. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* LINTLIBRARY */ 30 31 /* 32 * wio_get.c 33 * 34 * Wide I/O Library 35 * 36 * Copyright 1990, 1995 by Mortice Kern Systems Inc. All rights reserved. 37 * 38 */ 39 40 #if M_RCSID 41 #ifndef lint 42 static char rcsID[] = 43 "$Header: /team/ps/sun_xcurses/archive/local_changes/xcurses/src/lib/" 44 "libxcurses/src/libc/wide/rcs/wio_get.c 1.3 1998/05/22 17:56:47 " 45 "cbates Exp $"; 46 #endif 47 #endif 48 49 #include <mks.h> 50 #include <errno.h> 51 #include <m_wio.h> 52 53 /* 54 * Return a wide character or WEOF for EOF or error. 55 * 56 * The function referenced by "get" is passed the pointer "object" 57 * and returns an input byte or EOF if no further data available. 58 * 59 * This mechanism is used to do conversions of byte strings or 60 * streams into wide characters without loss of information in the 61 * case of a bad multibyte character conversion. The bad multibyte 62 * sequence is passed through as individual bytes. 63 */ 64 wint_t 65 m_wio_get(t_wide_io *wio) 66 { 67 int ch; 68 wchar_t wc; 69 70 if (wio == NULL || wio->get == (int (*)(void *)) NULL) { 71 errno = EINVAL; 72 return (-1); 73 } 74 75 /* Do still have bytes available? */ 76 if (wio->_next < wio->_size) { 77 return ((wint_t)wio->_mb[wio->_next++]); 78 } 79 80 /* Read in enough bytes to convert a multibyte character. */ 81 wio->_size = 0; 82 for (wio->_next = 0; wio->_next < (int)MB_CUR_MAX; ) { 83 if ((ch = (*wio->get)(wio->object)) == EOF) { 84 break; 85 } 86 87 wio->_mb[wio->_next] = (unsigned char)ch; 88 89 wio->_size = mbtowc(&wc, (char *)wio->_mb, wio->_next + 1); 90 91 ++wio->_next; 92 93 if (0 <= wio->_size) { 94 /* Remember the number of bytes converted. */ 95 wio->_size = wio->_next; 96 97 return ((wint_t) wc); 98 } 99 } 100 101 /* 102 * If we fill the multibyte character buffer or receive an 103 * EOF without recognising a multibyte character, then we 104 * will return individual bytes from the buffer. The buffer 105 * is restored to its state before the bogus byte sequence 106 * was read. 107 */ 108 wio->_size = wio->_next; 109 wio->_next = 0; 110 111 return (0 < wio->_size ? (wint_t) wio->_mb[wio->_next++] : WEOF); 112 } 113