Replaced PTR_SEG_TO_LIN macro by exported MapSL function.
[wine.git] / dlls / user / lstr.c
blob1fc3c6b2def9a24fa2cd40a67f397601071eee4c
1 /*
2 * USER string functions
4 * Copyright 1993 Yngvi Sigurjonsson (yngvi@hafro.is)
5 * Copyright 1996 Alexandre Julliard
6 * Copyright 1996 Marcus Meissner
7 */
9 #include <ctype.h>
10 #include <stdarg.h>
11 #include <stdlib.h>
12 #include <stdio.h>
14 #include "windef.h"
15 #include "winbase.h"
16 #include "winnls.h"
17 #include "wine/winbase16.h"
18 #include "wine/winuser16.h"
19 #include "wine/unicode.h"
21 #include "heap.h"
22 #include "debugtools.h"
24 DEFAULT_DEBUG_CHANNEL(resource);
26 /***********************************************************************
27 * AnsiToOem16 (KEYBOARD.5)
29 INT16 WINAPI AnsiToOem16( LPCSTR s, LPSTR d )
31 CharToOemA( s, d );
32 return -1;
36 /***********************************************************************
37 * OemToAnsi16 (KEYBOARD.6)
39 INT16 WINAPI OemToAnsi16( LPCSTR s, LPSTR d )
41 OemToCharA( s, d );
42 return -1;
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) );
82 return 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) );
97 return 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 );
109 return len;
113 /***********************************************************************
114 * AnsiLowerBuff16 (USER.438)
116 UINT16 WINAPI AnsiLowerBuff16( LPSTR str, UINT16 len )
118 CharLowerBuffA( str, len ? len : 65536 );
119 return len;
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 */
171 return NULL;
175 /***********************************************************************
176 * CharNextW (USER32.@)
178 LPWSTR WINAPI CharNextW(LPCWSTR x)
180 if (*x) x++;
182 return (LPWSTR)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;
195 start = next;
197 return (LPSTR)start;
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;
210 start = next;
212 return (LPSTR)start;
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 */
222 return NULL;
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 )
251 WCHAR *bufW;
253 bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
254 if( bufW )
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 );
260 return TRUE;
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 );
271 return TRUE;
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 )
298 WCHAR *bufW;
300 bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
301 if( bufW )
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 );
307 return TRUE;
311 /***********************************************************************
312 * OemToCharBuffW (USER32.@)
314 BOOL WINAPI OemToCharBuffW( LPCSTR s, LPWSTR d, DWORD len )
316 MultiByteToWideChar( CP_OEMCP, 0, s, len, d, len );
317 return TRUE;
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)
336 LPSTR s;
338 if (HIWORD(x))
340 s=x;
341 while (*s)
343 *s=tolower(*s);
344 s++;
346 return x;
348 else return (LPSTR)tolower((char)(int)x);
352 /***********************************************************************
353 * CharUpperA (USER32.@)
354 * FIXME: handle current locale
356 LPSTR WINAPI CharUpperA(LPSTR x)
358 if (HIWORD(x))
360 LPSTR s = x;
361 while (*s)
363 *s=toupper(*s);
364 s++;
366 return 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 )
399 DWORD ret = len;
400 if (!str) return 0; /* YES */
401 for (; len; len--, str++) *str = tolower(*str);
402 return ret;
406 /***********************************************************************
407 * CharLowerBuffW (USER32.@)
409 DWORD WINAPI CharLowerBuffW( LPWSTR str, DWORD len )
411 DWORD ret = len;
412 if (!str) return 0; /* YES */
413 for (; len; len--, str++) *str = tolowerW(*str);
414 return ret;
418 /***********************************************************************
419 * CharUpperBuffA (USER32.@)
420 * FIXME: handle current locale
422 DWORD WINAPI CharUpperBuffA( LPSTR str, DWORD len )
424 DWORD ret = len;
425 if (!str) return 0; /* YES */
426 for (; len; len--, str++) *str = toupper(*str);
427 return ret;
431 /***********************************************************************
432 * CharUpperBuffW (USER32.@)
434 DWORD WINAPI CharUpperBuffW( LPWSTR str, DWORD len )
436 DWORD ret = len;
437 if (!str) return 0; /* YES */
438 for (; len; len--, str++) *str = toupperW(*str);
439 return ret;
443 /***********************************************************************
444 * IsCharLowerA (USER.436) (USER32.@)
445 * FIXME: handle current locale
447 BOOL WINAPI IsCharLowerA(CHAR x)
449 return islower(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)
468 return isupper(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(
503 DWORD dwFlags,
504 SEGPTR lpSource, /* [in] NOTE: not always a valid pointer */
505 WORD dwMessageId,
506 WORD dwLanguageId,
507 LPSTR lpBuffer, /* [out] NOTE: *((HLOCAL16*)) for FORMAT_MESSAGE_ALLOCATE_BUFFER*/
508 WORD nSize,
509 LPDWORD args /* [in] NOTE: va_list *args */
511 #ifdef __i386__
512 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
513 LPSTR target,t;
514 DWORD talloced;
515 LPSTR from,f;
516 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
517 BOOL eos = FALSE;
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);
530 from = NULL;
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) {
538 INT16 bufsize;
539 HINSTANCE16 hinst16 = ((HMODULE)lpSource & 0xffff);
541 dwMessageId &= 0xFFFF;
542 bufsize=LoadString16(hinst16,dwMessageId,NULL,0);
543 if (bufsize) {
544 from = HeapAlloc( GetProcessHeap(), 0, bufsize +1);
545 LoadString16(hinst16,dwMessageId,from,bufsize+1);
548 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
549 t = target;
550 talloced= 100;
552 #define ADD_TO_T(c) \
553 *t++=c;\
554 if (t-target == talloced) {\
555 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
556 t = target+talloced;\
557 talloced*=2;\
560 if (from) {
561 f=from;
562 while (*f && !eos) {
563 if (*f=='%') {
564 int insertnr;
565 char *fmtstr,*x,*lastf;
566 DWORD *argliststart;
568 fmtstr = NULL;
569 lastf = f;
570 f++;
571 if (!*f) {
572 ADD_TO_T('%');
573 continue;
575 switch (*f) {
576 case '1':case '2':case '3':case '4':case '5':
577 case '6':case '7':case '8':case '9':
578 insertnr=*f-'0';
579 switch (f[1]) {
580 case '0':case '1':case '2':case '3':
581 case '4':case '5':case '6':case '7':
582 case '8':case '9':
583 f++;
584 insertnr=insertnr*10+*f-'0';
585 f++;
586 break;
587 default:
588 f++;
589 break;
591 if (*f=='!') {
592 f++;
593 if (NULL!=(x=strchr(f,'!'))) {
594 *x='\0';
595 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
596 sprintf(fmtstr,"%%%s",f);
597 f=x+1;
598 } else {
599 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
600 sprintf(fmtstr,"%%%s",f);
601 f+=strlen(f); /*at \0*/
603 } else
604 if(!args)
605 break;
606 else
607 fmtstr=HEAP_strdupA(GetProcessHeap(),0,"%s");
608 if (args) {
609 int sz;
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);
619 } else {
620 /* NULL args - copy formatstr
621 * (probably wrong)
623 while ((lastf<f)&&(*lastf)) {
624 ADD_TO_T(*lastf++);
627 HeapFree(GetProcessHeap(),0,fmtstr);
628 break;
629 case '0': /* Just stop processing format string */
630 eos = TRUE;
631 f++;
632 break;
633 case 'n': /* 16 bit version just outputs 'n' */
634 default:
635 ADD_TO_T(*f++);
636 break;
638 } else { /* '\n' or '\r' gets mapped to "\r\n" */
639 if(*f == '\n' || *f == '\r') {
640 if (width == 0) {
641 ADD_TO_T('\r');
642 ADD_TO_T('\n');
643 if(*f++ == '\r' && *f == '\n')
644 f++;
646 } else {
647 ADD_TO_T(*f++);
651 *t='\0';
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);
664 LocalUnlock16( h );
665 *((HLOCAL16*)lpBuffer) = h;
666 } else
667 lstrcpynA(lpBuffer,target,nSize);
668 HeapFree(GetProcessHeap(),0,target);
669 if (from) HeapFree(GetProcessHeap(),0,from);
670 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
671 strlen(allocstring):
672 strlen(lpBuffer);
673 #else
674 return 0;
675 #endif /* __i386__ */
677 #undef ADD_TO_T