2 * USER string functions
4 * Copyright 1993 Yngvi Sigurjonsson (yngvi@hafro.is)
5 * Copyright 1996 Alexandre Julliard
6 * Copyright 1996 Marcus Meissner
20 #include "wine/exception.h"
21 #include "wine/unicode.h"
22 #include "wine/winbase16.h"
23 #include "wine/winuser16.h"
25 #include "debugtools.h"
27 DEFAULT_DEBUG_CHANNEL(resource);
29 /* filter for page-fault exceptions */
30 static WINE_EXCEPTION_FILTER(page_fault)
32 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
33 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
34 return EXCEPTION_EXECUTE_HANDLER;
35 return EXCEPTION_CONTINUE_SEARCH;
38 /***********************************************************************
39 * AnsiToOem (KEYBOARD.5)
41 INT16 WINAPI AnsiToOem16( LPCSTR s, LPSTR d )
48 /***********************************************************************
49 * OemToAnsi (KEYBOARD.6)
51 INT16 WINAPI OemToAnsi16( LPCSTR s, LPSTR d )
58 /***********************************************************************
59 * AnsiToOemBuff (KEYBOARD.134)
61 void WINAPI AnsiToOemBuff16( LPCSTR s, LPSTR d, UINT16 len )
63 if (len != 0) CharToOemBuffA( s, d, len );
67 /***********************************************************************
68 * OemToAnsiBuff (KEYBOARD.135)
70 void WINAPI OemToAnsiBuff16( LPCSTR s, LPSTR d, UINT16 len )
72 if (len != 0) OemToCharBuffA( s, d, len );
76 /***********************************************************************
79 INT16 WINAPI lstrcmp16( LPCSTR str1, LPCSTR str2 )
81 return (INT16)strcmp( str1, str2 );
85 /***********************************************************************
86 * AnsiUpper (USER.431)
88 SEGPTR WINAPI AnsiUpper16( SEGPTR strOrChar )
90 /* uppercase only one char if strOrChar < 0x10000 */
91 if (HIWORD(strOrChar))
93 CharUpperA( MapSL(strOrChar) );
96 else return toupper((char)strOrChar);
100 /***********************************************************************
101 * AnsiLower (USER.432)
103 SEGPTR WINAPI AnsiLower16( SEGPTR strOrChar )
105 /* lowercase only one char if strOrChar < 0x10000 */
106 if (HIWORD(strOrChar))
108 CharLowerA( MapSL(strOrChar) );
111 else return tolower((char)strOrChar);
115 /***********************************************************************
116 * AnsiUpperBuff (USER.437)
118 UINT16 WINAPI AnsiUpperBuff16( LPSTR str, UINT16 len )
120 CharUpperBuffA( str, len ? len : 65536 );
125 /***********************************************************************
126 * AnsiLowerBuff (USER.438)
128 UINT16 WINAPI AnsiLowerBuff16( LPSTR str, UINT16 len )
130 CharLowerBuffA( str, len ? len : 65536 );
135 /***********************************************************************
136 * AnsiNext (USER.472)
138 SEGPTR WINAPI AnsiNext16(SEGPTR current)
140 char *ptr = MapSL(current);
141 return current + (CharNextA(ptr) - ptr);
145 /***********************************************************************
146 * AnsiPrev (USER.473)
148 SEGPTR WINAPI AnsiPrev16( LPCSTR start, SEGPTR current )
150 char *ptr = MapSL(current);
151 return current - (ptr - CharPrevA( start, ptr ));
155 /***********************************************************************
156 * CharNextA (USER32.@)
158 LPSTR WINAPI CharNextA( LPCSTR ptr )
160 if (!*ptr) return (LPSTR)ptr;
161 if (IsDBCSLeadByte( ptr[0] ) && ptr[1]) return (LPSTR)(ptr + 2);
162 return (LPSTR)(ptr + 1);
166 /***********************************************************************
167 * CharNextExA (USER32.@)
169 LPSTR WINAPI CharNextExA( WORD codepage, LPCSTR ptr, DWORD flags )
171 if (!*ptr) return (LPSTR)ptr;
172 if (IsDBCSLeadByteEx( codepage, ptr[0] ) && ptr[1]) return (LPSTR)(ptr + 2);
173 return (LPSTR)(ptr + 1);
177 /***********************************************************************
178 * CharNextExW (USER32.@)
180 LPWSTR WINAPI CharNextExW( WORD codepage, LPCWSTR ptr, DWORD flags )
182 /* doesn't make sense, there are no codepages for Unicode */
187 /***********************************************************************
188 * CharNextW (USER32.@)
190 LPWSTR WINAPI CharNextW(LPCWSTR x)
198 /***********************************************************************
199 * CharPrevA (USER32.@)
201 LPSTR WINAPI CharPrevA( LPCSTR start, LPCSTR ptr )
203 while (*start && (start < ptr))
205 LPCSTR next = CharNextA( start );
206 if (next >= ptr) break;
213 /***********************************************************************
214 * CharPrevExA (USER32.@)
216 LPSTR WINAPI CharPrevExA( WORD codepage, LPCSTR start, LPCSTR ptr, DWORD flags )
218 while (*start && (start < ptr))
220 LPCSTR next = CharNextExA( codepage, start, flags );
221 if (next > ptr) break;
228 /***********************************************************************
229 * CharPrevExW (USER32.@)
231 LPSTR WINAPI CharPrevExW( WORD codepage, LPCWSTR start, LPCWSTR ptr, DWORD flags )
233 /* doesn't make sense, there are no codepages for Unicode */
238 /***********************************************************************
239 * CharPrevW (USER32.@)
241 LPWSTR WINAPI CharPrevW(LPCWSTR start,LPCWSTR x)
243 if (x>start) return (LPWSTR)(x-1);
244 else return (LPWSTR)x;
248 /***********************************************************************
249 * CharToOemA (USER32.@)
251 BOOL WINAPI CharToOemA( LPCSTR s, LPSTR d )
253 if ( !s || !d ) return TRUE;
254 return CharToOemBuffA( s, d, strlen( s ) + 1 );
258 /***********************************************************************
259 * CharToOemBuffA (USER32.@)
261 BOOL WINAPI CharToOemBuffA( LPCSTR s, LPSTR d, DWORD len )
265 bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
268 MultiByteToWideChar( CP_ACP, 0, s, len, bufW, len );
269 WideCharToMultiByte( CP_OEMCP, 0, bufW, len, d, len, NULL, NULL );
270 HeapFree( GetProcessHeap(), 0, bufW );
276 /***********************************************************************
277 * CharToOemBuffW (USER32.@)
279 BOOL WINAPI CharToOemBuffW( LPCWSTR s, LPSTR d, DWORD len )
281 if ( !s || !d ) return TRUE;
282 WideCharToMultiByte( CP_OEMCP, 0, s, len, d, len, NULL, NULL );
287 /***********************************************************************
288 * CharToOemW (USER32.@)
290 BOOL WINAPI CharToOemW( LPCWSTR s, LPSTR d )
292 return CharToOemBuffW( s, d, strlenW( s ) + 1 );
296 /***********************************************************************
297 * OemToCharA (USER32.@)
299 BOOL WINAPI OemToCharA( LPCSTR s, LPSTR d )
301 return OemToCharBuffA( s, d, strlen( s ) + 1 );
305 /***********************************************************************
306 * OemToCharBuffA (USER32.@)
308 BOOL WINAPI OemToCharBuffA( LPCSTR s, LPSTR d, DWORD len )
312 bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
315 MultiByteToWideChar( CP_OEMCP, 0, s, len, bufW, len );
316 WideCharToMultiByte( CP_ACP, 0, bufW, len, d, len, NULL, NULL );
317 HeapFree( GetProcessHeap(), 0, bufW );
323 /***********************************************************************
324 * OemToCharBuffW (USER32.@)
326 BOOL WINAPI OemToCharBuffW( LPCSTR s, LPWSTR d, DWORD len )
328 MultiByteToWideChar( CP_OEMCP, 0, s, len, d, len );
333 /***********************************************************************
334 * OemToCharW (USER32.@)
336 BOOL WINAPI OemToCharW( LPCSTR s, LPWSTR d )
338 return OemToCharBuffW( s, d, strlen( s ) + 1 );
342 /***********************************************************************
343 * CharLowerA (USER32.@)
344 * FIXME: handle current locale
346 LPSTR WINAPI CharLowerA(LPSTR x)
348 if (!HIWORD(x)) return (LPSTR)tolower((char)(int)x);
361 SetLastError( ERROR_INVALID_PARAMETER );
369 /***********************************************************************
370 * CharUpperA (USER32.@)
371 * FIXME: handle current locale
373 LPSTR WINAPI CharUpperA(LPSTR x)
375 if (!HIWORD(x)) return (LPSTR)toupper((char)(int)x);
388 SetLastError( ERROR_INVALID_PARAMETER );
396 /***********************************************************************
397 * CharLowerW (USER32.@)
399 LPWSTR WINAPI CharLowerW(LPWSTR x)
401 if (HIWORD(x)) return strlwrW(x);
402 else return (LPWSTR)((UINT)tolowerW(LOWORD(x)));
406 /***********************************************************************
407 * CharUpperW (USER32.@)
409 LPWSTR WINAPI CharUpperW(LPWSTR x)
411 if (HIWORD(x)) return struprW(x);
412 else return (LPWSTR)((UINT)toupperW(LOWORD(x)));
416 /***********************************************************************
417 * CharLowerBuffA (USER32.@)
419 DWORD WINAPI CharLowerBuffA( LPSTR str, DWORD len )
423 if (!str) return 0; /* YES */
425 lenW = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0);
426 strW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
429 MultiByteToWideChar(CP_ACP, 0, str, len, strW, lenW);
430 CharLowerBuffW(strW, lenW);
431 len = WideCharToMultiByte(CP_ACP, 0, strW, lenW, str, len, NULL, NULL);
432 HeapFree(GetProcessHeap(), 0, strW);
439 /***********************************************************************
440 * CharLowerBuffW (USER32.@)
442 DWORD WINAPI CharLowerBuffW( LPWSTR str, DWORD len )
445 if (!str) return 0; /* YES */
446 for (; len; len--, str++) *str = tolowerW(*str);
451 /***********************************************************************
452 * CharUpperBuffA (USER32.@)
454 DWORD WINAPI CharUpperBuffA( LPSTR str, DWORD len )
458 if (!str) return 0; /* YES */
460 lenW = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0);
461 strW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
464 MultiByteToWideChar(CP_ACP, 0, str, len, strW, lenW);
465 CharUpperBuffW(strW, lenW);
466 len = WideCharToMultiByte(CP_ACP, 0, strW, lenW, str, len, NULL, NULL);
467 HeapFree(GetProcessHeap(), 0, strW);
474 /***********************************************************************
475 * CharUpperBuffW (USER32.@)
477 DWORD WINAPI CharUpperBuffW( LPWSTR str, DWORD len )
480 if (!str) return 0; /* YES */
481 for (; len; len--, str++) *str = toupperW(*str);
486 /***********************************************************************
487 * IsCharLower (USER.436)
488 * IsCharLowerA (USER32.@)
490 BOOL WINAPI IsCharLowerA(CHAR x)
493 MultiByteToWideChar(CP_ACP, 0, &x, 1, &wch, 1);
494 return IsCharLowerW(wch);
498 /***********************************************************************
499 * IsCharLowerW (USER32.@)
501 BOOL WINAPI IsCharLowerW(WCHAR x)
503 return (get_char_typeW(x) & C1_LOWER) != 0;
507 /***********************************************************************
508 * IsCharUpper (USER.435)
509 * IsCharUpperA (USER32.@)
511 BOOL WINAPI IsCharUpperA(CHAR x)
514 MultiByteToWideChar(CP_ACP, 0, &x, 1, &wch, 1);
515 return IsCharUpperW(wch);
519 /***********************************************************************
520 * IsCharUpperW (USER32.@)
522 BOOL WINAPI IsCharUpperW(WCHAR x)
524 return (get_char_typeW(x) & C1_UPPER) != 0;
528 /***********************************************************************
529 * IsCharAlphaNumeric (USER.434)
530 * IsCharAlphaNumericA (USER32.@)
532 BOOL WINAPI IsCharAlphaNumericA(CHAR x)
535 MultiByteToWideChar(CP_ACP, 0, &x, 1, &wch, 1);
536 return IsCharAlphaNumericW(wch);
540 /***********************************************************************
541 * IsCharAlphaNumericW (USER32.@)
543 BOOL WINAPI IsCharAlphaNumericW(WCHAR x)
545 return (get_char_typeW(x) & (C1_ALPHA|C1_DIGIT)) != 0;
549 /***********************************************************************
550 * IsCharAlpha (USER.433)
551 * IsCharAlphaA (USER32.@)
553 BOOL WINAPI IsCharAlphaA(CHAR x)
556 MultiByteToWideChar(CP_ACP, 0, &x, 1, &wch, 1);
557 return IsCharAlphaW(wch);
561 /***********************************************************************
562 * IsCharAlphaW (USER32.@)
564 BOOL WINAPI IsCharAlphaW(WCHAR x)
566 return (get_char_typeW(x) & C1_ALPHA) != 0;
570 /***********************************************************************
571 * FormatMessage (USER.606)
573 DWORD WINAPI FormatMessage16(
575 SEGPTR lpSource, /* [in] NOTE: not always a valid pointer */
578 LPSTR lpBuffer, /* [out] NOTE: *((HLOCAL16*)) for FORMAT_MESSAGE_ALLOCATE_BUFFER*/
580 LPDWORD args /* [in] NOTE: va_list *args */
583 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
587 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
589 LPSTR allocstring = NULL;
591 TRACE("(0x%lx,%lx,%d,0x%x,%p,%d,%p)\n",
592 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
593 if ((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
594 && (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)) return 0;
595 if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
596 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
597 || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
599 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
600 FIXME("line wrapping (%lu) not supported.\n", width);
602 if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
604 char *source = MapSL(lpSource);
605 from = HeapAlloc( GetProcessHeap(), 0, strlen(source)+1 );
606 strcpy( from, source );
608 if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) {
609 from = HeapAlloc( GetProcessHeap(),0,200 );
610 sprintf(from,"Systemmessage, messageid = 0x%08x\n",dwMessageId);
612 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE) {
614 HINSTANCE16 hinst16 = ((HMODULE)lpSource & 0xffff);
616 dwMessageId &= 0xFFFF;
617 bufsize=LoadString16(hinst16,dwMessageId,NULL,0);
619 from = HeapAlloc( GetProcessHeap(), 0, bufsize +1);
620 LoadString16(hinst16,dwMessageId,from,bufsize+1);
623 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
627 #define ADD_TO_T(c) \
629 if (t-target == talloced) {\
630 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
631 t = target+talloced;\
640 char *fmtstr,*x,*lastf;
651 case '1':case '2':case '3':case '4':case '5':
652 case '6':case '7':case '8':case '9':
655 case '0':case '1':case '2':case '3':
656 case '4':case '5':case '6':case '7':
659 insertnr=insertnr*10+*f-'0';
668 if (NULL!=(x=strchr(f,'!'))) {
670 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
671 sprintf(fmtstr,"%%%s",f);
674 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
675 sprintf(fmtstr,"%%%s",f);
676 f+=strlen(f); /*at \0*/
682 fmtstr=HeapAlloc( GetProcessHeap(), 0, 3 );
683 strcpy( fmtstr, "%s" );
688 LPSTR b = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz = 100);
690 argliststart=args+insertnr-1;
692 /* CMF - This makes a BIG assumption about va_list */
693 while ((ret = vsnprintf(b, sz, fmtstr, (va_list) argliststart) < 0) || (ret >= sz)) {
694 sz = (ret == -1 ? sz + 100 : ret + 1);
695 b = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, b, sz);
697 for (x=b; *x; x++) ADD_TO_T(*x);
699 /* NULL args - copy formatstr
702 while ((lastf<f)&&(*lastf)) {
706 HeapFree(GetProcessHeap(),0,fmtstr);
708 case '0': /* Just stop processing format string */
712 case 'n': /* 16 bit version just outputs 'n' */
717 } else { /* '\n' or '\r' gets mapped to "\r\n" */
718 if(*f == '\n' || *f == '\r') {
722 if(*f++ == '\r' && *f == '\n')
732 talloced = strlen(target)+1;
733 if (nSize && talloced<nSize) {
734 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
736 TRACE("-- %s\n",debugstr_a(target));
737 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
738 /* nSize is the MINIMUM size */
739 HLOCAL16 h = LocalAlloc16(LPTR,talloced);
740 SEGPTR ptr = LocalLock16(h);
741 allocstring = MapSL( ptr );
742 memcpy( allocstring,target,talloced);
744 *((HLOCAL16*)lpBuffer) = h;
746 lstrcpynA(lpBuffer,target,nSize);
747 HeapFree(GetProcessHeap(),0,target);
748 if (from) HeapFree(GetProcessHeap(),0,from);
749 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
754 #endif /* __i386__ */