Sweden-Number/dlls/ntdll/string.c

434 lines
11 KiB
C

/*
* NTDLL string functions
*
* Copyright 2000 Alexandre Julliard
* Copyright 2000 Jon Griffiths
* Copyright 2003 Thomas Mertes
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include <ctype.h>
#include <stdarg.h>
#include <string.h>
#include "ntstatus.h"
#include "windef.h"
#include "winbase.h"
#include "winreg.h"
#include "winternl.h"
/*********************************************************************
* _memicmp (NTDLL.@)
*
* Compare two blocks of memory as strings, ignoring case.
*
* PARAMS
* s1 [I] First string to compare to s2
* s2 [I] Second string to compare to s1
* len [I] Number of bytes to compare
*
* RETURNS
* An integer less than, equal to, or greater than zero indicating that
* s1 is less than, equal to or greater than s2 respectively.
*
* NOTES
* Any Nul characters in s1 or s2 are ignored. This function always
* compares up to len bytes or the first place where s1 and s2 differ.
*/
INT __cdecl NTDLL__memicmp( LPCSTR s1, LPCSTR s2, DWORD len )
{
int ret = 0;
while (len--)
{
if ((ret = tolower(*s1) - tolower(*s2))) break;
s1++;
s2++;
}
return ret;
}
/*********************************************************************
* _strupr (NTDLL.@)
*
* Convert a string to upper case.
*
* PARAMS
* str [I/O] String to convert
*
* RETURNS
* str. There is no error return, if str is NULL or invalid, this
* function will crash.
*/
LPSTR __cdecl _strupr( LPSTR str )
{
LPSTR ret = str;
for ( ; *str; str++) *str = toupper(*str);
return ret;
}
/*********************************************************************
* _strlwr (NTDLL.@)
*
* Convert a string to lowercase
*
* PARAMS
* str [I/O] String to convert
*
* RETURNS
* str. There is no error return, if str is NULL or invalid, this
* function will crash.
*/
LPSTR __cdecl _strlwr( LPSTR str )
{
LPSTR ret = str;
for ( ; *str; str++) *str = tolower(*str);
return ret;
}
/*********************************************************************
* _ultoa (NTDLL.@)
*
* Convert an unsigned long integer to a string.
*
* RETURNS
* str.
*
* NOTES
* - Converts value to a Nul terminated string which is copied to str.
* - The maximum length of the copied str is 33 bytes.
* - Does not check if radix is in the range of 2 to 36.
* - If str is NULL it crashes, as the native function does.
*/
char * __cdecl _ultoa(
unsigned long value, /* [I] Value to be converted */
char *str, /* [O] Destination for the converted value */
int radix) /* [I] Number base for conversion */
{
char buffer[33];
char *pos;
int digit;
pos = &buffer[32];
*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(str, pos, &buffer[32] - pos + 1);
return str;
}
/*********************************************************************
* _ltoa (NTDLL.@)
*
* Convert a long integer to a string.
*
* RETURNS
* str.
*
* NOTES
* - Converts value to a Nul terminated string which is copied to str.
* - The maximum length of the copied str is 33 bytes. If radix
* is 10 and value is negative, the value is converted with sign.
* - Does not check if radix is in the range of 2 to 36.
* - If str is NULL it crashes, as the native function does.
*/
char * __cdecl _ltoa(
long value, /* [I] Value to be converted */
char *str, /* [O] Destination for the converted value */
int radix) /* [I] Number base for conversion */
{
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(str, pos, &buffer[32] - pos + 1);
return str;
}
/*********************************************************************
* _itoa (NTDLL.@)
*
* Converts an integer to a string.
*
* RETURNS
* str.
*
* NOTES
* - Converts value to a '\0' terminated string which is copied to str.
* - The maximum length of the copied str is 33 bytes. If radix
* is 10 and value is negative, the value is converted with sign.
* - Does not check if radix is in the range of 2 to 36.
* - If str is NULL it crashes, as the native function does.
*/
char * __cdecl _itoa(
int value, /* [I] Value to be converted */
char *str, /* [O] Destination for the converted value */
int radix) /* [I] Number base for conversion */
{
return _ltoa(value, str, radix);
}
/*********************************************************************
* _ui64toa (NTDLL.@)
*
* Converts a large unsigned integer to a string.
*
* RETURNS
* str.
*
* NOTES
* - Converts value to a '\0' terminated string which is copied to str.
* - The maximum length of the copied str is 65 bytes.
* - Does not check if radix is in the range of 2 to 36.
* - If str is NULL it crashes, as the native function does.
*/
char * __cdecl _ui64toa(
ULONGLONG value, /* [I] Value to be converted */
char *str, /* [O] Destination for the converted value */
int radix) /* [I] Number base for conversion */
{
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(str, pos, &buffer[64] - pos + 1);
return str;
}
/*********************************************************************
* _i64toa (NTDLL.@)
*
* Converts a large integer to a string.
*
* RETURNS
* str.
*
* NOTES
* - Converts value to a Nul terminated string which is copied to str.
* - The maximum length of the copied str is 65 bytes. If radix
* is 10 and value is negative, the value is converted with sign.
* - Does not check if radix is in the range of 2 to 36.
* - If str is NULL it crashes, as the native function does.
*
* DIFFERENCES
* - The native DLL converts negative values (for base 10) wrong:
*| -1 is converted to -18446744073709551615
*| -2 is converted to -18446744073709551614
*| -9223372036854775807 is converted to -9223372036854775809
*| -9223372036854775808 is converted to -9223372036854775808
* The native msvcrt _i64toa function and our ntdll _i64toa function
* do not have this bug.
*/
char * __cdecl _i64toa(
LONGLONG value, /* [I] Value to be converted */
char *str, /* [O] Destination for the converted value */
int radix) /* [I] Number base for conversion */
{
ULONGLONG val;
int negative;
char buffer[65];
char *pos;
int 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 */
memcpy(str, pos, &buffer[64] - pos + 1);
return str;
}
/*********************************************************************
* _atoi64 (NTDLL.@)
*
* Convert a string to a large integer.
*
* PARAMS
* str [I] String to be converted
*
* RETURNS
* Success: The integer value represented by str.
* Failure: 0. Note that this cannot be distinguished from a successful
* return, if the string contains "0".
*
* NOTES
* - Accepts: {whitespace} [+|-] {digits}
* - No check is made for value overflow, only the lower 64 bits are assigned.
* - If str is NULL it crashes, as the native function does.
*/
LONGLONG __cdecl _atoi64( char *str )
{
ULONGLONG RunningTotal = 0;
char bMinus = 0;
while (*str == ' ' || (*str >= '\011' && *str <= '\015')) {
str++;
} /* while */
if (*str == '+') {
str++;
} else if (*str == '-') {
bMinus = 1;
str++;
} /* if */
while (*str >= '0' && *str <= '9') {
RunningTotal = RunningTotal * 10 + *str - '0';
str++;
} /* while */
return bMinus ? -RunningTotal : RunningTotal;
}
/*********************************************************************
* _splitpath (NTDLL.@)
*
* Split a path into its component pieces.
*
* PARAMS
* inpath [I] Path to split
* drv [O] Destination for drive component (e.g. "A:"). Must be at least 3 characters.
* dir [O] Destination for directory component. Should be at least MAX_PATH characters.
* fname [O] Destination for File name component. Should be at least MAX_PATH characters.
* ext [O] Destination for file extension component. Should be at least MAX_PATH characters.
*/
void __cdecl _splitpath(const char* inpath, char * drv, char * dir,
char* fname, char * ext )
{
const char *p, *end;
if (inpath[0] && inpath[1] == ':')
{
if (drv)
{
drv[0] = inpath[0];
drv[1] = inpath[1];
drv[2] = 0;
}
inpath += 2;
}
else if (drv) drv[0] = 0;
/* look for end of directory part */
end = NULL;
for (p = inpath; *p; p++) if (*p == '/' || *p == '\\') end = p + 1;
if (end) /* got a directory */
{
if (dir)
{
memcpy( dir, inpath, end - inpath );
dir[end - inpath] = 0;
}
inpath = end;
}
else if (dir) dir[0] = 0;
/* look for extension: what's after the last dot */
end = NULL;
for (p = inpath; *p; p++) if (*p == '.') end = p;
if (!end) end = p; /* there's no extension */
if (fname)
{
memcpy( fname, inpath, end - inpath );
fname[end - inpath] = 0;
}
if (ext) strcpy( ext, end );
}