Change logic of listbox resizing for 16 bits apps for lists without
[wine.git] / misc / lstr.c
blob5379a04e93b7f470202b0b3fe6f9740757228a21
1 /*
2 * String functions
4 * Copyright 1993 Yngvi Sigurjonsson (yngvi@hafro.is)
5 * Copyright 1996 Marcus Meissner
6 */
8 #include "config.h"
10 #include <stdarg.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <ctype.h>
16 #ifdef HAVE_WCTYPE_H
17 # include <wctype.h>
18 #else
19 # define iswalnum(c) isalnum(c)
20 # define iswalpha(c) isalpha(c)
21 # define iswupper(c) isupper(c)
22 # define iswlower(c) islower(c)
23 #endif /* HAVE_WCTYPE_H */
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29 #include "wine/winbase16.h"
30 #include "wine/winuser16.h"
31 #include "wine/unicode.h"
32 #include "winnls.h"
33 #include "task.h"
34 #include "heap.h"
35 #include "ldt.h"
36 #include "stackframe.h"
37 #include "module.h"
38 #include "debugtools.h"
40 DEFAULT_DEBUG_CHANNEL(resource);
42 extern const WORD OLE2NLS_CT_CType3_LUT[]; /* FIXME: does not belong here */
44 /* Funny to divide them between user and kernel. */
46 /***********************************************************************
47 * IsCharAlpha (USER.433)
49 BOOL16 WINAPI IsCharAlpha16(CHAR ch)
51 return isalpha(ch); /* This is probably not right for NLS */
54 /***********************************************************************
55 * IsCharAlphaNumeric (USER.434)
57 BOOL16 WINAPI IsCharAlphaNumeric16(CHAR ch)
59 return isalnum(ch);
62 /***********************************************************************
63 * IsCharUpper (USER.435)
65 BOOL16 WINAPI IsCharUpper16(CHAR ch)
67 return isupper(ch);
70 /***********************************************************************
71 * IsCharLower (USER.436)
73 BOOL16 WINAPI IsCharLower16(CHAR ch)
75 return islower(ch);
78 /***********************************************************************
79 * AnsiUpper16 (USER.431)
81 SEGPTR WINAPI AnsiUpper16( SEGPTR strOrChar )
83 /* I am not sure if the locale stuff works with toupper, but then again
84 I am not sure if the Linux libc locale stuffs works at all */
86 /* uppercase only one char if strOrChar < 0x10000 */
87 if (HIWORD(strOrChar))
89 char *s;
90 for (s = PTR_SEG_TO_LIN(strOrChar); *s; s++) *s = toupper(*s);
91 return strOrChar;
93 else return toupper((char)strOrChar);
97 /***********************************************************************
98 * AnsiUpperBuff16 (USER.437)
100 UINT16 WINAPI AnsiUpperBuff16( LPSTR str, UINT16 len )
102 UINT count = len ? len : 65536;
103 for (; count; count--, str++) *str = toupper(*str);
104 return len;
107 /***********************************************************************
108 * AnsiLower16 (USER.432)
110 SEGPTR WINAPI AnsiLower16( SEGPTR strOrChar )
112 /* I am not sure if the locale stuff works with toupper, but then again
113 I am not sure if the Linux libc locale stuffs works at all */
115 /* lowercase only one char if strOrChar < 0x10000 */
116 if (HIWORD(strOrChar))
118 char *s;
119 for (s = PTR_SEG_TO_LIN( strOrChar ); *s; s++) *s = tolower( *s );
120 return strOrChar;
122 else return tolower((char)strOrChar);
126 /***********************************************************************
127 * AnsiLowerBuff16 (USER.438)
129 UINT16 WINAPI AnsiLowerBuff16( LPSTR str, UINT16 len )
131 UINT count = len ? len : 65536;
132 for (; count; count--, str++) *str = tolower(*str);
133 return len;
137 /***********************************************************************
138 * AnsiNext16 (USER.472)
140 SEGPTR WINAPI AnsiNext16(SEGPTR current)
142 return (*(char *)PTR_SEG_TO_LIN(current)) ? current + 1 : current;
146 /***********************************************************************
147 * AnsiPrev16 (USER.473)
149 SEGPTR WINAPI AnsiPrev16( SEGPTR start, SEGPTR current )
151 return (current == start) ? start : current - 1;
155 /***********************************************************************
156 * CharNextA (USER32.29)
158 LPSTR WINAPI CharNextA( LPCSTR ptr )
160 if (!*ptr) return (LPSTR)ptr;
161 if (IsDBCSLeadByte( *ptr ) && (*(ptr+1) != 0) ) return (LPSTR)(ptr + 2);
162 return (LPSTR)(ptr + 1);
166 /***********************************************************************
167 * CharNextExA (USER32.30)
169 LPSTR WINAPI CharNextExA( WORD codepage, LPCSTR ptr, DWORD flags )
171 if (!*ptr) return (LPSTR)ptr;
172 if (IsDBCSLeadByteEx( codepage, *ptr ) && (*(ptr+1) != 0) ) return (LPSTR)(ptr + 2);
173 return (LPSTR)(ptr + 1);
177 /***********************************************************************
178 * CharNextExW (USER32.31)
180 LPWSTR WINAPI CharNextExW(WORD codepage,LPCWSTR x,DWORD flags)
182 /* FIXME: add DBCS / codepage stuff */
183 if (*x) return (LPWSTR)(x+1);
184 else return (LPWSTR)x;
187 /***********************************************************************
188 * CharNextW (USER32.32)
190 LPWSTR WINAPI CharNextW(LPCWSTR x)
192 if (*x) return (LPWSTR)(x+1);
193 else return (LPWSTR)x;
196 /***********************************************************************
197 * CharPrevA (USER32.33)
199 LPSTR WINAPI CharPrevA( LPCSTR start, LPCSTR ptr )
201 while (*start && (start < ptr))
203 LPCSTR next = CharNextA( start );
204 if (next >= ptr) break;
205 start = next;
207 return (LPSTR)start;
211 /***********************************************************************
212 * CharPrevExA (USER32.34)
214 LPSTR WINAPI CharPrevExA( WORD codepage, LPCSTR start, LPCSTR ptr, DWORD flags )
216 while (*start && (start < ptr))
218 LPCSTR next = CharNextExA( codepage, start, flags );
219 if (next > ptr) break;
220 start = next;
222 return (LPSTR)start;
226 /***********************************************************************
227 * CharPrevExW (USER32.35)
229 LPWSTR WINAPI CharPrevExW(WORD codepage,LPCWSTR start,LPCWSTR x,DWORD flags)
231 /* FIXME: add DBCS / codepage stuff */
232 if (x>start) return (LPWSTR)(x-1);
233 else return (LPWSTR)x;
236 /***********************************************************************
237 * CharPrevW (USER32.36)
239 LPWSTR WINAPI CharPrevW(LPCWSTR start,LPCWSTR x)
241 if (x>start) return (LPWSTR)(x-1);
242 else return (LPWSTR)x;
245 /***********************************************************************
246 * CharLowerA (USER32.25)
247 * FIXME: handle current locale
249 LPSTR WINAPI CharLowerA(LPSTR x)
251 LPSTR s;
253 if (HIWORD(x))
255 s=x;
256 while (*s)
258 *s=tolower(*s);
259 s++;
261 return x;
263 else return (LPSTR)tolower((char)(int)x);
266 /***********************************************************************
267 * CharLowerBuffA (USER32.26)
268 * FIXME: handle current locale
270 DWORD WINAPI CharLowerBuffA(LPSTR x,DWORD buflen)
272 DWORD done=0;
274 if (!x) return 0; /* YES */
275 while (*x && (buflen--))
277 *x=tolower(*x);
278 x++;
279 done++;
281 return done;
284 /***********************************************************************
285 * CharLowerBuffW (USER32.27)
287 DWORD WINAPI CharLowerBuffW(LPWSTR x,DWORD buflen)
289 DWORD done=0;
291 if (!x) return 0; /* YES */
292 while (*x && (buflen--))
294 *x=tolowerW(*x);
295 x++;
296 done++;
298 return done;
301 /***********************************************************************
302 * CharLowerW (USER32.28)
303 * FIXME: handle current locale
305 LPWSTR WINAPI CharLowerW(LPWSTR x)
307 if (HIWORD(x))
309 LPWSTR s = x;
310 while (*s)
312 *s = tolowerW(*s);
313 s++;
315 return x;
317 else return (LPWSTR)((UINT)tolowerW(LOWORD(x)));
320 /***********************************************************************
321 * CharUpperA (USER32.41)
322 * FIXME: handle current locale
324 LPSTR WINAPI CharUpperA(LPSTR x)
326 if (HIWORD(x))
328 LPSTR s = x;
329 while (*s)
331 *s=toupper(*s);
332 s++;
334 return x;
336 return (LPSTR)toupper((char)(int)x);
339 /***********************************************************************
340 * CharUpperBuffA (USER32.42)
341 * FIXME: handle current locale
343 DWORD WINAPI CharUpperBuffA(LPSTR x,DWORD buflen)
345 DWORD done=0;
347 if (!x) return 0; /* YES */
348 while (*x && (buflen--))
350 *x=toupper(*x);
351 x++;
352 done++;
354 return done;
357 /***********************************************************************
358 * CharUpperBuffW (USER32.43)
359 * FIXME: handle current locale
361 DWORD WINAPI CharUpperBuffW(LPWSTR x,DWORD buflen)
363 DWORD done=0;
365 if (!x) return 0; /* YES */
366 while (*x && (buflen--))
368 *x=toupperW(*x);
369 x++;
370 done++;
372 return done;
375 /***********************************************************************
376 * CharUpperW (USER32.44)
377 * FIXME: handle current locale
379 LPWSTR WINAPI CharUpperW(LPWSTR x)
381 if (HIWORD(x))
383 LPWSTR s = x;
384 while (*s)
386 *s = toupperW(*s);
387 s++;
389 return x;
391 else return (LPWSTR)((UINT)toupperW(LOWORD(x)));
394 /***********************************************************************
395 * IsCharAlphaA (USER32.331)
396 * FIXME: handle current locale
398 BOOL WINAPI IsCharAlphaA(CHAR x)
400 return (OLE2NLS_CT_CType3_LUT[(unsigned char)x] & C3_ALPHA);
403 /***********************************************************************
404 * IsCharAlphaNumericA (USER32.332)
405 * FIXME: handle current locale
407 BOOL WINAPI IsCharAlphaNumericA(CHAR x)
409 return IsCharAlphaA(x) || isdigit(x) ;
412 /***********************************************************************
413 * IsCharAlphaNumericW (USER32.333)
414 * FIXME: handle current locale
416 BOOL WINAPI IsCharAlphaNumericW(WCHAR x)
418 return iswalnum(x);
421 /***********************************************************************
422 * IsCharAlphaW (USER32.334)
423 * FIXME: handle current locale
425 BOOL WINAPI IsCharAlphaW(WCHAR x)
427 return iswalpha(x);
430 /***********************************************************************
431 * IsCharLowerA (USER32.335)
432 * FIXME: handle current locale
434 BOOL WINAPI IsCharLowerA(CHAR x)
436 return islower(x);
439 /***********************************************************************
440 * IsCharLowerW (USER32.336)
441 * FIXME: handle current locale
443 BOOL WINAPI IsCharLowerW(WCHAR x)
445 return iswlower(x);
448 /***********************************************************************
449 * IsCharUpperA (USER32.337)
450 * FIXME: handle current locale
452 BOOL WINAPI IsCharUpperA(CHAR x)
454 return isupper(x);
457 /***********************************************************************
458 * IsCharUpperW (USER32.338)
459 * FIXME: handle current locale
461 BOOL WINAPI IsCharUpperW(WCHAR x)
463 return iswupper(x);
466 /***********************************************************************
467 * FormatMessage16 (USER.606)
469 DWORD WINAPI FormatMessage16(
470 DWORD dwFlags,
471 SEGPTR lpSource, /*not always a valid pointer*/
472 WORD dwMessageId,
473 WORD dwLanguageId,
474 LPSTR lpBuffer, /* *((HLOCAL16*)) for FORMAT_MESSAGE_ALLOCATE_BUFFER*/
475 WORD nSize,
476 LPDWORD args /* va_list *args */
478 #ifdef __i386__
479 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
480 LPSTR target,t;
481 DWORD talloced;
482 LPSTR from,f;
483 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
484 BOOL eos = FALSE;
485 LPSTR allocstring = NULL;
487 TRACE("(0x%lx,%lx,%d,0x%x,%p,%d,%p)\n",
488 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
489 if ((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
490 && (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)) return 0;
491 if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
492 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
493 || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
495 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
496 FIXME("line wrapping (%lu) not supported.\n", width);
497 from = NULL;
498 if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
499 from = HEAP_strdupA( GetProcessHeap(), 0, PTR_SEG_TO_LIN(lpSource));
500 if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) {
501 from = HeapAlloc( GetProcessHeap(),0,200 );
502 sprintf(from,"Systemmessage, messageid = 0x%08x\n",dwMessageId);
504 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE) {
505 INT16 bufsize;
506 HINSTANCE16 hinst16 = ((HMODULE)lpSource & 0xffff);
508 dwMessageId &= 0xFFFF;
509 bufsize=LoadString16(hinst16,dwMessageId,NULL,0);
510 if (bufsize) {
511 from = HeapAlloc( GetProcessHeap(), 0, bufsize +1);
512 LoadString16(hinst16,dwMessageId,from,bufsize+1);
515 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
516 t = target;
517 talloced= 100;
519 #define ADD_TO_T(c) \
520 *t++=c;\
521 if (t-target == talloced) {\
522 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
523 t = target+talloced;\
524 talloced*=2;\
527 if (from) {
528 f=from;
529 while (*f && !eos) {
530 if (*f=='%') {
531 int insertnr;
532 char *fmtstr,*x,*lastf;
533 DWORD *argliststart;
535 fmtstr = NULL;
536 lastf = f;
537 f++;
538 if (!*f) {
539 ADD_TO_T('%');
540 continue;
542 switch (*f) {
543 case '1':case '2':case '3':case '4':case '5':
544 case '6':case '7':case '8':case '9':
545 insertnr=*f-'0';
546 switch (f[1]) {
547 case '0':case '1':case '2':case '3':
548 case '4':case '5':case '6':case '7':
549 case '8':case '9':
550 f++;
551 insertnr=insertnr*10+*f-'0';
552 f++;
553 break;
554 default:
555 f++;
556 break;
558 if (*f=='!') {
559 f++;
560 if (NULL!=(x=strchr(f,'!'))) {
561 *x='\0';
562 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
563 sprintf(fmtstr,"%%%s",f);
564 f=x+1;
565 } else {
566 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
567 sprintf(fmtstr,"%%%s",f);
568 f+=strlen(f); /*at \0*/
570 } else
571 if(!args)
572 break;
573 else
574 fmtstr=HEAP_strdupA(GetProcessHeap(),0,"%s");
575 if (args) {
576 int sz;
577 LPSTR b = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz = 100);
579 argliststart=args+insertnr-1;
581 /* CMF - This makes a BIG assumption about va_list */
582 while (vsnprintf(b, sz, fmtstr, (va_list) argliststart) < 0) {
583 b = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, b, sz += 100);
585 for (x=b; *x; x++) ADD_TO_T(*x);
586 } else {
587 /* NULL args - copy formatstr
588 * (probably wrong)
590 while ((lastf<f)&&(*lastf)) {
591 ADD_TO_T(*lastf++);
594 HeapFree(GetProcessHeap(),0,fmtstr);
595 break;
596 case '0': /* Just stop processing format string */
597 eos = TRUE;
598 f++;
599 break;
600 case 'n': /* 16 bit version just outputs 'n' */
601 default:
602 ADD_TO_T(*f++);
603 break;
605 } else { /* '\n' or '\r' gets mapped to "\r\n" */
606 if(*f == '\n' || *f == '\r') {
607 if (width == 0) {
608 ADD_TO_T('\r');
609 ADD_TO_T('\n');
610 if(*f++ == '\r' && *f == '\n')
611 f++;
613 } else {
614 ADD_TO_T(*f++);
618 *t='\0';
620 talloced = strlen(target)+1;
621 if (nSize && talloced<nSize) {
622 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
624 TRACE("-- %s\n",debugstr_a(target));
625 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
626 /* nSize is the MINIMUM size */
627 *((HLOCAL16*)lpBuffer)= LocalAlloc16(LPTR,talloced);
628 allocstring=PTR_SEG_OFF_TO_LIN(CURRENT_DS,*((HLOCAL16*)lpBuffer));
629 memcpy( allocstring,target,talloced);
630 } else
631 lstrcpynA(lpBuffer,target,nSize);
632 HeapFree(GetProcessHeap(),0,target);
633 if (from) HeapFree(GetProcessHeap(),0,from);
634 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
635 strlen(allocstring):
636 strlen(lpBuffer);
637 #else
638 return 0;
639 #endif /* __i386__ */
641 #undef ADD_TO_T