Initialize Xlib threading support to see what it breaks...
[wine.git] / dlls / user / lstr.c
blob6dafd250da4d1a5592d756e6d05e49eb9e700c26
1 /*
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
23 #include "config.h"
24 #include "wine/port.h"
26 #include <ctype.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winerror.h"
35 #include "ntstatus.h"
37 #include "wine/exception.h"
38 #include "wine/unicode.h"
39 #include "wine/winbase16.h"
40 #include "wine/winuser16.h"
42 #include "excpt.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 )
62 CharToOemA( s, d );
63 return -1;
67 /***********************************************************************
68 * OemToAnsi (KEYBOARD.6)
70 INT16 WINAPI OemToAnsi16( LPCSTR s, LPSTR d )
72 OemToCharA( s, d );
73 return -1;
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 /***********************************************************************
96 * lstrcmp (USER.430)
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) );
113 return 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) );
128 return 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 );
140 return len;
144 /***********************************************************************
145 * AnsiLowerBuff (USER.438)
147 UINT16 WINAPI AnsiLowerBuff16( LPSTR str, UINT16 len )
149 CharLowerBuffA( str, len ? len : 65536 );
150 return len;
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 */
202 return NULL;
206 /***********************************************************************
207 * CharNextW (USER32.@)
209 LPWSTR WINAPI CharNextW(LPCWSTR x)
211 if (*x) x++;
213 return (LPWSTR)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;
226 start = next;
228 return (LPSTR)start;
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;
241 start = next;
243 return (LPSTR)start;
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 */
253 return NULL;
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 )
282 WCHAR *bufW;
284 bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
285 if( bufW )
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 );
291 return TRUE;
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 );
302 return TRUE;
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 )
329 WCHAR *bufW;
331 bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
332 if( bufW )
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 );
338 return TRUE;
342 /***********************************************************************
343 * OemToCharBuffW (USER32.@)
345 BOOL WINAPI OemToCharBuffW( LPCSTR s, LPWSTR d, DWORD len )
347 MultiByteToWideChar( CP_OEMCP, 0, s, len, d, len );
348 return TRUE;
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);
369 __TRY
371 LPSTR s = x;
372 while (*s)
374 *s=tolower(*s);
375 s++;
378 __EXCEPT(page_fault)
380 SetLastError( ERROR_INVALID_PARAMETER );
381 return NULL;
383 __ENDTRY
384 return x;
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);
396 __TRY
398 LPSTR s = x;
399 while (*s)
401 *s=toupper(*s);
402 s++;
405 __EXCEPT(page_fault)
407 SetLastError( ERROR_INVALID_PARAMETER );
408 return NULL;
410 __ENDTRY
411 return x;
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 )
440 DWORD lenW;
441 WCHAR *strW;
442 if (!str) return 0; /* YES */
444 lenW = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0);
445 strW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
446 if(strW)
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);
452 return len;
454 return 0;
458 /***********************************************************************
459 * CharLowerBuffW (USER32.@)
461 DWORD WINAPI CharLowerBuffW( LPWSTR str, DWORD len )
463 DWORD ret = len;
464 if (!str) return 0; /* YES */
465 for (; len; len--, str++) *str = tolowerW(*str);
466 return ret;
470 /***********************************************************************
471 * CharUpperBuffA (USER32.@)
473 DWORD WINAPI CharUpperBuffA( LPSTR str, DWORD len )
475 DWORD lenW;
476 WCHAR *strW;
477 if (!str) return 0; /* YES */
479 lenW = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0);
480 strW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
481 if(strW)
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);
487 return len;
489 return 0;
493 /***********************************************************************
494 * CharUpperBuffW (USER32.@)
496 DWORD WINAPI CharUpperBuffW( LPWSTR str, DWORD len )
498 DWORD ret = len;
499 if (!str) return 0; /* YES */
500 for (; len; len--, str++) *str = toupperW(*str);
501 return ret;
505 /***********************************************************************
506 * IsCharLower (USER.436)
507 * IsCharLowerA (USER32.@)
509 BOOL WINAPI IsCharLowerA(CHAR x)
511 WCHAR wch;
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)
532 WCHAR wch;
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)
553 WCHAR wch;
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)
574 WCHAR wch;
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(
593 DWORD dwFlags,
594 SEGPTR lpSource, /* [in] NOTE: not always a valid pointer */
595 WORD dwMessageId,
596 WORD dwLanguageId,
597 LPSTR lpBuffer, /* [out] NOTE: *((HLOCAL16*)) for FORMAT_MESSAGE_ALLOCATE_BUFFER*/
598 WORD nSize,
599 LPDWORD args /* [in] NOTE: va_list *args */
601 #ifdef __i386__
602 /* This implementation is completely dependent on the format of the va_list on x86 CPUs */
603 LPSTR target,t;
604 DWORD talloced;
605 LPSTR from,f;
606 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
607 BOOL eos = FALSE;
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);
620 from = NULL;
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) {
632 INT16 bufsize;
633 HINSTANCE16 hinst16 = ((HINSTANCE16)lpSource & 0xffff);
635 dwMessageId &= 0xFFFF;
636 bufsize=LoadString16(hinst16,dwMessageId,NULL,0);
637 if (bufsize) {
638 from = HeapAlloc( GetProcessHeap(), 0, bufsize +1);
639 LoadString16(hinst16,dwMessageId,from,bufsize+1);
642 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
643 t = target;
644 talloced= 100;
646 #define ADD_TO_T(c) \
647 *t++=c;\
648 if (t-target == talloced) {\
649 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
650 t = target+talloced;\
651 talloced*=2;\
654 if (from) {
655 f=from;
656 while (*f && !eos) {
657 if (*f=='%') {
658 int insertnr;
659 char *fmtstr,*x,*lastf;
660 DWORD *argliststart;
662 fmtstr = NULL;
663 lastf = f;
664 f++;
665 if (!*f) {
666 ADD_TO_T('%');
667 continue;
669 switch (*f) {
670 case '1':case '2':case '3':case '4':case '5':
671 case '6':case '7':case '8':case '9':
672 insertnr=*f-'0';
673 switch (f[1]) {
674 case '0':case '1':case '2':case '3':
675 case '4':case '5':case '6':case '7':
676 case '8':case '9':
677 f++;
678 insertnr=insertnr*10+*f-'0';
679 f++;
680 break;
681 default:
682 f++;
683 break;
685 if (*f=='!') {
686 f++;
687 if (NULL!=(x=strchr(f,'!'))) {
688 *x='\0';
689 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
690 sprintf(fmtstr,"%%%s",f);
691 f=x+1;
692 } else {
693 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
694 sprintf(fmtstr,"%%%s",f);
695 f+=strlen(f); /*at \0*/
698 else
700 if(!args) break;
701 fmtstr=HeapAlloc( GetProcessHeap(), 0, 3 );
702 strcpy( fmtstr, "%s" );
704 if (args) {
705 int ret;
706 int sz;
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);
718 } else {
719 /* NULL args - copy formatstr
720 * (probably wrong)
722 while ((lastf<f)&&(*lastf)) {
723 ADD_TO_T(*lastf++);
726 HeapFree(GetProcessHeap(),0,fmtstr);
727 break;
728 case '0': /* Just stop processing format string */
729 eos = TRUE;
730 f++;
731 break;
732 case 'n': /* 16 bit version just outputs 'n' */
733 default:
734 ADD_TO_T(*f++);
735 break;
737 } else { /* '\n' or '\r' gets mapped to "\r\n" */
738 if(*f == '\n' || *f == '\r') {
739 if (width == 0) {
740 ADD_TO_T('\r');
741 ADD_TO_T('\n');
742 if(*f++ == '\r' && *f == '\n')
743 f++;
745 } else {
746 ADD_TO_T(*f++);
750 *t='\0';
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);
763 LocalUnlock16( h );
764 *((HLOCAL16*)lpBuffer) = h;
765 } else
766 lstrcpynA(lpBuffer,target,nSize);
767 HeapFree(GetProcessHeap(),0,target);
768 if (from) HeapFree(GetProcessHeap(),0,from);
769 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
770 strlen(allocstring):
771 strlen(lpBuffer);
772 #else
773 return 0;
774 #endif /* __i386__ */
776 #undef ADD_TO_T