4 * Copyright 1993 Yngvi Sigurjonsson (yngvi@hafro.is)
5 * Copyright 1996 Marcus Meissner
14 #include "wine/winbase16.h"
15 #include "wine/winuser16.h"
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(resource
);
23 /***********************************************************************
24 * FormatMessage16 (USER.606)
26 DWORD WINAPI
FormatMessage16(
28 SEGPTR lpSource
, /*not always a valid pointer*/
31 LPSTR lpBuffer
, /* *((HLOCAL16*)) for FORMAT_MESSAGE_ALLOCATE_BUFFER*/
33 LPDWORD args
/* va_list *args */
36 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
40 DWORD width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
42 LPSTR allocstring
= NULL
;
44 TRACE("(0x%lx,%lx,%d,0x%x,%p,%d,%p)\n",
45 dwFlags
,lpSource
,dwMessageId
,dwLanguageId
,lpBuffer
,nSize
,args
);
46 if ((dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
)
47 && (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
)) return 0;
48 if ((dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
49 &&((dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
)
50 || (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
))) return 0;
52 if (width
&& width
!= FORMAT_MESSAGE_MAX_WIDTH_MASK
)
53 FIXME("line wrapping (%lu) not supported.\n", width
);
55 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
56 from
= HEAP_strdupA( GetProcessHeap(), 0, PTR_SEG_TO_LIN(lpSource
));
57 if (dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
) {
58 from
= HeapAlloc( GetProcessHeap(),0,200 );
59 sprintf(from
,"Systemmessage, messageid = 0x%08x\n",dwMessageId
);
61 if (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
) {
63 HINSTANCE16 hinst16
= ((HMODULE
)lpSource
& 0xffff);
65 dwMessageId
&= 0xFFFF;
66 bufsize
=LoadString16(hinst16
,dwMessageId
,NULL
,0);
68 from
= HeapAlloc( GetProcessHeap(), 0, bufsize
+1);
69 LoadString16(hinst16
,dwMessageId
,from
,bufsize
+1);
72 target
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 100);
78 if (t-target == talloced) {\
79 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
89 char *fmtstr
,*x
,*lastf
;
100 case '1':case '2':case '3':case '4':case '5':
101 case '6':case '7':case '8':case '9':
104 case '0':case '1':case '2':case '3':
105 case '4':case '5':case '6':case '7':
108 insertnr
=insertnr
*10+*f
-'0';
117 if (NULL
!=(x
=strchr(f
,'!'))) {
119 fmtstr
=HeapAlloc(GetProcessHeap(),0,strlen(f
)+2);
120 sprintf(fmtstr
,"%%%s",f
);
123 fmtstr
=HeapAlloc(GetProcessHeap(),0,strlen(f
)+2);
124 sprintf(fmtstr
,"%%%s",f
);
125 f
+=strlen(f
); /*at \0*/
131 fmtstr
=HEAP_strdupA(GetProcessHeap(),0,"%s");
134 LPSTR b
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sz
= 100);
136 argliststart
=args
+insertnr
-1;
138 /* CMF - This makes a BIG assumption about va_list */
139 while (vsnprintf(b
, sz
, fmtstr
, (va_list) argliststart
) < 0) {
140 b
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, b
, sz
+= 100);
142 for (x
=b
; *x
; x
++) ADD_TO_T(*x
);
144 /* NULL args - copy formatstr
147 while ((lastf
<f
)&&(*lastf
)) {
151 HeapFree(GetProcessHeap(),0,fmtstr
);
153 case '0': /* Just stop processing format string */
157 case 'n': /* 16 bit version just outputs 'n' */
162 } else { /* '\n' or '\r' gets mapped to "\r\n" */
163 if(*f
== '\n' || *f
== '\r') {
167 if(*f
++ == '\r' && *f
== '\n')
177 talloced
= strlen(target
)+1;
178 if (nSize
&& talloced
<nSize
) {
179 target
= (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,target
,nSize
);
181 TRACE("-- %s\n",debugstr_a(target
));
182 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) {
183 /* nSize is the MINIMUM size */
184 HLOCAL16 h
= LocalAlloc16(LPTR
,talloced
);
185 SEGPTR ptr
= LocalLock16(h
);
186 allocstring
= PTR_SEG_TO_LIN( ptr
);
187 memcpy( allocstring
,target
,talloced
);
189 *((HLOCAL16
*)lpBuffer
) = h
;
191 lstrcpynA(lpBuffer
,target
,nSize
);
192 HeapFree(GetProcessHeap(),0,target
);
193 if (from
) HeapFree(GetProcessHeap(),0,from
);
194 return (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ?
199 #endif /* __i386__ */