Renamed all LPIMAGE_* types to PIMAGE_*.
[wine/multimedia.git] / misc / lstr.c
blobc121ad41eb021f0dff58ffa9ac1f851e846bafa6
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 <string.h>
13 #include <ctype.h>
15 #ifdef HAVE_WCTYPE_H
16 # include <wctype.h>
17 #else
18 # define iswalnum(c) isalnum(c)
19 # define iswalpha(c) isalpha(c)
20 # define iswupper(c) isupper(c)
21 # define iswlower(c) islower(c)
22 #endif /* HAVE_WCTYPE_H */
25 #include "windows.h"
26 #include "winnt.h" /* HEAP_ macros */
27 #include "task.h"
28 #include "heap.h"
29 #include "ldt.h"
30 #include "stackframe.h"
31 #include "module.h"
32 #include "debug.h"
34 /* Funny to divide them between user and kernel. */
36 /* be careful: always use functions from wctype.h if character > 255 */
39 * Unicode case conversion routines ... these should be used where
40 * toupper/tolower are used for ASCII.
42 #ifndef HAVE_WCTYPE_H
43 /* FIXME: should probably get rid of wctype.h altogether */
44 #include "casemap.h"
46 WCHAR towupper(WCHAR code)
48 const WCHAR * ptr = uprtable[HIBYTE(code)];
49 return ptr ? ptr[LOBYTE(code)] : code;
52 WCHAR towlower(WCHAR code)
54 const WCHAR * ptr = lwrtable[HIBYTE(code)];
55 return ptr ? ptr[LOBYTE(code)] : code;
57 #endif /* HAVE_WCTYPE_H */
59 /***********************************************************************
60 * IsCharAlpha (USER.433)
62 BOOL16 WINAPI IsCharAlpha16(CHAR ch)
64 return isalpha(ch); /* This is probably not right for NLS */
67 /***********************************************************************
68 * IsCharAlphanumeric (USER.434)
70 BOOL16 WINAPI IsCharAlphaNumeric16(CHAR ch)
72 return isalnum(ch);
75 /***********************************************************************
76 * IsCharUpper (USER.435)
78 BOOL16 WINAPI IsCharUpper16(CHAR ch)
80 return isupper(ch);
83 /***********************************************************************
84 * IsCharLower (USER.436)
86 BOOL16 WINAPI IsCharLower16(CHAR ch)
88 return islower(ch);
91 /***********************************************************************
92 * AnsiUpper16 (USER.431)
94 SEGPTR WINAPI AnsiUpper16( SEGPTR strOrChar )
96 /* I am not sure if the locale stuff works with toupper, but then again
97 I am not sure if the Linux libc locale stuffs works at all */
99 /* uppercase only one char if strOrChar < 0x10000 */
100 if (HIWORD(strOrChar))
102 char *s;
103 for (s = PTR_SEG_TO_LIN(strOrChar); *s; s++) *s = toupper(*s);
104 return strOrChar;
106 else return toupper((char)strOrChar);
110 /***********************************************************************
111 * AnsiUpperBuff16 (USER.437)
113 UINT16 WINAPI AnsiUpperBuff16( LPSTR str, UINT16 len )
115 UINT32 count = len ? len : 65536;
116 for (; count; count--, str++) *str = toupper(*str);
117 return len;
120 /***********************************************************************
121 * AnsiLower16 (USER.432)
123 SEGPTR WINAPI AnsiLower16( SEGPTR strOrChar )
125 /* I am not sure if the locale stuff works with toupper, but then again
126 I am not sure if the Linux libc locale stuffs works at all */
128 /* lowercase only one char if strOrChar < 0x10000 */
129 if (HIWORD(strOrChar))
131 char *s;
132 for (s = PTR_SEG_TO_LIN( strOrChar ); *s; s++) *s = tolower( *s );
133 return strOrChar;
135 else return tolower((char)strOrChar);
139 /***********************************************************************
140 * AnsiLowerBuff16 (USER.438)
142 UINT16 WINAPI AnsiLowerBuff16( LPSTR str, UINT16 len )
144 UINT32 count = len ? len : 65536;
145 for (; count; count--, str++) *str = tolower(*str);
146 return len;
150 /***********************************************************************
151 * AnsiNext16 (USER.472)
153 SEGPTR WINAPI AnsiNext16(SEGPTR current)
155 return (*(char *)PTR_SEG_TO_LIN(current)) ? current + 1 : current;
159 /***********************************************************************
160 * AnsiPrev16 (USER.473)
162 SEGPTR WINAPI AnsiPrev16( SEGPTR start, SEGPTR current )
164 return (current == start) ? start : current - 1;
168 /***********************************************************************
169 * OutputDebugString16 (KERNEL.115)
171 void WINAPI OutputDebugString16( LPCSTR str )
173 char module[10];
174 if (!GetModuleName( GetCurrentTask(), module, sizeof(module) ))
175 strcpy( module, "???" );
177 DUMP( "%s says %s\n", module, debugstr_a(str) );
181 /***********************************************************************
182 * OutputDebugString32A (KERNEL32
184 void WINAPI OutputDebugString32A( LPCSTR str )
186 OutputDebugString16( str );
191 /***********************************************************************
192 * OutputDebugString32W (KERNEL32
194 void WINAPI OutputDebugString32W( LPCWSTR str )
196 LPSTR p = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
197 OutputDebugString32A( p );
198 HeapFree( GetProcessHeap(), 0, p );
203 /***********************************************************************
204 * CharNext32A (USER32.29)
206 LPSTR WINAPI CharNext32A( LPCSTR ptr )
208 if (!*ptr) return (LPSTR)ptr;
209 if (IsDBCSLeadByte32( *ptr )) return (LPSTR)(ptr + 2);
210 return (LPSTR)(ptr + 1);
214 /***********************************************************************
215 * CharNextEx32A (USER32.30)
217 LPSTR WINAPI CharNextEx32A( WORD codepage, LPCSTR ptr, DWORD flags )
219 if (!*ptr) return (LPSTR)ptr;
220 if (IsDBCSLeadByteEx( codepage, *ptr )) return (LPSTR)(ptr + 2);
221 return (LPSTR)(ptr + 1);
225 /***********************************************************************
226 * CharNextExW (USER32.31)
228 LPWSTR WINAPI CharNextEx32W(WORD codepage,LPCWSTR x,DWORD flags)
230 /* FIXME: add DBCS / codepage stuff */
231 if (*x) return (LPWSTR)(x+1);
232 else return (LPWSTR)x;
235 /***********************************************************************
236 * CharNextW (USER32.32)
238 LPWSTR WINAPI CharNext32W(LPCWSTR x)
240 if (*x) return (LPWSTR)(x+1);
241 else return (LPWSTR)x;
244 /***********************************************************************
245 * CharPrev32A (USER32.33)
247 LPSTR WINAPI CharPrev32A( LPCSTR start, LPCSTR ptr )
249 while (*start && (start < ptr))
251 LPCSTR next = CharNext32A( start );
252 if (next >= ptr) break;
253 start = next;
255 return (LPSTR)start;
259 /***********************************************************************
260 * CharPrevEx32A (USER32.34)
262 LPSTR WINAPI CharPrevEx32A( WORD codepage, LPCSTR start, LPCSTR ptr, DWORD flags )
264 while (*start && (start < ptr))
266 LPCSTR next = CharNextEx32A( codepage, start, flags );
267 if (next > ptr) break;
268 start = next;
270 return (LPSTR)start;
274 /***********************************************************************
275 * CharPrevExW (USER32.35)
277 LPWSTR WINAPI CharPrevEx32W(WORD codepage,LPCWSTR start,LPCWSTR x,DWORD flags)
279 /* FIXME: add DBCS / codepage stuff */
280 if (x>start) return (LPWSTR)(x-1);
281 else return (LPWSTR)x;
284 /***********************************************************************
285 * CharPrevW (USER32.36)
287 LPWSTR WINAPI CharPrev32W(LPCWSTR start,LPCWSTR x)
289 if (x>start) return (LPWSTR)(x-1);
290 else return (LPWSTR)x;
293 /***********************************************************************
294 * CharLowerA (USER32.25)
295 * FIXME: handle current locale
297 LPSTR WINAPI CharLower32A(LPSTR x)
299 LPSTR s;
301 if (HIWORD(x))
303 s=x;
304 while (*s)
306 *s=tolower(*s);
307 s++;
309 return x;
311 else return (LPSTR)tolower((char)(int)x);
314 /***********************************************************************
315 * CharLowerBuffA (USER32.26)
316 * FIXME: handle current locale
318 DWORD WINAPI CharLowerBuff32A(LPSTR x,DWORD buflen)
320 DWORD done=0;
322 if (!x) return 0; /* YES */
323 while (*x && (buflen--))
325 *x=tolower(*x);
326 x++;
327 done++;
329 return done;
332 /***********************************************************************
333 * CharLowerBuffW (USER32.27)
334 * FIXME: handle current locale
336 DWORD WINAPI CharLowerBuff32W(LPWSTR x,DWORD buflen)
338 DWORD done=0;
340 if (!x) return 0; /* YES */
341 while (*x && (buflen--))
343 *x=towlower(*x);
344 x++;
345 done++;
347 return done;
350 /***********************************************************************
351 * CharLowerW (USER32.28)
352 * FIXME: handle current locale
354 LPWSTR WINAPI CharLower32W(LPWSTR x)
356 if (HIWORD(x))
358 LPWSTR s = x;
359 while (*s)
361 *s=towlower(*s);
362 s++;
364 return x;
366 else return (LPWSTR)((UINT32)towlower(LOWORD(x)));
369 /***********************************************************************
370 * CharUpper32A (USER32.41)
371 * FIXME: handle current locale
373 LPSTR WINAPI CharUpper32A(LPSTR x)
375 if (HIWORD(x))
377 LPSTR s = x;
378 while (*s)
380 *s=toupper(*s);
381 s++;
383 return x;
385 return (LPSTR)toupper((char)(int)x);
388 /***********************************************************************
389 * CharUpperBuffA (USER32.42)
390 * FIXME: handle current locale
392 DWORD WINAPI CharUpperBuff32A(LPSTR x,DWORD buflen)
394 DWORD done=0;
396 if (!x) return 0; /* YES */
397 while (*x && (buflen--))
399 *x=toupper(*x);
400 x++;
401 done++;
403 return done;
406 /***********************************************************************
407 * CharUpperBuffW (USER32.43)
408 * FIXME: handle current locale
410 DWORD WINAPI CharUpperBuff32W(LPWSTR x,DWORD buflen)
412 DWORD done=0;
414 if (!x) return 0; /* YES */
415 while (*x && (buflen--))
417 *x=towupper(*x);
418 x++;
419 done++;
421 return done;
424 /***********************************************************************
425 * CharUpperW (USER32.44)
426 * FIXME: handle current locale
428 LPWSTR WINAPI CharUpper32W(LPWSTR x)
430 if (HIWORD(x))
432 LPWSTR s = x;
433 while (*s)
435 *s=towupper(*s);
436 s++;
438 return x;
440 else return (LPWSTR)((UINT32)towupper(LOWORD(x)));
443 /***********************************************************************
444 * IsCharAlphaA (USER32.331)
445 * FIXME: handle current locale
447 BOOL32 WINAPI IsCharAlpha32A(CHAR x)
449 return isalpha(x);
452 /***********************************************************************
453 * IsCharAlphaNumericA (USER32.332)
454 * FIXME: handle current locale
456 BOOL32 WINAPI IsCharAlphaNumeric32A(CHAR x)
458 return isalnum(x);
461 /***********************************************************************
462 * IsCharAlphaNumericW (USER32.333)
463 * FIXME: handle current locale
465 BOOL32 WINAPI IsCharAlphaNumeric32W(WCHAR x)
467 return iswalnum(x);
470 /***********************************************************************
471 * IsCharAlphaW (USER32.334)
472 * FIXME: handle current locale
474 BOOL32 WINAPI IsCharAlpha32W(WCHAR x)
476 return iswalpha(x);
479 /***********************************************************************
480 * IsCharLower32A (USER32.335)
481 * FIXME: handle current locale
483 BOOL32 WINAPI IsCharLower32A(CHAR x)
485 return islower(x);
488 /***********************************************************************
489 * IsCharLower32W (USER32.336)
490 * FIXME: handle current locale
492 BOOL32 WINAPI IsCharLower32W(WCHAR x)
494 return iswlower(x);
497 /***********************************************************************
498 * IsCharUpper32A (USER32.337)
499 * FIXME: handle current locale
501 BOOL32 WINAPI IsCharUpper32A(CHAR x)
503 return isupper(x);
506 /***********************************************************************
507 * IsCharUpper32W (USER32.338)
508 * FIXME: handle current locale
510 BOOL32 WINAPI IsCharUpper32W(WCHAR x)
512 return iswupper(x);
515 /***********************************************************************
516 * FormatMessage32A (KERNEL32.138)
517 * FIXME: missing wrap,FROM_SYSTEM message-loading,
519 DWORD WINAPI FormatMessage32A(
520 DWORD dwFlags,
521 LPCVOID lpSource,
522 DWORD dwMessageId,
523 DWORD dwLanguageId,
524 LPSTR lpBuffer,
525 DWORD nSize,
526 LPDWORD args /* va_list *args */
528 LPSTR target,t;
529 DWORD talloced;
530 LPSTR from,f;
531 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
532 DWORD nolinefeed = 0;
534 TRACE(resource, "(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
535 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
536 if (width)
537 FIXME(resource,"line wrapping not supported.\n");
538 from = NULL;
539 if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
540 from = HEAP_strdupA( GetProcessHeap(), 0, (LPSTR)lpSource);
541 if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) {
542 from = HeapAlloc( GetProcessHeap(),0,200 );
543 sprintf(from,"Systemmessage, messageid = 0x%08lx\n",dwMessageId);
545 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE) {
546 INT32 bufsize;
548 dwMessageId &= 0xFFFF;
549 bufsize=LoadMessage32A((HMODULE32)lpSource,dwMessageId,dwLanguageId,NULL,100);
550 if (bufsize) {
551 from = HeapAlloc( GetProcessHeap(), 0, bufsize + 1 );
552 LoadMessage32A((HMODULE32)lpSource,dwMessageId,dwLanguageId,from,bufsize+1);
555 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
556 t = target;
557 talloced= 100;
559 #define ADD_TO_T(c) \
560 *t++=c;\
561 if (t-target == talloced) {\
562 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
563 t = target+talloced;\
564 talloced*=2;\
567 if (from) {
568 f=from;
569 while (*f && !nolinefeed) {
570 if (*f=='%') {
571 int insertnr;
572 char *fmtstr,*sprintfbuf,*x,*lastf;
573 DWORD *argliststart;
575 fmtstr = NULL;
576 lastf = f;
577 f++;
578 if (!*f) {
579 ADD_TO_T('%');
580 continue;
582 switch (*f) {
583 case '1':case '2':case '3':case '4':case '5':
584 case '6':case '7':case '8':case '9':
585 insertnr=*f-'0';
586 switch (f[1]) {
587 case '0':case '1':case '2':case '3':
588 case '4':case '5':case '6':case '7':
589 case '8':case '9':
590 f++;
591 insertnr=insertnr*10+*f-'0';
592 f++;
593 break;
594 default:
595 f++;
596 break;
598 if (*f=='!') {
599 f++;
600 if (NULL!=(x=strchr(f,'!'))) {
601 *x='\0';
602 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
603 sprintf(fmtstr,"%%%s",f);
604 f=x+1;
605 } else {
606 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f));
607 sprintf(fmtstr,"%%%s",f);
608 f+=strlen(f); /*at \0*/
610 } else
611 if(!args)
612 break;
613 else
614 fmtstr=HEAP_strdupA(GetProcessHeap(),0,"%s");
615 if (args) {
616 if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
617 argliststart=args+insertnr-1;
618 else
619 argliststart=(*(DWORD**)args)+insertnr-1;
621 if (fmtstr[strlen(fmtstr)-1]=='s')
622 sprintfbuf=HeapAlloc(GetProcessHeap(),0,strlen((LPSTR)argliststart[0])+1);
623 else
624 sprintfbuf=HeapAlloc(GetProcessHeap(),0,100);
626 /* CMF - This makes a BIG assumption about va_list */
627 wvsprintf32A(sprintfbuf, fmtstr, (va_list) argliststart);
628 x=sprintfbuf;
629 while (*x) {
630 ADD_TO_T(*x++);
632 HeapFree(GetProcessHeap(),0,sprintfbuf);
633 } else {
634 /* NULL args - copy formatstr
635 * (probably wrong)
637 while ((lastf<f)&&(*lastf)) {
638 ADD_TO_T(*lastf++);
641 HeapFree(GetProcessHeap(),0,fmtstr);
642 break;
643 case 'n':
644 /* FIXME: perhaps add \r too? */
645 ADD_TO_T('\n');
646 f++;
647 break;
648 case '0':
649 nolinefeed=1;
650 f++;
651 break;
652 default:ADD_TO_T(*f++)
653 break;
656 } else {
657 ADD_TO_T(*f++)
660 *t='\0';
662 if (!nolinefeed) {
663 /* add linefeed */
664 if(t==target || t[-1]!='\n')
665 ADD_TO_T('\n'); /* FIXME: perhaps add \r too? */
667 talloced = strlen(target)+1;
668 if (nSize && talloced<nSize) {
669 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
671 TRACE(resource,"-- %s\n",debugstr_a(target));
672 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
673 /* nSize is the MINIMUM size */
674 *((LPVOID*)lpBuffer) = (LPVOID)LocalAlloc32(GMEM_ZEROINIT,talloced);
675 memcpy(*(LPSTR*)lpBuffer,target,talloced);
676 } else
677 strncpy(lpBuffer,target,nSize);
678 HeapFree(GetProcessHeap(),0,target);
679 if (from) HeapFree(GetProcessHeap(),0,from);
680 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
681 strlen(*(LPSTR*)lpBuffer):
682 strlen(lpBuffer);
684 #undef ADD_TO_T
687 /***********************************************************************
688 * FormatMessage32W (KERNEL32.138)
690 DWORD WINAPI FormatMessage32W(
691 DWORD dwFlags,
692 LPCVOID lpSource,
693 DWORD dwMessageId,
694 DWORD dwLanguageId,
695 LPWSTR lpBuffer,
696 DWORD nSize,
697 LPDWORD args /* va_list *args */
699 LPSTR target,t;
700 DWORD talloced;
701 LPSTR from,f;
702 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
703 DWORD nolinefeed = 0;
705 TRACE(resource, "(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
706 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
707 if (width)
708 FIXME(resource,"line wrapping not supported.\n");
709 from = NULL;
710 if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
711 from = HEAP_strdupWtoA(GetProcessHeap(),0,(LPWSTR)lpSource);
712 if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) {
713 /* gather information from system message tables ... */
714 from = HeapAlloc( GetProcessHeap(),0,200 );
715 sprintf(from,"Systemmessage, messageid = 0x%08lx\n",dwMessageId);
717 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE) {
718 INT32 bufsize;
720 dwMessageId &= 0xFFFF;
721 bufsize=LoadMessage32A((HMODULE32)lpSource,dwMessageId,dwLanguageId,NULL,100);
722 if (bufsize)
724 from = HeapAlloc( GetProcessHeap(), 0, bufsize + 1 );
725 LoadMessage32A((HMODULE32)lpSource,dwMessageId,dwLanguageId,from,bufsize+1);
728 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100 );
729 t = target;
730 talloced= 100;
732 #define ADD_TO_T(c) \
733 *t++=c;\
734 if (t-target == talloced) {\
735 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
736 t = target+talloced;\
737 talloced*=2;\
740 if (from) {
741 f=from;
742 while (*f && !nolinefeed) {
743 if (*f=='%') {
744 int insertnr;
745 char *fmtstr,*sprintfbuf,*x;
746 DWORD *argliststart;
748 fmtstr = NULL;
749 f++;
750 if (!*f) {
751 ADD_TO_T('%');
752 continue;
754 switch (*f) {
755 case '1':case '2':case '3':case '4':case '5':
756 case '6':case '7':case '8':case '9':
757 insertnr=*f-'0';
758 switch (f[1]) {
759 case '0':case '1':case '2':case '3':
760 case '4':case '5':case '6':case '7':
761 case '8':case '9':
762 f++;
763 insertnr=insertnr*10+*f-'0';
764 f++;
765 break;
766 default:
767 f++;
768 break;
770 if (*f=='!') {
771 f++;
772 if (NULL!=(x=strchr(f,'!')))
774 *x='\0';
775 fmtstr=HeapAlloc( GetProcessHeap(), 0, strlen(f)+2);
776 sprintf(fmtstr,"%%%s",f);
777 f=x+1;
778 } else {
779 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f));
780 sprintf(fmtstr,"%%%s",f);
781 f+=strlen(f); /*at \0*/
783 } else
784 if(!args)
785 break;
786 else
787 fmtstr=HEAP_strdupA( GetProcessHeap(),0,"%s");
788 if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
789 argliststart=args+insertnr-1;
790 else
791 argliststart=(*(DWORD**)args)+insertnr-1;
793 if (fmtstr[strlen(fmtstr)-1]=='s') {
794 DWORD xarr[3];
796 xarr[0]=(DWORD)HEAP_strdupWtoA(GetProcessHeap(),0,(LPWSTR)(*(argliststart+0)));
797 /* possible invalid pointers */
798 xarr[1]=*(argliststart+1);
799 xarr[2]=*(argliststart+2);
800 sprintfbuf=HeapAlloc(GetProcessHeap(),0,lstrlen32W((LPWSTR)argliststart[0])*2+1);
802 /* CMF - This makes a BIG assumption about va_list */
803 vsprintf(sprintfbuf, fmtstr, (va_list) xarr);
804 } else {
805 sprintfbuf=HeapAlloc(GetProcessHeap(),0,100);
807 /* CMF - This makes a BIG assumption about va_list */
808 wvsprintf32A(sprintfbuf, fmtstr, (va_list) argliststart);
810 x=sprintfbuf;
811 while (*x) {
812 ADD_TO_T(*x++);
814 HeapFree(GetProcessHeap(),0,sprintfbuf);
815 HeapFree(GetProcessHeap(),0,fmtstr);
816 break;
817 case 'n':
818 /* FIXME: perhaps add \r too? */
819 ADD_TO_T('\n');
820 f++;
821 break;
822 case '0':
823 nolinefeed=1;
824 f++;
825 break;
826 default:ADD_TO_T(*f++)
827 break;
830 } else {
831 ADD_TO_T(*f++)
834 *t='\0';
836 if (!nolinefeed) {
837 /* add linefeed */
838 if(t==target || t[-1]!='\n')
839 ADD_TO_T('\n'); /* FIXME: perhaps add \r too? */
841 talloced = strlen(target)+1;
842 if (nSize && talloced<nSize)
843 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
844 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
845 /* nSize is the MINIMUM size */
846 *((LPVOID*)lpBuffer) = (LPVOID)LocalAlloc32(GMEM_ZEROINIT,talloced*2+2);
847 lstrcpynAtoW(*(LPWSTR*)lpBuffer,target,talloced);
848 } else
849 lstrcpynAtoW(lpBuffer,target,nSize);
850 HeapFree(GetProcessHeap(),0,target);
851 if (from) HeapFree(GetProcessHeap(),0,from);
852 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
853 lstrlen32W(*(LPWSTR*)lpBuffer):
854 lstrlen32W(lpBuffer);
856 #undef ADD_TO_T