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
34 #include "wine/exception.h"
35 #include "wine/unicode.h"
36 #include "wine/winbase16.h"
37 #include "wine/winuser16.h"
39 #include "msvcrt/excpt.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(resource);
45 /* filter for page-fault exceptions */
46 static WINE_EXCEPTION_FILTER(page_fault)
48 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
49 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
50 return EXCEPTION_EXECUTE_HANDLER;
51 return EXCEPTION_CONTINUE_SEARCH;
54 /***********************************************************************
55 * AnsiToOem (KEYBOARD.5)
57 INT16 WINAPI AnsiToOem16( LPCSTR s, LPSTR d )
64 /***********************************************************************
65 * OemToAnsi (KEYBOARD.6)
67 INT16 WINAPI OemToAnsi16( LPCSTR s, LPSTR d )
74 /***********************************************************************
75 * AnsiToOemBuff (KEYBOARD.134)
77 void WINAPI AnsiToOemBuff16( LPCSTR s, LPSTR d, UINT16 len )
79 if (len != 0) CharToOemBuffA( s, d, len );
83 /***********************************************************************
84 * OemToAnsiBuff (KEYBOARD.135)
86 void WINAPI OemToAnsiBuff16( LPCSTR s, LPSTR d, UINT16 len )
88 if (len != 0) OemToCharBuffA( s, d, len );
92 /***********************************************************************
95 INT16 WINAPI lstrcmp16( LPCSTR str1, LPCSTR str2 )
97 return (INT16)strcmp( str1, str2 );
101 /***********************************************************************
102 * AnsiUpper (USER.431)
104 SEGPTR WINAPI AnsiUpper16( SEGPTR strOrChar )
106 /* uppercase only one char if strOrChar < 0x10000 */
107 if (HIWORD(strOrChar))
109 CharUpperA( MapSL(strOrChar) );
112 else return toupper((char)strOrChar);
116 /***********************************************************************
117 * AnsiLower (USER.432)
119 SEGPTR WINAPI AnsiLower16( SEGPTR strOrChar )
121 /* lowercase only one char if strOrChar < 0x10000 */
122 if (HIWORD(strOrChar))
124 CharLowerA( MapSL(strOrChar) );
127 else return tolower((char)strOrChar);
131 /***********************************************************************
132 * AnsiUpperBuff (USER.437)
134 UINT16 WINAPI AnsiUpperBuff16( LPSTR str, UINT16 len )
136 CharUpperBuffA( str, len ? len : 65536 );
141 /***********************************************************************
142 * AnsiLowerBuff (USER.438)
144 UINT16 WINAPI AnsiLowerBuff16( LPSTR str, UINT16 len )
146 CharLowerBuffA( str, len ? len : 65536 );
151 /***********************************************************************
152 * AnsiNext (USER.472)
154 SEGPTR WINAPI AnsiNext16(SEGPTR current)
156 char *ptr = MapSL(current);
157 return current + (CharNextA(ptr) - ptr);
161 /***********************************************************************
162 * AnsiPrev (USER.473)
164 SEGPTR WINAPI AnsiPrev16( LPCSTR start, SEGPTR current )
166 char *ptr = MapSL(current);
167 return current - (ptr - CharPrevA( start, ptr ));
171 /***********************************************************************
172 * CharNextA (USER32.@)
174 LPSTR WINAPI CharNextA( LPCSTR ptr )
176 if (!*ptr) return (LPSTR)ptr;
177 if (IsDBCSLeadByte( ptr[0] ) && ptr[1]) return (LPSTR)(ptr + 2);
178 return (LPSTR)(ptr + 1);
182 /***********************************************************************
183 * CharNextExA (USER32.@)
185 LPSTR WINAPI CharNextExA( WORD codepage, LPCSTR ptr, DWORD flags )
187 if (!*ptr) return (LPSTR)ptr;
188 if (IsDBCSLeadByteEx( codepage, ptr[0] ) && ptr[1]) return (LPSTR)(ptr + 2);
189 return (LPSTR)(ptr + 1);
193 /***********************************************************************
194 * CharNextExW (USER32.@)
196 LPWSTR WINAPI CharNextExW( WORD codepage, LPCWSTR ptr, DWORD flags )
198 /* doesn't make sense, there are no codepages for Unicode */
203 /***********************************************************************
204 * CharNextW (USER32.@)
206 LPWSTR WINAPI CharNextW(LPCWSTR x)
214 /***********************************************************************
215 * CharPrevA (USER32.@)
217 LPSTR WINAPI CharPrevA( LPCSTR start, LPCSTR ptr )
219 while (*start && (start < ptr))
221 LPCSTR next = CharNextA( start );
222 if (next >= ptr) break;
229 /***********************************************************************
230 * CharPrevExA (USER32.@)
232 LPSTR WINAPI CharPrevExA( WORD codepage, LPCSTR start, LPCSTR ptr, DWORD flags )
234 while (*start && (start < ptr))
236 LPCSTR next = CharNextExA( codepage, start, flags );
237 if (next > ptr) break;
244 /***********************************************************************
245 * CharPrevExW (USER32.@)
247 LPSTR WINAPI CharPrevExW( WORD codepage, LPCWSTR start, LPCWSTR ptr, DWORD flags )
249 /* doesn't make sense, there are no codepages for Unicode */
254 /***********************************************************************
255 * CharPrevW (USER32.@)
257 LPWSTR WINAPI CharPrevW(LPCWSTR start,LPCWSTR x)
259 if (x>start) return (LPWSTR)(x-1);
260 else return (LPWSTR)x;
264 /***********************************************************************
265 * CharToOemA (USER32.@)
267 BOOL WINAPI CharToOemA( LPCSTR s, LPSTR d )
269 if ( !s || !d ) return TRUE;
270 return CharToOemBuffA( s, d, strlen( s ) + 1 );
274 /***********************************************************************
275 * CharToOemBuffA (USER32.@)
277 BOOL WINAPI CharToOemBuffA( LPCSTR s, LPSTR d, DWORD len )
281 bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
284 MultiByteToWideChar( CP_ACP, 0, s, len, bufW, len );
285 WideCharToMultiByte( CP_OEMCP, 0, bufW, len, d, len, NULL, NULL );
286 HeapFree( GetProcessHeap(), 0, bufW );
292 /***********************************************************************
293 * CharToOemBuffW (USER32.@)
295 BOOL WINAPI CharToOemBuffW( LPCWSTR s, LPSTR d, DWORD len )
297 if ( !s || !d ) return TRUE;
298 WideCharToMultiByte( CP_OEMCP, 0, s, len, d, len, NULL, NULL );
303 /***********************************************************************
304 * CharToOemW (USER32.@)
306 BOOL WINAPI CharToOemW( LPCWSTR s, LPSTR d )
308 return CharToOemBuffW( s, d, strlenW( s ) + 1 );
312 /***********************************************************************
313 * OemToCharA (USER32.@)
315 BOOL WINAPI OemToCharA( LPCSTR s, LPSTR d )
317 return OemToCharBuffA( s, d, strlen( s ) + 1 );
321 /***********************************************************************
322 * OemToCharBuffA (USER32.@)
324 BOOL WINAPI OemToCharBuffA( LPCSTR s, LPSTR d, DWORD len )
328 bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
331 MultiByteToWideChar( CP_OEMCP, 0, s, len, bufW, len );
332 WideCharToMultiByte( CP_ACP, 0, bufW, len, d, len, NULL, NULL );
333 HeapFree( GetProcessHeap(), 0, bufW );
339 /***********************************************************************
340 * OemToCharBuffW (USER32.@)
342 BOOL WINAPI OemToCharBuffW( LPCSTR s, LPWSTR d, DWORD len )
344 MultiByteToWideChar( CP_OEMCP, 0, s, len, d, len );
349 /***********************************************************************
350 * OemToCharW (USER32.@)
352 BOOL WINAPI OemToCharW( LPCSTR s, LPWSTR d )
354 return OemToCharBuffW( s, d, strlen( s ) + 1 );
358 /***********************************************************************
359 * CharLowerA (USER32.@)
360 * FIXME: handle current locale
362 LPSTR WINAPI CharLowerA(LPSTR x)
364 if (!HIWORD(x)) return (LPSTR)tolower((char)(int)x);
377 SetLastError( ERROR_INVALID_PARAMETER );
385 /***********************************************************************
386 * CharUpperA (USER32.@)
387 * FIXME: handle current locale
389 LPSTR WINAPI CharUpperA(LPSTR x)
391 if (!HIWORD(x)) return (LPSTR)toupper((char)(int)x);
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 if (!str) return 0; /* YES */
441 lenW = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0);
442 strW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
445 MultiByteToWideChar(CP_ACP, 0, str, len, strW, lenW);
446 CharLowerBuffW(strW, lenW);
447 len = WideCharToMultiByte(CP_ACP, 0, strW, lenW, str, len, NULL, NULL);
448 HeapFree(GetProcessHeap(), 0, strW);
455 /***********************************************************************
456 * CharLowerBuffW (USER32.@)
458 DWORD WINAPI CharLowerBuffW( LPWSTR str, DWORD len )
461 if (!str) return 0; /* YES */
462 for (; len; len--, str++) *str = tolowerW(*str);
467 /***********************************************************************
468 * CharUpperBuffA (USER32.@)
470 DWORD WINAPI CharUpperBuffA( LPSTR str, DWORD len )
474 if (!str) return 0; /* YES */
476 lenW = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0);
477 strW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
480 MultiByteToWideChar(CP_ACP, 0, str, len, strW, lenW);
481 CharUpperBuffW(strW, lenW);
482 len = WideCharToMultiByte(CP_ACP, 0, strW, lenW, str, len, NULL, NULL);
483 HeapFree(GetProcessHeap(), 0, strW);
490 /***********************************************************************
491 * CharUpperBuffW (USER32.@)
493 DWORD WINAPI CharUpperBuffW( LPWSTR str, DWORD len )
496 if (!str) return 0; /* YES */
497 for (; len; len--, str++) *str = toupperW(*str);
502 /***********************************************************************
503 * IsCharLower (USER.436)
504 * IsCharLowerA (USER32.@)
506 BOOL WINAPI IsCharLowerA(CHAR x)
509 MultiByteToWideChar(CP_ACP, 0, &x, 1, &wch, 1);
510 return IsCharLowerW(wch);
514 /***********************************************************************
515 * IsCharLowerW (USER32.@)
517 BOOL WINAPI IsCharLowerW(WCHAR x)
519 return (get_char_typeW(x) & C1_LOWER) != 0;
523 /***********************************************************************
524 * IsCharUpper (USER.435)
525 * IsCharUpperA (USER32.@)
527 BOOL WINAPI IsCharUpperA(CHAR x)
530 MultiByteToWideChar(CP_ACP, 0, &x, 1, &wch, 1);
531 return IsCharUpperW(wch);
535 /***********************************************************************
536 * IsCharUpperW (USER32.@)
538 BOOL WINAPI IsCharUpperW(WCHAR x)
540 return (get_char_typeW(x) & C1_UPPER) != 0;
544 /***********************************************************************
545 * IsCharAlphaNumeric (USER.434)
546 * IsCharAlphaNumericA (USER32.@)
548 BOOL WINAPI IsCharAlphaNumericA(CHAR x)
551 MultiByteToWideChar(CP_ACP, 0, &x, 1, &wch, 1);
552 return IsCharAlphaNumericW(wch);
556 /***********************************************************************
557 * IsCharAlphaNumericW (USER32.@)
559 BOOL WINAPI IsCharAlphaNumericW(WCHAR x)
561 return (get_char_typeW(x) & (C1_ALPHA|C1_DIGIT)) != 0;
565 /***********************************************************************
566 * IsCharAlpha (USER.433)
567 * IsCharAlphaA (USER32.@)
569 BOOL WINAPI IsCharAlphaA(CHAR x)
572 MultiByteToWideChar(CP_ACP, 0, &x, 1, &wch, 1);
573 return IsCharAlphaW(wch);
577 /***********************************************************************
578 * IsCharAlphaW (USER32.@)
580 BOOL WINAPI IsCharAlphaW(WCHAR x)
582 return (get_char_typeW(x) & C1_ALPHA) != 0;
586 /***********************************************************************
587 * FormatMessage (USER.606)
589 DWORD WINAPI FormatMessage16(
591 SEGPTR lpSource, /* [in] NOTE: not always a valid pointer */
594 LPSTR lpBuffer, /* [out] NOTE: *((HLOCAL16*)) for FORMAT_MESSAGE_ALLOCATE_BUFFER*/
596 LPDWORD args /* [in] NOTE: va_list *args */
599 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
603 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
605 LPSTR allocstring = NULL;
607 TRACE("(0x%lx,%lx,%d,0x%x,%p,%d,%p)\n",
608 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
609 if ((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
610 && (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)) return 0;
611 if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
612 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
613 || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
615 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
616 FIXME("line wrapping (%lu) not supported.\n", width);
618 if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
620 char *source = MapSL(lpSource);
621 from = HeapAlloc( GetProcessHeap(), 0, strlen(source)+1 );
622 strcpy( from, source );
624 if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) {
625 from = HeapAlloc( GetProcessHeap(),0,200 );
626 sprintf(from,"Systemmessage, messageid = 0x%08x\n",dwMessageId);
628 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE) {
630 HINSTANCE16 hinst16 = ((HMODULE)lpSource & 0xffff);
632 dwMessageId &= 0xFFFF;
633 bufsize=LoadString16(hinst16,dwMessageId,NULL,0);
635 from = HeapAlloc( GetProcessHeap(), 0, bufsize +1);
636 LoadString16(hinst16,dwMessageId,from,bufsize+1);
639 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
643 #define ADD_TO_T(c) \
645 if (t-target == talloced) {\
646 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
647 t = target+talloced;\
656 char *fmtstr,*x,*lastf;
667 case '1':case '2':case '3':case '4':case '5':
668 case '6':case '7':case '8':case '9':
671 case '0':case '1':case '2':case '3':
672 case '4':case '5':case '6':case '7':
675 insertnr=insertnr*10+*f-'0';
684 if (NULL!=(x=strchr(f,'!'))) {
686 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
687 sprintf(fmtstr,"%%%s",f);
690 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
691 sprintf(fmtstr,"%%%s",f);
692 f+=strlen(f); /*at \0*/
698 fmtstr=HeapAlloc( GetProcessHeap(), 0, 3 );
699 strcpy( fmtstr, "%s" );
704 LPSTR b = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz = 100);
706 argliststart=args+insertnr-1;
708 /* CMF - This makes a BIG assumption about va_list */
709 while ((ret = vsnprintf(b, sz, fmtstr, (va_list) argliststart) < 0) || (ret >= sz)) {
710 sz = (ret == -1 ? sz + 100 : ret + 1);
711 b = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, b, sz);
713 for (x=b; *x; x++) ADD_TO_T(*x);
715 /* NULL args - copy formatstr
718 while ((lastf<f)&&(*lastf)) {
722 HeapFree(GetProcessHeap(),0,fmtstr);
724 case '0': /* Just stop processing format string */
728 case 'n': /* 16 bit version just outputs 'n' */
733 } else { /* '\n' or '\r' gets mapped to "\r\n" */
734 if(*f == '\n' || *f == '\r') {
738 if(*f++ == '\r' && *f == '\n')
748 talloced = strlen(target)+1;
749 if (nSize && talloced<nSize) {
750 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
752 TRACE("-- %s\n",debugstr_a(target));
753 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
754 /* nSize is the MINIMUM size */
755 HLOCAL16 h = LocalAlloc16(LPTR,talloced);
756 SEGPTR ptr = LocalLock16(h);
757 allocstring = MapSL( ptr );
758 memcpy( allocstring,target,talloced);
760 *((HLOCAL16*)lpBuffer) = h;
762 lstrcpynA(lpBuffer,target,nSize);
763 HeapFree(GetProcessHeap(),0,target);
764 if (from) HeapFree(GetProcessHeap(),0,from);
765 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
770 #endif /* __i386__ */