2 * USER string functions
4 * Copyright 1993 Yngvi Sigurjonsson (yngvi@hafro.is)
5 * Copyright 1996 Alexandre Julliard
6 * Copyright 1996 Marcus Meissner
19 #include "wine/winbase16.h"
20 #include "wine/winuser16.h"
21 #include "wine/unicode.h"
22 #include "wine/exception.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 * AnsiToOem16 (KEYBOARD.5)
41 INT16 WINAPI AnsiToOem16( LPCSTR s, LPSTR d )
48 /***********************************************************************
49 * OemToAnsi16 (KEYBOARD.6)
51 INT16 WINAPI OemToAnsi16( LPCSTR s, LPSTR d )
58 /***********************************************************************
59 * AnsiToOemBuff16 (KEYBOARD.134)
61 void WINAPI AnsiToOemBuff16( LPCSTR s, LPSTR d, UINT16 len )
63 if (len != 0) CharToOemBuffA( s, d, len );
67 /***********************************************************************
68 * OemToAnsiBuff16 (KEYBOARD.135)
70 void WINAPI OemToAnsiBuff16( LPCSTR s, LPSTR d, UINT16 len )
72 if (len != 0) OemToCharBuffA( s, d, len );
76 /***********************************************************************
77 * lstrcmp16 (USER.430)
79 INT16 WINAPI lstrcmp16( LPCSTR str1, LPCSTR str2 )
81 return (INT16)strcmp( str1, str2 );
85 /***********************************************************************
86 * AnsiUpper16 (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 * AnsiLower16 (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 * AnsiUpperBuff16 (USER.437)
118 UINT16 WINAPI AnsiUpperBuff16( LPSTR str, UINT16 len )
120 CharUpperBuffA( str, len ? len : 65536 );
125 /***********************************************************************
126 * AnsiLowerBuff16 (USER.438)
128 UINT16 WINAPI AnsiLowerBuff16( LPSTR str, UINT16 len )
130 CharLowerBuffA( str, len ? len : 65536 );
135 /***********************************************************************
136 * AnsiNext16 (USER.472)
138 SEGPTR WINAPI AnsiNext16(SEGPTR current)
140 char *ptr = MapSL(current);
141 return current + (CharNextA(ptr) - ptr);
145 /***********************************************************************
146 * AnsiPrev16 (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.@)
408 * FIXME: handle current locale
410 LPWSTR WINAPI CharUpperW(LPWSTR x)
412 if (HIWORD(x)) return struprW(x);
413 else return (LPWSTR)((UINT)toupperW(LOWORD(x)));
417 /***********************************************************************
418 * CharLowerBuffA (USER32.@)
419 * FIXME: handle current locale
421 DWORD WINAPI CharLowerBuffA( LPSTR str, DWORD len )
424 if (!str) return 0; /* YES */
425 for (; len; len--, str++) *str = tolower(*str);
430 /***********************************************************************
431 * CharLowerBuffW (USER32.@)
433 DWORD WINAPI CharLowerBuffW( LPWSTR str, DWORD len )
436 if (!str) return 0; /* YES */
437 for (; len; len--, str++) *str = tolowerW(*str);
442 /***********************************************************************
443 * CharUpperBuffA (USER32.@)
444 * FIXME: handle current locale
446 DWORD WINAPI CharUpperBuffA( LPSTR str, DWORD len )
449 if (!str) return 0; /* YES */
450 for (; len; len--, str++) *str = toupper(*str);
455 /***********************************************************************
456 * CharUpperBuffW (USER32.@)
458 DWORD WINAPI CharUpperBuffW( LPWSTR str, DWORD len )
461 if (!str) return 0; /* YES */
462 for (; len; len--, str++) *str = toupperW(*str);
467 /***********************************************************************
468 * IsCharLowerA (USER.436) (USER32.@)
469 * FIXME: handle current locale
471 BOOL WINAPI IsCharLowerA(CHAR x)
477 /***********************************************************************
478 * IsCharLowerW (USER32.@)
480 BOOL WINAPI IsCharLowerW(WCHAR x)
482 return get_char_typeW(x) & C1_LOWER;
486 /***********************************************************************
487 * IsCharUpperA (USER.435) (USER32.@)
488 * FIXME: handle current locale
490 BOOL WINAPI IsCharUpperA(CHAR x)
496 /***********************************************************************
497 * IsCharUpperW (USER32.@)
499 BOOL WINAPI IsCharUpperW(WCHAR x)
501 return get_char_typeW(x) & C1_UPPER;
505 /***********************************************************************
506 * IsCharAlphaNumericW (USER32.@)
508 BOOL WINAPI IsCharAlphaNumericW(WCHAR x)
510 return get_char_typeW(x) & (C1_ALPHA|C1_DIGIT|C1_LOWER|C1_UPPER);
514 /***********************************************************************
515 * IsCharAlphaW (USER32.@)
517 BOOL WINAPI IsCharAlphaW(WCHAR x)
519 return get_char_typeW(x) & (C1_ALPHA|C1_LOWER|C1_UPPER);
523 /***********************************************************************
524 * FormatMessage16 (USER.606)
526 DWORD WINAPI FormatMessage16(
528 SEGPTR lpSource, /* [in] NOTE: not always a valid pointer */
531 LPSTR lpBuffer, /* [out] NOTE: *((HLOCAL16*)) for FORMAT_MESSAGE_ALLOCATE_BUFFER*/
533 LPDWORD args /* [in] NOTE: va_list *args */
536 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
540 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
542 LPSTR allocstring = NULL;
544 TRACE("(0x%lx,%lx,%d,0x%x,%p,%d,%p)\n",
545 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
546 if ((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
547 && (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)) return 0;
548 if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
549 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
550 || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
552 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
553 FIXME("line wrapping (%lu) not supported.\n", width);
555 if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
556 from = HEAP_strdupA( GetProcessHeap(), 0, MapSL(lpSource));
557 if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) {
558 from = HeapAlloc( GetProcessHeap(),0,200 );
559 sprintf(from,"Systemmessage, messageid = 0x%08x\n",dwMessageId);
561 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE) {
563 HINSTANCE16 hinst16 = ((HMODULE)lpSource & 0xffff);
565 dwMessageId &= 0xFFFF;
566 bufsize=LoadString16(hinst16,dwMessageId,NULL,0);
568 from = HeapAlloc( GetProcessHeap(), 0, bufsize +1);
569 LoadString16(hinst16,dwMessageId,from,bufsize+1);
572 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
576 #define ADD_TO_T(c) \
578 if (t-target == talloced) {\
579 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
580 t = target+talloced;\
589 char *fmtstr,*x,*lastf;
600 case '1':case '2':case '3':case '4':case '5':
601 case '6':case '7':case '8':case '9':
604 case '0':case '1':case '2':case '3':
605 case '4':case '5':case '6':case '7':
608 insertnr=insertnr*10+*f-'0';
617 if (NULL!=(x=strchr(f,'!'))) {
619 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
620 sprintf(fmtstr,"%%%s",f);
623 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
624 sprintf(fmtstr,"%%%s",f);
625 f+=strlen(f); /*at \0*/
631 fmtstr=HEAP_strdupA(GetProcessHeap(),0,"%s");
634 LPSTR b = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz = 100);
636 argliststart=args+insertnr-1;
638 /* CMF - This makes a BIG assumption about va_list */
639 while (vsnprintf(b, sz, fmtstr, (va_list) argliststart) < 0) {
640 b = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, b, sz += 100);
642 for (x=b; *x; x++) ADD_TO_T(*x);
644 /* NULL args - copy formatstr
647 while ((lastf<f)&&(*lastf)) {
651 HeapFree(GetProcessHeap(),0,fmtstr);
653 case '0': /* Just stop processing format string */
657 case 'n': /* 16 bit version just outputs 'n' */
662 } else { /* '\n' or '\r' gets mapped to "\r\n" */
663 if(*f == '\n' || *f == '\r') {
667 if(*f++ == '\r' && *f == '\n')
677 talloced = strlen(target)+1;
678 if (nSize && talloced<nSize) {
679 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
681 TRACE("-- %s\n",debugstr_a(target));
682 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
683 /* nSize is the MINIMUM size */
684 HLOCAL16 h = LocalAlloc16(LPTR,talloced);
685 SEGPTR ptr = LocalLock16(h);
686 allocstring = MapSL( ptr );
687 memcpy( allocstring,target,talloced);
689 *((HLOCAL16*)lpBuffer) = h;
691 lstrcpynA(lpBuffer,target,nSize);
692 HeapFree(GetProcessHeap(),0,target);
693 if (from) HeapFree(GetProcessHeap(),0,from);
694 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
699 #endif /* __i386__ */