Files

222 lines
5.1 KiB
C

/*
* PROJECT: ReactOS CRT library
* LICENSE: LGPL - See COPYING in the top level directory
* FILE: lib/sdk/crt/string/wcs.c
* PURPOSE: wcs* CRT functions
* PROGRAMMERS: Wine team
* Ported to ReactOS by Aleksey Bragin ([email protected])
*/
/*
* msvcrt.dll wide-char functions
*
* Copyright 1999 Alexandre Julliard
* Copyright 2000 Jon Griffiths
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <precomp.h>
#include <assert.h>
#include "wine/unicode.h"
/*********************************************************************
* _wcsnset (MSVCRT.@)
*/
wchar_t* CDECL _wcsnset( wchar_t* str, wchar_t c, size_t n )
{
wchar_t* ret = str;
while ((n-- > 0) && *str) *str++ = c;
return ret;
}
/*********************************************************************
* _wcsrev (MSVCRT.@)
*/
wchar_t* CDECL _wcsrev( wchar_t* str )
{
wchar_t* ret = str;
wchar_t* end = str + strlenW(str) - 1;
while (end > str)
{
wchar_t t = *end;
*end-- = *str;
*str++ = t;
}
return ret;
}
/*********************************************************************
* wcscoll (MSVCRT.@)
*/
int CDECL wcscoll( const wchar_t* str1, const wchar_t* str2 )
{
/* FIXME: handle collates */
return strcmpW( str1, str2 );
}
/*********************************************************************
* wcspbrk (MSVCRT.@)
*/
wchar_t* CDECL wcspbrk( const wchar_t* str, const wchar_t* accept )
{
const wchar_t* p;
while (*str)
{
for (p = accept; *p; p++) if (*p == *str) return (wchar_t*)str;
str++;
}
return NULL;
}
/*********************************************************************
* wcscpy_s (MSVCRT.@)
*/
INT CDECL wcscpy_s( wchar_t* wcDest, size_t numElement, const wchar_t *wcSrc)
{
size_t size = 0;
if(!wcDest || !numElement)
return EINVAL;
wcDest[0] = 0;
if(!wcSrc)
{
return EINVAL;
}
size = strlenW(wcSrc) + 1;
if(size > numElement)
{
return ERANGE;
}
memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
return 0;
}
/******************************************************************
* wcsncpy_s (MSVCRT.@)
*/
INT CDECL wcsncpy_s( wchar_t* wcDest, size_t numElement, const wchar_t *wcSrc,
size_t count )
{
size_t size = 0;
if (!wcDest || !numElement)
return EINVAL;
wcDest[0] = 0;
if (!wcSrc)
{
return EINVAL;
}
size = min(strlenW(wcSrc), count);
if (size >= numElement)
{
return ERANGE;
}
memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
wcDest[size] = '\0';
return 0;
}
/******************************************************************
* wcscat_s (MSVCRT.@)
*
*/
INT CDECL wcscat_s(wchar_t* dst, size_t elem, const wchar_t* src)
{
wchar_t* ptr = dst;
if (!dst || elem == 0) return EINVAL;
if (!src)
{
dst[0] = '\0';
return EINVAL;
}
/* seek to end of dst string (or elem if no end of string is found */
while (ptr < dst + elem && *ptr != '\0') ptr++;
while (ptr < dst + elem)
{
if ((*ptr++ = *src++) == '\0') return 0;
}
/* not enough space */
dst[0] = '\0';
return ERANGE;
}
/*********************************************************************
* wcsncat_s (MSVCRT.@)
*
*/
INT CDECL wcsncat_s(wchar_t *dst, size_t elem,
const wchar_t *src, size_t count)
{
size_t srclen;
wchar_t dststart;
INT ret = 0;
if (!MSVCRT_CHECK_PMT(dst != NULL) || !MSVCRT_CHECK_PMT(elem > 0))
{
return EINVAL;
}
if (!MSVCRT_CHECK_PMT(src != NULL || count == 0))
return EINVAL;
if (count == 0)
return 0;
for (dststart = 0; dststart < elem; dststart++)
{
if (dst[dststart] == '\0')
break;
}
if (dststart == elem)
{
MSVCRT_INVALID_PMT("dst[elem] is not NULL terminated\n", EINVAL);
return EINVAL;
}
if (count == _TRUNCATE)
{
srclen = strlenW(src);
if (srclen >= (elem - dststart))
{
srclen = elem - dststart - 1;
ret = STRUNCATE;
}
}
else
srclen = min(strlenW(src), count);
if (srclen < (elem - dststart))
{
memcpy(&dst[dststart], src, srclen*sizeof(wchar_t));
dst[dststart+srclen] = '\0';
return ret;
}
MSVCRT_INVALID_PMT("dst[elem] is too small", ERANGE);
dst[0] = '\0';
return ERANGE;
}