4 * Copyright 1993 Yngvi Sigurjonsson (yngvi@hafro.is)
5 * Copyright 1996 Marcus Meissner
19 # define iswalnum(c) isalnum(c)
20 # define iswalpha(c) isalpha(c)
21 # define iswupper(c) isupper(c)
22 # define iswlower(c) islower(c)
23 #endif /* HAVE_WCTYPE_H */
29 #include "wine/winbase16.h"
30 #include "wine/winuser16.h"
31 #include "wine/unicode.h"
36 #include "stackframe.h"
38 #include "debugtools.h"
40 DEFAULT_DEBUG_CHANNEL(resource);
42 extern const WORD OLE2NLS_CT_CType3_LUT[]; /* FIXME: does not belong here */
44 /* Funny to divide them between user and kernel. */
46 /***********************************************************************
47 * IsCharAlpha (USER.433)
49 BOOL16 WINAPI IsCharAlpha16(CHAR ch)
51 return isalpha(ch); /* This is probably not right for NLS */
54 /***********************************************************************
55 * IsCharAlphaNumeric (USER.434)
57 BOOL16 WINAPI IsCharAlphaNumeric16(CHAR ch)
62 /***********************************************************************
63 * IsCharUpper (USER.435)
65 BOOL16 WINAPI IsCharUpper16(CHAR ch)
70 /***********************************************************************
71 * IsCharLower (USER.436)
73 BOOL16 WINAPI IsCharLower16(CHAR ch)
78 /***********************************************************************
79 * AnsiUpper16 (USER.431)
81 SEGPTR WINAPI AnsiUpper16( SEGPTR strOrChar )
83 /* I am not sure if the locale stuff works with toupper, but then again
84 I am not sure if the Linux libc locale stuffs works at all */
86 /* uppercase only one char if strOrChar < 0x10000 */
87 if (HIWORD(strOrChar))
90 for (s = PTR_SEG_TO_LIN(strOrChar); *s; s++) *s = toupper(*s);
93 else return toupper((char)strOrChar);
97 /***********************************************************************
98 * AnsiUpperBuff16 (USER.437)
100 UINT16 WINAPI AnsiUpperBuff16( LPSTR str, UINT16 len )
102 UINT count = len ? len : 65536;
103 for (; count; count--, str++) *str = toupper(*str);
107 /***********************************************************************
108 * AnsiLower16 (USER.432)
110 SEGPTR WINAPI AnsiLower16( SEGPTR strOrChar )
112 /* I am not sure if the locale stuff works with toupper, but then again
113 I am not sure if the Linux libc locale stuffs works at all */
115 /* lowercase only one char if strOrChar < 0x10000 */
116 if (HIWORD(strOrChar))
119 for (s = PTR_SEG_TO_LIN( strOrChar ); *s; s++) *s = tolower( *s );
122 else return tolower((char)strOrChar);
126 /***********************************************************************
127 * AnsiLowerBuff16 (USER.438)
129 UINT16 WINAPI AnsiLowerBuff16( LPSTR str, UINT16 len )
131 UINT count = len ? len : 65536;
132 for (; count; count--, str++) *str = tolower(*str);
137 /***********************************************************************
138 * AnsiNext16 (USER.472)
140 SEGPTR WINAPI AnsiNext16(SEGPTR current)
142 return (*(char *)PTR_SEG_TO_LIN(current)) ? current + 1 : current;
146 /***********************************************************************
147 * AnsiPrev16 (USER.473)
149 SEGPTR WINAPI AnsiPrev16( SEGPTR start, SEGPTR current )
151 return (current == start) ? start : current - 1;
155 /***********************************************************************
156 * CharNextA (USER32.29)
158 LPSTR WINAPI CharNextA( LPCSTR ptr )
160 if (!*ptr) return (LPSTR)ptr;
161 if (IsDBCSLeadByte( *ptr ) && (*(ptr+1) != 0) ) return (LPSTR)(ptr + 2);
162 return (LPSTR)(ptr + 1);
166 /***********************************************************************
167 * CharNextExA (USER32.30)
169 LPSTR WINAPI CharNextExA( WORD codepage, LPCSTR ptr, DWORD flags )
171 if (!*ptr) return (LPSTR)ptr;
172 if (IsDBCSLeadByteEx( codepage, *ptr ) && (*(ptr+1) != 0) ) return (LPSTR)(ptr + 2);
173 return (LPSTR)(ptr + 1);
177 /***********************************************************************
178 * CharNextExW (USER32.31)
180 LPWSTR WINAPI CharNextExW(WORD codepage,LPCWSTR x,DWORD flags)
182 /* FIXME: add DBCS / codepage stuff */
183 if (*x) return (LPWSTR)(x+1);
184 else return (LPWSTR)x;
187 /***********************************************************************
188 * CharNextW (USER32.32)
190 LPWSTR WINAPI CharNextW(LPCWSTR x)
192 if (*x) return (LPWSTR)(x+1);
193 else return (LPWSTR)x;
196 /***********************************************************************
197 * CharPrevA (USER32.33)
199 LPSTR WINAPI CharPrevA( LPCSTR start, LPCSTR ptr )
201 while (*start && (start < ptr))
203 LPCSTR next = CharNextA( start );
204 if (next >= ptr) break;
211 /***********************************************************************
212 * CharPrevExA (USER32.34)
214 LPSTR WINAPI CharPrevExA( WORD codepage, LPCSTR start, LPCSTR ptr, DWORD flags )
216 while (*start && (start < ptr))
218 LPCSTR next = CharNextExA( codepage, start, flags );
219 if (next > ptr) break;
226 /***********************************************************************
227 * CharPrevExW (USER32.35)
229 LPWSTR WINAPI CharPrevExW(WORD codepage,LPCWSTR start,LPCWSTR x,DWORD flags)
231 /* FIXME: add DBCS / codepage stuff */
232 if (x>start) return (LPWSTR)(x-1);
233 else return (LPWSTR)x;
236 /***********************************************************************
237 * CharPrevW (USER32.36)
239 LPWSTR WINAPI CharPrevW(LPCWSTR start,LPCWSTR x)
241 if (x>start) return (LPWSTR)(x-1);
242 else return (LPWSTR)x;
245 /***********************************************************************
246 * CharLowerA (USER32.25)
247 * FIXME: handle current locale
249 LPSTR WINAPI CharLowerA(LPSTR x)
263 else return (LPSTR)tolower((char)(int)x);
266 /***********************************************************************
267 * CharLowerBuffA (USER32.26)
268 * FIXME: handle current locale
270 DWORD WINAPI CharLowerBuffA(LPSTR x,DWORD buflen)
274 if (!x) return 0; /* YES */
275 while (*x && (buflen--))
284 /***********************************************************************
285 * CharLowerBuffW (USER32.27)
287 DWORD WINAPI CharLowerBuffW(LPWSTR x,DWORD buflen)
291 if (!x) return 0; /* YES */
292 while (*x && (buflen--))
301 /***********************************************************************
302 * CharLowerW (USER32.28)
303 * FIXME: handle current locale
305 LPWSTR WINAPI CharLowerW(LPWSTR x)
317 else return (LPWSTR)((UINT)tolowerW(LOWORD(x)));
320 /***********************************************************************
321 * CharUpperA (USER32.41)
322 * FIXME: handle current locale
324 LPSTR WINAPI CharUpperA(LPSTR x)
336 return (LPSTR)toupper((char)(int)x);
339 /***********************************************************************
340 * CharUpperBuffA (USER32.42)
341 * FIXME: handle current locale
343 DWORD WINAPI CharUpperBuffA(LPSTR x,DWORD buflen)
347 if (!x) return 0; /* YES */
348 while (*x && (buflen--))
357 /***********************************************************************
358 * CharUpperBuffW (USER32.43)
359 * FIXME: handle current locale
361 DWORD WINAPI CharUpperBuffW(LPWSTR x,DWORD buflen)
365 if (!x) return 0; /* YES */
366 while (*x && (buflen--))
375 /***********************************************************************
376 * CharUpperW (USER32.44)
377 * FIXME: handle current locale
379 LPWSTR WINAPI CharUpperW(LPWSTR x)
391 else return (LPWSTR)((UINT)toupperW(LOWORD(x)));
394 /***********************************************************************
395 * IsCharAlphaA (USER32.331)
396 * FIXME: handle current locale
398 BOOL WINAPI IsCharAlphaA(CHAR x)
400 return (OLE2NLS_CT_CType3_LUT[(unsigned char)x] & C3_ALPHA);
403 /***********************************************************************
404 * IsCharAlphaNumericA (USER32.332)
405 * FIXME: handle current locale
407 BOOL WINAPI IsCharAlphaNumericA(CHAR x)
409 return IsCharAlphaA(x) || isdigit(x) ;
412 /***********************************************************************
413 * IsCharAlphaNumericW (USER32.333)
414 * FIXME: handle current locale
416 BOOL WINAPI IsCharAlphaNumericW(WCHAR x)
421 /***********************************************************************
422 * IsCharAlphaW (USER32.334)
423 * FIXME: handle current locale
425 BOOL WINAPI IsCharAlphaW(WCHAR x)
430 /***********************************************************************
431 * IsCharLowerA (USER32.335)
432 * FIXME: handle current locale
434 BOOL WINAPI IsCharLowerA(CHAR x)
439 /***********************************************************************
440 * IsCharLowerW (USER32.336)
441 * FIXME: handle current locale
443 BOOL WINAPI IsCharLowerW(WCHAR x)
448 /***********************************************************************
449 * IsCharUpperA (USER32.337)
450 * FIXME: handle current locale
452 BOOL WINAPI IsCharUpperA(CHAR x)
457 /***********************************************************************
458 * IsCharUpperW (USER32.338)
459 * FIXME: handle current locale
461 BOOL WINAPI IsCharUpperW(WCHAR x)
466 /***********************************************************************
467 * FormatMessage16 (USER.606)
469 DWORD WINAPI FormatMessage16(
471 SEGPTR lpSource, /*not always a valid pointer*/
474 LPSTR lpBuffer, /* *((HLOCAL16*)) for FORMAT_MESSAGE_ALLOCATE_BUFFER*/
476 LPDWORD args /* va_list *args */
479 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
483 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
485 LPSTR allocstring = NULL;
487 TRACE("(0x%lx,%lx,%d,0x%x,%p,%d,%p)\n",
488 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
489 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
490 FIXME("line wrapping (%lu) not supported.\n", width);
492 if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
493 from = HEAP_strdupA( GetProcessHeap(), 0, PTR_SEG_TO_LIN(lpSource));
494 if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) {
495 from = HeapAlloc( GetProcessHeap(),0,200 );
496 sprintf(from,"Systemmessage, messageid = 0x%08x\n",dwMessageId);
498 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE) {
500 HINSTANCE16 hinst16 = ((HMODULE)lpSource & 0xffff);
502 dwMessageId &= 0xFFFF;
503 bufsize=LoadString16(hinst16,dwMessageId,NULL,0);
505 from = HeapAlloc( GetProcessHeap(), 0, bufsize +1);
506 LoadString16(hinst16,dwMessageId,from,bufsize+1);
509 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
513 #define ADD_TO_T(c) \
515 if (t-target == talloced) {\
516 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
517 t = target+talloced;\
526 char *fmtstr,*x,*lastf;
537 case '1':case '2':case '3':case '4':case '5':
538 case '6':case '7':case '8':case '9':
541 case '0':case '1':case '2':case '3':
542 case '4':case '5':case '6':case '7':
545 insertnr=insertnr*10+*f-'0';
554 if (NULL!=(x=strchr(f,'!'))) {
556 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
557 sprintf(fmtstr,"%%%s",f);
560 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
561 sprintf(fmtstr,"%%%s",f);
562 f+=strlen(f); /*at \0*/
568 fmtstr=HEAP_strdupA(GetProcessHeap(),0,"%s");
571 LPSTR b = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz = 100);
573 argliststart=args+insertnr-1;
575 /* CMF - This makes a BIG assumption about va_list */
576 while (wvsnprintf16(b, sz, fmtstr, (va_list) argliststart) < 0) {
577 b = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, b, sz += 100);
579 for (x=b; *x; x++) ADD_TO_T(*x);
581 /* NULL args - copy formatstr
584 while ((lastf<f)&&(*lastf)) {
588 HeapFree(GetProcessHeap(),0,fmtstr);
590 case '0': /* Just stop processing format string */
594 case 'n': /* 16 bit version just outputs 'n' */
599 } else { /* '\n' or '\r' gets mapped to "\r\n" */
600 if(*f == '\n' || *f == '\r') {
604 if(*f++ == '\r' && *f == '\n')
614 talloced = strlen(target)+1;
615 if (nSize && talloced<nSize) {
616 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
618 TRACE("-- %s\n",debugstr_a(target));
619 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
620 /* nSize is the MINIMUM size */
621 *((HLOCAL16*)lpBuffer)= LocalAlloc16(LPTR,talloced);
622 allocstring=PTR_SEG_OFF_TO_LIN(CURRENT_DS,*((HLOCAL16*)lpBuffer));
623 memcpy( allocstring,target,talloced);
625 lstrcpynA(lpBuffer,target,nSize);
626 HeapFree(GetProcessHeap(),0,target);
627 if (from) HeapFree(GetProcessHeap(),0,from);
628 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
633 #endif /* __i386__ */
637 /***********************************************************************
638 * FormatMessageA (KERNEL32.138)
639 * FIXME: missing wrap,FROM_SYSTEM message-loading,
641 DWORD WINAPI FormatMessageA(
648 LPDWORD args /* va_list *args */
651 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
655 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
658 TRACE("(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
659 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
660 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
661 FIXME("line wrapping (%lu) not supported.\n", width);
663 if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
664 from = HEAP_strdupA( GetProcessHeap(), 0, (LPSTR)lpSource);
665 if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) {
666 from = HeapAlloc( GetProcessHeap(),0,200 );
667 sprintf(from,"Systemmessage, messageid = 0x%08lx\n",dwMessageId);
669 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE) {
672 dwMessageId &= 0xFFFF;
673 bufsize=LoadMessageA((HMODULE)lpSource,dwMessageId,dwLanguageId,NULL,100);
675 from = HeapAlloc( GetProcessHeap(), 0, bufsize + 1 );
676 LoadMessageA((HMODULE)lpSource,dwMessageId,dwLanguageId,from,bufsize+1);
679 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
683 #define ADD_TO_T(c) \
685 if (t-target == talloced) {\
686 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
687 t = target+talloced;\
696 char *fmtstr,*x,*lastf;
707 case '1':case '2':case '3':case '4':case '5':
708 case '6':case '7':case '8':case '9':
711 case '0':case '1':case '2':case '3':
712 case '4':case '5':case '6':case '7':
715 insertnr=insertnr*10+*f-'0';
724 if (NULL!=(x=strchr(f,'!'))) {
726 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
727 sprintf(fmtstr,"%%%s",f);
730 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
731 sprintf(fmtstr,"%%%s",f);
732 f+=strlen(f); /*at \0*/
738 fmtstr=HEAP_strdupA(GetProcessHeap(),0,"%s");
741 LPSTR b = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz = 100);
743 if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
744 argliststart=args+insertnr-1;
746 argliststart=(*(DWORD**)args)+insertnr-1;
748 /* CMF - This makes a BIG assumption about va_list */
749 while (wvsnprintfA(b, sz, fmtstr, (va_list) argliststart) < 0) {
750 b = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, b, sz += 100);
752 for (x=b; *x; x++) ADD_TO_T(*x);
754 HeapFree(GetProcessHeap(),0,b);
756 /* NULL args - copy formatstr
759 while ((lastf<f)&&(*lastf)) {
763 HeapFree(GetProcessHeap(),0,fmtstr);
778 } else { /* '\n' or '\r' gets mapped to "\r\n" */
779 if(*f == '\n' || *f == '\r') {
783 if(*f++ == '\r' && *f == '\n')
793 talloced = strlen(target)+1;
794 if (nSize && talloced<nSize) {
795 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
797 TRACE("-- %s\n",debugstr_a(target));
798 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
799 *((LPVOID*)lpBuffer) = (LPVOID)LocalAlloc(GMEM_ZEROINIT,max(nSize, talloced));
800 memcpy(*(LPSTR*)lpBuffer,target,talloced);
802 lstrcpynA(lpBuffer,target,nSize);
804 HeapFree(GetProcessHeap(),0,target);
805 if (from) HeapFree(GetProcessHeap(),0,from);
806 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
807 strlen(*(LPSTR*)lpBuffer):
811 #endif /* __i386__ */
816 /***********************************************************************
817 * FormatMessageW (KERNEL32.138)
819 DWORD WINAPI FormatMessageW(
826 LPDWORD args /* va_list *args */
829 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
833 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
836 TRACE("(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
837 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
838 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
839 FIXME("line wrapping not supported.\n");
841 if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
842 from = HEAP_strdupWtoA(GetProcessHeap(),0,(LPWSTR)lpSource);
843 if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) {
844 /* gather information from system message tables ... */
845 from = HeapAlloc( GetProcessHeap(),0,200 );
846 sprintf(from,"Systemmessage, messageid = 0x%08lx\n",dwMessageId);
848 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE) {
851 dwMessageId &= 0xFFFF;
852 bufsize=LoadMessageA((HMODULE)lpSource,dwMessageId,dwLanguageId,NULL,100);
855 from = HeapAlloc( GetProcessHeap(), 0, bufsize + 1 );
856 LoadMessageA((HMODULE)lpSource,dwMessageId,dwLanguageId,from,bufsize+1);
859 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100 );
863 #define ADD_TO_T(c) \
865 if (t-target == talloced) {\
866 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
867 t = target+talloced;\
876 char *fmtstr,*sprintfbuf,*x;
886 case '1':case '2':case '3':case '4':case '5':
887 case '6':case '7':case '8':case '9':
890 case '0':case '1':case '2':case '3':
891 case '4':case '5':case '6':case '7':
894 insertnr=insertnr*10+*f-'0';
903 if (NULL!=(x=strchr(f,'!')))
906 fmtstr=HeapAlloc( GetProcessHeap(), 0, strlen(f)+2);
907 sprintf(fmtstr,"%%%s",f);
910 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f));
911 sprintf(fmtstr,"%%%s",f);
912 f+=strlen(f); /*at \0*/
918 fmtstr=HEAP_strdupA( GetProcessHeap(),0,"%s");
919 if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
920 argliststart=args+insertnr-1;
922 argliststart=(*(DWORD**)args)+insertnr-1;
924 if (fmtstr[strlen(fmtstr)-1]=='s' && argliststart[0]) {
927 xarr[0]=(DWORD)HEAP_strdupWtoA(GetProcessHeap(),0,(LPWSTR)(*(argliststart+0)));
928 /* possible invalid pointers */
929 xarr[1]=*(argliststart+1);
930 xarr[2]=*(argliststart+2);
931 sprintfbuf=HeapAlloc(GetProcessHeap(),0,lstrlenW((LPWSTR)argliststart[0])*2+1);
933 /* CMF - This makes a BIG assumption about va_list */
934 vsprintf(sprintfbuf, fmtstr, (va_list) xarr);
936 sprintfbuf=HeapAlloc(GetProcessHeap(),0,100);
938 /* CMF - This makes a BIG assumption about va_list */
939 wvsprintfA(sprintfbuf, fmtstr, (va_list) argliststart);
945 HeapFree(GetProcessHeap(),0,sprintfbuf);
946 HeapFree(GetProcessHeap(),0,fmtstr);
961 } else { /* '\n' or '\r' gets mapped to "\r\n" */
962 if(*f == '\n' || *f == '\r') {
966 if(*f++ == '\r' && *f == '\n')
976 talloced = strlen(target)+1;
977 if (nSize && talloced<nSize)
978 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
979 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
980 /* nSize is the MINIMUM size */
981 *((LPVOID*)lpBuffer) = (LPVOID)LocalAlloc(GMEM_ZEROINIT,talloced*2+2);
982 lstrcpynAtoW(*(LPWSTR*)lpBuffer,target,talloced);
984 lstrcpynAtoW(lpBuffer,target,nSize);
985 HeapFree(GetProcessHeap(),0,target);
986 if (from) HeapFree(GetProcessHeap(),0,from);
987 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
988 lstrlenW(*(LPWSTR*)lpBuffer):
992 #endif /* __i386__ */