/*
 * |-----------------------------------------------------------|
 * | Copyright (c) 1990 MIPS Computer Systems, Inc.            |
 * | All Rights Reserved                                       |
 * |-----------------------------------------------------------|
 * |          Restricted Rights Legend                         |
 * | Use, duplication, or disclosure by the Government is      |
 * | subject to restrictions as set forth in                   |
 * | subparagraph (c)(1)(ii) of the Rights in Technical        |
 * | Data and Computer Software Clause of DFARS 52.227-7013.   |
 * |         MIPS Computer Systems, Inc.                       |
 * |         928 Arques Avenue                                 |
 * |         Sunnyvale, CA 94086                               |
 * |-----------------------------------------------------------|
 */
#ident	"$Header: wssize.c,v 1.1.2.1 92/06/24 14:11:16 marker Exp $"

/*	Copyright (c) 1986 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/

/*	This module is created for NLS on Sep.03.86		*/

/*	This module is modified for MNLS 3.2 on Jan 18 1989	*/

#ident	"@(#)libw-port:wstring/wssize.c	1.0"

/*
 * Wssize returns number of bytes in EUC for wchar_t string.
 */

#include "shlib.h"
#include <widec.h>
/*	start for MNLS 3.2 by UNIX Pacific on Jan 18 1989	*/
#include <euc.h>
/*	end for MNLS 3.2	*/
#include "pcode.h"

extern int  _cswidth[];
extern void _xgetwidth();
/*	start for MNLS 3.2 by UNIX Pacific on Jan 18 1989	*/
extern eucwidth_t _eucwidth;
#define pcw _eucwidth._pcw	/* use for checking wchar_t is 2 or 4 bytes	*/
/*	end for MNLS 3.2	*/

int
_wssize(s)
register wchar_t *s;
{
	register size = 0;

	if (! *_cswidth)
		_xgetwidth(); /* get character width */

	while (*s) { /* end if wchar_t NULL character */
		/*
		 * "Switch" on a pattern of "00", "11", "01", or "10",
		 * depending on high bits of low two bytes of Process Code.
		 */
/*	start for MNLS 3.2 by UNIX Pacific on Jan 18 1989	*/
		if (pcw == sizeof(unsigned short))
/*	end for MNLS 3.2	*/
		switch (*s & EUCMASK) {
			case P00: /* Code Set 0 */
				size += 1;
				break;
			case P11: /* Code Set 1 */
				size += _cswidth[1];
				break;
			case P01: /* Code Set 2 */
				if (control1(*s)) { /* function character 1 */
					size += 1;
					break;
				}
				size += _cswidth[2] + 1;
				break;
			case P10: /* Code Set 3 */
				size += _cswidth[3] + 1;
		}
/*	start for MNLS 3.2 by UNIX Pacific on Jan 18 1989	*/
		else
		switch (*s & F_EUCMASK) {
			case F_P00: /* Code Set 0 */
				size += 1;
				break;
			case F_P11: /* Code Set 1 */
				size += _cswidth[1];
				break;
			case F_P01: /* Code Set 2 */
				if (control1(*s)) { /* function character 1 */
					size += 1;
					break;
				}
				size += _cswidth[2] + 1;
				break;
			case F_P10: /* Code Set 3 */
				size += _cswidth[3] + 1;
		}
/*	end for MNLS 3.2	*/
		s++; /* next wchar_t */
	}
	return(size);
}
