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 toupper((char)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 tolower((char)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.@)
363 * FIXME: handle current locale
365 LPSTR WINAPI
CharLowerA(LPSTR x
)
367 if (!HIWORD(x
)) return (LPSTR
)tolower((char)(int)x
);
380 SetLastError( ERROR_INVALID_PARAMETER
);
388 /***********************************************************************
389 * CharUpperA (USER32.@)
390 * FIXME: handle current locale
392 LPSTR WINAPI
CharUpperA(LPSTR x
)
394 if (!HIWORD(x
)) return (LPSTR
)toupper((char)(int)x
);
407 SetLastError( ERROR_INVALID_PARAMETER
);
415 /***********************************************************************
416 * CharLowerW (USER32.@)
418 LPWSTR WINAPI
CharLowerW(LPWSTR x
)
420 if (HIWORD(x
)) return strlwrW(x
);
421 else return (LPWSTR
)((UINT
)tolowerW(LOWORD(x
)));
425 /***********************************************************************
426 * CharUpperW (USER32.@)
428 LPWSTR WINAPI
CharUpperW(LPWSTR x
)
430 if (HIWORD(x
)) return struprW(x
);
431 else return (LPWSTR
)((UINT
)toupperW(LOWORD(x
)));
435 /***********************************************************************
436 * CharLowerBuffA (USER32.@)
438 DWORD WINAPI
CharLowerBuffA( LPSTR str
, DWORD len
)
442 if (!str
) return 0; /* YES */
444 lenW
= MultiByteToWideChar(CP_ACP
, 0, str
, len
, NULL
, 0);
445 strW
= HeapAlloc(GetProcessHeap(), 0, lenW
* sizeof(WCHAR
));
448 MultiByteToWideChar(CP_ACP
, 0, str
, len
, strW
, lenW
);
449 CharLowerBuffW(strW
, lenW
);
450 len
= WideCharToMultiByte(CP_ACP
, 0, strW
, lenW
, str
, len
, NULL
, NULL
);
451 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 if (!str
) return 0; /* YES */
479 lenW
= MultiByteToWideChar(CP_ACP
, 0, str
, len
, NULL
, 0);
480 strW
= HeapAlloc(GetProcessHeap(), 0, lenW
* sizeof(WCHAR
));
483 MultiByteToWideChar(CP_ACP
, 0, str
, len
, strW
, lenW
);
484 CharUpperBuffW(strW
, lenW
);
485 len
= WideCharToMultiByte(CP_ACP
, 0, strW
, lenW
, str
, len
, NULL
, NULL
);
486 HeapFree(GetProcessHeap(), 0, strW
);
493 /***********************************************************************
494 * CharUpperBuffW (USER32.@)
496 DWORD WINAPI
CharUpperBuffW( LPWSTR str
, DWORD len
)
499 if (!str
) return 0; /* YES */
500 for (; len
; len
--, str
++) *str
= toupperW(*str
);
505 /***********************************************************************
506 * IsCharLower (USER.436)
507 * IsCharLowerA (USER32.@)
509 BOOL WINAPI
IsCharLowerA(CHAR x
)
512 MultiByteToWideChar(CP_ACP
, 0, &x
, 1, &wch
, 1);
513 return IsCharLowerW(wch
);
517 /***********************************************************************
518 * IsCharLowerW (USER32.@)
520 BOOL WINAPI
IsCharLowerW(WCHAR x
)
522 return (get_char_typeW(x
) & C1_LOWER
) != 0;
526 /***********************************************************************
527 * IsCharUpper (USER.435)
528 * IsCharUpperA (USER32.@)
530 BOOL WINAPI
IsCharUpperA(CHAR x
)
533 MultiByteToWideChar(CP_ACP
, 0, &x
, 1, &wch
, 1);
534 return IsCharUpperW(wch
);
538 /***********************************************************************
539 * IsCharUpperW (USER32.@)
541 BOOL WINAPI
IsCharUpperW(WCHAR x
)
543 return (get_char_typeW(x
) & C1_UPPER
) != 0;
547 /***********************************************************************
548 * IsCharAlphaNumeric (USER.434)
549 * IsCharAlphaNumericA (USER32.@)
551 BOOL WINAPI
IsCharAlphaNumericA(CHAR x
)
554 MultiByteToWideChar(CP_ACP
, 0, &x
, 1, &wch
, 1);
555 return IsCharAlphaNumericW(wch
);
559 /***********************************************************************
560 * IsCharAlphaNumericW (USER32.@)
562 BOOL WINAPI
IsCharAlphaNumericW(WCHAR x
)
564 return (get_char_typeW(x
) & (C1_ALPHA
|C1_DIGIT
)) != 0;
568 /***********************************************************************
569 * IsCharAlpha (USER.433)
570 * IsCharAlphaA (USER32.@)
572 BOOL WINAPI
IsCharAlphaA(CHAR x
)
575 MultiByteToWideChar(CP_ACP
, 0, &x
, 1, &wch
, 1);
576 return IsCharAlphaW(wch
);
580 /***********************************************************************
581 * IsCharAlphaW (USER32.@)
583 BOOL WINAPI
IsCharAlphaW(WCHAR x
)
585 return (get_char_typeW(x
) & C1_ALPHA
) != 0;
589 /***********************************************************************
590 * FormatMessage (USER.606)
592 DWORD WINAPI
FormatMessage16(
594 SEGPTR lpSource
, /* [in] NOTE: not always a valid pointer */
597 LPSTR lpBuffer
, /* [out] NOTE: *((HLOCAL16*)) for FORMAT_MESSAGE_ALLOCATE_BUFFER*/
599 LPDWORD args
/* [in] NOTE: va_list *args */
602 /* This implementation is completely dependent on the format of the va_list on x86 CPUs */
606 DWORD width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
608 LPSTR allocstring
= NULL
;
610 TRACE("(0x%lx,%lx,%d,0x%x,%p,%d,%p)\n",
611 dwFlags
,lpSource
,dwMessageId
,dwLanguageId
,lpBuffer
,nSize
,args
);
612 if ((dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
)
613 && (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
)) return 0;
614 if ((dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
615 &&((dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
)
616 || (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
))) return 0;
618 if (width
&& width
!= FORMAT_MESSAGE_MAX_WIDTH_MASK
)
619 FIXME("line wrapping (%lu) not supported.\n", width
);
621 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
623 char *source
= MapSL(lpSource
);
624 from
= HeapAlloc( GetProcessHeap(), 0, strlen(source
)+1 );
625 strcpy( from
, source
);
627 if (dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
) {
628 from
= HeapAlloc( GetProcessHeap(),0,200 );
629 sprintf(from
,"Systemmessage, messageid = 0x%08x\n",dwMessageId
);
631 if (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
) {
633 HINSTANCE16 hinst16
= ((HINSTANCE16
)lpSource
& 0xffff);
635 dwMessageId
&= 0xFFFF;
636 bufsize
=LoadString16(hinst16
,dwMessageId
,NULL
,0);
638 from
= HeapAlloc( GetProcessHeap(), 0, bufsize
+1);
639 LoadString16(hinst16
,dwMessageId
,from
,bufsize
+1);
642 target
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 100);
646 #define ADD_TO_T(c) \
648 if (t-target == talloced) {\
649 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
650 t = target+talloced;\
659 char *fmtstr
,*x
,*lastf
;
670 case '1':case '2':case '3':case '4':case '5':
671 case '6':case '7':case '8':case '9':
674 case '0':case '1':case '2':case '3':
675 case '4':case '5':case '6':case '7':
678 insertnr
=insertnr
*10+*f
-'0';
687 if (NULL
!=(x
=strchr(f
,'!'))) {
689 fmtstr
=HeapAlloc(GetProcessHeap(),0,strlen(f
)+2);
690 sprintf(fmtstr
,"%%%s",f
);
693 fmtstr
=HeapAlloc(GetProcessHeap(),0,strlen(f
)+2);
694 sprintf(fmtstr
,"%%%s",f
);
695 f
+=strlen(f
); /*at \0*/
701 fmtstr
=HeapAlloc( GetProcessHeap(), 0, 3 );
702 strcpy( fmtstr
, "%s" );
707 LPSTR b
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sz
= 100);
709 argliststart
=args
+insertnr
-1;
711 /* CMF - This makes a BIG assumption about va_list */
712 while ((ret
= vsnprintf(b
, sz
, fmtstr
, (va_list) argliststart
) < 0) || (ret
>= sz
)) {
713 sz
= (ret
== -1 ? sz
+ 100 : ret
+ 1);
714 b
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, b
, sz
);
716 for (x
=b
; *x
; x
++) ADD_TO_T(*x
);
717 HeapFree(GetProcessHeap(), 0, b
);
719 /* NULL args - copy formatstr
722 while ((lastf
<f
)&&(*lastf
)) {
726 HeapFree(GetProcessHeap(),0,fmtstr
);
728 case '0': /* Just stop processing format string */
732 case 'n': /* 16 bit version just outputs 'n' */
737 } else { /* '\n' or '\r' gets mapped to "\r\n" */
738 if(*f
== '\n' || *f
== '\r') {
742 if(*f
++ == '\r' && *f
== '\n')
752 talloced
= strlen(target
)+1;
753 if (nSize
&& talloced
<nSize
) {
754 target
= (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,target
,nSize
);
756 TRACE("-- %s\n",debugstr_a(target
));
757 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) {
758 /* nSize is the MINIMUM size */
759 HLOCAL16 h
= LocalAlloc16(LPTR
,talloced
);
760 SEGPTR ptr
= LocalLock16(h
);
761 allocstring
= MapSL( ptr
);
762 memcpy( allocstring
,target
,talloced
);
764 *((HLOCAL16
*)lpBuffer
) = h
;
766 lstrcpynA(lpBuffer
,target
,nSize
);
767 HeapFree(GetProcessHeap(),0,target
);
768 if (from
) HeapFree(GetProcessHeap(),0,from
);
769 return (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ?
774 #endif /* __i386__ */