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"
37 #include "wine/exception.h"
38 #include "wine/unicode.h"
39 #include "wine/winbase16.h"
40 #include "wine/winuser16.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(resource);
48 /* filter for page-fault exceptions */
49 static WINE_EXCEPTION_FILTER(page_fault)
51 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
52 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
53 return EXCEPTION_EXECUTE_HANDLER;
54 return EXCEPTION_CONTINUE_SEARCH;
57 /***********************************************************************
58 * AnsiToOem (KEYBOARD.5)
60 INT16 WINAPI AnsiToOem16( LPCSTR s, LPSTR d )
67 /***********************************************************************
68 * OemToAnsi (KEYBOARD.6)
70 INT16 WINAPI OemToAnsi16( LPCSTR s, LPSTR d )
77 /***********************************************************************
78 * AnsiToOemBuff (KEYBOARD.134)
80 void WINAPI AnsiToOemBuff16( LPCSTR s, LPSTR d, UINT16 len )
82 if (len != 0) CharToOemBuffA( s, d, len );
86 /***********************************************************************
87 * OemToAnsiBuff (KEYBOARD.135)
89 void WINAPI OemToAnsiBuff16( LPCSTR s, LPSTR d, UINT16 len )
91 if (len != 0) OemToCharBuffA( s, d, len );
95 /***********************************************************************
98 INT16 WINAPI lstrcmp16( LPCSTR str1, LPCSTR str2 )
100 return (INT16)strcmp( str1, str2 );
104 /***********************************************************************
105 * AnsiUpper (USER.431)
107 SEGPTR WINAPI AnsiUpper16( SEGPTR strOrChar )
109 /* uppercase only one char if strOrChar < 0x10000 */
110 if (HIWORD(strOrChar))
112 CharUpperA( MapSL(strOrChar) );
115 else return (SEGPTR)CharUpperA( (LPSTR)strOrChar );
119 /***********************************************************************
120 * AnsiLower (USER.432)
122 SEGPTR WINAPI AnsiLower16( SEGPTR strOrChar )
124 /* lowercase only one char if strOrChar < 0x10000 */
125 if (HIWORD(strOrChar))
127 CharLowerA( MapSL(strOrChar) );
130 else return (SEGPTR)CharLowerA( (LPSTR)strOrChar );
134 /***********************************************************************
135 * AnsiUpperBuff (USER.437)
137 UINT16 WINAPI AnsiUpperBuff16( LPSTR str, UINT16 len )
139 CharUpperBuffA( str, len ? len : 65536 );
144 /***********************************************************************
145 * AnsiLowerBuff (USER.438)
147 UINT16 WINAPI AnsiLowerBuff16( LPSTR str, UINT16 len )
149 CharLowerBuffA( str, len ? len : 65536 );
154 /***********************************************************************
155 * AnsiNext (USER.472)
157 SEGPTR WINAPI AnsiNext16(SEGPTR current)
159 char *ptr = MapSL(current);
160 return current + (CharNextA(ptr) - ptr);
164 /***********************************************************************
165 * AnsiPrev (USER.473)
167 SEGPTR WINAPI AnsiPrev16( LPCSTR start, SEGPTR current )
169 char *ptr = MapSL(current);
170 return current - (ptr - CharPrevA( start, ptr ));
174 /***********************************************************************
175 * CharNextA (USER32.@)
177 LPSTR WINAPI CharNextA( LPCSTR ptr )
179 if (!*ptr) return (LPSTR)ptr;
180 if (IsDBCSLeadByte( ptr[0] ) && ptr[1]) return (LPSTR)(ptr + 2);
181 return (LPSTR)(ptr + 1);
185 /***********************************************************************
186 * CharNextExA (USER32.@)
188 LPSTR WINAPI CharNextExA( WORD codepage, LPCSTR ptr, DWORD flags )
190 if (!*ptr) return (LPSTR)ptr;
191 if (IsDBCSLeadByteEx( codepage, ptr[0] ) && ptr[1]) return (LPSTR)(ptr + 2);
192 return (LPSTR)(ptr + 1);
196 /***********************************************************************
197 * CharNextExW (USER32.@)
199 LPWSTR WINAPI CharNextExW( WORD codepage, LPCWSTR ptr, DWORD flags )
201 /* doesn't make sense, there are no codepages for Unicode */
206 /***********************************************************************
207 * CharNextW (USER32.@)
209 LPWSTR WINAPI CharNextW(LPCWSTR x)
217 /***********************************************************************
218 * CharPrevA (USER32.@)
220 LPSTR WINAPI CharPrevA( LPCSTR start, LPCSTR ptr )
222 while (*start && (start < ptr))
224 LPCSTR next = CharNextA( start );
225 if (next >= ptr) break;
232 /***********************************************************************
233 * CharPrevExA (USER32.@)
235 LPSTR WINAPI CharPrevExA( WORD codepage, LPCSTR start, LPCSTR ptr, DWORD flags )
237 while (*start && (start < ptr))
239 LPCSTR next = CharNextExA( codepage, start, flags );
240 if (next > ptr) break;
247 /***********************************************************************
248 * CharPrevExW (USER32.@)
250 LPSTR WINAPI CharPrevExW( WORD codepage, LPCWSTR start, LPCWSTR ptr, DWORD flags )
252 /* doesn't make sense, there are no codepages for Unicode */
257 /***********************************************************************
258 * CharPrevW (USER32.@)
260 LPWSTR WINAPI CharPrevW(LPCWSTR start,LPCWSTR x)
262 if (x>start) return (LPWSTR)(x-1);
263 else return (LPWSTR)x;
267 /***********************************************************************
268 * CharToOemA (USER32.@)
270 BOOL WINAPI CharToOemA( LPCSTR s, LPSTR d )
272 if ( !s || !d ) return TRUE;
273 return CharToOemBuffA( s, d, strlen( s ) + 1 );
277 /***********************************************************************
278 * CharToOemBuffA (USER32.@)
280 BOOL WINAPI CharToOemBuffA( LPCSTR s, LPSTR d, DWORD len )
284 bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
287 MultiByteToWideChar( CP_ACP, 0, s, len, bufW, len );
288 WideCharToMultiByte( CP_OEMCP, 0, bufW, len, d, len, NULL, NULL );
289 HeapFree( GetProcessHeap(), 0, bufW );
295 /***********************************************************************
296 * CharToOemBuffW (USER32.@)
298 BOOL WINAPI CharToOemBuffW( LPCWSTR s, LPSTR d, DWORD len )
300 if ( !s || !d ) return TRUE;
301 WideCharToMultiByte( CP_OEMCP, 0, s, len, d, len, NULL, NULL );
306 /***********************************************************************
307 * CharToOemW (USER32.@)
309 BOOL WINAPI CharToOemW( LPCWSTR s, LPSTR d )
311 return CharToOemBuffW( s, d, strlenW( s ) + 1 );
315 /***********************************************************************
316 * OemToCharA (USER32.@)
318 BOOL WINAPI OemToCharA( LPCSTR s, LPSTR d )
320 return OemToCharBuffA( s, d, strlen( s ) + 1 );
324 /***********************************************************************
325 * OemToCharBuffA (USER32.@)
327 BOOL WINAPI OemToCharBuffA( LPCSTR s, LPSTR d, DWORD len )
331 bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
334 MultiByteToWideChar( CP_OEMCP, 0, s, len, bufW, len );
335 WideCharToMultiByte( CP_ACP, 0, bufW, len, d, len, NULL, NULL );
336 HeapFree( GetProcessHeap(), 0, bufW );
342 /***********************************************************************
343 * OemToCharBuffW (USER32.@)
345 BOOL WINAPI OemToCharBuffW( LPCSTR s, LPWSTR d, DWORD len )
347 MultiByteToWideChar( CP_OEMCP, 0, s, len, d, len );
352 /***********************************************************************
353 * OemToCharW (USER32.@)
355 BOOL WINAPI OemToCharW( LPCSTR s, LPWSTR d )
357 return OemToCharBuffW( s, d, strlen( s ) + 1 );
361 /***********************************************************************
362 * CharLowerA (USER32.@)
364 LPSTR WINAPI CharLowerA(LPSTR str)
368 char ch = LOWORD(str);
369 CharLowerBuffA( &ch, 1 );
370 return (LPSTR)(UINT_PTR)(BYTE)ch;
375 CharLowerBuffA( str, strlen(str) );
379 SetLastError( ERROR_INVALID_PARAMETER );
387 /***********************************************************************
388 * CharUpperA (USER32.@)
390 LPSTR WINAPI CharUpperA(LPSTR str)
394 char ch = LOWORD(str);
395 CharUpperBuffA( &ch, 1 );
396 return (LPSTR)(UINT_PTR)(BYTE)ch;
401 CharUpperBuffA( str, strlen(str) );
405 SetLastError( ERROR_INVALID_PARAMETER );
413 /***********************************************************************
414 * CharLowerW (USER32.@)
416 LPWSTR WINAPI CharLowerW(LPWSTR x)
418 if (HIWORD(x)) return strlwrW(x);
419 else return (LPWSTR)((UINT)tolowerW(LOWORD(x)));
423 /***********************************************************************
424 * CharUpperW (USER32.@)
426 LPWSTR WINAPI CharUpperW(LPWSTR x)
428 if (HIWORD(x)) return struprW(x);
429 else return (LPWSTR)((UINT)toupperW(LOWORD(x)));
433 /***********************************************************************
434 * CharLowerBuffA (USER32.@)
436 DWORD WINAPI CharLowerBuffA( LPSTR str, DWORD len )
440 WCHAR *strW = buffer;
442 if (!str) return 0; /* YES */
444 lenW = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0);
445 if (lenW > sizeof(buffer)/sizeof(WCHAR))
447 strW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
450 MultiByteToWideChar(CP_ACP, 0, str, len, strW, lenW);
451 CharLowerBuffW(strW, lenW);
452 len = WideCharToMultiByte(CP_ACP, 0, strW, lenW, str, len, NULL, NULL);
453 if (strW != buffer) HeapFree(GetProcessHeap(), 0, strW);
458 /***********************************************************************
459 * CharLowerBuffW (USER32.@)
461 DWORD WINAPI CharLowerBuffW( LPWSTR str, DWORD len )
464 if (!str) return 0; /* YES */
465 for (; len; len--, str++) *str = tolowerW(*str);
470 /***********************************************************************
471 * CharUpperBuffA (USER32.@)
473 DWORD WINAPI CharUpperBuffA( LPSTR str, DWORD len )
477 WCHAR *strW = buffer;
479 if (!str) return 0; /* YES */
481 lenW = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0);
482 if (lenW > sizeof(buffer)/sizeof(WCHAR))
484 strW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
487 MultiByteToWideChar(CP_ACP, 0, str, len, strW, lenW);
488 CharUpperBuffW(strW, lenW);
489 len = WideCharToMultiByte(CP_ACP, 0, strW, lenW, str, len, NULL, NULL);
490 if (strW != buffer) HeapFree(GetProcessHeap(), 0, strW);
495 /***********************************************************************
496 * CharUpperBuffW (USER32.@)
498 DWORD WINAPI CharUpperBuffW( LPWSTR str, DWORD len )
501 if (!str) return 0; /* YES */
502 for (; len; len--, str++) *str = toupperW(*str);
507 /***********************************************************************
508 * IsCharLower (USER.436)
509 * IsCharLowerA (USER32.@)
511 BOOL WINAPI IsCharLowerA(CHAR x)
514 MultiByteToWideChar(CP_ACP, 0, &x, 1, &wch, 1);
515 return IsCharLowerW(wch);
519 /***********************************************************************
520 * IsCharLowerW (USER32.@)
522 BOOL WINAPI IsCharLowerW(WCHAR x)
524 return (get_char_typeW(x) & C1_LOWER) != 0;
528 /***********************************************************************
529 * IsCharUpper (USER.435)
530 * IsCharUpperA (USER32.@)
532 BOOL WINAPI IsCharUpperA(CHAR x)
535 MultiByteToWideChar(CP_ACP, 0, &x, 1, &wch, 1);
536 return IsCharUpperW(wch);
540 /***********************************************************************
541 * IsCharUpperW (USER32.@)
543 BOOL WINAPI IsCharUpperW(WCHAR x)
545 return (get_char_typeW(x) & C1_UPPER) != 0;
549 /***********************************************************************
550 * IsCharAlphaNumeric (USER.434)
551 * IsCharAlphaNumericA (USER32.@)
553 BOOL WINAPI IsCharAlphaNumericA(CHAR x)
556 MultiByteToWideChar(CP_ACP, 0, &x, 1, &wch, 1);
557 return IsCharAlphaNumericW(wch);
561 /***********************************************************************
562 * IsCharAlphaNumericW (USER32.@)
564 BOOL WINAPI IsCharAlphaNumericW(WCHAR x)
566 return (get_char_typeW(x) & (C1_ALPHA|C1_DIGIT)) != 0;
570 /***********************************************************************
571 * IsCharAlpha (USER.433)
572 * IsCharAlphaA (USER32.@)
574 BOOL WINAPI IsCharAlphaA(CHAR x)
577 MultiByteToWideChar(CP_ACP, 0, &x, 1, &wch, 1);
578 return IsCharAlphaW(wch);
582 /***********************************************************************
583 * IsCharAlphaW (USER32.@)
585 BOOL WINAPI IsCharAlphaW(WCHAR x)
587 return (get_char_typeW(x) & C1_ALPHA) != 0;
591 /***********************************************************************
592 * FormatMessage (USER.606)
594 DWORD WINAPI FormatMessage16(
596 SEGPTR lpSource, /* [in] NOTE: not always a valid pointer */
599 LPSTR lpBuffer, /* [out] NOTE: *((HLOCAL16*)) for FORMAT_MESSAGE_ALLOCATE_BUFFER*/
601 LPDWORD args /* [in] NOTE: va_list *args */
604 /* This implementation is completely dependent on the format of the va_list on x86 CPUs */
608 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
610 LPSTR allocstring = NULL;
612 TRACE("(0x%lx,%lx,%d,0x%x,%p,%d,%p)\n",
613 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
614 if ((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
615 && (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)) return 0;
616 if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
617 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
618 || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
620 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
621 FIXME("line wrapping (%lu) not supported.\n", width);
623 if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
625 char *source = MapSL(lpSource);
626 from = HeapAlloc( GetProcessHeap(), 0, strlen(source)+1 );
627 strcpy( from, source );
629 if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) {
630 from = HeapAlloc( GetProcessHeap(),0,200 );
631 sprintf(from,"Systemmessage, messageid = 0x%08x\n",dwMessageId);
633 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE) {
635 HINSTANCE16 hinst16 = ((HINSTANCE16)lpSource & 0xffff);
637 dwMessageId &= 0xFFFF;
638 bufsize=LoadString16(hinst16,dwMessageId,NULL,0);
640 from = HeapAlloc( GetProcessHeap(), 0, bufsize +1);
641 LoadString16(hinst16,dwMessageId,from,bufsize+1);
644 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
648 #define ADD_TO_T(c) \
650 if (t-target == talloced) {\
651 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
652 t = target+talloced;\
661 char *fmtstr,*x,*lastf;
672 case '1':case '2':case '3':case '4':case '5':
673 case '6':case '7':case '8':case '9':
676 case '0':case '1':case '2':case '3':
677 case '4':case '5':case '6':case '7':
680 insertnr=insertnr*10+*f-'0';
689 if (NULL!=(x=strchr(f,'!'))) {
691 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
692 sprintf(fmtstr,"%%%s",f);
695 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
696 sprintf(fmtstr,"%%%s",f);
697 f+=strlen(f); /*at \0*/
703 fmtstr=HeapAlloc( GetProcessHeap(), 0, 3 );
704 strcpy( fmtstr, "%s" );
709 LPSTR b = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz = 100);
711 argliststart=args+insertnr-1;
713 /* CMF - This makes a BIG assumption about va_list */
714 while ((ret = vsnprintf(b, sz, fmtstr, (va_list) argliststart) < 0) || (ret >= sz)) {
715 sz = (ret == -1 ? sz + 100 : ret + 1);
716 b = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, b, sz);
718 for (x=b; *x; x++) ADD_TO_T(*x);
719 HeapFree(GetProcessHeap(), 0, b);
721 /* NULL args - copy formatstr
724 while ((lastf<f)&&(*lastf)) {
728 HeapFree(GetProcessHeap(),0,fmtstr);
730 case '0': /* Just stop processing format string */
734 case 'n': /* 16 bit version just outputs 'n' */
739 } else { /* '\n' or '\r' gets mapped to "\r\n" */
740 if(*f == '\n' || *f == '\r') {
744 if(*f++ == '\r' && *f == '\n')
754 talloced = strlen(target)+1;
755 if (nSize && talloced<nSize) {
756 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
758 TRACE("-- %s\n",debugstr_a(target));
759 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
760 /* nSize is the MINIMUM size */
761 HLOCAL16 h = LocalAlloc16(LPTR,talloced);
762 SEGPTR ptr = LocalLock16(h);
763 allocstring = MapSL( ptr );
764 memcpy( allocstring,target,talloced);
766 *((HLOCAL16*)lpBuffer) = h;
768 lstrcpynA(lpBuffer,target,nSize);
769 HeapFree(GetProcessHeap(),0,target);
770 if (from) HeapFree(GetProcessHeap(),0,from);
771 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
776 #endif /* __i386__ */