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
33 /*********************************************************************
36 * Compare two blocks of memory as strings, ignoring case.
39 * s1 [I] First string to compare to s2
40 * s2 [I] Second string to compare to s1
41 * len [I] Number of bytes to compare
44 * An integer less than, equal to, or greater than zero indicating that
45 * s1 is less than, equal to or greater than s2 respectively.
48 * Any Nul characters in s1 or s2 are ignored. This function always
49 * compares up to len bytes or the first place where s1 and s2 differ.
51 INT __cdecl NTDLL__memicmp( LPCSTR s1, LPCSTR s2, DWORD len )
56 if ((ret = tolower(*s1) - tolower(*s2))) break;
64 /*********************************************************************
67 * Convert a string to upper case.
70 * str [I/O] String to convert
73 * str. There is no error return, if str is NULL or invalid, this
74 * function will crash.
76 LPSTR __cdecl _strupr( LPSTR str )
79 for ( ; *str; str++) *str = toupper(*str);
84 /*********************************************************************
87 * Convert a string to lowercase
90 * str [I/O] String to convert
93 * str. There is no error return, if str is NULL or invalid, this
94 * function will crash.
96 LPSTR __cdecl _strlwr( LPSTR str )
99 for ( ; *str; str++) *str = tolower(*str);
104 /*********************************************************************
107 * Convert an unsigned long integer to a string.
113 * - Converts value to a Nul terminated string which is copied to str.
114 * - The maximum length of the copied str is 33 bytes.
115 * - Does not check if radix is in the range of 2 to 36.
116 * - If str is NULL it crashes, as the native function does.
118 char * __cdecl _ultoa(
119 unsigned long value, /* [I] Value to be converted */
120 char *str, /* [O] Destination for the converted value */
121 int radix) /* [I] Number base for conversion */
131 digit = value % radix;
132 value = value / radix;
134 *--pos = '0' + digit;
136 *--pos = 'a' + digit - 10;
138 } while (value != 0L);
140 memcpy(str, pos, &buffer[32] - pos + 1);
145 /*********************************************************************
148 * Convert a long integer to a string.
154 * - Converts value to a Nul terminated string which is copied to str.
155 * - The maximum length of the copied str is 33 bytes. If radix
156 * is 10 and value is negative, the value is converted with sign.
157 * - Does not check if radix is in the range of 2 to 36.
158 * - If str is NULL it crashes, as the native function does.
160 char * __cdecl _ltoa(
161 long value, /* [I] Value to be converted */
162 char *str, /* [O] Destination for the converted value */
163 int radix) /* [I] Number base for conversion */
171 if (value < 0 && radix == 10) {
186 *--pos = '0' + digit;
188 *--pos = 'a' + digit - 10;
196 memcpy(str, pos, &buffer[32] - pos + 1);
201 /*********************************************************************
204 * Converts an integer to a string.
210 * - Converts value to a '\0' terminated string which is copied to str.
211 * - The maximum length of the copied str is 33 bytes. If radix
212 * is 10 and value is negative, the value is converted with sign.
213 * - Does not check if radix is in the range of 2 to 36.
214 * - If str is NULL it crashes, as the native function does.
216 char * __cdecl _itoa(
217 int value, /* [I] Value to be converted */
218 char *str, /* [O] Destination for the converted value */
219 int radix) /* [I] Number base for conversion */
221 return _ltoa(value, str, radix);
225 /*********************************************************************
228 * Converts a large unsigned integer to a string.
234 * - Converts value to a '\0' terminated string which is copied to str.
235 * - The maximum length of the copied str is 65 bytes.
236 * - Does not check if radix is in the range of 2 to 36.
237 * - If str is NULL it crashes, as the native function does.
239 char * __cdecl _ui64toa(
240 ULONGLONG value, /* [I] Value to be converted */
241 char *str, /* [O] Destination for the converted value */
242 int radix) /* [I] Number base for conversion */
252 digit = value % radix;
253 value = value / radix;
255 *--pos = '0' + digit;
257 *--pos = 'a' + digit - 10;
259 } while (value != 0L);
261 memcpy(str, pos, &buffer[64] - pos + 1);
266 /*********************************************************************
269 * Converts a large integer to a string.
275 * - Converts value to a Nul terminated string which is copied to str.
276 * - The maximum length of the copied str is 65 bytes. If radix
277 * is 10 and value is negative, the value is converted with sign.
278 * - Does not check if radix is in the range of 2 to 36.
279 * - If str is NULL it crashes, as the native function does.
282 * - The native DLL converts negative values (for base 10) wrong:
283 *| -1 is converted to -18446744073709551615
284 *| -2 is converted to -18446744073709551614
285 *| -9223372036854775807 is converted to -9223372036854775809
286 *| -9223372036854775808 is converted to -9223372036854775808
287 * The native msvcrt _i64toa function and our ntdll _i64toa function
288 * do not have this bug.
290 char * __cdecl _i64toa(
291 LONGLONG value, /* [I] Value to be converted */
292 char *str, /* [O] Destination for the converted value */
293 int radix) /* [I] Number base for conversion */
301 if (value < 0 && radix == 10) {
316 *--pos = '0' + digit;
318 *--pos = 'a' + digit - 10;
326 memcpy(str, pos, &buffer[64] - pos + 1);
331 /*********************************************************************
334 * Convert a string to a large integer.
337 * str [I] String to be converted
340 * Success: The integer value represented by str.
341 * Failure: 0. Note that this cannot be distinguished from a successful
342 * return, if the string contains "0".
345 * - Accepts: {whitespace} [+|-] {digits}
346 * - No check is made for value overflow, only the lower 64 bits are assigned.
347 * - If str is NULL it crashes, as the native function does.
349 LONGLONG __cdecl _atoi64( char *str )
351 ULONGLONG RunningTotal = 0;
354 while (*str == ' ' || (*str >= '\011' && *str <= '\015')) {
360 } else if (*str == '-') {
365 while (*str >= '0' && *str <= '9') {
366 RunningTotal = RunningTotal * 10 + *str - '0';
370 return bMinus ? -RunningTotal : RunningTotal;
374 /*********************************************************************
375 * _splitpath (NTDLL.@)
377 * Split a path into its component pieces.
380 * inpath [I] Path to split
381 * drv [O] Destination for drive component (e.g. "A:"). Must be at least 3 characters.
382 * dir [O] Destination for directory component. Should be at least MAX_PATH characters.
383 * fname [O] Destination for File name component. Should be at least MAX_PATH characters.
384 * ext [O] Destination for file extension component. Should be at least MAX_PATH characters.
389 void __cdecl _splitpath(const char* inpath, char * drv, char * dir,
390 char* fname, char * ext )
394 if (inpath[0] && inpath[1] == ':')
404 else if (drv) drv[0] = 0;
406 /* look for end of directory part */
408 for (p = inpath; *p; p++) if (*p == '/' || *p == '\\') end = p + 1;
410 if (end) /* got a directory */
414 memcpy( dir, inpath, end - inpath );
415 dir[end - inpath] = 0;
419 else if (dir) dir[0] = 0;
421 /* look for extension: what's after the last dot */
423 for (p = inpath; *p; p++) if (*p == '.') end = p;
425 if (!end) end = p; /* there's no extension */
429 memcpy( fname, inpath, end - inpath );
430 fname[end - inpath] = 0;
432 if (ext) strcpy( ext, end );