From 375959c2062dddb103222ff4a7fa29afc853ca74 Mon Sep 17 00:00:00 2001 From: Aleksey Bragin Date: Thu, 8 Nov 2007 21:06:20 +0000 Subject: [PATCH] - Sync some changes in strings from libcntpr. - Move string implementation (in "header" files) into /string dir. - Copy over libcntpr-specific implementations into _nt.c files (for now, later they could do with a define). - libcntpr's is now almost fully syncronized and could be merged into the tree except for "except" directory. svn path=/trunk/; revision=30283 --- reactos/lib/sdk/crt/string/itoa.c | 199 ++++++++++-------- reactos/lib/sdk/crt/string/itow.c | 101 +++++++-- reactos/lib/sdk/crt/string/mbstowcs_nt.c | 60 ++++++ reactos/lib/sdk/crt/string/strcat.c | 2 +- reactos/lib/sdk/crt/string/strchr.c | 2 +- reactos/lib/sdk/crt/string/strcmp.c | 2 +- reactos/lib/sdk/crt/string/strcpy.c | 2 +- reactos/lib/sdk/crt/string/stricmp.c | 1 - reactos/lib/sdk/crt/string/strlen.c | 2 +- reactos/lib/sdk/crt/string/strlwr.c | 9 - reactos/lib/sdk/crt/string/strncat.c | 2 +- reactos/lib/sdk/crt/string/strncmp.c | 2 +- reactos/lib/sdk/crt/string/strncpy.c | 2 +- reactos/lib/sdk/crt/string/strnicmp.c | 1 - reactos/lib/sdk/crt/string/strnlen.c | 2 +- reactos/lib/sdk/crt/string/strrchr.c | 2 +- reactos/lib/sdk/crt/string/strset.c | 10 +- reactos/lib/sdk/crt/string/strstr.c | 8 +- reactos/lib/sdk/crt/string/strtol.c | 4 +- reactos/lib/sdk/crt/string/strtoul.c | 4 +- reactos/lib/sdk/crt/string/strtoull.c | 27 +-- .../crt/{include/internal => string}/tcscat.h | 0 .../crt/{include/internal => string}/tcschr.h | 0 .../crt/{include/internal => string}/tcscmp.h | 0 .../crt/{include/internal => string}/tcscpy.h | 0 .../crt/{include/internal => string}/tcslen.h | 0 .../{include/internal => string}/tcsncat.h | 0 .../{include/internal => string}/tcsncmp.h | 0 .../{include/internal => string}/tcsncpy.h | 0 .../{include/internal => string}/tcsnlen.h | 0 .../{include/internal => string}/tcsrchr.h | 0 reactos/lib/sdk/crt/string/wcscat.c | 2 +- reactos/lib/sdk/crt/string/wcschr.c | 2 +- reactos/lib/sdk/crt/string/wcscmp.c | 2 +- reactos/lib/sdk/crt/string/wcscpy.c | 2 +- reactos/lib/sdk/crt/string/wcslen.c | 2 +- reactos/lib/sdk/crt/string/wcsncat.c | 2 +- reactos/lib/sdk/crt/string/wcsncmp.c | 2 +- reactos/lib/sdk/crt/string/wcsncpy.c | 2 +- reactos/lib/sdk/crt/string/wcsnlen.c | 2 +- reactos/lib/sdk/crt/string/wcsrchr.c | 2 +- reactos/lib/sdk/crt/string/wcstol.c | 106 +++++++--- reactos/lib/sdk/crt/string/wcstombs_nt.c | 59 ++++++ reactos/lib/sdk/crt/string/wcstoul.c | 40 +--- reactos/lib/sdk/crt/string/wtoi64.c | 44 ++-- reactos/lib/sdk/crt/string/wtol.c | 35 ++- 46 files changed, 502 insertions(+), 246 deletions(-) create mode 100644 reactos/lib/sdk/crt/string/mbstowcs_nt.c rename reactos/lib/sdk/crt/{include/internal => string}/tcscat.h (100%) rename reactos/lib/sdk/crt/{include/internal => string}/tcschr.h (100%) rename reactos/lib/sdk/crt/{include/internal => string}/tcscmp.h (100%) rename reactos/lib/sdk/crt/{include/internal => string}/tcscpy.h (100%) rename reactos/lib/sdk/crt/{include/internal => string}/tcslen.h (100%) rename reactos/lib/sdk/crt/{include/internal => string}/tcsncat.h (100%) rename reactos/lib/sdk/crt/{include/internal => string}/tcsncmp.h (100%) rename reactos/lib/sdk/crt/{include/internal => string}/tcsncpy.h (100%) rename reactos/lib/sdk/crt/{include/internal => string}/tcsnlen.h (100%) rename reactos/lib/sdk/crt/{include/internal => string}/tcsrchr.h (100%) create mode 100644 reactos/lib/sdk/crt/string/wcstombs_nt.c diff --git a/reactos/lib/sdk/crt/string/itoa.c b/reactos/lib/sdk/crt/string/itoa.c index d1840aab29d..e7cf0a960e4 100644 --- a/reactos/lib/sdk/crt/string/itoa.c +++ b/reactos/lib/sdk/crt/string/itoa.c @@ -1,112 +1,144 @@ /* * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS system libraries - * FILE: lib/msvcrt/stdlib/itoa.c - * PURPOSE: converts a integer to ascii - * PROGRAMER: + * FILE: lib/crt/?????? + * PURPOSE: Unknown + * PROGRAMER: Unknown * UPDATE HISTORY: - * 1995: Created - * 1998: Added ltoa by Ariadne + * 25/11/05: Added license header */ -/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */ - #include /* * @implemented + * copy _i64toa from wine cvs 2006 month 05 day 21 */ -char* _itoa(int value, char* string, int radix) +char * +_i64toa(__int64 value, char *string, int radix) { - char tmp[33]; - char* tp = tmp; - int i; - unsigned v; - int sign; - char* sp; + ULONGLONG val; + int negative; + char buffer[65]; + char *pos; + int digit; - if (radix > 36 || radix <= 1) - { - __set_errno(EDOM); - return 0; - } + if (value < 0 && radix == 10) { + negative = 1; + val = -value; + } else { + negative = 0; + val = value; + } /* if */ - sign = (radix == 10 && value < 0); - if (sign) - v = -value; - else - v = (unsigned)value; - while (v || tp == tmp) - { - i = v % radix; - v = v / radix; - if (i < 10) - *tp++ = i+'0'; - else - *tp++ = i + 'a' - 10; - } + pos = &buffer[64]; + *pos = '\0'; - if (string == 0) - string = (char*)malloc((tp-tmp)+sign+1); - sp = string; + do { + digit = val % radix; + val = val / radix; + if (digit < 10) { + *--pos = '0' + digit; + } else { + *--pos = 'a' + digit - 10; + } /* if */ + } while (val != 0L); - if (sign) - *sp++ = '-'; - while (tp > tmp) - *sp++ = *--tp; - *sp = 0; - return string; + if (negative) { + *--pos = '-'; + } /* if */ + + memcpy(string, pos, &buffer[64] - pos + 1); + return string; } + +/* + * @implemented + * copy _i64toa from wine cvs 2006 month 05 day 21 + */ +char * +_ui64toa(unsigned __int64 value, char *string, int radix) +{ + char buffer[65]; + char *pos; + int digit; + + pos = &buffer[64]; + *pos = '\0'; + + do { + digit = value % radix; + value = value / radix; + if (digit < 10) { + *--pos = '0' + digit; + } else { + *--pos = 'a' + digit - 10; + } /* if */ + } while (value != 0L); + + memcpy(string, pos, &buffer[64] - pos + 1); + return string; +} + + /* * @implemented */ -char* _ltoa(long value, char* string, int radix) +char * +_itoa(int value, char *string, int radix) { - char tmp[33]; - char* tp = tmp; - long i; - unsigned long v; - int sign; - char* sp; - - if (radix > 36 || radix <= 1) - { - __set_errno(EDOM); - return 0; - } - - sign = (radix == 10 && value < 0); - if (sign) - v = -value; - else - v = (unsigned long)value; - while (v || tp == tmp) - { - i = v % radix; - v = v / radix; - if (i < 10) - *tp++ = i+'0'; - else - *tp++ = i + 'a' - 10; - } - - if (string == 0) - string = (char*)malloc((tp-tmp)+sign+1); - sp = string; - - if (sign) - *sp++ = '-'; - while (tp > tmp) - *sp++ = *--tp; - *sp = 0; - return string; + return _ltoa(value, string, radix); } + /* * @implemented - * copy it from wine 0.9.0 with small modifcations do check for NULL */ -char* ultoa(unsigned long value, char* string, int radix) +char * +_ltoa(long value, char *string, int radix) +{ + unsigned long val; + int negative; + char buffer[33]; + char *pos; + int digit; + + if (value < 0 && radix == 10) { + negative = 1; + val = -value; + } else { + negative = 0; + val = value; + } /* if */ + + pos = &buffer[32]; + *pos = '\0'; + + do { + digit = val % radix; + val = val / radix; + if (digit < 10) { + *--pos = '0' + digit; + } else { + *--pos = 'a' + digit - 10; + } /* if */ + } while (val != 0L); + + if (negative) { + *--pos = '-'; + } /* if */ + + memcpy(string, pos, &buffer[32] - pos + 1); + return string; +} + + +/* + * @implemented + * copy it from wine 0.9.0 with small modifcations do check for NULL + */ +char * +_ultoa(unsigned long value, char *string, int radix) { char buffer[33]; char *pos; @@ -131,5 +163,6 @@ char* ultoa(unsigned long value, char* string, int radix) } while (value != 0L); memcpy(string, pos, &buffer[32] - pos + 1); + return string; } diff --git a/reactos/lib/sdk/crt/string/itow.c b/reactos/lib/sdk/crt/string/itow.c index 7db9da3e85a..7f6bd42f388 100644 --- a/reactos/lib/sdk/crt/string/itow.c +++ b/reactos/lib/sdk/crt/string/itow.c @@ -1,16 +1,11 @@ -/* $Id$ - * +/* * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS system libraries - * FILE: lib/msvcrt/stdlib/itow.c + * FILE: lib/sdk/crt/itow.c * PURPOSE: converts a integer to wchar_t * PROGRAMER: * UPDATE HISTORY: - * 1995: Created - * 1998: Added ltoa by Ariadne - * 2000: derived from ./itoa.c by ea */ -/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */ #include @@ -18,18 +13,96 @@ * @implemented * from wine cvs 2006-05-21 */ -wchar_t* _itow(int value, wchar_t* string, int radix) +wchar_t * +_i64tow(__int64 value, wchar_t *string, int radix) { - return _ltow(value, string, radix); + ULONGLONG val; + int negative; + WCHAR buffer[65]; + PWCHAR pos; + WCHAR digit; + + if (value < 0 && radix == 10) { + negative = 1; + val = -value; + } else { + negative = 0; + val = value; + } /* if */ + + pos = &buffer[64]; + *pos = '\0'; + + do { + digit = val % radix; + val = val / radix; + if (digit < 10) { + *--pos = '0' + digit; + } else { + *--pos = 'a' + digit - 10; + } /* if */ + } while (val != 0L); + + if (negative) { + *--pos = '-'; + } /* if */ + + if (string != NULL) { + memcpy(string, pos, (&buffer[64] - pos + 1) * sizeof(WCHAR)); + } /* if */ + return string; } + +/* + * @implemented + */ +wchar_t * +_ui64tow(unsigned __int64 value, wchar_t *string, int radix) +{ + WCHAR buffer[65]; + PWCHAR pos; + WCHAR digit; + + pos = &buffer[64]; + *pos = '\0'; + + do { + digit = value % radix; + value = value / radix; + if (digit < 10) { + *--pos = '0' + digit; + } else { + *--pos = 'a' + digit - 10; + } /* if */ + } while (value != 0L); + + if (string != NULL) { + memcpy(string, pos, (&buffer[64] - pos + 1) * sizeof(WCHAR)); + } /* if */ + return string; +} + + /* * @implemented * from wine cvs 2006-05-21 */ -wchar_t* _ltow(long value, wchar_t* string, int radix) +wchar_t * +_itow(int value, wchar_t *string, int radix) { - unsigned long val; + return _ltow(value, string, radix); +} + + +/* + * @implemented + * from wine cvs 2006-05-21 + */ +wchar_t * +_ltow(long value, wchar_t *string, int radix) +{ + unsigned long val; int negative; WCHAR buffer[33]; PWCHAR pos; @@ -60,17 +133,19 @@ wchar_t* _ltow(long value, wchar_t* string, int radix) *--pos = '-'; } /* if */ - if (str != NULL) { + if (string != NULL) { memcpy(string, pos, (&buffer[32] - pos + 1) * sizeof(WCHAR)); } /* if */ return string; } + /* * @implemented * from wine cvs 2006-05-21 */ -wchar_t* _ultow(unsigned long value, wchar_t* string, int radix) +wchar_t * +_ultow(unsigned long value, wchar_t *string, int radix) { WCHAR buffer[33]; PWCHAR pos; diff --git a/reactos/lib/sdk/crt/string/mbstowcs_nt.c b/reactos/lib/sdk/crt/string/mbstowcs_nt.c new file mode 100644 index 00000000000..8ab1edd23e3 --- /dev/null +++ b/reactos/lib/sdk/crt/string/mbstowcs_nt.c @@ -0,0 +1,60 @@ +#define WIN32_NO_STATUS +#include +#include +#include +#include + +/* + * @implemented + */ +int mbtowc (wchar_t *wchar, const char *mbchar, size_t count) +{ + NTSTATUS Status; + ULONG Size; + + if (wchar == NULL) + return 0; + + Status = RtlMultiByteToUnicodeN (wchar, + sizeof(WCHAR), + &Size, + mbchar, + count); + if (!NT_SUCCESS(Status)) + return -1; + + return (int)Size; +} + +/* + * @implemented + */ +size_t mbstowcs (wchar_t *wcstr, const char *mbstr, size_t count) +{ + NTSTATUS Status; + ULONG Size; + ULONG Length; + + Length = strlen (mbstr); + + if (wcstr == NULL) + { + RtlMultiByteToUnicodeSize (&Size, + mbstr, + Length); + + return (size_t)Size; + } + + Status = RtlMultiByteToUnicodeN (wcstr, + count, + &Size, + mbstr, + Length); + if (!NT_SUCCESS(Status)) + return -1; + + return (size_t)Size; +} + +/* EOF */ diff --git a/reactos/lib/sdk/crt/string/strcat.c b/reactos/lib/sdk/crt/string/strcat.c index 244df5fc059..59aee1a90fd 100644 --- a/reactos/lib/sdk/crt/string/strcat.c +++ b/reactos/lib/sdk/crt/string/strcat.c @@ -2,6 +2,6 @@ */ #include -#include "internal/tcscat.h" +#include "tcscat.h" /* EOF */ diff --git a/reactos/lib/sdk/crt/string/strchr.c b/reactos/lib/sdk/crt/string/strchr.c index 76bb035c60b..e504e4bcd90 100644 --- a/reactos/lib/sdk/crt/string/strchr.c +++ b/reactos/lib/sdk/crt/string/strchr.c @@ -3,6 +3,6 @@ #define _XINT int #include -#include "internal/tcschr.h" +#include "tcschr.h" /* EOF */ diff --git a/reactos/lib/sdk/crt/string/strcmp.c b/reactos/lib/sdk/crt/string/strcmp.c index 8b5ab72a22b..df8e1408196 100644 --- a/reactos/lib/sdk/crt/string/strcmp.c +++ b/reactos/lib/sdk/crt/string/strcmp.c @@ -2,6 +2,6 @@ */ #include -#include "internal/tcscmp.h" +#include "tcscmp.h" /* EOF */ diff --git a/reactos/lib/sdk/crt/string/strcpy.c b/reactos/lib/sdk/crt/string/strcpy.c index c07e1ed8170..9f09b15be97 100644 --- a/reactos/lib/sdk/crt/string/strcpy.c +++ b/reactos/lib/sdk/crt/string/strcpy.c @@ -2,6 +2,6 @@ */ #include -#include "internal/tcscpy.h" +#include "tcscpy.h" /* EOF */ diff --git a/reactos/lib/sdk/crt/string/stricmp.c b/reactos/lib/sdk/crt/string/stricmp.c index 13b6ad426df..339cdc5ac74 100644 --- a/reactos/lib/sdk/crt/string/stricmp.c +++ b/reactos/lib/sdk/crt/string/stricmp.c @@ -1,4 +1,3 @@ -/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */ #include /* diff --git a/reactos/lib/sdk/crt/string/strlen.c b/reactos/lib/sdk/crt/string/strlen.c index 4c1c4424063..7dd8d68c170 100644 --- a/reactos/lib/sdk/crt/string/strlen.c +++ b/reactos/lib/sdk/crt/string/strlen.c @@ -2,6 +2,6 @@ */ #include -#include "internal/tcslen.h" +#include "tcslen.h" /* EOF */ diff --git a/reactos/lib/sdk/crt/string/strlwr.c b/reactos/lib/sdk/crt/string/strlwr.c index 95487969cd2..e7830235f78 100644 --- a/reactos/lib/sdk/crt/string/strlwr.c +++ b/reactos/lib/sdk/crt/string/strlwr.c @@ -1,12 +1,3 @@ -/* - * The C RunTime DLL - * - * Implements C run-time functionality as known from UNIX. - * - * Copyright 1996,1998 Marcus Meissner - * Copyright 1996 Jukka Iivonen - * Copyright 1997 Uwe Bonnes - */ #include diff --git a/reactos/lib/sdk/crt/string/strncat.c b/reactos/lib/sdk/crt/string/strncat.c index 2805353a169..b2390f55da6 100644 --- a/reactos/lib/sdk/crt/string/strncat.c +++ b/reactos/lib/sdk/crt/string/strncat.c @@ -2,6 +2,6 @@ */ #include -#include "internal/tcsncat.h" +#include "tcsncat.h" /* EOF */ diff --git a/reactos/lib/sdk/crt/string/strncmp.c b/reactos/lib/sdk/crt/string/strncmp.c index 9ea20089e1d..383782206d6 100644 --- a/reactos/lib/sdk/crt/string/strncmp.c +++ b/reactos/lib/sdk/crt/string/strncmp.c @@ -2,6 +2,6 @@ */ #include -#include "internal/tcsncmp.h" +#include "tcsncmp.h" /* EOF */ diff --git a/reactos/lib/sdk/crt/string/strncpy.c b/reactos/lib/sdk/crt/string/strncpy.c index e05c4835153..c172eb7cc4d 100644 --- a/reactos/lib/sdk/crt/string/strncpy.c +++ b/reactos/lib/sdk/crt/string/strncpy.c @@ -1,6 +1,6 @@ /* $Id$ */ #include -#include "internal/tcsncpy.h" +#include "tcsncpy.h" /* EOF */ diff --git a/reactos/lib/sdk/crt/string/strnicmp.c b/reactos/lib/sdk/crt/string/strnicmp.c index d5f3897afe8..9ebe8973a00 100644 --- a/reactos/lib/sdk/crt/string/strnicmp.c +++ b/reactos/lib/sdk/crt/string/strnicmp.c @@ -1,4 +1,3 @@ -/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */ #include /* diff --git a/reactos/lib/sdk/crt/string/strnlen.c b/reactos/lib/sdk/crt/string/strnlen.c index 16fc2a13750..269c4953561 100644 --- a/reactos/lib/sdk/crt/string/strnlen.c +++ b/reactos/lib/sdk/crt/string/strnlen.c @@ -2,6 +2,6 @@ */ #include -#include "internal/tcsnlen.h" +#include "tcsnlen.h" /* EOF */ diff --git a/reactos/lib/sdk/crt/string/strrchr.c b/reactos/lib/sdk/crt/string/strrchr.c index 8c136e89cdf..152561537c8 100644 --- a/reactos/lib/sdk/crt/string/strrchr.c +++ b/reactos/lib/sdk/crt/string/strrchr.c @@ -3,6 +3,6 @@ #define _XINT int #include -#include "internal/tcsrchr.h" +#include "tcsrchr.h" /* EOF */ diff --git a/reactos/lib/sdk/crt/string/strset.c b/reactos/lib/sdk/crt/string/strset.c index 5a1cbec0bee..ee4fd708595 100644 --- a/reactos/lib/sdk/crt/string/strset.c +++ b/reactos/lib/sdk/crt/string/strset.c @@ -13,11 +13,11 @@ /* * @implemented */ -char* _strnset (char* szToFill, int szFill, size_t sizeMaxFill) +char* _strnset(char* szToFill, int szFill, size_t sizeMaxFill) { char *t = szToFill; - size_t i = 0; - while( *szToFill != 0 && i < sizeMaxFill) + int i = 0; + while (*szToFill != 0 && i < (int) sizeMaxFill) { *szToFill = szFill; szToFill++; @@ -30,10 +30,10 @@ char* _strnset (char* szToFill, int szFill, size_t sizeMaxFill) /* * @implemented */ -char* _strset (char* szToFill, int szFill) +char* _strset(char* szToFill, int szFill) { char *t = szToFill; - while( *szToFill != 0 ) + while (*szToFill != 0) { *szToFill = szFill; szToFill++; diff --git a/reactos/lib/sdk/crt/string/strstr.c b/reactos/lib/sdk/crt/string/strstr.c index c21934b2f9c..4056acd8b88 100644 --- a/reactos/lib/sdk/crt/string/strstr.c +++ b/reactos/lib/sdk/crt/string/strstr.c @@ -8,14 +8,12 @@ * 25/11/05: Added license header */ -/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */ #include - - /* * @implemented */ -char *strstr(const char *s, const char *find) +char * +strstr(const char *s, const char *find) { char c, sc; size_t len; @@ -31,5 +29,5 @@ char *strstr(const char *s, const char *find) } while (strncmp(s, find, len) != 0); s--; } - return (char *)s; + return (char *)((size_t)s); } diff --git a/reactos/lib/sdk/crt/string/strtol.c b/reactos/lib/sdk/crt/string/strtol.c index 982acdebf1b..6e6706def8e 100644 --- a/reactos/lib/sdk/crt/string/strtol.c +++ b/reactos/lib/sdk/crt/string/strtol.c @@ -1,4 +1,3 @@ -/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */ #include /* @@ -80,11 +79,10 @@ strtol(const char *nptr, char **endptr, int base) if (any < 0) { acc = neg ? LONG_MIN : LONG_MAX; - __set_errno(ERANGE); } else if (neg) acc = -acc; if (endptr != 0) - *endptr = any ? (char *)s - 1 : (char *)nptr; + *endptr = any ? (char *)((size_t)(s - 1)) : (char *)((size_t)nptr); return acc; } diff --git a/reactos/lib/sdk/crt/string/strtoul.c b/reactos/lib/sdk/crt/string/strtoul.c index 2e530f53b6d..052d57a2c93 100644 --- a/reactos/lib/sdk/crt/string/strtoul.c +++ b/reactos/lib/sdk/crt/string/strtoul.c @@ -1,4 +1,3 @@ -/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */ #include #include @@ -64,11 +63,10 @@ strtoul(const char *nptr, char **endptr, int base) if (any < 0) { acc = ULONG_MAX; - __set_errno(ERANGE); } else if (neg) acc = -acc; if (endptr != 0) - *endptr = any ? (char *)s - 1 : (char *)nptr; + *endptr = any ? (char *)((size_t)(s - 1)) : (char *)((size_t)nptr); return acc; } diff --git a/reactos/lib/sdk/crt/string/strtoull.c b/reactos/lib/sdk/crt/string/strtoull.c index d02328af92a..b3f4b476114 100644 --- a/reactos/lib/sdk/crt/string/strtoull.c +++ b/reactos/lib/sdk/crt/string/strtoull.c @@ -1,24 +1,12 @@ -/* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */ -/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */ #include -#if defined (_MSC_VER) -#define UINT64_MAX 0xffffffffffffffff -#endif - -/* - * Convert a string to an unsigned long integer. - * - * Ignores `locale' stuff. Assumes that the upper and lower case - * alphabets and digits are each contiguous. - */ -UINT64 +unsigned long long strtoull(const char *nptr, char **endptr, int base) { const char *s = nptr; - UINT64 acc; + unsigned long long acc; int c; - UINT64 cutoff; + unsigned long long cutoff; int neg = 0, any, cutlim; /* @@ -43,8 +31,8 @@ strtoull(const char *nptr, char **endptr, int base) } if (base == 0) base = c == '0' ? 8 : 10; - cutoff = UINT64_MAX / base; - cutlim = (int)(UINT64_MAX % base); + cutoff = (unsigned long long)ULLONG_MAX / (unsigned long long)base; + cutlim = (unsigned long long)ULLONG_MAX % (unsigned long long)base; for (acc = 0, any = 0;; c = *s++) { if (isdigit(c)) @@ -65,12 +53,11 @@ strtoull(const char *nptr, char **endptr, int base) } if (any < 0) { - acc = UINT64_MAX; - __set_errno ( ERANGE ); + acc = ULLONG_MAX; } else if (neg) acc = -acc; if (endptr != 0) - *endptr = any ? (char *)s - 1 : (char *)nptr; + *endptr = any ? (char *)((size_t)(s - 1)) : (char *)((size_t)nptr); return acc; } diff --git a/reactos/lib/sdk/crt/include/internal/tcscat.h b/reactos/lib/sdk/crt/string/tcscat.h similarity index 100% rename from reactos/lib/sdk/crt/include/internal/tcscat.h rename to reactos/lib/sdk/crt/string/tcscat.h diff --git a/reactos/lib/sdk/crt/include/internal/tcschr.h b/reactos/lib/sdk/crt/string/tcschr.h similarity index 100% rename from reactos/lib/sdk/crt/include/internal/tcschr.h rename to reactos/lib/sdk/crt/string/tcschr.h diff --git a/reactos/lib/sdk/crt/include/internal/tcscmp.h b/reactos/lib/sdk/crt/string/tcscmp.h similarity index 100% rename from reactos/lib/sdk/crt/include/internal/tcscmp.h rename to reactos/lib/sdk/crt/string/tcscmp.h diff --git a/reactos/lib/sdk/crt/include/internal/tcscpy.h b/reactos/lib/sdk/crt/string/tcscpy.h similarity index 100% rename from reactos/lib/sdk/crt/include/internal/tcscpy.h rename to reactos/lib/sdk/crt/string/tcscpy.h diff --git a/reactos/lib/sdk/crt/include/internal/tcslen.h b/reactos/lib/sdk/crt/string/tcslen.h similarity index 100% rename from reactos/lib/sdk/crt/include/internal/tcslen.h rename to reactos/lib/sdk/crt/string/tcslen.h diff --git a/reactos/lib/sdk/crt/include/internal/tcsncat.h b/reactos/lib/sdk/crt/string/tcsncat.h similarity index 100% rename from reactos/lib/sdk/crt/include/internal/tcsncat.h rename to reactos/lib/sdk/crt/string/tcsncat.h diff --git a/reactos/lib/sdk/crt/include/internal/tcsncmp.h b/reactos/lib/sdk/crt/string/tcsncmp.h similarity index 100% rename from reactos/lib/sdk/crt/include/internal/tcsncmp.h rename to reactos/lib/sdk/crt/string/tcsncmp.h diff --git a/reactos/lib/sdk/crt/include/internal/tcsncpy.h b/reactos/lib/sdk/crt/string/tcsncpy.h similarity index 100% rename from reactos/lib/sdk/crt/include/internal/tcsncpy.h rename to reactos/lib/sdk/crt/string/tcsncpy.h diff --git a/reactos/lib/sdk/crt/include/internal/tcsnlen.h b/reactos/lib/sdk/crt/string/tcsnlen.h similarity index 100% rename from reactos/lib/sdk/crt/include/internal/tcsnlen.h rename to reactos/lib/sdk/crt/string/tcsnlen.h diff --git a/reactos/lib/sdk/crt/include/internal/tcsrchr.h b/reactos/lib/sdk/crt/string/tcsrchr.h similarity index 100% rename from reactos/lib/sdk/crt/include/internal/tcsrchr.h rename to reactos/lib/sdk/crt/string/tcsrchr.h diff --git a/reactos/lib/sdk/crt/string/wcscat.c b/reactos/lib/sdk/crt/string/wcscat.c index fb13ae3fd76..adf0e1c7919 100644 --- a/reactos/lib/sdk/crt/string/wcscat.c +++ b/reactos/lib/sdk/crt/string/wcscat.c @@ -3,6 +3,6 @@ #define _UNICODE #include -#include "internal/tcscat.h" +#include "tcscat.h" /* EOF */ diff --git a/reactos/lib/sdk/crt/string/wcschr.c b/reactos/lib/sdk/crt/string/wcschr.c index 88a5bc1b087..20c852aaad9 100644 --- a/reactos/lib/sdk/crt/string/wcschr.c +++ b/reactos/lib/sdk/crt/string/wcschr.c @@ -4,6 +4,6 @@ #define _UNICODE #define _XINT wchar_t #include -#include "internal/tcschr.h" +#include "tcschr.h" /* EOF */ diff --git a/reactos/lib/sdk/crt/string/wcscmp.c b/reactos/lib/sdk/crt/string/wcscmp.c index f916226141e..dc2ca0d9d34 100644 --- a/reactos/lib/sdk/crt/string/wcscmp.c +++ b/reactos/lib/sdk/crt/string/wcscmp.c @@ -3,6 +3,6 @@ #define _UNICODE #include -#include "internal/tcscmp.h" +#include "tcscmp.h" /* EOF */ diff --git a/reactos/lib/sdk/crt/string/wcscpy.c b/reactos/lib/sdk/crt/string/wcscpy.c index b57a3a73b17..283d6ab6eb8 100644 --- a/reactos/lib/sdk/crt/string/wcscpy.c +++ b/reactos/lib/sdk/crt/string/wcscpy.c @@ -3,6 +3,6 @@ #define _UNICODE #include -#include "internal/tcscpy.h" +#include "tcscpy.h" /* EOF */ diff --git a/reactos/lib/sdk/crt/string/wcslen.c b/reactos/lib/sdk/crt/string/wcslen.c index b8404f675b1..768c52afd7b 100644 --- a/reactos/lib/sdk/crt/string/wcslen.c +++ b/reactos/lib/sdk/crt/string/wcslen.c @@ -3,6 +3,6 @@ #define _UNICODE #include -#include "internal/tcslen.h" +#include "tcslen.h" /* EOF */ diff --git a/reactos/lib/sdk/crt/string/wcsncat.c b/reactos/lib/sdk/crt/string/wcsncat.c index 1bcddda7438..c2ec3af009a 100644 --- a/reactos/lib/sdk/crt/string/wcsncat.c +++ b/reactos/lib/sdk/crt/string/wcsncat.c @@ -3,6 +3,6 @@ #define _UNICODE #include -#include "internal/tcsncat.h" +#include "tcsncat.h" /* EOF */ diff --git a/reactos/lib/sdk/crt/string/wcsncmp.c b/reactos/lib/sdk/crt/string/wcsncmp.c index fdb7d17b390..13059cc957f 100644 --- a/reactos/lib/sdk/crt/string/wcsncmp.c +++ b/reactos/lib/sdk/crt/string/wcsncmp.c @@ -3,6 +3,6 @@ #define _UNICODE #include -#include "internal/tcsncmp.h" +#include "tcsncmp.h" /* EOF */ diff --git a/reactos/lib/sdk/crt/string/wcsncpy.c b/reactos/lib/sdk/crt/string/wcsncpy.c index 9875dcda0d4..90a93ab24dd 100644 --- a/reactos/lib/sdk/crt/string/wcsncpy.c +++ b/reactos/lib/sdk/crt/string/wcsncpy.c @@ -3,6 +3,6 @@ #define _UNICODE #include -#include "internal/tcsncpy.h" +#include "tcsncpy.h" /* EOF */ diff --git a/reactos/lib/sdk/crt/string/wcsnlen.c b/reactos/lib/sdk/crt/string/wcsnlen.c index c7f7ccc5f8e..445a4aab299 100644 --- a/reactos/lib/sdk/crt/string/wcsnlen.c +++ b/reactos/lib/sdk/crt/string/wcsnlen.c @@ -3,6 +3,6 @@ #define _UNICODE #include -#include "internal/tcsnlen.h" +#include "tcsnlen.h" /* EOF */ diff --git a/reactos/lib/sdk/crt/string/wcsrchr.c b/reactos/lib/sdk/crt/string/wcsrchr.c index df21aeb7f42..0bd83205150 100644 --- a/reactos/lib/sdk/crt/string/wcsrchr.c +++ b/reactos/lib/sdk/crt/string/wcsrchr.c @@ -4,6 +4,6 @@ #define _UNICODE #define _XINT wchar_t #include -#include "internal/tcsrchr.h" +#include "tcsrchr.h" /* EOF */ diff --git a/reactos/lib/sdk/crt/string/wcstol.c b/reactos/lib/sdk/crt/string/wcstol.c index be1a0616952..ab87e4016d7 100644 --- a/reactos/lib/sdk/crt/string/wcstol.c +++ b/reactos/lib/sdk/crt/string/wcstol.c @@ -9,37 +9,89 @@ */ #include - /* * @implemented */ -long wcstol(const wchar_t *cp,wchar_t **endp,int base) +long +wcstol(const wchar_t *nptr, wchar_t **endptr, int base) { - long result = 0,value; - int sign = 1; + const wchar_t *s = nptr; + unsigned long acc; + int c; + unsigned long cutoff; + int neg = 0, any, cutlim; - if ( *cp == L'-' ) { - sign = -1; - cp++; - } + /* + * Skip white space and pick up leading +/- sign if any. + * If base is 0, allow 0x for hex and 0 for octal, else + * assume decimal; if base is already 16, allow 0x. + */ + do { + c = *s++; + } while (iswctype(c, _SPACE)); + if (c == '-') + { + neg = 1; + c = *s++; + } + else if (c == L'+') + c = *s++; + if ((base == 0 || base == 16) && + c == L'0' && (*s == L'x' || *s == L'X')) + { + c = s[1]; + s += 2; + base = 16; + } + if (base == 0) + base = c == L'0' ? 8 : 10; - if (!base) { - base = 10; - if (*cp == L'0') { - base = 8; - cp++; - if ((*cp == L'x') && iswxdigit(cp[1])) { - cp++; - base = 16; - } - } - } - while (iswxdigit(*cp) && (value = iswdigit(*cp) ? *cp-L'0' : (iswlower(*cp) - ? towupper(*cp) : *cp)-L'A'+10) < base) { - result = result*base + value; - cp++; - } - if (endp) - *endp = (wchar_t *)cp; - return result * sign; + /* + * Compute the cutoff value between legal numbers and illegal + * numbers. That is the largest legal value, divided by the + * base. An input number that is greater than this value, if + * followed by a legal input character, is too big. One that + * is equal to this value may be valid or not; the limit + * between valid and invalid numbers is then based on the last + * digit. For instance, if the range for longs is + * [-2147483648..2147483647] and the input base is 10, + * cutoff will be set to 214748364 and cutlim to either + * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated + * a value > 214748364, or equal but the next digit is > 7 (or 8), + * the number is too big, and we will return a range error. + * + * Set any if any `digits' consumed; make it negative to indicate + * overflow. + */ + cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; + cutlim = cutoff % (unsigned long)base; + cutoff /= (unsigned long)base; + for (acc = 0, any = 0;; c = *s++) + { + if (iswctype(c, _DIGIT)) + c -= L'0'; + else if (iswctype(c, _ALPHA)) + c -= iswctype(c, _UPPER) ? L'A' - 10 : L'a' - 10; + else + break; + if (c >= base) + break; + if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) + any = -1; + else + { + any = 1; + acc *= base; + acc += c; + } + } + if (any < 0) + { + acc = neg ? LONG_MIN : LONG_MAX; + } + else if (neg) + acc = -acc; + if (endptr != 0) + *endptr = any ? (wchar_t *)((size_t)(s - 1)) : (wchar_t *)((size_t)nptr); + return acc; } diff --git a/reactos/lib/sdk/crt/string/wcstombs_nt.c b/reactos/lib/sdk/crt/string/wcstombs_nt.c new file mode 100644 index 00000000000..d927b7e9681 --- /dev/null +++ b/reactos/lib/sdk/crt/string/wcstombs_nt.c @@ -0,0 +1,59 @@ +#define WIN32_NO_STATUS +#include +#include +#include + +/* + * @implemented + */ +int wctomb (char *mbchar, wchar_t wchar) +{ + NTSTATUS Status; + ULONG Size; + + if (mbchar == NULL) + return 0; + + Status = RtlUnicodeToMultiByteN (mbchar, + 1, + &Size, + &wchar, + sizeof(WCHAR)); + if (!NT_SUCCESS(Status)) + return -1; + + return (int)Size; +} + +/* + * @implemented + */ +size_t wcstombs (char *mbstr, const wchar_t *wcstr, size_t count) +{ + NTSTATUS Status; + ULONG Size; + ULONG Length; + + Length = wcslen (wcstr); + + if (mbstr == NULL) + { + RtlUnicodeToMultiByteSize (&Size, + (wchar_t*)((size_t)wcstr), + Length * sizeof(WCHAR)); + + return (size_t)Size; + } + + Status = RtlUnicodeToMultiByteN (mbstr, + count, + &Size, + (wchar_t*)((size_t)wcstr), + Length * sizeof(WCHAR)); + if (!NT_SUCCESS(Status)) + return -1; + + return (size_t)Size; +} + +/* EOF */ diff --git a/reactos/lib/sdk/crt/string/wcstoul.c b/reactos/lib/sdk/crt/string/wcstoul.c index b017eb92d51..07b1fdfb747 100644 --- a/reactos/lib/sdk/crt/string/wcstoul.c +++ b/reactos/lib/sdk/crt/string/wcstoul.c @@ -1,6 +1,6 @@ -/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */ #include + /* * Convert a unicode string to an unsigned long integer. * @@ -23,7 +23,7 @@ wcstoul(const wchar_t *nptr, wchar_t **endptr, int base) */ do { c = *s++; - } while (iswspace(c)); + } while (iswctype(c, _SPACE)); if (c == L'-') { neg = 1; @@ -44,10 +44,10 @@ wcstoul(const wchar_t *nptr, wchar_t **endptr, int base) cutlim = (unsigned long)ULONG_MAX % (unsigned long)base; for (acc = 0, any = 0;; c = *s++) { - if (iswdigit(c)) + if (iswctype(c, _DIGIT)) c -= L'0'; - else if (iswalpha(c)) - c -= iswupper(c) ? L'A' - 10 : L'a' - 10; + else if (iswctype(c, _ALPHA)) + c -= iswctype(c, _UPPER) ? L'A' - 10 : L'a' - 10; else break; if (c >= base) @@ -63,38 +63,10 @@ wcstoul(const wchar_t *nptr, wchar_t **endptr, int base) if (any < 0) { acc = ULONG_MAX; - __set_errno(ERANGE); } else if (neg) acc = -acc; if (endptr != 0) - *endptr = any ? (wchar_t *)s - 1 : (wchar_t *)nptr; + *endptr = any ? (wchar_t *)((size_t)s - 1) : (wchar_t *)((size_t)nptr); return acc; } - -#if 0 -unsigned long wcstoul(const wchar_t *cp,wchar_t **endp,int base) -{ - unsigned long result = 0,value; - - if (!base) { - base = 10; - if (*cp == L'0') { - base = 8; - cp++; - if ((*cp == L'x') && iswxdigit(cp[1])) { - cp++; - base = 16; - } - } - } - while (iswxdigit(*cp) && (value = iswdigit(*cp) ? *cp-L'0' : (iswlower(*cp) - ? towupper(*cp) : *cp)-L'A'+10) < base) { - result = result*base + value; - cp++; - } - if (endp) - *endp = (wchar_t *)cp; - return result; -} -#endif diff --git a/reactos/lib/sdk/crt/string/wtoi64.c b/reactos/lib/sdk/crt/string/wtoi64.c index f842292759d..6e0f9dbdd64 100644 --- a/reactos/lib/sdk/crt/string/wtoi64.c +++ b/reactos/lib/sdk/crt/string/wtoi64.c @@ -13,27 +13,33 @@ /* * @implemented */ -__int64 _wtoi64(const wchar_t* nptr) +__int64 +_wtoi64 (const wchar_t *nptr) { - wchar_t* s = (wchar_t*)nptr; - __int64 acc = 0; - int neg = 0; + int c; + __int64 value; + int sign; - while (iswspace((int)*s)) - s++; - if (*s == '-') { - neg = 1; - s++; - } - else if (*s == '+') - s++; + while (iswctype((int)*nptr, _SPACE)) + ++nptr; - while (iswdigit((int)*s)) { - acc = 10 * acc + ((int)*s - '0'); - s++; - } + c = (int)*nptr++; + sign = c; + if (c == L'-' || c == L'+') + c = (int)*nptr++; - if (neg) - acc *= -1; - return acc; + value = 0; + + while (iswctype(c, _DIGIT)) + { + value = 10 * value + (c - L'0'); + c = (int)*nptr++; + } + + if (sign == L'-') + return -value; + else + return value; } + +/* EOF */ diff --git a/reactos/lib/sdk/crt/string/wtol.c b/reactos/lib/sdk/crt/string/wtol.c index 1d56c7d7076..9944956384b 100644 --- a/reactos/lib/sdk/crt/string/wtol.c +++ b/reactos/lib/sdk/crt/string/wtol.c @@ -1,6 +1,35 @@ +#include +#include +#include -#define _UNICODE -#define UNICODE +/* Implementation comes from wine/dlls/ntdll/wcstring.c */ + +/* + * @implemented + */ +long +_wtol(const wchar_t *str) +{ + unsigned long RunningTotal = 0; + char bMinus = 0; + + while (iswctype(*str, _SPACE) ) { + str++; + } /* while */ + + if (*str == L'+') { + str++; + } else if (*str == L'-') { + bMinus = 1; + str++; + } /* if */ + + while (*str >= L'0' && *str <= L'9') { + RunningTotal = RunningTotal * 10 + *str - L'0'; + str++; + } /* while */ + + return bMinus ? -RunningTotal : RunningTotal; +} -#include "atol.c"