2 * NTDLL string functions
4 * Copyright 2000 Alexandre Julliard
5 * Copyright 2000 Jon Griffiths
6 * Copyright 2003 Thomas Mertes
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wine/port.h"
34 /*********************************************************************
37 * Compare two blocks of memory as strings, ignoring case.
40 * s1 [I] First string to compare to s2
41 * s2 [I] Second string to compare to s1
42 * len [I] Number of bytes to compare
45 * An integer less than, equal to, or greater than zero indicating that
46 * s1 is less than, equal to or greater than s2 respectively.
49 * Any Nul characters in s1 or s2 are ignored. This function always
50 * compares up to len bytes or the first place where s1 and s2 differ.
52 INT __cdecl NTDLL__memicmp( LPCSTR s1, LPCSTR s2, DWORD len )
57 if ((ret = tolower(*s1) - tolower(*s2))) break;
65 /*********************************************************************
69 * Behaves like memmove.
71 void * __cdecl NTDLL_memcpy( void *dst, const void *src, size_t n )
73 return memmove( dst, src, n );
77 /*********************************************************************
80 * Convert a string to upper case.
83 * str [I/O] String to convert
86 * str. There is no error return, if str is NULL or invalid, this
87 * function will crash.
89 LPSTR __cdecl _strupr( LPSTR str )
92 for ( ; *str; str++) *str = toupper(*str);
97 /*********************************************************************
100 * Convert a string to lowercase
103 * str [I/O] String to convert
106 * str. There is no error return, if str is NULL or invalid, this
107 * function will crash.
109 LPSTR __cdecl _strlwr( LPSTR str )
112 for ( ; *str; str++) *str = tolower(*str);
117 /*********************************************************************
120 * Convert an unsigned long integer to a string.
126 * - Converts value to a Nul terminated string which is copied to str.
127 * - The maximum length of the copied str is 33 bytes.
128 * - Does not check if radix is in the range of 2 to 36.
129 * - If str is NULL it crashes, as the native function does.
131 char * __cdecl _ultoa(
132 unsigned long value, /* [I] Value to be converted */
133 char *str, /* [O] Destination for the converted value */
134 int radix) /* [I] Number base for conversion */
144 digit = value % radix;
145 value = value / radix;
147 *--pos = '0' + digit;
149 *--pos = 'a' + digit - 10;
151 } while (value != 0L);
153 memcpy(str, pos, &buffer[32] - pos + 1);
158 /*********************************************************************
161 * Convert a long integer to a string.
167 * - Converts value to a Nul terminated string which is copied to str.
168 * - The maximum length of the copied str is 33 bytes. If radix
169 * is 10 and value is negative, the value is converted with sign.
170 * - Does not check if radix is in the range of 2 to 36.
171 * - If str is NULL it crashes, as the native function does.
173 char * __cdecl _ltoa(
174 long value, /* [I] Value to be converted */
175 char *str, /* [O] Destination for the converted value */
176 int radix) /* [I] Number base for conversion */
184 if (value < 0 && radix == 10) {
199 *--pos = '0' + digit;
201 *--pos = 'a' + digit - 10;
209 memcpy(str, pos, &buffer[32] - pos + 1);
214 /*********************************************************************
217 * Converts an integer to a string.
223 * - Converts value to a '\0' terminated string which is copied to str.
224 * - The maximum length of the copied str is 33 bytes. If radix
225 * is 10 and value is negative, the value is converted with sign.
226 * - Does not check if radix is in the range of 2 to 36.
227 * - If str is NULL it crashes, as the native function does.
229 char * __cdecl _itoa(
230 int value, /* [I] Value to be converted */
231 char *str, /* [O] Destination for the converted value */
232 int radix) /* [I] Number base for conversion */
234 return _ltoa(value, str, radix);
238 /*********************************************************************
241 * Converts a large unsigned integer to a string.
247 * - Converts value to a '\0' terminated string which is copied to str.
248 * - The maximum length of the copied str is 65 bytes.
249 * - Does not check if radix is in the range of 2 to 36.
250 * - If str is NULL it crashes, as the native function does.
252 char * __cdecl _ui64toa(
253 ULONGLONG value, /* [I] Value to be converted */
254 char *str, /* [O] Destination for the converted value */
255 int radix) /* [I] Number base for conversion */
265 digit = value % radix;
266 value = value / radix;
268 *--pos = '0' + digit;
270 *--pos = 'a' + digit - 10;
272 } while (value != 0L);
274 memcpy(str, pos, &buffer[64] - pos + 1);
279 /*********************************************************************
282 * Converts a large integer to a string.
288 * - Converts value to a Nul terminated string which is copied to str.
289 * - The maximum length of the copied str is 65 bytes. If radix
290 * is 10 and value is negative, the value is converted with sign.
291 * - Does not check if radix is in the range of 2 to 36.
292 * - If str is NULL it crashes, as the native function does.
295 * - The native DLL converts negative values (for base 10) wrong:
296 *| -1 is converted to -18446744073709551615
297 *| -2 is converted to -18446744073709551614
298 *| -9223372036854775807 is converted to -9223372036854775809
299 *| -9223372036854775808 is converted to -9223372036854775808
300 * The native msvcrt _i64toa function and our ntdll _i64toa function
301 * do not have this bug.
303 char * __cdecl _i64toa(
304 LONGLONG value, /* [I] Value to be converted */
305 char *str, /* [O] Destination for the converted value */
306 int radix) /* [I] Number base for conversion */
314 if (value < 0 && radix == 10) {
329 *--pos = '0' + digit;
331 *--pos = 'a' + digit - 10;
339 memcpy(str, pos, &buffer[64] - pos + 1);
344 /*********************************************************************
347 * Convert a string to a large integer.
350 * str [I] String to be converted
353 * Success: The integer value represented by str.
354 * Failure: 0. Note that this cannot be distinguished from a successful
355 * return, if the string contains "0".
358 * - Accepts: {whitespace} [+|-] {digits}
359 * - No check is made for value overflow, only the lower 64 bits are assigned.
360 * - If str is NULL it crashes, as the native function does.
362 LONGLONG __cdecl _atoi64( char *str )
364 ULONGLONG RunningTotal = 0;
367 while (*str == ' ' || (*str >= '\011' && *str <= '\015')) {
373 } else if (*str == '-') {
378 while (*str >= '0' && *str <= '9') {
379 RunningTotal = RunningTotal * 10 + *str - '0';
383 return bMinus ? -RunningTotal : RunningTotal;
387 /*********************************************************************
388 * _splitpath (NTDLL.@)
390 * Split a path into its component pieces.
393 * inpath [I] Path to split
394 * drv [O] Destination for drive component (e.g. "A:"). Must be at least 3 characters.
395 * dir [O] Destination for directory component. Should be at least MAX_PATH characters.
396 * fname [O] Destination for File name component. Should be at least MAX_PATH characters.
397 * ext [O] Destination for file extension component. Should be at least MAX_PATH characters.
402 void __cdecl _splitpath(const char* inpath, char * drv, char * dir,
403 char* fname, char * ext )
407 if (inpath[0] && inpath[1] == ':')
417 else if (drv) drv[0] = 0;
419 /* look for end of directory part */
421 for (p = inpath; *p; p++) if (*p == '/' || *p == '\\') end = p + 1;
423 if (end) /* got a directory */
427 memcpy( dir, inpath, end - inpath );
428 dir[end - inpath] = 0;
432 else if (dir) dir[0] = 0;
434 /* look for extension: what's after the last dot */
436 for (p = inpath; *p; p++) if (*p == '.') end = p;
438 if (!end) end = p; /* there's no extension */
442 memcpy( fname, inpath, end - inpath );
443 fname[end - inpath] = 0;
445 if (ext) strcpy( ext, end );