2 * USER string functions
4 * Copyright 1993 Yngvi Sigurjonsson (yngvi@hafro.is)
5 * Copyright 1996 Alexandre Julliard
6 * Copyright 1996 Marcus Meissner
17 #include "wine/winbase16.h"
18 #include "wine/winuser16.h"
19 #include "wine/unicode.h"
22 #include "debugtools.h"
24 DEFAULT_DEBUG_CHANNEL(resource
);
26 /***********************************************************************
27 * AnsiToOem16 (KEYBOARD.5)
29 INT16 WINAPI
AnsiToOem16( LPCSTR s
, LPSTR d
)
36 /***********************************************************************
37 * OemToAnsi16 (KEYBOARD.6)
39 INT16 WINAPI
OemToAnsi16( LPCSTR s
, LPSTR d
)
46 /***********************************************************************
47 * AnsiToOemBuff16 (KEYBOARD.134)
49 void WINAPI
AnsiToOemBuff16( LPCSTR s
, LPSTR d
, UINT16 len
)
51 if (len
!= 0) CharToOemBuffA( s
, d
, len
);
55 /***********************************************************************
56 * OemToAnsiBuff16 (KEYBOARD.135)
58 void WINAPI
OemToAnsiBuff16( LPCSTR s
, LPSTR d
, UINT16 len
)
60 if (len
!= 0) OemToCharBuffA( s
, d
, len
);
64 /***********************************************************************
65 * lstrcmp16 (USER.430)
67 INT16 WINAPI
lstrcmp16( LPCSTR str1
, LPCSTR str2
)
69 return (INT16
)strcmp( str1
, str2
);
73 /***********************************************************************
74 * AnsiUpper16 (USER.431)
76 SEGPTR WINAPI
AnsiUpper16( SEGPTR strOrChar
)
78 /* uppercase only one char if strOrChar < 0x10000 */
79 if (HIWORD(strOrChar
))
81 CharUpperA( MapSL(strOrChar
) );
84 else return toupper((char)strOrChar
);
88 /***********************************************************************
89 * AnsiLower16 (USER.432)
91 SEGPTR WINAPI
AnsiLower16( SEGPTR strOrChar
)
93 /* lowercase only one char if strOrChar < 0x10000 */
94 if (HIWORD(strOrChar
))
96 CharLowerA( MapSL(strOrChar
) );
99 else return tolower((char)strOrChar
);
103 /***********************************************************************
104 * AnsiUpperBuff16 (USER.437)
106 UINT16 WINAPI
AnsiUpperBuff16( LPSTR str
, UINT16 len
)
108 CharUpperBuffA( str
, len
? len
: 65536 );
113 /***********************************************************************
114 * AnsiLowerBuff16 (USER.438)
116 UINT16 WINAPI
AnsiLowerBuff16( LPSTR str
, UINT16 len
)
118 CharLowerBuffA( str
, len
? len
: 65536 );
123 /***********************************************************************
124 * AnsiNext16 (USER.472)
126 SEGPTR WINAPI
AnsiNext16(SEGPTR current
)
128 char *ptr
= MapSL(current
);
129 return current
+ (CharNextA(ptr
) - ptr
);
133 /***********************************************************************
134 * AnsiPrev16 (USER.473)
136 SEGPTR WINAPI
AnsiPrev16( LPCSTR start
, SEGPTR current
)
138 char *ptr
= MapSL(current
);
139 return current
- (ptr
- CharPrevA( start
, ptr
));
143 /***********************************************************************
144 * CharNextA (USER32.@)
146 LPSTR WINAPI
CharNextA( LPCSTR ptr
)
148 if (!*ptr
) return (LPSTR
)ptr
;
149 if (IsDBCSLeadByte( ptr
[0] ) && ptr
[1]) return (LPSTR
)(ptr
+ 2);
150 return (LPSTR
)(ptr
+ 1);
154 /***********************************************************************
155 * CharNextExA (USER32.@)
157 LPSTR WINAPI
CharNextExA( WORD codepage
, LPCSTR ptr
, DWORD flags
)
159 if (!*ptr
) return (LPSTR
)ptr
;
160 if (IsDBCSLeadByteEx( codepage
, ptr
[0] ) && ptr
[1]) return (LPSTR
)(ptr
+ 2);
161 return (LPSTR
)(ptr
+ 1);
165 /***********************************************************************
166 * CharNextExW (USER32.@)
168 LPWSTR WINAPI
CharNextExW( WORD codepage
, LPCWSTR ptr
, DWORD flags
)
170 /* doesn't make sense, there are no codepages for Unicode */
175 /***********************************************************************
176 * CharNextW (USER32.@)
178 LPWSTR WINAPI
CharNextW(LPCWSTR x
)
186 /***********************************************************************
187 * CharPrevA (USER32.@)
189 LPSTR WINAPI
CharPrevA( LPCSTR start
, LPCSTR ptr
)
191 while (*start
&& (start
< ptr
))
193 LPCSTR next
= CharNextA( start
);
194 if (next
>= ptr
) break;
201 /***********************************************************************
202 * CharPrevExA (USER32.@)
204 LPSTR WINAPI
CharPrevExA( WORD codepage
, LPCSTR start
, LPCSTR ptr
, DWORD flags
)
206 while (*start
&& (start
< ptr
))
208 LPCSTR next
= CharNextExA( codepage
, start
, flags
);
209 if (next
> ptr
) break;
216 /***********************************************************************
217 * CharPrevExW (USER32.@)
219 LPSTR WINAPI
CharPrevExW( WORD codepage
, LPCWSTR start
, LPCWSTR ptr
, DWORD flags
)
221 /* doesn't make sense, there are no codepages for Unicode */
226 /***********************************************************************
227 * CharPrevW (USER32.@)
229 LPWSTR WINAPI
CharPrevW(LPCWSTR start
,LPCWSTR x
)
231 if (x
>start
) return (LPWSTR
)(x
-1);
232 else return (LPWSTR
)x
;
236 /***********************************************************************
237 * CharToOemA (USER32.@)
239 BOOL WINAPI
CharToOemA( LPCSTR s
, LPSTR d
)
241 if ( !s
|| !d
) return TRUE
;
242 return CharToOemBuffA( s
, d
, strlen( s
) + 1 );
246 /***********************************************************************
247 * CharToOemBuffA (USER32.@)
249 BOOL WINAPI
CharToOemBuffA( LPCSTR s
, LPSTR d
, DWORD len
)
253 bufW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
256 MultiByteToWideChar( CP_ACP
, 0, s
, len
, bufW
, len
);
257 WideCharToMultiByte( CP_OEMCP
, 0, bufW
, len
, d
, len
, NULL
, NULL
);
258 HeapFree( GetProcessHeap(), 0, bufW
);
264 /***********************************************************************
265 * CharToOemBuffW (USER32.@)
267 BOOL WINAPI
CharToOemBuffW( LPCWSTR s
, LPSTR d
, DWORD len
)
269 if ( !s
|| !d
) return TRUE
;
270 WideCharToMultiByte( CP_OEMCP
, 0, s
, len
, d
, len
, NULL
, NULL
);
275 /***********************************************************************
276 * CharToOemW (USER32.@)
278 BOOL WINAPI
CharToOemW( LPCWSTR s
, LPSTR d
)
280 return CharToOemBuffW( s
, d
, strlenW( s
) + 1 );
284 /***********************************************************************
285 * OemToCharA (USER32.@)
287 BOOL WINAPI
OemToCharA( LPCSTR s
, LPSTR d
)
289 return OemToCharBuffA( s
, d
, strlen( s
) + 1 );
293 /***********************************************************************
294 * OemToCharBuffA (USER32.@)
296 BOOL WINAPI
OemToCharBuffA( LPCSTR s
, LPSTR d
, DWORD len
)
300 bufW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
303 MultiByteToWideChar( CP_OEMCP
, 0, s
, len
, bufW
, len
);
304 WideCharToMultiByte( CP_ACP
, 0, bufW
, len
, d
, len
, NULL
, NULL
);
305 HeapFree( GetProcessHeap(), 0, bufW
);
311 /***********************************************************************
312 * OemToCharBuffW (USER32.@)
314 BOOL WINAPI
OemToCharBuffW( LPCSTR s
, LPWSTR d
, DWORD len
)
316 MultiByteToWideChar( CP_OEMCP
, 0, s
, len
, d
, len
);
321 /***********************************************************************
322 * OemToCharW (USER32.@)
324 BOOL WINAPI
OemToCharW( LPCSTR s
, LPWSTR d
)
326 return OemToCharBuffW( s
, d
, strlen( s
) + 1 );
330 /***********************************************************************
331 * CharLowerA (USER32.25)
332 * FIXME: handle current locale
334 LPSTR WINAPI
CharLowerA(LPSTR x
)
348 else return (LPSTR
)tolower((char)(int)x
);
352 /***********************************************************************
353 * CharUpperA (USER32.@)
354 * FIXME: handle current locale
356 LPSTR WINAPI
CharUpperA(LPSTR x
)
368 return (LPSTR
)toupper((char)(int)x
);
372 /***********************************************************************
373 * CharLowerW (USER32.@)
375 LPWSTR WINAPI
CharLowerW(LPWSTR x
)
377 if (HIWORD(x
)) return strlwrW(x
);
378 else return (LPWSTR
)((UINT
)tolowerW(LOWORD(x
)));
382 /***********************************************************************
383 * CharUpperW (USER32.@)
384 * FIXME: handle current locale
386 LPWSTR WINAPI
CharUpperW(LPWSTR x
)
388 if (HIWORD(x
)) return struprW(x
);
389 else return (LPWSTR
)((UINT
)toupperW(LOWORD(x
)));
393 /***********************************************************************
394 * CharLowerBuffA (USER32.@)
395 * FIXME: handle current locale
397 DWORD WINAPI
CharLowerBuffA( LPSTR str
, DWORD len
)
400 if (!str
) return 0; /* YES */
401 for (; len
; len
--, str
++) *str
= tolower(*str
);
406 /***********************************************************************
407 * CharLowerBuffW (USER32.@)
409 DWORD WINAPI
CharLowerBuffW( LPWSTR str
, DWORD len
)
412 if (!str
) return 0; /* YES */
413 for (; len
; len
--, str
++) *str
= tolowerW(*str
);
418 /***********************************************************************
419 * CharUpperBuffA (USER32.@)
420 * FIXME: handle current locale
422 DWORD WINAPI
CharUpperBuffA( LPSTR str
, DWORD len
)
425 if (!str
) return 0; /* YES */
426 for (; len
; len
--, str
++) *str
= toupper(*str
);
431 /***********************************************************************
432 * CharUpperBuffW (USER32.@)
434 DWORD WINAPI
CharUpperBuffW( LPWSTR str
, DWORD len
)
437 if (!str
) return 0; /* YES */
438 for (; len
; len
--, str
++) *str
= toupperW(*str
);
443 /***********************************************************************
444 * IsCharLowerA (USER.436) (USER32.@)
445 * FIXME: handle current locale
447 BOOL WINAPI
IsCharLowerA(CHAR x
)
453 /***********************************************************************
454 * IsCharLowerW (USER32.@)
456 BOOL WINAPI
IsCharLowerW(WCHAR x
)
458 return get_char_typeW(x
) & C1_LOWER
;
462 /***********************************************************************
463 * IsCharUpperA (USER.435) (USER32.337)
464 * FIXME: handle current locale
466 BOOL WINAPI
IsCharUpperA(CHAR x
)
472 /***********************************************************************
473 * IsCharUpperW (USER32.@)
475 BOOL WINAPI
IsCharUpperW(WCHAR x
)
477 return get_char_typeW(x
) & C1_UPPER
;
481 /***********************************************************************
482 * IsCharAlphaNumericW (USER32.@)
484 BOOL WINAPI
IsCharAlphaNumericW(WCHAR x
)
486 return get_char_typeW(x
) & (C1_ALPHA
|C1_DIGIT
|C1_LOWER
|C1_UPPER
);
490 /***********************************************************************
491 * IsCharAlphaW (USER32.@)
493 BOOL WINAPI
IsCharAlphaW(WCHAR x
)
495 return get_char_typeW(x
) & (C1_ALPHA
|C1_LOWER
|C1_UPPER
);
499 /***********************************************************************
500 * FormatMessage16 (USER.606)
502 DWORD WINAPI
FormatMessage16(
504 SEGPTR lpSource
, /* [in] NOTE: not always a valid pointer */
507 LPSTR lpBuffer
, /* [out] NOTE: *((HLOCAL16*)) for FORMAT_MESSAGE_ALLOCATE_BUFFER*/
509 LPDWORD args
/* [in] NOTE: va_list *args */
512 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
516 DWORD width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
518 LPSTR allocstring
= NULL
;
520 TRACE("(0x%lx,%lx,%d,0x%x,%p,%d,%p)\n",
521 dwFlags
,lpSource
,dwMessageId
,dwLanguageId
,lpBuffer
,nSize
,args
);
522 if ((dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
)
523 && (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
)) return 0;
524 if ((dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
525 &&((dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
)
526 || (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
))) return 0;
528 if (width
&& width
!= FORMAT_MESSAGE_MAX_WIDTH_MASK
)
529 FIXME("line wrapping (%lu) not supported.\n", width
);
531 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
532 from
= HEAP_strdupA( GetProcessHeap(), 0, MapSL(lpSource
));
533 if (dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
) {
534 from
= HeapAlloc( GetProcessHeap(),0,200 );
535 sprintf(from
,"Systemmessage, messageid = 0x%08x\n",dwMessageId
);
537 if (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
) {
539 HINSTANCE16 hinst16
= ((HMODULE
)lpSource
& 0xffff);
541 dwMessageId
&= 0xFFFF;
542 bufsize
=LoadString16(hinst16
,dwMessageId
,NULL
,0);
544 from
= HeapAlloc( GetProcessHeap(), 0, bufsize
+1);
545 LoadString16(hinst16
,dwMessageId
,from
,bufsize
+1);
548 target
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 100);
552 #define ADD_TO_T(c) \
554 if (t-target == talloced) {\
555 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
556 t = target+talloced;\
565 char *fmtstr
,*x
,*lastf
;
576 case '1':case '2':case '3':case '4':case '5':
577 case '6':case '7':case '8':case '9':
580 case '0':case '1':case '2':case '3':
581 case '4':case '5':case '6':case '7':
584 insertnr
=insertnr
*10+*f
-'0';
593 if (NULL
!=(x
=strchr(f
,'!'))) {
595 fmtstr
=HeapAlloc(GetProcessHeap(),0,strlen(f
)+2);
596 sprintf(fmtstr
,"%%%s",f
);
599 fmtstr
=HeapAlloc(GetProcessHeap(),0,strlen(f
)+2);
600 sprintf(fmtstr
,"%%%s",f
);
601 f
+=strlen(f
); /*at \0*/
607 fmtstr
=HEAP_strdupA(GetProcessHeap(),0,"%s");
610 LPSTR b
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sz
= 100);
612 argliststart
=args
+insertnr
-1;
614 /* CMF - This makes a BIG assumption about va_list */
615 while (vsnprintf(b
, sz
, fmtstr
, (va_list) argliststart
) < 0) {
616 b
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, b
, sz
+= 100);
618 for (x
=b
; *x
; x
++) ADD_TO_T(*x
);
620 /* NULL args - copy formatstr
623 while ((lastf
<f
)&&(*lastf
)) {
627 HeapFree(GetProcessHeap(),0,fmtstr
);
629 case '0': /* Just stop processing format string */
633 case 'n': /* 16 bit version just outputs 'n' */
638 } else { /* '\n' or '\r' gets mapped to "\r\n" */
639 if(*f
== '\n' || *f
== '\r') {
643 if(*f
++ == '\r' && *f
== '\n')
653 talloced
= strlen(target
)+1;
654 if (nSize
&& talloced
<nSize
) {
655 target
= (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,target
,nSize
);
657 TRACE("-- %s\n",debugstr_a(target
));
658 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) {
659 /* nSize is the MINIMUM size */
660 HLOCAL16 h
= LocalAlloc16(LPTR
,talloced
);
661 SEGPTR ptr
= LocalLock16(h
);
662 allocstring
= MapSL( ptr
);
663 memcpy( allocstring
,target
,talloced
);
665 *((HLOCAL16
*)lpBuffer
) = h
;
667 lstrcpynA(lpBuffer
,target
,nSize
);
668 HeapFree(GetProcessHeap(),0,target
);
669 if (from
) HeapFree(GetProcessHeap(),0,from
);
670 return (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ?
675 #endif /* __i386__ */