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 * AnsiUpper16 (USER.431)
68 SEGPTR WINAPI
AnsiUpper16( SEGPTR strOrChar
)
70 /* uppercase only one char if strOrChar < 0x10000 */
71 if (HIWORD(strOrChar
))
73 CharUpperA( PTR_SEG_TO_LIN(strOrChar
) );
76 else return toupper((char)strOrChar
);
80 /***********************************************************************
81 * AnsiLower16 (USER.432)
83 SEGPTR WINAPI
AnsiLower16( SEGPTR strOrChar
)
85 /* lowercase only one char if strOrChar < 0x10000 */
86 if (HIWORD(strOrChar
))
88 CharLowerA( PTR_SEG_TO_LIN(strOrChar
) );
91 else return tolower((char)strOrChar
);
95 /***********************************************************************
96 * AnsiUpperBuff16 (USER.437)
98 UINT16 WINAPI
AnsiUpperBuff16( LPSTR str
, UINT16 len
)
100 CharUpperBuffA( str
, len
? len
: 65536 );
105 /***********************************************************************
106 * AnsiLowerBuff16 (USER.438)
108 UINT16 WINAPI
AnsiLowerBuff16( LPSTR str
, UINT16 len
)
110 CharLowerBuffA( str
, len
? len
: 65536 );
115 /***********************************************************************
116 * AnsiNext16 (USER.472)
118 SEGPTR WINAPI
AnsiNext16(SEGPTR current
)
120 char *ptr
= (char *)PTR_SEG_TO_LIN(current
);
121 return current
+ (CharNextA(ptr
) - ptr
);
125 /***********************************************************************
126 * AnsiPrev16 (USER.473)
128 SEGPTR WINAPI
AnsiPrev16( LPCSTR start
, SEGPTR current
)
130 char *ptr
= (char *)PTR_SEG_TO_LIN(current
);
131 return current
- (ptr
- CharPrevA( start
, ptr
));
135 /***********************************************************************
136 * CharNextA (USER32.@)
138 LPSTR WINAPI
CharNextA( LPCSTR ptr
)
140 if (!*ptr
) return (LPSTR
)ptr
;
141 if (IsDBCSLeadByte( ptr
[0] ) && ptr
[1]) return (LPSTR
)(ptr
+ 2);
142 return (LPSTR
)(ptr
+ 1);
146 /***********************************************************************
147 * CharNextExA (USER32.@)
149 LPSTR WINAPI
CharNextExA( WORD codepage
, LPCSTR ptr
, DWORD flags
)
151 if (!*ptr
) return (LPSTR
)ptr
;
152 if (IsDBCSLeadByteEx( codepage
, ptr
[0] ) && ptr
[1]) return (LPSTR
)(ptr
+ 2);
153 return (LPSTR
)(ptr
+ 1);
157 /***********************************************************************
158 * CharNextExW (USER32.@)
160 LPWSTR WINAPI
CharNextExW( WORD codepage
, LPCWSTR ptr
, DWORD flags
)
162 /* doesn't make sense, there are no codepages for Unicode */
167 /***********************************************************************
168 * CharNextW (USER32.@)
170 LPWSTR WINAPI
CharNextW(LPCWSTR x
)
178 /***********************************************************************
179 * CharPrevA (USER32.@)
181 LPSTR WINAPI
CharPrevA( LPCSTR start
, LPCSTR ptr
)
183 while (*start
&& (start
< ptr
))
185 LPCSTR next
= CharNextA( start
);
186 if (next
>= ptr
) break;
193 /***********************************************************************
194 * CharPrevExA (USER32.@)
196 LPSTR WINAPI
CharPrevExA( WORD codepage
, LPCSTR start
, LPCSTR ptr
, DWORD flags
)
198 while (*start
&& (start
< ptr
))
200 LPCSTR next
= CharNextExA( codepage
, start
, flags
);
201 if (next
> ptr
) break;
208 /***********************************************************************
209 * CharPrevExW (USER32.@)
211 LPSTR WINAPI
CharPrevExW( WORD codepage
, LPCWSTR start
, LPCWSTR ptr
, DWORD flags
)
213 /* doesn't make sense, there are no codepages for Unicode */
218 /***********************************************************************
219 * CharPrevW (USER32.@)
221 LPWSTR WINAPI
CharPrevW(LPCWSTR start
,LPCWSTR x
)
223 if (x
>start
) return (LPWSTR
)(x
-1);
224 else return (LPWSTR
)x
;
228 /***********************************************************************
229 * CharToOemA (USER32.@)
231 BOOL WINAPI
CharToOemA( LPCSTR s
, LPSTR d
)
233 if ( !s
|| !d
) return TRUE
;
234 return CharToOemBuffA( s
, d
, strlen( s
) + 1 );
238 /***********************************************************************
239 * CharToOemBuffA (USER32.@)
241 BOOL WINAPI
CharToOemBuffA( LPCSTR s
, LPSTR d
, DWORD len
)
245 bufW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
248 MultiByteToWideChar( CP_ACP
, 0, s
, len
, bufW
, len
);
249 WideCharToMultiByte( CP_OEMCP
, 0, bufW
, len
, d
, len
, NULL
, NULL
);
250 HeapFree( GetProcessHeap(), 0, bufW
);
256 /***********************************************************************
257 * CharToOemBuffW (USER32.@)
259 BOOL WINAPI
CharToOemBuffW( LPCWSTR s
, LPSTR d
, DWORD len
)
261 if ( !s
|| !d
) return TRUE
;
262 WideCharToMultiByte( CP_OEMCP
, 0, s
, len
, d
, len
, NULL
, NULL
);
267 /***********************************************************************
268 * CharToOemW (USER32.@)
270 BOOL WINAPI
CharToOemW( LPCWSTR s
, LPSTR d
)
272 return CharToOemBuffW( s
, d
, strlenW( s
) + 1 );
276 /***********************************************************************
277 * OemToCharA (USER32.@)
279 BOOL WINAPI
OemToCharA( LPCSTR s
, LPSTR d
)
281 return OemToCharBuffA( s
, d
, strlen( s
) + 1 );
285 /***********************************************************************
286 * OemToCharBuffA (USER32.@)
288 BOOL WINAPI
OemToCharBuffA( LPCSTR s
, LPSTR d
, DWORD len
)
292 bufW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
295 MultiByteToWideChar( CP_OEMCP
, 0, s
, len
, bufW
, len
);
296 WideCharToMultiByte( CP_ACP
, 0, bufW
, len
, d
, len
, NULL
, NULL
);
297 HeapFree( GetProcessHeap(), 0, bufW
);
303 /***********************************************************************
304 * OemToCharBuffW (USER32.@)
306 BOOL WINAPI
OemToCharBuffW( LPCSTR s
, LPWSTR d
, DWORD len
)
308 MultiByteToWideChar( CP_OEMCP
, 0, s
, len
, d
, len
);
313 /***********************************************************************
314 * OemToCharW (USER32.@)
316 BOOL WINAPI
OemToCharW( LPCSTR s
, LPWSTR d
)
318 return OemToCharBuffW( s
, d
, strlen( s
) + 1 );
322 /***********************************************************************
323 * FormatMessage16 (USER.606)
325 DWORD WINAPI
FormatMessage16(
327 SEGPTR lpSource
, /*not always a valid pointer*/
330 LPSTR lpBuffer
, /* *((HLOCAL16*)) for FORMAT_MESSAGE_ALLOCATE_BUFFER*/
332 LPDWORD args
/* va_list *args */
335 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
339 DWORD width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
341 LPSTR allocstring
= NULL
;
343 TRACE("(0x%lx,%lx,%d,0x%x,%p,%d,%p)\n",
344 dwFlags
,lpSource
,dwMessageId
,dwLanguageId
,lpBuffer
,nSize
,args
);
345 if ((dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
)
346 && (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
)) return 0;
347 if ((dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
348 &&((dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
)
349 || (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
))) return 0;
351 if (width
&& width
!= FORMAT_MESSAGE_MAX_WIDTH_MASK
)
352 FIXME("line wrapping (%lu) not supported.\n", width
);
354 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
355 from
= HEAP_strdupA( GetProcessHeap(), 0, PTR_SEG_TO_LIN(lpSource
));
356 if (dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
) {
357 from
= HeapAlloc( GetProcessHeap(),0,200 );
358 sprintf(from
,"Systemmessage, messageid = 0x%08x\n",dwMessageId
);
360 if (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
) {
362 HINSTANCE16 hinst16
= ((HMODULE
)lpSource
& 0xffff);
364 dwMessageId
&= 0xFFFF;
365 bufsize
=LoadString16(hinst16
,dwMessageId
,NULL
,0);
367 from
= HeapAlloc( GetProcessHeap(), 0, bufsize
+1);
368 LoadString16(hinst16
,dwMessageId
,from
,bufsize
+1);
371 target
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 100);
375 #define ADD_TO_T(c) \
377 if (t-target == talloced) {\
378 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
379 t = target+talloced;\
388 char *fmtstr
,*x
,*lastf
;
399 case '1':case '2':case '3':case '4':case '5':
400 case '6':case '7':case '8':case '9':
403 case '0':case '1':case '2':case '3':
404 case '4':case '5':case '6':case '7':
407 insertnr
=insertnr
*10+*f
-'0';
416 if (NULL
!=(x
=strchr(f
,'!'))) {
418 fmtstr
=HeapAlloc(GetProcessHeap(),0,strlen(f
)+2);
419 sprintf(fmtstr
,"%%%s",f
);
422 fmtstr
=HeapAlloc(GetProcessHeap(),0,strlen(f
)+2);
423 sprintf(fmtstr
,"%%%s",f
);
424 f
+=strlen(f
); /*at \0*/
430 fmtstr
=HEAP_strdupA(GetProcessHeap(),0,"%s");
433 LPSTR b
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sz
= 100);
435 argliststart
=args
+insertnr
-1;
437 /* CMF - This makes a BIG assumption about va_list */
438 while (vsnprintf(b
, sz
, fmtstr
, (va_list) argliststart
) < 0) {
439 b
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, b
, sz
+= 100);
441 for (x
=b
; *x
; x
++) ADD_TO_T(*x
);
443 /* NULL args - copy formatstr
446 while ((lastf
<f
)&&(*lastf
)) {
450 HeapFree(GetProcessHeap(),0,fmtstr
);
452 case '0': /* Just stop processing format string */
456 case 'n': /* 16 bit version just outputs 'n' */
461 } else { /* '\n' or '\r' gets mapped to "\r\n" */
462 if(*f
== '\n' || *f
== '\r') {
466 if(*f
++ == '\r' && *f
== '\n')
476 talloced
= strlen(target
)+1;
477 if (nSize
&& talloced
<nSize
) {
478 target
= (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,target
,nSize
);
480 TRACE("-- %s\n",debugstr_a(target
));
481 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) {
482 /* nSize is the MINIMUM size */
483 HLOCAL16 h
= LocalAlloc16(LPTR
,talloced
);
484 SEGPTR ptr
= LocalLock16(h
);
485 allocstring
= PTR_SEG_TO_LIN( ptr
);
486 memcpy( allocstring
,target
,talloced
);
488 *((HLOCAL16
*)lpBuffer
) = h
;
490 lstrcpynA(lpBuffer
,target
,nSize
);
491 HeapFree(GetProcessHeap(),0,target
);
492 if (from
) HeapFree(GetProcessHeap(),0,from
);
493 return (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ?
498 #endif /* __i386__ */