2 * USER string functions
4 * Copyright 1993 Yngvi Sigurjonsson (yngvi@hafro.is)
5 * Copyright 1996 Alexandre Julliard
6 * Copyright 1996 Marcus Meissner
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
24 #include "wine/port.h"
36 #include "wine/exception.h"
37 #include "wine/unicode.h"
38 #include "wine/winbase16.h"
39 #include "wine/winuser16.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(resource);
47 /* filter for page-fault exceptions */
48 static WINE_EXCEPTION_FILTER(page_fault)
50 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
51 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
52 return EXCEPTION_EXECUTE_HANDLER;
53 return EXCEPTION_CONTINUE_SEARCH;
56 /***********************************************************************
57 * AnsiToOem (KEYBOARD.5)
59 INT16 WINAPI AnsiToOem16( LPCSTR s, LPSTR d )
66 /***********************************************************************
67 * OemToAnsi (KEYBOARD.6)
69 INT16 WINAPI OemToAnsi16( LPCSTR s, LPSTR d )
76 /***********************************************************************
77 * AnsiToOemBuff (KEYBOARD.134)
79 void WINAPI AnsiToOemBuff16( LPCSTR s, LPSTR d, UINT16 len )
81 if (len != 0) CharToOemBuffA( s, d, len );
85 /***********************************************************************
86 * OemToAnsiBuff (KEYBOARD.135)
88 void WINAPI OemToAnsiBuff16( LPCSTR s, LPSTR d, UINT16 len )
90 if (len != 0) OemToCharBuffA( s, d, len );
94 /***********************************************************************
97 INT16 WINAPI lstrcmp16( LPCSTR str1, LPCSTR str2 )
99 return (INT16)strcmp( str1, str2 );
103 /***********************************************************************
104 * AnsiUpper (USER.431)
106 SEGPTR WINAPI AnsiUpper16( SEGPTR strOrChar )
108 /* uppercase only one char if strOrChar < 0x10000 */
109 if (HIWORD(strOrChar))
111 CharUpperA( MapSL(strOrChar) );
114 else return (SEGPTR)CharUpperA( (LPSTR)strOrChar );
118 /***********************************************************************
119 * AnsiLower (USER.432)
121 SEGPTR WINAPI AnsiLower16( SEGPTR strOrChar )
123 /* lowercase only one char if strOrChar < 0x10000 */
124 if (HIWORD(strOrChar))
126 CharLowerA( MapSL(strOrChar) );
129 else return (SEGPTR)CharLowerA( (LPSTR)strOrChar );
133 /***********************************************************************
134 * AnsiUpperBuff (USER.437)
136 UINT16 WINAPI AnsiUpperBuff16( LPSTR str, UINT16 len )
138 CharUpperBuffA( str, len ? len : 65536 );
143 /***********************************************************************
144 * AnsiLowerBuff (USER.438)
146 UINT16 WINAPI AnsiLowerBuff16( LPSTR str, UINT16 len )
148 CharLowerBuffA( str, len ? len : 65536 );
153 /***********************************************************************
154 * AnsiNext (USER.472)
156 SEGPTR WINAPI AnsiNext16(SEGPTR current)
158 char *ptr = MapSL(current);
159 return current + (CharNextA(ptr) - ptr);
163 /***********************************************************************
164 * AnsiPrev (USER.473)
166 SEGPTR WINAPI AnsiPrev16( LPCSTR start, SEGPTR current )
168 char *ptr = MapSL(current);
169 return current - (ptr - CharPrevA( start, ptr ));
173 /***********************************************************************
174 * CharNextA (USER32.@)
176 LPSTR WINAPI CharNextA( LPCSTR ptr )
178 if (!*ptr) return (LPSTR)ptr;
179 if (IsDBCSLeadByte( ptr[0] ) && ptr[1]) return (LPSTR)(ptr + 2);
180 return (LPSTR)(ptr + 1);
184 /***********************************************************************
185 * CharNextExA (USER32.@)
187 LPSTR WINAPI CharNextExA( WORD codepage, LPCSTR ptr, DWORD flags )
189 if (!*ptr) return (LPSTR)ptr;
190 if (IsDBCSLeadByteEx( codepage, ptr[0] ) && ptr[1]) return (LPSTR)(ptr + 2);
191 return (LPSTR)(ptr + 1);
195 /***********************************************************************
196 * CharNextExW (USER32.@)
198 LPWSTR WINAPI CharNextExW( WORD codepage, LPCWSTR ptr, DWORD flags )
200 /* doesn't make sense, there are no codepages for Unicode */
205 /***********************************************************************
206 * CharNextW (USER32.@)
208 LPWSTR WINAPI CharNextW(LPCWSTR x)
216 /***********************************************************************
217 * CharPrevA (USER32.@)
219 LPSTR WINAPI CharPrevA( LPCSTR start, LPCSTR ptr )
221 while (*start && (start < ptr))
223 LPCSTR next = CharNextA( start );
224 if (next >= ptr) break;
231 /***********************************************************************
232 * CharPrevExA (USER32.@)
234 LPSTR WINAPI CharPrevExA( WORD codepage, LPCSTR start, LPCSTR ptr, DWORD flags )
236 while (*start && (start < ptr))
238 LPCSTR next = CharNextExA( codepage, start, flags );
239 if (next >= ptr) break;
246 /***********************************************************************
247 * CharPrevExW (USER32.@)
249 LPSTR WINAPI CharPrevExW( WORD codepage, LPCWSTR start, LPCWSTR ptr, DWORD flags )
251 /* doesn't make sense, there are no codepages for Unicode */
256 /***********************************************************************
257 * CharPrevW (USER32.@)
259 LPWSTR WINAPI CharPrevW(LPCWSTR start,LPCWSTR x)
261 if (x>start) return (LPWSTR)(x-1);
262 else return (LPWSTR)x;
266 /***********************************************************************
267 * CharToOemA (USER32.@)
269 BOOL WINAPI CharToOemA( LPCSTR s, LPSTR d )
271 if ( !s || !d ) return TRUE;
272 return CharToOemBuffA( s, d, strlen( s ) + 1 );
276 /***********************************************************************
277 * CharToOemBuffA (USER32.@)
279 BOOL WINAPI CharToOemBuffA( LPCSTR s, LPSTR d, DWORD len )
283 bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
286 MultiByteToWideChar( CP_ACP, 0, s, len, bufW, len );
287 WideCharToMultiByte( CP_OEMCP, 0, bufW, len, d, len, NULL, NULL );
288 HeapFree( GetProcessHeap(), 0, bufW );
294 /***********************************************************************
295 * CharToOemBuffW (USER32.@)
297 BOOL WINAPI CharToOemBuffW( LPCWSTR s, LPSTR d, DWORD len )
299 if ( !s || !d ) return TRUE;
300 WideCharToMultiByte( CP_OEMCP, 0, s, len, d, len, NULL, NULL );
305 /***********************************************************************
306 * CharToOemW (USER32.@)
308 BOOL WINAPI CharToOemW( LPCWSTR s, LPSTR d )
310 return CharToOemBuffW( s, d, strlenW( s ) + 1 );
314 /***********************************************************************
315 * OemToCharA (USER32.@)
317 BOOL WINAPI OemToCharA( LPCSTR s, LPSTR d )
319 return OemToCharBuffA( s, d, strlen( s ) + 1 );
323 /***********************************************************************
324 * OemToCharBuffA (USER32.@)
326 BOOL WINAPI OemToCharBuffA( LPCSTR s, LPSTR d, DWORD len )
330 bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
333 MultiByteToWideChar( CP_OEMCP, 0, s, len, bufW, len );
334 WideCharToMultiByte( CP_ACP, 0, bufW, len, d, len, NULL, NULL );
335 HeapFree( GetProcessHeap(), 0, bufW );
341 /***********************************************************************
342 * OemToCharBuffW (USER32.@)
344 BOOL WINAPI OemToCharBuffW( LPCSTR s, LPWSTR d, DWORD len )
346 MultiByteToWideChar( CP_OEMCP, 0, s, len, d, len );
351 /***********************************************************************
352 * OemToCharW (USER32.@)
354 BOOL WINAPI OemToCharW( LPCSTR s, LPWSTR d )
356 return OemToCharBuffW( s, d, strlen( s ) + 1 );
360 /***********************************************************************
361 * CharLowerA (USER32.@)
363 LPSTR WINAPI CharLowerA(LPSTR str)
367 char ch = LOWORD(str);
368 CharLowerBuffA( &ch, 1 );
369 return (LPSTR)(UINT_PTR)(BYTE)ch;
374 CharLowerBuffA( str, strlen(str) );
378 SetLastError( ERROR_INVALID_PARAMETER );
386 /***********************************************************************
387 * CharUpperA (USER32.@)
389 LPSTR WINAPI CharUpperA(LPSTR str)
393 char ch = LOWORD(str);
394 CharUpperBuffA( &ch, 1 );
395 return (LPSTR)(UINT_PTR)(BYTE)ch;
400 CharUpperBuffA( str, strlen(str) );
404 SetLastError( ERROR_INVALID_PARAMETER );
412 /***********************************************************************
413 * CharLowerW (USER32.@)
415 LPWSTR WINAPI CharLowerW(LPWSTR x)
417 if (HIWORD(x)) return strlwrW(x);
418 else return (LPWSTR)((UINT)tolowerW(LOWORD(x)));
422 /***********************************************************************
423 * CharUpperW (USER32.@)
425 LPWSTR WINAPI CharUpperW(LPWSTR x)
427 if (HIWORD(x)) return struprW(x);
428 else return (LPWSTR)((UINT)toupperW(LOWORD(x)));
432 /***********************************************************************
433 * CharLowerBuffA (USER32.@)
435 DWORD WINAPI CharLowerBuffA( LPSTR str, DWORD len )
439 WCHAR *strW = buffer;
441 if (!str) return 0; /* YES */
443 lenW = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0);
444 if (lenW > sizeof(buffer)/sizeof(WCHAR))
446 strW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
449 MultiByteToWideChar(CP_ACP, 0, str, len, strW, lenW);
450 CharLowerBuffW(strW, lenW);
451 len = WideCharToMultiByte(CP_ACP, 0, strW, lenW, str, len, NULL, NULL);
452 if (strW != buffer) HeapFree(GetProcessHeap(), 0, strW);
457 /***********************************************************************
458 * CharLowerBuffW (USER32.@)
460 DWORD WINAPI CharLowerBuffW( LPWSTR str, DWORD len )
463 if (!str) return 0; /* YES */
464 for (; len; len--, str++) *str = tolowerW(*str);
469 /***********************************************************************
470 * CharUpperBuffA (USER32.@)
472 DWORD WINAPI CharUpperBuffA( LPSTR str, DWORD len )
476 WCHAR *strW = buffer;
478 if (!str) return 0; /* YES */
480 lenW = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0);
481 if (lenW > sizeof(buffer)/sizeof(WCHAR))
483 strW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
486 MultiByteToWideChar(CP_ACP, 0, str, len, strW, lenW);
487 CharUpperBuffW(strW, lenW);
488 len = WideCharToMultiByte(CP_ACP, 0, strW, lenW, str, len, NULL, NULL);
489 if (strW != buffer) HeapFree(GetProcessHeap(), 0, strW);
494 /***********************************************************************
495 * CharUpperBuffW (USER32.@)
497 DWORD WINAPI CharUpperBuffW( LPWSTR str, DWORD len )
500 if (!str) return 0; /* YES */
501 for (; len; len--, str++) *str = toupperW(*str);
506 /***********************************************************************
507 * IsCharLower (USER.436)
508 * IsCharLowerA (USER32.@)
510 BOOL WINAPI IsCharLowerA(CHAR x)
513 MultiByteToWideChar(CP_ACP, 0, &x, 1, &wch, 1);
514 return IsCharLowerW(wch);
518 /***********************************************************************
519 * IsCharLowerW (USER32.@)
521 BOOL WINAPI IsCharLowerW(WCHAR x)
523 return (get_char_typeW(x) & C1_LOWER) != 0;
527 /***********************************************************************
528 * IsCharUpper (USER.435)
529 * IsCharUpperA (USER32.@)
531 BOOL WINAPI IsCharUpperA(CHAR x)
534 MultiByteToWideChar(CP_ACP, 0, &x, 1, &wch, 1);
535 return IsCharUpperW(wch);
539 /***********************************************************************
540 * IsCharUpperW (USER32.@)
542 BOOL WINAPI IsCharUpperW(WCHAR x)
544 return (get_char_typeW(x) & C1_UPPER) != 0;
548 /***********************************************************************
549 * IsCharAlphaNumeric (USER.434)
550 * IsCharAlphaNumericA (USER32.@)
552 BOOL WINAPI IsCharAlphaNumericA(CHAR x)
555 MultiByteToWideChar(CP_ACP, 0, &x, 1, &wch, 1);
556 return IsCharAlphaNumericW(wch);
560 /***********************************************************************
561 * IsCharAlphaNumericW (USER32.@)
563 BOOL WINAPI IsCharAlphaNumericW(WCHAR x)
565 return (get_char_typeW(x) & (C1_ALPHA|C1_DIGIT)) != 0;
569 /***********************************************************************
570 * IsCharAlpha (USER.433)
571 * IsCharAlphaA (USER32.@)
573 BOOL WINAPI IsCharAlphaA(CHAR x)
576 MultiByteToWideChar(CP_ACP, 0, &x, 1, &wch, 1);
577 return IsCharAlphaW(wch);
581 /***********************************************************************
582 * IsCharAlphaW (USER32.@)
584 BOOL WINAPI IsCharAlphaW(WCHAR x)
586 return (get_char_typeW(x) & C1_ALPHA) != 0;
590 /***********************************************************************
591 * FormatMessage (USER.606)
593 DWORD WINAPI FormatMessage16(
595 SEGPTR lpSource, /* [in] NOTE: not always a valid pointer */
598 LPSTR lpBuffer, /* [out] NOTE: *((HLOCAL16*)) for FORMAT_MESSAGE_ALLOCATE_BUFFER*/
600 LPDWORD args /* [in] NOTE: va_list *args */
603 /* This implementation is completely dependent on the format of the va_list on x86 CPUs */
607 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
609 LPSTR allocstring = NULL;
611 TRACE("(0x%lx,%lx,%d,0x%x,%p,%d,%p)\n",
612 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
613 if ((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
614 && (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)) return 0;
615 if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
616 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
617 || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
619 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
620 FIXME("line wrapping (%lu) not supported.\n", width);
622 if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
624 char *source = MapSL(lpSource);
625 from = HeapAlloc( GetProcessHeap(), 0, strlen(source)+1 );
626 strcpy( from, source );
628 if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) {
629 from = HeapAlloc( GetProcessHeap(),0,200 );
630 sprintf(from,"Systemmessage, messageid = 0x%08x\n",dwMessageId);
632 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE) {
634 HINSTANCE16 hinst16 = ((HINSTANCE16)lpSource & 0xffff);
636 dwMessageId &= 0xFFFF;
637 bufsize=LoadString16(hinst16,dwMessageId,NULL,0);
639 from = HeapAlloc( GetProcessHeap(), 0, bufsize +1);
640 LoadString16(hinst16,dwMessageId,from,bufsize+1);
643 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
647 #define ADD_TO_T(c) \
649 if (t-target == talloced) {\
650 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
651 t = target+talloced;\
660 char *fmtstr,*x,*lastf;
671 case '1':case '2':case '3':case '4':case '5':
672 case '6':case '7':case '8':case '9':
675 case '0':case '1':case '2':case '3':
676 case '4':case '5':case '6':case '7':
679 insertnr=insertnr*10+*f-'0';
688 if (NULL!=(x=strchr(f,'!'))) {
690 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
691 sprintf(fmtstr,"%%%s",f);
694 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
695 sprintf(fmtstr,"%%%s",f);
696 f+=strlen(f); /*at \0*/
702 fmtstr=HeapAlloc( GetProcessHeap(), 0, 3 );
703 strcpy( fmtstr, "%s" );
708 LPSTR b = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz = 100);
710 argliststart=args+insertnr-1;
712 /* CMF - This makes a BIG assumption about va_list */
713 while ((ret = vsnprintf(b, sz, fmtstr, (va_list) argliststart) < 0) || (ret >= sz)) {
714 sz = (ret == -1 ? sz + 100 : ret + 1);
715 b = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, b, sz);
717 for (x=b; *x; x++) ADD_TO_T(*x);
718 HeapFree(GetProcessHeap(), 0, b);
720 /* NULL args - copy formatstr
723 while ((lastf<f)&&(*lastf)) {
727 HeapFree(GetProcessHeap(),0,fmtstr);
729 case '0': /* Just stop processing format string */
733 case 'n': /* 16 bit version just outputs 'n' */
738 } else { /* '\n' or '\r' gets mapped to "\r\n" */
739 if(*f == '\n' || *f == '\r') {
743 if(*f++ == '\r' && *f == '\n')
753 talloced = strlen(target)+1;
754 if (nSize && talloced<nSize) {
755 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
757 TRACE("-- %s\n",debugstr_a(target));
758 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
759 /* nSize is the MINIMUM size */
760 HLOCAL16 h = LocalAlloc16(LPTR,talloced);
761 SEGPTR ptr = LocalLock16(h);
762 allocstring = MapSL( ptr );
763 memcpy( allocstring,target,talloced);
765 *((HLOCAL16*)lpBuffer) = h;
767 lstrcpynA(lpBuffer,target,nSize);
768 HeapFree(GetProcessHeap(),0,target);
769 HeapFree(GetProcessHeap(),0,from);
770 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
775 #endif /* __i386__ */