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"
23 #include "debugtools.h"
25 DEFAULT_DEBUG_CHANNEL(resource);
27 /***********************************************************************
28 * AnsiToOem16 (KEYBOARD.5)
30 INT16 WINAPI AnsiToOem16( LPCSTR s, LPSTR d )
37 /***********************************************************************
38 * OemToAnsi16 (KEYBOARD.6)
40 INT16 WINAPI OemToAnsi16( LPCSTR s, LPSTR d )
47 /***********************************************************************
48 * AnsiToOemBuff16 (KEYBOARD.134)
50 void WINAPI AnsiToOemBuff16( LPCSTR s, LPSTR d, UINT16 len )
52 if (len != 0) CharToOemBuffA( s, d, len );
56 /***********************************************************************
57 * OemToAnsiBuff16 (KEYBOARD.135)
59 void WINAPI OemToAnsiBuff16( LPCSTR s, LPSTR d, UINT16 len )
61 if (len != 0) OemToCharBuffA( s, d, len );
65 /***********************************************************************
66 * lstrcmp16 (USER.430)
68 INT16 WINAPI lstrcmp16( LPCSTR str1, LPCSTR str2 )
70 return (INT16)strcmp( str1, str2 );
74 /***********************************************************************
75 * AnsiUpper16 (USER.431)
77 SEGPTR WINAPI AnsiUpper16( SEGPTR strOrChar )
79 /* uppercase only one char if strOrChar < 0x10000 */
80 if (HIWORD(strOrChar))
82 CharUpperA( PTR_SEG_TO_LIN(strOrChar) );
85 else return toupper((char)strOrChar);
89 /***********************************************************************
90 * AnsiLower16 (USER.432)
92 SEGPTR WINAPI AnsiLower16( SEGPTR strOrChar )
94 /* lowercase only one char if strOrChar < 0x10000 */
95 if (HIWORD(strOrChar))
97 CharLowerA( PTR_SEG_TO_LIN(strOrChar) );
100 else return tolower((char)strOrChar);
104 /***********************************************************************
105 * AnsiUpperBuff16 (USER.437)
107 UINT16 WINAPI AnsiUpperBuff16( LPSTR str, UINT16 len )
109 CharUpperBuffA( str, len ? len : 65536 );
114 /***********************************************************************
115 * AnsiLowerBuff16 (USER.438)
117 UINT16 WINAPI AnsiLowerBuff16( LPSTR str, UINT16 len )
119 CharLowerBuffA( str, len ? len : 65536 );
124 /***********************************************************************
125 * AnsiNext16 (USER.472)
127 SEGPTR WINAPI AnsiNext16(SEGPTR current)
129 char *ptr = (char *)PTR_SEG_TO_LIN(current);
130 return current + (CharNextA(ptr) - ptr);
134 /***********************************************************************
135 * AnsiPrev16 (USER.473)
137 SEGPTR WINAPI AnsiPrev16( LPCSTR start, SEGPTR current )
139 char *ptr = (char *)PTR_SEG_TO_LIN(current);
140 return current - (ptr - CharPrevA( start, ptr ));
144 /***********************************************************************
145 * CharNextA (USER32.@)
147 LPSTR WINAPI CharNextA( LPCSTR ptr )
149 if (!*ptr) return (LPSTR)ptr;
150 if (IsDBCSLeadByte( ptr[0] ) && ptr[1]) return (LPSTR)(ptr + 2);
151 return (LPSTR)(ptr + 1);
155 /***********************************************************************
156 * CharNextExA (USER32.@)
158 LPSTR WINAPI CharNextExA( WORD codepage, LPCSTR ptr, DWORD flags )
160 if (!*ptr) return (LPSTR)ptr;
161 if (IsDBCSLeadByteEx( codepage, ptr[0] ) && ptr[1]) return (LPSTR)(ptr + 2);
162 return (LPSTR)(ptr + 1);
166 /***********************************************************************
167 * CharNextExW (USER32.@)
169 LPWSTR WINAPI CharNextExW( WORD codepage, LPCWSTR ptr, DWORD flags )
171 /* doesn't make sense, there are no codepages for Unicode */
176 /***********************************************************************
177 * CharNextW (USER32.@)
179 LPWSTR WINAPI CharNextW(LPCWSTR x)
187 /***********************************************************************
188 * CharPrevA (USER32.@)
190 LPSTR WINAPI CharPrevA( LPCSTR start, LPCSTR ptr )
192 while (*start && (start < ptr))
194 LPCSTR next = CharNextA( start );
195 if (next >= ptr) break;
202 /***********************************************************************
203 * CharPrevExA (USER32.@)
205 LPSTR WINAPI CharPrevExA( WORD codepage, LPCSTR start, LPCSTR ptr, DWORD flags )
207 while (*start && (start < ptr))
209 LPCSTR next = CharNextExA( codepage, start, flags );
210 if (next > ptr) break;
217 /***********************************************************************
218 * CharPrevExW (USER32.@)
220 LPSTR WINAPI CharPrevExW( WORD codepage, LPCWSTR start, LPCWSTR ptr, DWORD flags )
222 /* doesn't make sense, there are no codepages for Unicode */
227 /***********************************************************************
228 * CharPrevW (USER32.@)
230 LPWSTR WINAPI CharPrevW(LPCWSTR start,LPCWSTR x)
232 if (x>start) return (LPWSTR)(x-1);
233 else return (LPWSTR)x;
237 /***********************************************************************
238 * CharToOemA (USER32.@)
240 BOOL WINAPI CharToOemA( LPCSTR s, LPSTR d )
242 if ( !s || !d ) return TRUE;
243 return CharToOemBuffA( s, d, strlen( s ) + 1 );
247 /***********************************************************************
248 * CharToOemBuffA (USER32.@)
250 BOOL WINAPI CharToOemBuffA( LPCSTR s, LPSTR d, DWORD len )
254 bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
257 MultiByteToWideChar( CP_ACP, 0, s, len, bufW, len );
258 WideCharToMultiByte( CP_OEMCP, 0, bufW, len, d, len, NULL, NULL );
259 HeapFree( GetProcessHeap(), 0, bufW );
265 /***********************************************************************
266 * CharToOemBuffW (USER32.@)
268 BOOL WINAPI CharToOemBuffW( LPCWSTR s, LPSTR d, DWORD len )
270 if ( !s || !d ) return TRUE;
271 WideCharToMultiByte( CP_OEMCP, 0, s, len, d, len, NULL, NULL );
276 /***********************************************************************
277 * CharToOemW (USER32.@)
279 BOOL WINAPI CharToOemW( LPCWSTR s, LPSTR d )
281 return CharToOemBuffW( s, d, strlenW( s ) + 1 );
285 /***********************************************************************
286 * OemToCharA (USER32.@)
288 BOOL WINAPI OemToCharA( LPCSTR s, LPSTR d )
290 return OemToCharBuffA( s, d, strlen( s ) + 1 );
294 /***********************************************************************
295 * OemToCharBuffA (USER32.@)
297 BOOL WINAPI OemToCharBuffA( LPCSTR s, LPSTR d, DWORD len )
301 bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
304 MultiByteToWideChar( CP_OEMCP, 0, s, len, bufW, len );
305 WideCharToMultiByte( CP_ACP, 0, bufW, len, d, len, NULL, NULL );
306 HeapFree( GetProcessHeap(), 0, bufW );
312 /***********************************************************************
313 * OemToCharBuffW (USER32.@)
315 BOOL WINAPI OemToCharBuffW( LPCSTR s, LPWSTR d, DWORD len )
317 MultiByteToWideChar( CP_OEMCP, 0, s, len, d, len );
322 /***********************************************************************
323 * OemToCharW (USER32.@)
325 BOOL WINAPI OemToCharW( LPCSTR s, LPWSTR d )
327 return OemToCharBuffW( s, d, strlen( s ) + 1 );
331 /***********************************************************************
332 * CharLowerA (USER32.25)
333 * FIXME: handle current locale
335 LPSTR WINAPI CharLowerA(LPSTR x)
349 else return (LPSTR)tolower((char)(int)x);
353 /***********************************************************************
354 * CharUpperA (USER32.@)
355 * FIXME: handle current locale
357 LPSTR WINAPI CharUpperA(LPSTR x)
369 return (LPSTR)toupper((char)(int)x);
373 /***********************************************************************
374 * CharLowerW (USER32.@)
376 LPWSTR WINAPI CharLowerW(LPWSTR x)
378 if (HIWORD(x)) return strlwrW(x);
379 else return (LPWSTR)((UINT)tolowerW(LOWORD(x)));
383 /***********************************************************************
384 * CharUpperW (USER32.@)
385 * FIXME: handle current locale
387 LPWSTR WINAPI CharUpperW(LPWSTR x)
389 if (HIWORD(x)) return struprW(x);
390 else return (LPWSTR)((UINT)toupperW(LOWORD(x)));
394 /***********************************************************************
395 * CharLowerBuffA (USER32.@)
396 * FIXME: handle current locale
398 DWORD WINAPI CharLowerBuffA( LPSTR str, DWORD len )
401 if (!str) return 0; /* YES */
402 for (; len; len--, str++) *str = tolower(*str);
407 /***********************************************************************
408 * CharLowerBuffW (USER32.@)
410 DWORD WINAPI CharLowerBuffW( LPWSTR str, DWORD len )
413 if (!str) return 0; /* YES */
414 for (; len; len--, str++) *str = tolowerW(*str);
419 /***********************************************************************
420 * CharUpperBuffA (USER32.@)
421 * FIXME: handle current locale
423 DWORD WINAPI CharUpperBuffA( LPSTR str, DWORD len )
426 if (!str) return 0; /* YES */
427 for (; len; len--, str++) *str = toupper(*str);
432 /***********************************************************************
433 * CharUpperBuffW (USER32.@)
435 DWORD WINAPI CharUpperBuffW( LPWSTR str, DWORD len )
438 if (!str) return 0; /* YES */
439 for (; len; len--, str++) *str = toupperW(*str);
444 /***********************************************************************
445 * IsCharLowerA (USER.436) (USER32.@)
446 * FIXME: handle current locale
448 BOOL WINAPI IsCharLowerA(CHAR x)
454 /***********************************************************************
455 * IsCharLowerW (USER32.@)
457 BOOL WINAPI IsCharLowerW(WCHAR x)
459 return get_char_typeW(x) & C1_LOWER;
463 /***********************************************************************
464 * IsCharUpperA (USER.435) (USER32.337)
465 * FIXME: handle current locale
467 BOOL WINAPI IsCharUpperA(CHAR x)
473 /***********************************************************************
474 * IsCharUpperW (USER32.@)
476 BOOL WINAPI IsCharUpperW(WCHAR x)
478 return get_char_typeW(x) & C1_UPPER;
482 /***********************************************************************
483 * IsCharAlphaNumericW (USER32.@)
485 BOOL WINAPI IsCharAlphaNumericW(WCHAR x)
487 return get_char_typeW(x) & (C1_ALPHA|C1_DIGIT|C1_LOWER|C1_UPPER);
491 /***********************************************************************
492 * IsCharAlphaW (USER32.@)
494 BOOL WINAPI IsCharAlphaW(WCHAR x)
496 return get_char_typeW(x) & (C1_ALPHA|C1_LOWER|C1_UPPER);
500 /***********************************************************************
501 * FormatMessage16 (USER.606)
503 DWORD WINAPI FormatMessage16(
505 SEGPTR lpSource, /*not always a valid pointer*/
508 LPSTR lpBuffer, /* *((HLOCAL16*)) for FORMAT_MESSAGE_ALLOCATE_BUFFER*/
510 LPDWORD args /* va_list *args */
513 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
517 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
519 LPSTR allocstring = NULL;
521 TRACE("(0x%lx,%lx,%d,0x%x,%p,%d,%p)\n",
522 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
523 if ((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
524 && (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)) return 0;
525 if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
526 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
527 || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
529 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
530 FIXME("line wrapping (%lu) not supported.\n", width);
532 if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
533 from = HEAP_strdupA( GetProcessHeap(), 0, PTR_SEG_TO_LIN(lpSource));
534 if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) {
535 from = HeapAlloc( GetProcessHeap(),0,200 );
536 sprintf(from,"Systemmessage, messageid = 0x%08x\n",dwMessageId);
538 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE) {
540 HINSTANCE16 hinst16 = ((HMODULE)lpSource & 0xffff);
542 dwMessageId &= 0xFFFF;
543 bufsize=LoadString16(hinst16,dwMessageId,NULL,0);
545 from = HeapAlloc( GetProcessHeap(), 0, bufsize +1);
546 LoadString16(hinst16,dwMessageId,from,bufsize+1);
549 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
553 #define ADD_TO_T(c) \
555 if (t-target == talloced) {\
556 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
557 t = target+talloced;\
566 char *fmtstr,*x,*lastf;
577 case '1':case '2':case '3':case '4':case '5':
578 case '6':case '7':case '8':case '9':
581 case '0':case '1':case '2':case '3':
582 case '4':case '5':case '6':case '7':
585 insertnr=insertnr*10+*f-'0';
594 if (NULL!=(x=strchr(f,'!'))) {
596 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
597 sprintf(fmtstr,"%%%s",f);
600 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
601 sprintf(fmtstr,"%%%s",f);
602 f+=strlen(f); /*at \0*/
608 fmtstr=HEAP_strdupA(GetProcessHeap(),0,"%s");
611 LPSTR b = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz = 100);
613 argliststart=args+insertnr-1;
615 /* CMF - This makes a BIG assumption about va_list */
616 while (vsnprintf(b, sz, fmtstr, (va_list) argliststart) < 0) {
617 b = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, b, sz += 100);
619 for (x=b; *x; x++) ADD_TO_T(*x);
621 /* NULL args - copy formatstr
624 while ((lastf<f)&&(*lastf)) {
628 HeapFree(GetProcessHeap(),0,fmtstr);
630 case '0': /* Just stop processing format string */
634 case 'n': /* 16 bit version just outputs 'n' */
639 } else { /* '\n' or '\r' gets mapped to "\r\n" */
640 if(*f == '\n' || *f == '\r') {
644 if(*f++ == '\r' && *f == '\n')
654 talloced = strlen(target)+1;
655 if (nSize && talloced<nSize) {
656 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
658 TRACE("-- %s\n",debugstr_a(target));
659 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
660 /* nSize is the MINIMUM size */
661 HLOCAL16 h = LocalAlloc16(LPTR,talloced);
662 SEGPTR ptr = LocalLock16(h);
663 allocstring = PTR_SEG_TO_LIN( ptr );
664 memcpy( allocstring,target,talloced);
666 *((HLOCAL16*)lpBuffer) = h;
668 lstrcpynA(lpBuffer,target,nSize);
669 HeapFree(GetProcessHeap(),0,target);
670 if (from) HeapFree(GetProcessHeap(),0,from);
671 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
676 #endif /* __i386__ */