Fixed CharNextW.
[wine.git] / dlls / user / lstr.c
blob614c69c8d6359764c4759f224962bbda292890de
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 "ldt.h"
23 #include "debugtools.h"
25 DEFAULT_DEBUG_CHANNEL(resource);
27 /***********************************************************************
28 * AnsiToOem16 (KEYBOARD.5)
30 INT16 WINAPI AnsiToOem16( LPCSTR s, LPSTR d )
32 CharToOemA( s, d );
33 return -1;
37 /***********************************************************************
38 * OemToAnsi16 (KEYBOARD.6)
40 INT16 WINAPI OemToAnsi16( LPCSTR s, LPSTR d )
42 OemToCharA( s, d );
43 return -1;
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) );
74 return 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) );
89 return 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 );
101 return len;
105 /***********************************************************************
106 * AnsiLowerBuff16 (USER.438)
108 UINT16 WINAPI AnsiLowerBuff16( LPSTR str, UINT16 len )
110 CharLowerBuffA( str, len ? len : 65536 );
111 return len;
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 */
163 return NULL;
167 /***********************************************************************
168 * CharNextW (USER32.@)
170 LPWSTR WINAPI CharNextW(LPCWSTR x)
172 if (*x) x++;
174 return (LPWSTR)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;
187 start = next;
189 return (LPSTR)start;
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;
202 start = next;
204 return (LPSTR)start;
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 */
214 return NULL;
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 )
243 WCHAR *bufW;
245 bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
246 if( bufW )
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 );
252 return TRUE;
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 );
263 return TRUE;
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 )
290 WCHAR *bufW;
292 bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
293 if( bufW )
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 );
299 return TRUE;
303 /***********************************************************************
304 * OemToCharBuffW (USER32.@)
306 BOOL WINAPI OemToCharBuffW( LPCSTR s, LPWSTR d, DWORD len )
308 MultiByteToWideChar( CP_OEMCP, 0, s, len, d, len );
309 return TRUE;
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(
326 DWORD dwFlags,
327 SEGPTR lpSource, /*not always a valid pointer*/
328 WORD dwMessageId,
329 WORD dwLanguageId,
330 LPSTR lpBuffer, /* *((HLOCAL16*)) for FORMAT_MESSAGE_ALLOCATE_BUFFER*/
331 WORD nSize,
332 LPDWORD args /* va_list *args */
334 #ifdef __i386__
335 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
336 LPSTR target,t;
337 DWORD talloced;
338 LPSTR from,f;
339 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
340 BOOL eos = FALSE;
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);
353 from = NULL;
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) {
361 INT16 bufsize;
362 HINSTANCE16 hinst16 = ((HMODULE)lpSource & 0xffff);
364 dwMessageId &= 0xFFFF;
365 bufsize=LoadString16(hinst16,dwMessageId,NULL,0);
366 if (bufsize) {
367 from = HeapAlloc( GetProcessHeap(), 0, bufsize +1);
368 LoadString16(hinst16,dwMessageId,from,bufsize+1);
371 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
372 t = target;
373 talloced= 100;
375 #define ADD_TO_T(c) \
376 *t++=c;\
377 if (t-target == talloced) {\
378 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
379 t = target+talloced;\
380 talloced*=2;\
383 if (from) {
384 f=from;
385 while (*f && !eos) {
386 if (*f=='%') {
387 int insertnr;
388 char *fmtstr,*x,*lastf;
389 DWORD *argliststart;
391 fmtstr = NULL;
392 lastf = f;
393 f++;
394 if (!*f) {
395 ADD_TO_T('%');
396 continue;
398 switch (*f) {
399 case '1':case '2':case '3':case '4':case '5':
400 case '6':case '7':case '8':case '9':
401 insertnr=*f-'0';
402 switch (f[1]) {
403 case '0':case '1':case '2':case '3':
404 case '4':case '5':case '6':case '7':
405 case '8':case '9':
406 f++;
407 insertnr=insertnr*10+*f-'0';
408 f++;
409 break;
410 default:
411 f++;
412 break;
414 if (*f=='!') {
415 f++;
416 if (NULL!=(x=strchr(f,'!'))) {
417 *x='\0';
418 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
419 sprintf(fmtstr,"%%%s",f);
420 f=x+1;
421 } else {
422 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
423 sprintf(fmtstr,"%%%s",f);
424 f+=strlen(f); /*at \0*/
426 } else
427 if(!args)
428 break;
429 else
430 fmtstr=HEAP_strdupA(GetProcessHeap(),0,"%s");
431 if (args) {
432 int sz;
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);
442 } else {
443 /* NULL args - copy formatstr
444 * (probably wrong)
446 while ((lastf<f)&&(*lastf)) {
447 ADD_TO_T(*lastf++);
450 HeapFree(GetProcessHeap(),0,fmtstr);
451 break;
452 case '0': /* Just stop processing format string */
453 eos = TRUE;
454 f++;
455 break;
456 case 'n': /* 16 bit version just outputs 'n' */
457 default:
458 ADD_TO_T(*f++);
459 break;
461 } else { /* '\n' or '\r' gets mapped to "\r\n" */
462 if(*f == '\n' || *f == '\r') {
463 if (width == 0) {
464 ADD_TO_T('\r');
465 ADD_TO_T('\n');
466 if(*f++ == '\r' && *f == '\n')
467 f++;
469 } else {
470 ADD_TO_T(*f++);
474 *t='\0';
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);
487 LocalUnlock16( h );
488 *((HLOCAL16*)lpBuffer) = h;
489 } else
490 lstrcpynA(lpBuffer,target,nSize);
491 HeapFree(GetProcessHeap(),0,target);
492 if (from) HeapFree(GetProcessHeap(),0,from);
493 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
494 strlen(allocstring):
495 strlen(lpBuffer);
496 #else
497 return 0;
498 #endif /* __i386__ */
500 #undef ADD_TO_T