mfmediaengine: Handle Play() when called before topology is set.
[wine.git] / dlls / ntdll / string.c
blobf1cea6caa839b439d006bdd8f47e6e00b4375e8a
1 /*
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include <limits.h>
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winnls.h"
32 #include "winternl.h"
33 #include "ntdll_misc.h"
36 /* same as wctypes except for TAB, which doesn't have C1_BLANK for some reason... */
37 static const unsigned short ctypes[257] =
39 /* -1 */
40 0x0000,
41 /* 00 */
42 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020,
43 0x0020, 0x0028, 0x0028, 0x0028, 0x0028, 0x0028, 0x0020, 0x0020,
44 /* 10 */
45 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020,
46 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020,
47 /* 20 */
48 0x0048, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
49 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
50 /* 30 */
51 0x0084, 0x0084, 0x0084, 0x0084, 0x0084, 0x0084, 0x0084, 0x0084,
52 0x0084, 0x0084, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
53 /* 40 */
54 0x0010, 0x0181, 0x0181, 0x0181, 0x0181, 0x0181, 0x0181, 0x0101,
55 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
56 /* 50 */
57 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
58 0x0101, 0x0101, 0x0101, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
59 /* 60 */
60 0x0010, 0x0182, 0x0182, 0x0182, 0x0182, 0x0182, 0x0182, 0x0102,
61 0x0102, 0x0102, 0x0102, 0x0102, 0x0102, 0x0102, 0x0102, 0x0102,
62 /* 70 */
63 0x0102, 0x0102, 0x0102, 0x0102, 0x0102, 0x0102, 0x0102, 0x0102,
64 0x0102, 0x0102, 0x0102, 0x0010, 0x0010, 0x0010, 0x0010, 0x0020
68 /*********************************************************************
69 * memchr (NTDLL.@)
71 void * __cdecl memchr( const void *ptr, int c, size_t n )
73 const unsigned char *p = ptr;
75 for (p = ptr; n; n--, p++) if (*p == (unsigned char)c) return (void *)(ULONG_PTR)p;
76 return NULL;
80 /*********************************************************************
81 * memcmp (NTDLL.@)
83 int __cdecl memcmp( const void *ptr1, const void *ptr2, size_t n )
85 const unsigned char *p1, *p2;
87 for (p1 = ptr1, p2 = ptr2; n; n--, p1++, p2++)
89 if (*p1 < *p2) return -1;
90 if (*p1 > *p2) return 1;
92 return 0;
96 /*********************************************************************
97 * memcpy (NTDLL.@)
99 * NOTES
100 * Behaves like memmove.
102 void * __cdecl memcpy( void *dst, const void *src, size_t n )
104 volatile unsigned char *d = dst; /* avoid gcc optimizations */
105 const unsigned char *s = src;
107 if ((size_t)dst - (size_t)src >= n)
109 while (n--) *d++ = *s++;
111 else
113 d += n - 1;
114 s += n - 1;
115 while (n--) *d-- = *s--;
117 return dst;
121 /*********************************************************************
122 * memmove (NTDLL.@)
124 void * __cdecl memmove( void *dst, const void *src, size_t n )
126 volatile unsigned char *d = dst; /* avoid gcc optimizations */
127 const unsigned char *s = src;
129 if ((size_t)dst - (size_t)src >= n)
131 while (n--) *d++ = *s++;
133 else
135 d += n - 1;
136 s += n - 1;
137 while (n--) *d-- = *s--;
139 return dst;
143 /*********************************************************************
144 * memset (NTDLL.@)
146 void * __cdecl memset( void *dst, int c, size_t n )
148 volatile unsigned char *d = dst; /* avoid gcc optimizations */
149 while (n--) *d++ = c;
150 return dst;
154 /*********************************************************************
155 * strcat (NTDLL.@)
157 char * __cdecl strcat( char *dst, const char *src )
159 char *d = dst;
160 while (*d) d++;
161 while ((*d++ = *src++));
162 return dst;
166 /*********************************************************************
167 * strchr (NTDLL.@)
169 char * __cdecl strchr( const char *str, int c )
171 do { if (*str == (char)c) return (char *)(ULONG_PTR)str; } while (*str++);
172 return NULL;
176 /*********************************************************************
177 * strcmp (NTDLL.@)
179 int __cdecl strcmp( const char *str1, const char *str2 )
181 while (*str1 && *str1 == *str2) { str1++; str2++; }
182 if ((unsigned char)*str1 > (unsigned char)*str2) return 1;
183 if ((unsigned char)*str1 < (unsigned char)*str2) return -1;
184 return 0;
188 /*********************************************************************
189 * strcpy (NTDLL.@)
191 char * __cdecl strcpy( char *dst, const char *src )
193 char *d = dst;
194 while ((*d++ = *src++));
195 return dst;
199 /*********************************************************************
200 * strcspn (NTDLL.@)
202 size_t __cdecl strcspn( const char *str, const char *reject )
204 const char *ptr;
205 for (ptr = str; *ptr; ptr++) if (strchr( reject, *ptr )) break;
206 return ptr - str;
210 /*********************************************************************
211 * strlen (NTDLL.@)
213 size_t __cdecl strlen( const char *str )
215 const char *s = str;
216 while (*s) s++;
217 return s - str;
221 /*********************************************************************
222 * strncat (NTDLL.@)
224 char * __cdecl strncat( char *dst, const char *src, size_t len )
226 char *d = dst;
227 while (*d) d++;
228 for ( ; len && *src; d++, src++, len--) *d = *src;
229 *d = 0;
230 return dst;
234 /*********************************************************************
235 * strncmp (NTDLL.@)
237 int __cdecl strncmp( const char *str1, const char *str2, size_t len )
239 if (!len) return 0;
240 while (--len && *str1 && *str1 == *str2) { str1++; str2++; }
241 return (unsigned char)*str1 - (unsigned char)*str2;
245 /*********************************************************************
246 * strncpy (NTDLL.@)
248 #undef strncpy
249 char * __cdecl strncpy( char *dst, const char *src, size_t len )
251 char *d;
252 for (d = dst; len && *src; d++, src++, len--) *d = *src;
253 while (len--) *d++ = 0;
254 return dst;
258 /*********************************************************************
259 * strnlen (NTDLL.@)
261 size_t __cdecl strnlen( const char *str, size_t len )
263 const char *s = str;
264 for (s = str; len && *s; s++, len--) ;
265 return s - str;
269 /*********************************************************************
270 * strpbrk (NTDLL.@)
272 char * __cdecl strpbrk( const char *str, const char *accept )
274 for ( ; *str; str++) if (strchr( accept, *str )) return (char *)(ULONG_PTR)str;
275 return NULL;
279 /*********************************************************************
280 * strrchr (NTDLL.@)
282 char * __cdecl strrchr( const char *str, int c )
284 char *ret = NULL;
285 do { if (*str == (char)c) ret = (char *)(ULONG_PTR)str; } while (*str++);
286 return ret;
290 /*********************************************************************
291 * strspn (NTDLL.@)
293 size_t __cdecl strspn( const char *str, const char *accept )
295 const char *ptr;
296 for (ptr = str; *ptr; ptr++) if (!strchr( accept, *ptr )) break;
297 return ptr - str;
301 /*********************************************************************
302 * strstr (NTDLL.@)
304 char * __cdecl strstr( const char *str, const char *sub )
306 while (*str)
308 const char *p1 = str, *p2 = sub;
309 while (*p1 && *p2 && *p1 == *p2) { p1++; p2++; }
310 if (!*p2) return (char *)str;
311 str++;
313 return NULL;
317 /*********************************************************************
318 * _memccpy (NTDLL.@)
320 void * __cdecl _memccpy( void *dst, const void *src, int c, size_t n )
322 unsigned char *d = dst;
323 const unsigned char *s = src;
324 while (n--) if ((*d++ = *s++) == (unsigned char)c) return d;
325 return NULL;
329 /*********************************************************************
330 * tolower (NTDLL.@)
332 int __cdecl tolower( int c )
334 return (char)c >= 'A' && (char)c <= 'Z' ? c - 'A' + 'a' : c;
338 /*********************************************************************
339 * _memicmp (NTDLL.@)
341 * Compare two blocks of memory as strings, ignoring case.
343 * PARAMS
344 * s1 [I] First string to compare to s2
345 * s2 [I] Second string to compare to s1
346 * len [I] Number of bytes to compare
348 * RETURNS
349 * An integer less than, equal to, or greater than zero indicating that
350 * s1 is less than, equal to or greater than s2 respectively.
352 * NOTES
353 * Any Nul characters in s1 or s2 are ignored. This function always
354 * compares up to len bytes or the first place where s1 and s2 differ.
356 int __cdecl _memicmp( const void *str1, const void *str2, size_t len )
358 const unsigned char *s1 = str1, *s2 = str2;
359 int ret = 0;
360 while (len--)
362 if ((ret = tolower(*s1) - tolower(*s2))) break;
363 s1++;
364 s2++;
366 return ret;
370 /*********************************************************************
371 * _strnicmp (NTDLL.@)
373 int __cdecl _strnicmp( LPCSTR str1, LPCSTR str2, size_t n )
375 int l1, l2;
377 while (n--)
379 l1 = (unsigned char)tolower(*str1);
380 l2 = (unsigned char)tolower(*str2);
381 if (l1 != l2)
383 if (sizeof(void *) > sizeof(int)) return l1 - l2;
384 return l1 - l2 > 0 ? 1 : -1;
386 if (!l1) return 0;
387 str1++;
388 str2++;
390 return 0;
394 /*********************************************************************
395 * _stricmp (NTDLL.@)
396 * _strcmpi (NTDLL.@)
398 int __cdecl _stricmp( LPCSTR str1, LPCSTR str2 )
400 return _strnicmp( str1, str2, -1 );
404 /*********************************************************************
405 * _strupr (NTDLL.@)
407 * Convert a string to upper case.
409 * PARAMS
410 * str [I/O] String to convert
412 * RETURNS
413 * str. There is no error return, if str is NULL or invalid, this
414 * function will crash.
416 LPSTR __cdecl _strupr( LPSTR str )
418 LPSTR ret = str;
419 for ( ; *str; str++) *str = RtlUpperChar(*str);
420 return ret;
424 /*********************************************************************
425 * _strlwr (NTDLL.@)
427 * Convert a string to lowercase
429 * PARAMS
430 * str [I/O] String to convert
432 * RETURNS
433 * str. There is no error return, if str is NULL or invalid, this
434 * function will crash.
436 LPSTR __cdecl _strlwr( LPSTR str )
438 LPSTR ret = str;
439 for ( ; *str; str++) *str = tolower(*str);
440 return ret;
444 /*********************************************************************
445 * toupper (NTDLL.@)
447 int __cdecl toupper( int c )
449 char str[2], *p = str;
450 WCHAR wc;
451 DWORD len;
453 str[0] = c;
454 str[1] = c >> 8;
455 wc = RtlAnsiCharToUnicodeChar( &p );
456 wc = RtlUpcaseUnicodeChar( wc );
457 RtlUnicodeToMultiByteN( str, sizeof(str), &len, &wc, sizeof(wc) );
458 if (len == 2) return ((unsigned char)str[0] << 8) + (unsigned char)str[1];
459 return (unsigned char)str[0];
463 /*********************************************************************
464 * isalnum (NTDLL.@)
466 int __cdecl isalnum( int c )
468 return ctypes[c + 1] & (C1_LOWER | C1_UPPER | C1_DIGIT);
472 /*********************************************************************
473 * isalpha (NTDLL.@)
475 int __cdecl isalpha( int c )
477 return ctypes[c + 1] & (C1_LOWER | C1_UPPER);
481 /*********************************************************************
482 * iscntrl (NTDLL.@)
484 int __cdecl iscntrl( int c )
486 return ctypes[c + 1] & C1_CNTRL;
490 /*********************************************************************
491 * isdigit (NTDLL.@)
493 int __cdecl isdigit( int c )
495 return ctypes[c + 1] & C1_DIGIT;
499 /*********************************************************************
500 * isgraph (NTDLL.@)
502 int __cdecl isgraph( int c )
504 return ctypes[c + 1] & (C1_LOWER | C1_UPPER | C1_DIGIT | C1_PUNCT);
508 /*********************************************************************
509 * islower (NTDLL.@)
511 int __cdecl islower( int c )
513 return ctypes[c + 1] & C1_LOWER;
517 /*********************************************************************
518 * isprint (NTDLL.@)
520 int __cdecl isprint( int c )
522 return ctypes[c + 1] & (C1_LOWER | C1_UPPER | C1_DIGIT | C1_PUNCT | C1_BLANK);
526 /*********************************************************************
527 * ispunct (NTDLL.@)
529 int __cdecl ispunct( int c )
531 return ctypes[c + 1] & C1_PUNCT;
535 /*********************************************************************
536 * isspace (NTDLL.@)
538 int __cdecl isspace( int c )
540 return ctypes[c + 1] & C1_SPACE;
544 /*********************************************************************
545 * isupper (NTDLL.@)
547 int __cdecl isupper( int c )
549 return ctypes[c + 1] & C1_UPPER;
553 /*********************************************************************
554 * isxdigit (NTDLL.@)
556 int __cdecl isxdigit( int c )
558 return ctypes[c + 1] & C1_XDIGIT;
562 /*********************************************************************
563 * __isascii (NTDLL.@)
565 int CDECL __isascii(int c)
567 return (unsigned)c < 0x80;
571 /*********************************************************************
572 * __toascii (NTDLL.@)
574 int CDECL __toascii(int c)
576 return (unsigned)c & 0x7f;
580 /*********************************************************************
581 * __iscsym (NTDLL.@)
583 int CDECL __iscsym(int c)
585 return (c < 127 && (isalnum(c) || c == '_'));
589 /*********************************************************************
590 * __iscsymf (NTDLL.@)
592 int CDECL __iscsymf(int c)
594 return (c < 127 && (isalpha(c) || c == '_'));
598 /*********************************************************************
599 * _toupper (NTDLL.@)
601 int CDECL _toupper(int c)
603 return c - 0x20; /* sic */
607 /*********************************************************************
608 * _tolower (NTDLL.@)
610 int CDECL _tolower(int c)
612 return c + 0x20; /* sic */
616 static int char_to_int( char c )
618 if ('0' <= c && c <= '9') return c - '0';
619 if ('A' <= c && c <= 'Z') return c - 'A' + 10;
620 if ('a' <= c && c <= 'z') return c - 'a' + 10;
621 return -1;
624 /*********************************************************************
625 * strtol (NTDLL.@)
627 __msvcrt_long __cdecl strtol( const char *s, char **end, int base )
629 BOOL negative = FALSE, empty = TRUE;
630 LONG ret = 0;
632 if (base < 0 || base == 1 || base > 36) return 0;
633 if (end) *end = (char *)s;
634 while (isspace(*s)) s++;
636 if (*s == '-')
638 negative = TRUE;
639 s++;
641 else if (*s == '+') s++;
643 if ((base == 0 || base == 16) && !char_to_int( *s ) && (s[1] == 'x' || s[1] == 'X'))
645 base = 16;
646 s += 2;
648 if (base == 0) base = char_to_int( *s ) ? 10 : 8;
650 while (*s)
652 int v = char_to_int( *s );
653 if (v < 0 || v >= base) break;
654 if (negative) v = -v;
655 s++;
656 empty = FALSE;
658 if (!negative && (ret > MAXLONG / base || ret * base > MAXLONG - v))
659 ret = MAXLONG;
660 else if (negative && (ret < (LONG)MINLONG / base || ret * base < (LONG)(MINLONG - v)))
661 ret = MINLONG;
662 else
663 ret = ret * base + v;
666 if (end && !empty) *end = (char *)s;
667 return ret;
671 /*********************************************************************
672 * strtoul (NTDLL.@)
674 __msvcrt_ulong __cdecl strtoul( const char *s, char **end, int base )
676 BOOL negative = FALSE, empty = TRUE;
677 ULONG ret = 0;
679 if (base < 0 || base == 1 || base > 36) return 0;
680 if (end) *end = (char *)s;
681 while (isspace(*s)) s++;
683 if (*s == '-')
685 negative = TRUE;
686 s++;
688 else if (*s == '+') s++;
690 if ((base == 0 || base == 16) && !char_to_int( *s ) && (s[1] == 'x' || s[1] == 'X'))
692 base = 16;
693 s += 2;
695 if (base == 0) base = char_to_int( *s ) ? 10 : 8;
697 while (*s)
699 int v = char_to_int( *s );
700 if (v < 0 || v >= base) break;
701 s++;
702 empty = FALSE;
704 if (ret > MAXDWORD / base || ret * base > MAXDWORD - v)
705 ret = MAXDWORD;
706 else
707 ret = ret * base + v;
710 if (end && !empty) *end = (char *)s;
711 return negative ? -ret : ret;
715 /*********************************************************************
716 * _ultoa (NTDLL.@)
718 * Convert an unsigned long integer to a string.
720 * RETURNS
721 * str.
723 * NOTES
724 * - Converts value to a Nul terminated string which is copied to str.
725 * - The maximum length of the copied str is 33 bytes.
726 * - Does not check if radix is in the range of 2 to 36.
727 * - If str is NULL it crashes, as the native function does.
729 char * __cdecl _ultoa( __msvcrt_ulong value, char *str, int radix )
731 char buffer[33];
732 char *pos;
733 int digit;
735 pos = &buffer[32];
736 *pos = '\0';
738 do {
739 digit = value % radix;
740 value = value / radix;
741 if (digit < 10) {
742 *--pos = '0' + digit;
743 } else {
744 *--pos = 'a' + digit - 10;
745 } /* if */
746 } while (value != 0L);
748 memcpy(str, pos, &buffer[32] - pos + 1);
749 return str;
753 /*********************************************************************
754 * _ltoa (NTDLL.@)
756 * Convert a long integer to a string.
758 * RETURNS
759 * str.
761 * NOTES
762 * - Converts value to a Nul terminated string which is copied to str.
763 * - The maximum length of the copied str is 33 bytes. If radix
764 * is 10 and value is negative, the value is converted with sign.
765 * - Does not check if radix is in the range of 2 to 36.
766 * - If str is NULL it crashes, as the native function does.
768 char * __cdecl _ltoa( __msvcrt_long value, char *str, int radix )
770 ULONG val;
771 int negative;
772 char buffer[33];
773 char *pos;
774 int digit;
776 if (value < 0 && radix == 10) {
777 negative = 1;
778 val = -value;
779 } else {
780 negative = 0;
781 val = value;
782 } /* if */
784 pos = &buffer[32];
785 *pos = '\0';
787 do {
788 digit = val % radix;
789 val = val / radix;
790 if (digit < 10) {
791 *--pos = '0' + digit;
792 } else {
793 *--pos = 'a' + digit - 10;
794 } /* if */
795 } while (val != 0L);
797 if (negative) {
798 *--pos = '-';
799 } /* if */
801 memcpy(str, pos, &buffer[32] - pos + 1);
802 return str;
806 /*********************************************************************
807 * _itoa (NTDLL.@)
809 * Converts an integer to a string.
811 * RETURNS
812 * str.
814 * NOTES
815 * - Converts value to a '\0' terminated string which is copied to str.
816 * - The maximum length of the copied str is 33 bytes. If radix
817 * is 10 and value is negative, the value is converted with sign.
818 * - Does not check if radix is in the range of 2 to 36.
819 * - If str is NULL it crashes, as the native function does.
821 char * __cdecl _itoa(
822 int value, /* [I] Value to be converted */
823 char *str, /* [O] Destination for the converted value */
824 int radix) /* [I] Number base for conversion */
826 return _ltoa(value, str, radix);
830 /*********************************************************************
831 * _ui64toa (NTDLL.@)
833 * Converts a large unsigned integer to a string.
835 * RETURNS
836 * str.
838 * NOTES
839 * - Converts value to a '\0' terminated string which is copied to str.
840 * - The maximum length of the copied str is 65 bytes.
841 * - Does not check if radix is in the range of 2 to 36.
842 * - If str is NULL it crashes, as the native function does.
844 char * __cdecl _ui64toa(
845 ULONGLONG value, /* [I] Value to be converted */
846 char *str, /* [O] Destination for the converted value */
847 int radix) /* [I] Number base for conversion */
849 char buffer[65];
850 char *pos;
851 int digit;
853 pos = &buffer[64];
854 *pos = '\0';
856 do {
857 digit = value % radix;
858 value = value / radix;
859 if (digit < 10) {
860 *--pos = '0' + digit;
861 } else {
862 *--pos = 'a' + digit - 10;
863 } /* if */
864 } while (value != 0L);
866 memcpy(str, pos, &buffer[64] - pos + 1);
867 return str;
871 /*********************************************************************
872 * _i64toa (NTDLL.@)
874 * Converts a large integer to a string.
876 * RETURNS
877 * str.
879 * NOTES
880 * - Converts value to a Nul terminated string which is copied to str.
881 * - The maximum length of the copied str is 65 bytes. If radix
882 * is 10 and value is negative, the value is converted with sign.
883 * - Does not check if radix is in the range of 2 to 36.
884 * - If str is NULL it crashes, as the native function does.
886 * DIFFERENCES
887 * - The native DLL converts negative values (for base 10) wrong:
888 *| -1 is converted to -18446744073709551615
889 *| -2 is converted to -18446744073709551614
890 *| -9223372036854775807 is converted to -9223372036854775809
891 *| -9223372036854775808 is converted to -9223372036854775808
892 * The native msvcrt _i64toa function and our ntdll _i64toa function
893 * do not have this bug.
895 char * __cdecl _i64toa(
896 LONGLONG value, /* [I] Value to be converted */
897 char *str, /* [O] Destination for the converted value */
898 int radix) /* [I] Number base for conversion */
900 ULONGLONG val;
901 int negative;
902 char buffer[65];
903 char *pos;
904 int digit;
906 if (value < 0 && radix == 10) {
907 negative = 1;
908 val = -value;
909 } else {
910 negative = 0;
911 val = value;
912 } /* if */
914 pos = &buffer[64];
915 *pos = '\0';
917 do {
918 digit = val % radix;
919 val = val / radix;
920 if (digit < 10) {
921 *--pos = '0' + digit;
922 } else {
923 *--pos = 'a' + digit - 10;
924 } /* if */
925 } while (val != 0L);
927 if (negative) {
928 *--pos = '-';
929 } /* if */
931 memcpy(str, pos, &buffer[64] - pos + 1);
932 return str;
936 /*********************************************************************
937 * _atoi64 (NTDLL.@)
939 * Convert a string to a large integer.
941 * PARAMS
942 * str [I] String to be converted
944 * RETURNS
945 * Success: The integer value represented by str.
946 * Failure: 0. Note that this cannot be distinguished from a successful
947 * return, if the string contains "0".
949 * NOTES
950 * - Accepts: {whitespace} [+|-] {digits}
951 * - No check is made for value overflow, only the lower 64 bits are assigned.
952 * - If str is NULL it crashes, as the native function does.
954 LONGLONG __cdecl _atoi64( const char *str )
956 ULONGLONG RunningTotal = 0;
957 BOOL bMinus = FALSE;
959 while (*str == ' ' || (*str >= '\011' && *str <= '\015')) {
960 str++;
961 } /* while */
963 if (*str == '+') {
964 str++;
965 } else if (*str == '-') {
966 bMinus = TRUE;
967 str++;
968 } /* if */
970 while (*str >= '0' && *str <= '9') {
971 RunningTotal = RunningTotal * 10 + *str - '0';
972 str++;
973 } /* while */
975 return bMinus ? -RunningTotal : RunningTotal;
979 /*********************************************************************
980 * atoi (NTDLL.@)
982 int __cdecl atoi( const char *nptr )
984 return _atoi64( nptr );
988 /*********************************************************************
989 * atol (NTDLL.@)
991 __msvcrt_long __cdecl atol( const char *nptr )
993 return _atoi64( nptr );
997 /* helper function for *scanf. Returns the value of character c in the
998 * given base, or -1 if the given character is not a digit of the base.
1000 static int char2digit( char c, int base )
1002 if ((c >= '0' && c <= '9') && (c <= '0'+base-1)) return (c-'0');
1003 if (base <= 10) return -1;
1004 if ((c >= 'A') && (c <= 'Z') && (c <= 'A'+base-11)) return (c-'A'+10);
1005 if ((c >= 'a') && (c <= 'z') && (c <= 'a'+base-11)) return (c-'a'+10);
1006 return -1;
1010 static int vsscanf( const char *str, const char *format, __ms_va_list ap)
1012 int rd = 0, consumed = 0;
1013 int nch;
1014 if (!*format) return 0;
1016 nch = (consumed++, *str++);
1017 if (nch == '\0')
1018 return EOF;
1020 while (*format)
1022 if (isspace( *format ))
1024 /* skip whitespace */
1025 while ((nch != '\0') && isspace( nch ))
1026 nch = (consumed++, *str++);
1028 else if (*format == '%')
1030 int st = 0;
1031 BOOLEAN suppress = 0;
1032 int width = 0;
1033 int base;
1034 int h_prefix = 0;
1035 BOOLEAN l_prefix = FALSE;
1036 BOOLEAN w_prefix = FALSE;
1037 BOOLEAN I64_prefix = FALSE;
1038 BOOLEAN prefix_finished = FALSE;
1039 format++;
1040 /* a leading asterisk means 'suppress assignment of this field' */
1041 if (*format == '*')
1043 format++;
1044 suppress = TRUE;
1046 /* look for width specification */
1047 while (isdigit( *format ))
1049 width *= 10;
1050 width += *format++ - '0';
1052 if (width == 0) width = -1; /* no width spec seen */
1053 /* read prefix (if any) */
1054 while (!prefix_finished)
1056 switch (*format)
1058 case 'h': h_prefix++; break;
1059 case 'l':
1060 if (*(format+1) == 'l')
1062 I64_prefix = TRUE;
1063 format++;
1065 l_prefix = TRUE;
1066 break;
1067 case 'w': w_prefix = TRUE; break;
1068 case 'I':
1069 if (*(format + 1) == '6' &&
1070 *(format + 2) == '4')
1072 I64_prefix = TRUE;
1073 format += 2;
1075 break;
1076 default:
1077 prefix_finished = TRUE;
1079 if (!prefix_finished) format++;
1081 /* read type */
1082 switch (*format)
1084 case 'p':
1085 case 'P': /* pointer. */
1086 if (sizeof(void *) == sizeof(LONGLONG)) I64_prefix = TRUE;
1087 /* fall through */
1088 case 'x':
1089 case 'X': /* hexadecimal integer. */
1090 base = 16;
1091 goto number;
1092 case 'o': /* octal integer */
1093 base = 8;
1094 goto number;
1095 case 'u': /* unsigned decimal integer */
1096 base = 10;
1097 goto number;
1098 case 'd': /* signed decimal integer */
1099 base = 10;
1100 goto number;
1101 case 'i': /* generic integer */
1102 base = 0;
1103 number:
1105 /* read an integer */
1106 ULONGLONG cur = 0;
1107 BOOLEAN negative = FALSE;
1108 BOOLEAN seendigit = FALSE;
1109 /* skip initial whitespace */
1110 while ((nch != '\0') && isspace( nch ))
1111 nch = (consumed++, *str++);
1112 /* get sign */
1113 if (nch == '-' || nch == '+')
1115 negative = (nch == '-');
1116 nch = (consumed++, *str++);
1117 if (width > 0) width--;
1119 /* look for leading indication of base */
1120 if (width != 0 && nch == '0' && *format != 'p' && *format != 'P')
1122 nch = (consumed++, *str++);
1123 if (width > 0) width--;
1124 seendigit = TRUE;
1125 if (width != 0 && (nch == 'x' || nch == 'X'))
1127 if (base == 0)
1128 base = 16;
1129 if (base == 16)
1131 nch = (consumed++, *str++);
1132 if (width > 0) width--;
1133 seendigit = FALSE;
1135 } else if (base == 0)
1136 base = 8;
1138 /* format %i without indication of base */
1139 if (base == 0)
1140 base = 10;
1141 /* throw away leading zeros */
1142 while (width != 0 && nch == '0')
1144 nch = (consumed++, *str++);
1145 if (width > 0) width--;
1146 seendigit = TRUE;
1148 if (width != 0 && char2digit( nch, base ) != -1)
1150 cur = char2digit( nch, base );
1151 nch = (consumed++, *str++);
1152 if (width > 0) width--;
1153 seendigit = TRUE;
1155 /* read until no more digits */
1156 while (width != 0 && nch != '\0' && char2digit( nch, base ) != -1)
1158 cur = cur*base + char2digit( nch, base );
1159 nch = (consumed++, *str++);
1160 if (width > 0) width--;
1161 seendigit = TRUE;
1163 /* okay, done! */
1164 if (!seendigit) break; /* not a valid number */
1165 st = 1;
1166 if (!suppress)
1168 #define _SET_NUMBER_( type ) *va_arg( ap, type* ) = negative ? -cur : cur
1169 if (I64_prefix) _SET_NUMBER_( LONGLONG );
1170 else if (l_prefix) _SET_NUMBER_( LONG );
1171 else if (h_prefix == 1) _SET_NUMBER_( short int );
1172 else _SET_NUMBER_( int );
1175 break;
1176 /* According to msdn,
1177 * 's' reads a character string in a call to fscanf
1178 * and 'S' a wide character string and vice versa in a
1179 * call to fwscanf. The 'h', 'w' and 'l' prefixes override
1180 * this behaviour. 'h' forces reading char * but 'l' and 'w'
1181 * force reading WCHAR. */
1182 case 's':
1183 if (w_prefix || l_prefix) goto widecharstring;
1184 else if (h_prefix) goto charstring;
1185 else goto charstring;
1186 case 'S':
1187 if (w_prefix || l_prefix) goto widecharstring;
1188 else if (h_prefix) goto charstring;
1189 else goto widecharstring;
1190 charstring:
1191 { /* read a word into a char */
1192 char *sptr = suppress ? NULL : va_arg( ap, char * );
1193 char *sptr_beg = sptr;
1194 unsigned size = UINT_MAX;
1195 /* skip initial whitespace */
1196 while (nch != '\0' && isspace( nch ))
1197 nch = (consumed++, *str++);
1198 /* read until whitespace */
1199 while (width != 0 && nch != '\0' && !isspace( nch ))
1201 if (!suppress)
1203 *sptr++ = nch;
1204 if(size > 1) size--;
1205 else
1207 *sptr_beg = 0;
1208 return rd;
1211 st++;
1212 nch = (consumed++, *str++);
1213 if (width > 0) width--;
1215 /* terminate */
1216 if (st && !suppress) *sptr = 0;
1218 break;
1219 widecharstring:
1220 { /* read a word into a WCHAR * */
1221 WCHAR *sptr = suppress ? NULL : va_arg( ap, WCHAR * );
1222 WCHAR *sptr_beg = sptr;
1223 unsigned size = UINT_MAX;
1224 /* skip initial whitespace */
1225 while (nch != '\0' && isspace( nch ))
1226 nch = (consumed++, *str++);
1227 /* read until whitespace */
1228 while (width != 0 && nch != '\0' && !isspace( nch ))
1230 if (!suppress)
1232 *sptr++ = nch;
1233 if (size > 1) size--;
1234 else
1236 *sptr_beg = 0;
1237 return rd;
1240 st++;
1241 nch = (consumed++, *str++);
1242 if (width > 0) width--;
1244 /* terminate */
1245 if (st && !suppress) *sptr = 0;
1247 break;
1248 /* 'c' and 'C work analogously to 's' and 'S' as described
1249 * above */
1250 case 'c':
1251 if (w_prefix || l_prefix) goto widecharacter;
1252 else if (h_prefix) goto character;
1253 else goto character;
1254 case 'C':
1255 if (w_prefix || l_prefix) goto widecharacter;
1256 else if (h_prefix) goto character;
1257 else goto widecharacter;
1258 character:
1259 { /* read single character into char */
1260 char *sptr = suppress ? NULL : va_arg( ap, char * );
1261 char *sptr_beg = sptr;
1262 unsigned size = UINT_MAX;
1263 if (width == -1) width = 1;
1264 while (width && nch != '\0')
1266 if (!suppress)
1268 *sptr++ = nch;
1269 if(size) size--;
1270 else
1272 *sptr_beg = 0;
1273 return rd;
1276 st++;
1277 width--;
1278 nch = (consumed++, *str++);
1281 break;
1282 widecharacter:
1283 { /* read single character into a WCHAR */
1284 WCHAR *sptr = suppress ? NULL : va_arg( ap, WCHAR * );
1285 WCHAR *sptr_beg = sptr;
1286 unsigned size = UINT_MAX;
1287 if (width == -1) width = 1;
1288 while (width && nch != '\0')
1290 if (!suppress)
1292 *sptr++ = nch;
1293 if (size) size--;
1294 else
1296 *sptr_beg = 0;
1297 return rd;
1300 st++;
1301 width--;
1302 nch = (consumed++, *str++);
1305 break;
1306 case 'n':
1308 if (!suppress)
1310 int *n = va_arg( ap, int * );
1311 *n = consumed - 1;
1313 /* This is an odd one: according to the standard,
1314 * "Execution of a %n directive does not increment the
1315 * assignment count returned at the completion of
1316 * execution" even if it wasn't suppressed with the
1317 * '*' flag. The Corrigendum to the standard seems
1318 * to contradict this (comment out the assignment to
1319 * suppress below if you want to implement these
1320 * alternate semantics) but the windows program I'm
1321 * looking at expects the behavior I've coded here
1322 * (which happens to be what glibc does as well).
1324 suppress = TRUE;
1325 st = 1;
1327 break;
1328 case '[':
1330 char *sptr = suppress ? NULL : va_arg( ap, char * );
1331 char *sptr_beg = sptr;
1332 RTL_BITMAP bitMask;
1333 ULONG Mask[8] = { 0 };
1334 BOOLEAN invert = FALSE; /* Set if we are NOT to find the chars */
1335 unsigned size = UINT_MAX;
1337 RtlInitializeBitMap( &bitMask, Mask, sizeof(Mask) * 8 );
1339 /* Read the format */
1340 format++;
1341 if (*format == '^')
1343 invert = TRUE;
1344 format++;
1346 if (*format == ']')
1348 RtlSetBits( &bitMask, ']', 1 );
1349 format++;
1351 while (*format && (*format != ']'))
1353 /* According to msdn:
1354 * "Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]." */
1355 if ((*format == '-') && (*(format + 1) != ']'))
1357 if ((*(format - 1)) < *(format + 1))
1358 RtlSetBits( &bitMask, *(format - 1) +1 , *(format + 1) - *(format - 1) );
1359 else
1360 RtlSetBits( &bitMask, *(format + 1) , *(format - 1) - *(format + 1) );
1361 format++;
1363 else
1364 RtlSetBits( &bitMask, *format, 1 );
1365 format++;
1367 /* read until char is not suitable */
1368 while (width != 0 && nch != '\0')
1370 if (!invert)
1372 if(RtlAreBitsSet( &bitMask, nch, 1 ))
1374 if (!suppress) *sptr++ = nch;
1376 else
1377 break;
1379 else
1381 if (RtlAreBitsClear( &bitMask, nch, 1 ))
1383 if (!suppress) *sptr++ = nch;
1385 else
1386 break;
1388 st++;
1389 nch = (consumed++, *str++);
1390 if (width > 0) width--;
1391 if(size > 1) size--;
1392 else
1394 if (!suppress) *sptr_beg = 0;
1395 return rd;
1398 /* terminate */
1399 if (!suppress) *sptr = 0;
1401 break;
1402 default:
1403 /* From spec: "if a percent sign is followed by a character
1404 * that has no meaning as a format-control character, that
1405 * character and the following characters are treated as
1406 * an ordinary sequence of characters, that is, a sequence
1407 * of characters that must match the input. For example,
1408 * to specify that a percent-sign character is to be input,
1409 * use %%." */
1410 while (nch != '\0' && isspace( nch ))
1411 nch = (consumed++, *str++);
1412 if (nch == *format)
1414 suppress = TRUE; /* whoops no field to be read */
1415 st = 1; /* but we got what we expected */
1416 nch = (consumed++, *str++);
1418 break;
1420 if (st && !suppress) rd++;
1421 else if (!st) break;
1423 /* A non-white-space character causes scanf to read, but not store,
1424 * a matching non-white-space character. */
1425 else
1427 if (nch == *format)
1428 nch = (consumed++, *str++);
1429 else break;
1431 format++;
1433 if (nch != '\0')
1435 consumed--, str--;
1438 return rd;
1442 /*********************************************************************
1443 * sscanf (NTDLL.@)
1445 int WINAPIV sscanf( const char *str, const char *format, ... )
1447 int ret;
1448 __ms_va_list valist;
1449 __ms_va_start( valist, format );
1450 ret = vsscanf( str, format, valist );
1451 __ms_va_end( valist );
1452 return ret;
1456 /*********************************************************************
1457 * _splitpath (NTDLL.@)
1459 * Split a path into its component pieces.
1461 * PARAMS
1462 * inpath [I] Path to split
1463 * drv [O] Destination for drive component (e.g. "A:"). Must be at least 3 characters.
1464 * dir [O] Destination for directory component. Should be at least MAX_PATH characters.
1465 * fname [O] Destination for File name component. Should be at least MAX_PATH characters.
1466 * ext [O] Destination for file extension component. Should be at least MAX_PATH characters.
1468 * RETURNS
1469 * Nothing.
1471 void __cdecl _splitpath(const char* inpath, char * drv, char * dir,
1472 char* fname, char * ext )
1474 const char *p, *end;
1476 if (inpath[0] && inpath[1] == ':')
1478 if (drv)
1480 drv[0] = inpath[0];
1481 drv[1] = inpath[1];
1482 drv[2] = 0;
1484 inpath += 2;
1486 else if (drv) drv[0] = 0;
1488 /* look for end of directory part */
1489 end = NULL;
1490 for (p = inpath; *p; p++) if (*p == '/' || *p == '\\') end = p + 1;
1492 if (end) /* got a directory */
1494 if (dir)
1496 memcpy( dir, inpath, end - inpath );
1497 dir[end - inpath] = 0;
1499 inpath = end;
1501 else if (dir) dir[0] = 0;
1503 /* look for extension: what's after the last dot */
1504 end = NULL;
1505 for (p = inpath; *p; p++) if (*p == '.') end = p;
1507 if (!end) end = p; /* there's no extension */
1509 if (fname)
1511 memcpy( fname, inpath, end - inpath );
1512 fname[end - inpath] = 0;
1514 if (ext) strcpy( ext, end );