4 * Copyright 1993 Yngvi Sigurjonsson (yngvi@hafro.is)
5 * Copyright 1996 Marcus Meissner
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 */
29 #include "wine/winbase16.h"
30 #include "wine/winuser16.h"
31 #include "wine/unicode.h"
36 #include "stackframe.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
)
62 /***********************************************************************
63 * IsCharUpper (USER.435)
65 BOOL16 WINAPI
IsCharUpper16(CHAR ch
)
70 /***********************************************************************
71 * IsCharLower (USER.436)
73 BOOL16 WINAPI
IsCharLower16(CHAR 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
))
90 for (s
= PTR_SEG_TO_LIN(strOrChar
); *s
; s
++) *s
= toupper(*s
);
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
);
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
))
119 for (s
= PTR_SEG_TO_LIN( strOrChar
); *s
; s
++) *s
= tolower( *s
);
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
);
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;
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;
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
)
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
)
274 if (!x
) return 0; /* YES */
275 while (*x
&& (buflen
--))
284 /***********************************************************************
285 * CharLowerBuffW (USER32.27)
287 DWORD WINAPI
CharLowerBuffW(LPWSTR x
,DWORD buflen
)
291 if (!x
) return 0; /* YES */
292 while (*x
&& (buflen
--))
301 /***********************************************************************
302 * CharLowerW (USER32.28)
303 * FIXME: handle current locale
305 LPWSTR WINAPI
CharLowerW(LPWSTR 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
)
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
)
347 if (!x
) return 0; /* YES */
348 while (*x
&& (buflen
--))
357 /***********************************************************************
358 * CharUpperBuffW (USER32.43)
359 * FIXME: handle current locale
361 DWORD WINAPI
CharUpperBuffW(LPWSTR x
,DWORD buflen
)
365 if (!x
) return 0; /* YES */
366 while (*x
&& (buflen
--))
375 /***********************************************************************
376 * CharUpperW (USER32.44)
377 * FIXME: handle current locale
379 LPWSTR WINAPI
CharUpperW(LPWSTR 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
)
421 /***********************************************************************
422 * IsCharAlphaW (USER32.334)
423 * FIXME: handle current locale
425 BOOL WINAPI
IsCharAlphaW(WCHAR x
)
430 /***********************************************************************
431 * IsCharLowerA (USER32.335)
432 * FIXME: handle current locale
434 BOOL WINAPI
IsCharLowerA(CHAR x
)
439 /***********************************************************************
440 * IsCharLowerW (USER32.336)
441 * FIXME: handle current locale
443 BOOL WINAPI
IsCharLowerW(WCHAR x
)
448 /***********************************************************************
449 * IsCharUpperA (USER32.337)
450 * FIXME: handle current locale
452 BOOL WINAPI
IsCharUpperA(CHAR x
)
457 /***********************************************************************
458 * IsCharUpperW (USER32.338)
459 * FIXME: handle current locale
461 BOOL WINAPI
IsCharUpperW(WCHAR x
)
466 /***********************************************************************
467 * FormatMessage16 (USER.606)
469 DWORD WINAPI
FormatMessage16(
471 SEGPTR lpSource
, /*not always a valid pointer*/
474 LPSTR lpBuffer
, /* *((HLOCAL16*)) for FORMAT_MESSAGE_ALLOCATE_BUFFER*/
476 LPDWORD args
/* va_list *args */
479 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
483 DWORD width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
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
);
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
) {
506 HINSTANCE16 hinst16
= ((HMODULE
)lpSource
& 0xffff);
508 dwMessageId
&= 0xFFFF;
509 bufsize
=LoadString16(hinst16
,dwMessageId
,NULL
,0);
511 from
= HeapAlloc( GetProcessHeap(), 0, bufsize
+1);
512 LoadString16(hinst16
,dwMessageId
,from
,bufsize
+1);
515 target
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 100);
519 #define ADD_TO_T(c) \
521 if (t-target == talloced) {\
522 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
523 t = target+talloced;\
532 char *fmtstr
,*x
,*lastf
;
543 case '1':case '2':case '3':case '4':case '5':
544 case '6':case '7':case '8':case '9':
547 case '0':case '1':case '2':case '3':
548 case '4':case '5':case '6':case '7':
551 insertnr
=insertnr
*10+*f
-'0';
560 if (NULL
!=(x
=strchr(f
,'!'))) {
562 fmtstr
=HeapAlloc(GetProcessHeap(),0,strlen(f
)+2);
563 sprintf(fmtstr
,"%%%s",f
);
566 fmtstr
=HeapAlloc(GetProcessHeap(),0,strlen(f
)+2);
567 sprintf(fmtstr
,"%%%s",f
);
568 f
+=strlen(f
); /*at \0*/
574 fmtstr
=HEAP_strdupA(GetProcessHeap(),0,"%s");
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
);
587 /* NULL args - copy formatstr
590 while ((lastf
<f
)&&(*lastf
)) {
594 HeapFree(GetProcessHeap(),0,fmtstr
);
596 case '0': /* Just stop processing format string */
600 case 'n': /* 16 bit version just outputs 'n' */
605 } else { /* '\n' or '\r' gets mapped to "\r\n" */
606 if(*f
== '\n' || *f
== '\r') {
610 if(*f
++ == '\r' && *f
== '\n')
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
);
631 lstrcpynA(lpBuffer
,target
,nSize
);
632 HeapFree(GetProcessHeap(),0,target
);
633 if (from
) HeapFree(GetProcessHeap(),0,from
);
634 return (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ?
639 #endif /* __i386__ */