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
35 /*********************************************************************
38 * Compare two blocks of memory as strings, ignoring case.
41 * s1 [I] First string to compare to s2
42 * s2 [I] Second string to compare to s1
43 * len [I] Number of bytes to compare
46 * An integer less than, equal to, or greater than zero indicating that
47 * s1 is less than, equal to or greater than s2 respectively.
50 * Any Nul characters in s1 or s2 are ignored. This function always
51 * compares up to len bytes or the first place where s1 and s2 differ.
53 INT __cdecl NTDLL__memicmp( LPCSTR s1, LPCSTR s2, DWORD len )
58 if ((ret = tolower(*s1) - tolower(*s2))) break;
66 /*********************************************************************
69 * Convert a string to upper case.
72 * str [I/O] String to convert
75 * str. There is no error return, if str is NULL or invalid, this
76 * function will crash.
78 LPSTR __cdecl _strupr( LPSTR str )
81 for ( ; *str; str++) *str = toupper(*str);
86 /*********************************************************************
89 * Convert a string to lowercase
92 * str [I/O] String to convert
95 * str. There is no error return, if str is NULL or invalid, this
96 * function will crash.
98 LPSTR __cdecl _strlwr( LPSTR str )
101 for ( ; *str; str++) *str = tolower(*str);
106 /*********************************************************************
109 * Convert an unsigned long integer to a string.
115 * - Converts value to a Nul terminated string which is copied to str.
116 * - The maximum length of the copied str is 33 bytes.
117 * - Does not check if radix is in the range of 2 to 36.
118 * - If str is NULL it crashes, as the native function does.
120 char * __cdecl _ultoa(
121 unsigned long value, /* [I] Value to be converted */
122 char *str, /* [O] Destination for the converted value */
123 int radix) /* [I] Number base for conversion */
133 digit = value % radix;
134 value = value / radix;
136 *--pos = '0' + digit;
138 *--pos = 'a' + digit - 10;
140 } while (value != 0L);
142 memcpy(str, pos, &buffer[32] - pos + 1);
147 /*********************************************************************
150 * Convert a long integer to a string.
156 * - Converts value to a Nul terminated string which is copied to str.
157 * - The maximum length of the copied str is 33 bytes. If radix
158 * is 10 and value is negative, the value is converted with sign.
159 * - Does not check if radix is in the range of 2 to 36.
160 * - If str is NULL it crashes, as the native function does.
162 char * __cdecl _ltoa(
163 long value, /* [I] Value to be converted */
164 char *str, /* [O] Destination for the converted value */
165 int radix) /* [I] Number base for conversion */
173 if (value < 0 && radix == 10) {
188 *--pos = '0' + digit;
190 *--pos = 'a' + digit - 10;
198 memcpy(str, pos, &buffer[32] - pos + 1);
203 /*********************************************************************
206 * Converts an integer to a string.
212 * - Converts value to a '\0' terminated string which is copied to str.
213 * - The maximum length of the copied str is 33 bytes. If radix
214 * is 10 and value is negative, the value is converted with sign.
215 * - Does not check if radix is in the range of 2 to 36.
216 * - If str is NULL it crashes, as the native function does.
218 char * __cdecl _itoa(
219 int value, /* [I] Value to be converted */
220 char *str, /* [O] Destination for the converted value */
221 int radix) /* [I] Number base for conversion */
223 return _ltoa(value, str, radix);
227 /*********************************************************************
230 * Converts a large unsigned integer to a string.
236 * - Converts value to a '\0' terminated string which is copied to str.
237 * - The maximum length of the copied str is 65 bytes.
238 * - Does not check if radix is in the range of 2 to 36.
239 * - If str is NULL it crashes, as the native function does.
241 char * __cdecl _ui64toa(
242 ULONGLONG value, /* [I] Value to be converted */
243 char *str, /* [O] Destination for the converted value */
244 int radix) /* [I] Number base for conversion */
254 digit = value % radix;
255 value = value / radix;
257 *--pos = '0' + digit;
259 *--pos = 'a' + digit - 10;
261 } while (value != 0L);
263 memcpy(str, pos, &buffer[64] - pos + 1);
268 /*********************************************************************
271 * Converts a large integer to a string.
277 * - Converts value to a Nul terminated string which is copied to str.
278 * - The maximum length of the copied str is 65 bytes. If radix
279 * is 10 and value is negative, the value is converted with sign.
280 * - Does not check if radix is in the range of 2 to 36.
281 * - If str is NULL it crashes, as the native function does.
284 * - The native DLL converts negative values (for base 10) wrong:
285 *| -1 is converted to -18446744073709551615
286 *| -2 is converted to -18446744073709551614
287 *| -9223372036854775807 is converted to -9223372036854775809
288 *| -9223372036854775808 is converted to -9223372036854775808
289 * The native msvcrt _i64toa function and our ntdll _i64toa function
290 * do not have this bug.
292 char * __cdecl _i64toa(
293 LONGLONG value, /* [I] Value to be converted */
294 char *str, /* [O] Destination for the converted value */
295 int radix) /* [I] Number base for conversion */
303 if (value < 0 && radix == 10) {
318 *--pos = '0' + digit;
320 *--pos = 'a' + digit - 10;
328 memcpy(str, pos, &buffer[64] - pos + 1);
333 /*********************************************************************
336 * Convert a string to a large integer.
339 * str [I] String to be converted
342 * Success: The integer value represented by str.
343 * Failure: 0. Note that this cannot be distinguished from a successful
344 * return, if the string contains "0".
347 * - Accepts: {whitespace} [+|-] {digits}
348 * - No check is made for value overflow, only the lower 64 bits are assigned.
349 * - If str is NULL it crashes, as the native function does.
351 LONGLONG __cdecl _atoi64( char *str )
353 ULONGLONG RunningTotal = 0;
356 while (*str == ' ' || (*str >= '\011' && *str <= '\015')) {
362 } else if (*str == '-') {
367 while (*str >= '0' && *str <= '9') {
368 RunningTotal = RunningTotal * 10 + *str - '0';
372 return bMinus ? -RunningTotal : RunningTotal;
376 /*********************************************************************
377 * _splitpath (NTDLL.@)
379 * Split a path into its component pieces.
382 * inpath [I] Path to split
383 * drv [O] Destination for drive component (e.g. "A:"). Must be at least 3 characters.
384 * dir [O] Destination for directory component. Should be at least MAX_PATH characters.
385 * fname [O] Destination for File name component. Should be at least MAX_PATH characters.
386 * ext [O] Destination for file extension component. Should be at least MAX_PATH characters.
388 void __cdecl _splitpath(const char* inpath, char * drv, char * dir,
389 char* fname, char * ext )
393 if (inpath[0] && inpath[1] == ':')
403 else if (drv) drv[0] = 0;
405 /* look for end of directory part */
407 for (p = inpath; *p; p++) if (*p == '/' || *p == '\\') end = p + 1;
409 if (end) /* got a directory */
413 memcpy( dir, inpath, end - inpath );
414 dir[end - inpath] = 0;
418 else if (dir) dir[0] = 0;
420 /* look for extension: what's after the last dot */
422 for (p = inpath; *p; p++) if (*p == '.') end = p;
424 if (!end) end = p; /* there's no extension */
428 memcpy( fname, inpath, end - inpath );
429 fname[end - inpath] = 0;
431 if (ext) strcpy( ext, end );