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
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 /*********************************************************************
68 * Convert a string to upper case.
71 * str [I/O] String to convert
74 * str. There is no error return, if str is NULL or invalid, this
75 * function will crash.
77 LPSTR __cdecl _strupr( LPSTR str )
80 for ( ; *str; str++) *str = toupper(*str);
85 /*********************************************************************
88 * Convert a string to lowercase
91 * str [I/O] String to convert
94 * str. There is no error return, if str is NULL or invalid, this
95 * function will crash.
97 LPSTR __cdecl _strlwr( LPSTR str )
100 for ( ; *str; str++) *str = tolower(*str);
105 /*********************************************************************
108 * Convert an unsigned long integer to a string.
114 * - Converts value to a Nul terminated string which is copied to str.
115 * - The maximum length of the copied str is 33 bytes.
116 * - Does not check if radix is in the range of 2 to 36.
117 * - If str is NULL it crashes, as the native function does.
119 char * __cdecl _ultoa(
120 unsigned long value, /* [I] Value to be converted */
121 char *str, /* [O] Destination for the converted value */
122 int radix) /* [I] Number base for conversion */
132 digit = value % radix;
133 value = value / radix;
135 *--pos = '0' + digit;
137 *--pos = 'a' + digit - 10;
139 } while (value != 0L);
141 memcpy(str, pos, &buffer[32] - pos + 1);
146 /*********************************************************************
149 * Convert a long integer to a string.
155 * - Converts value to a Nul terminated string which is copied to str.
156 * - The maximum length of the copied str is 33 bytes. If radix
157 * is 10 and value is negative, the value is converted with sign.
158 * - Does not check if radix is in the range of 2 to 36.
159 * - If str is NULL it crashes, as the native function does.
161 char * __cdecl _ltoa(
162 long value, /* [I] Value to be converted */
163 char *str, /* [O] Destination for the converted value */
164 int radix) /* [I] Number base for conversion */
172 if (value < 0 && radix == 10) {
187 *--pos = '0' + digit;
189 *--pos = 'a' + digit - 10;
197 memcpy(str, pos, &buffer[32] - pos + 1);
202 /*********************************************************************
205 * Converts an integer to a string.
211 * - Converts value to a '\0' terminated string which is copied to str.
212 * - The maximum length of the copied str is 33 bytes. If radix
213 * is 10 and value is negative, the value is converted with sign.
214 * - Does not check if radix is in the range of 2 to 36.
215 * - If str is NULL it crashes, as the native function does.
217 char * __cdecl _itoa(
218 int value, /* [I] Value to be converted */
219 char *str, /* [O] Destination for the converted value */
220 int radix) /* [I] Number base for conversion */
222 return _ltoa(value, str, radix);
226 /*********************************************************************
229 * Converts a large unsigned integer to a string.
235 * - Converts value to a '\0' terminated string which is copied to str.
236 * - The maximum length of the copied str is 65 bytes.
237 * - Does not check if radix is in the range of 2 to 36.
238 * - If str is NULL it crashes, as the native function does.
240 char * __cdecl _ui64toa(
241 ULONGLONG value, /* [I] Value to be converted */
242 char *str, /* [O] Destination for the converted value */
243 int radix) /* [I] Number base for conversion */
253 digit = value % radix;
254 value = value / radix;
256 *--pos = '0' + digit;
258 *--pos = 'a' + digit - 10;
260 } while (value != 0L);
262 memcpy(str, pos, &buffer[64] - pos + 1);
267 /*********************************************************************
270 * Converts a large integer to a string.
276 * - Converts value to a Nul terminated string which is copied to str.
277 * - The maximum length of the copied str is 65 bytes. If radix
278 * is 10 and value is negative, the value is converted with sign.
279 * - Does not check if radix is in the range of 2 to 36.
280 * - If str is NULL it crashes, as the native function does.
283 * - The native DLL converts negative values (for base 10) wrong:
284 *| -1 is converted to -18446744073709551615
285 *| -2 is converted to -18446744073709551614
286 *| -9223372036854775807 is converted to -9223372036854775809
287 *| -9223372036854775808 is converted to -9223372036854775808
288 * The native msvcrt _i64toa function and our ntdll _i64toa function
289 * do not have this bug.
291 char * __cdecl _i64toa(
292 LONGLONG value, /* [I] Value to be converted */
293 char *str, /* [O] Destination for the converted value */
294 int radix) /* [I] Number base for conversion */
302 if (value < 0 && radix == 10) {
317 *--pos = '0' + digit;
319 *--pos = 'a' + digit - 10;
327 memcpy(str, pos, &buffer[64] - pos + 1);
332 /*********************************************************************
335 * Convert a string to a large integer.
338 * str [I] String to be converted
341 * Success: The integer value represented by str.
342 * Failure: 0. Note that this cannot be distinguished from a successful
343 * return, if the string contains "0".
346 * - Accepts: {whitespace} [+|-] {digits}
347 * - No check is made for value overflow, only the lower 64 bits are assigned.
348 * - If str is NULL it crashes, as the native function does.
350 LONGLONG __cdecl _atoi64( char *str )
352 ULONGLONG RunningTotal = 0;
355 while (*str == ' ' || (*str >= '\011' && *str <= '\015')) {
361 } else if (*str == '-') {
366 while (*str >= '0' && *str <= '9') {
367 RunningTotal = RunningTotal * 10 + *str - '0';
371 return bMinus ? -RunningTotal : RunningTotal;
375 /*********************************************************************
376 * _splitpath (NTDLL.@)
378 * Split a path into its component pieces.
381 * inpath [I] Path to split
382 * drv [O] Destination for drive component (e.g. "A:"). Must be at least 3 characters.
383 * dir [O] Destination for directory component. Should be at least MAX_PATH characters.
384 * fname [O] Destination for File name component. Should be at least MAX_PATH characters.
385 * ext [O] Destination for file extension component. Should be at least MAX_PATH characters.
387 void __cdecl _splitpath(const char* inpath, char * drv, char * dir,
388 char* fname, char * ext )
392 if (inpath[0] && inpath[1] == ':')
402 else if (drv) drv[0] = 0;
404 /* look for end of directory part */
406 for (p = inpath; *p; p++) if (*p == '/' || *p == '\\') end = p + 1;
408 if (end) /* got a directory */
412 memcpy( dir, inpath, end - inpath );
413 dir[end - inpath] = 0;
417 else if (dir) dir[0] = 0;
419 /* look for extension: what's after the last dot */
421 for (p = inpath; *p; p++) if (*p == '.') end = p;
423 if (!end) end = p; /* there's no extension */
427 memcpy( fname, inpath, end - inpath );
428 fname[end - inpath] = 0;
430 if (ext) strcpy( ext, end );