/* EWL
 * Copyright © 1995-2007 Freescale Corporation.  All rights reserved.
 *
 * $Date: 2012/06/01 16:00:33 $
 * $Revision: 1.1 $
 */

/*
 *	Routines
 *	--------
 *
 *		wcscspn
 */

#include <ansi_parms.h>
_MISRA_EXCEPTION_RULE_19_6()
#ifdef __STDC_WANT_LIB_EXT1__
/* enforce __STDC_WANT_LIB_EXT1__ support */
#undef __STDC_WANT_LIB_EXT1__
#endif
_MISRA_RESTORE()
#define __STDC_WANT_LIB_EXT1__ 1

#if _EWL_WIDE_CHAR

#include <ewl_misra_types.h>
#include <errno.h>
MISRA_EXCEPTION_RULE_20_9()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>

MISRA_ALLOW_POINTER_CASTS()

static int_t tst_char_map(const wchar_t *cm, wchar_t c)
{
	wchar_t	xc, uc = c;

#if !defined(__POWERPC__)

	const wchar_t *str = cm;

    MISRA_EXCEPTION_RULE_13_1()
    while ((xc = *str++)) {
         if (xc == uc) {
			MISRA_EXCEPTION_RULE_14_7()
         	return 1;
         }
    }
	return 0;

#else

	const wchar_t *str = cm - 1;

    MISRA_EXCEPTION_RULE_13_1()
    while ((xc = *++str)) {
         if (xc == uc) {
			MISRA_EXCEPTION_RULE_14_7()
         	return 1;
         }
    }
	return 0;

#endif
}

size_t _EWL_CDECL wcscspn(const wchar_t * str, const wchar_t * set)
{
	const wchar_t *	p;
	      wchar_t	c;

#if !defined(__POWERPC__)

	p = (wchar_t *) str;
	MISRA_EXCEPTION_RULE_13_1()
	while ((c = *p++)) {
		if (tst_char_map(set, c)) {
			break;
		}
	}
	MISRA_EXCEPTION_RULE_10_3()
	MISRA_EXCEPTION_RULE_17_3()
	return (size_t)((p - str) - 1);

#else

	p = (wchar_t *) str - 1;
	MISRA_EXCEPTION_RULE_13_1()
	while ((c = *++p)) {
		if (tst_char_map(set, c)) {
			break;
		}
	}
	MISRA_EXCEPTION_RULE_10_3()
	MISRA_EXCEPTION_RULE_17_3()
	return (size_t)(p - str);

#endif
}

#endif /* #if _EWL_WIDE_CHAR */
