Move the FPS computation from the D3D code to the common code.
[wine/multimedia.git] / dlls / kernel / format_msg.c
blob6df11604d659653465b0d96ef675285df5f2de9d
1 /*
2 * FormatMessage implementation
4 * Copyright 1996 Marcus Meissner
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <string.h>
27 #include "ntstatus.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "winreg.h"
32 #include "winternl.h"
33 #include "winuser.h"
34 #include "winnls.h"
35 #include "wine/unicode.h"
36 #include "kernel_private.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(resource);
42 /* Messages...used by FormatMessage32* (KERNEL32.something)
44 * They can be specified either directly or using a message ID and
45 * loading them from the resource.
47 * The resourcedata has following format:
48 * start:
49 * 0: DWORD nrofentries
50 * nrofentries * subentry:
51 * 0: DWORD firstentry
52 * 4: DWORD lastentry
53 * 8: DWORD offset from start to the stringentries
55 * (lastentry-firstentry) * stringentry:
56 * 0: WORD len (0 marks end) [ includes the 4 byte header length ]
57 * 2: WORD flags
58 * 4: CHAR[len-4]
59 * (stringentry i of a subentry refers to the ID 'firstentry+i')
61 * Yes, ANSI strings in win32 resources. Go figure.
64 static const WCHAR PCNTFMTWSTR[] = { '%','%','%','s',0 };
65 static const WCHAR FMTWSTR[] = { '%','s',0 };
67 /**********************************************************************
68 * load_messageW (internal)
70 static INT load_messageW( HMODULE instance, UINT id, WORD lang,
71 LPWSTR buffer, INT buflen )
73 const MESSAGE_RESOURCE_ENTRY *mre;
74 int i,slen;
76 TRACE("instance = %08lx, id = %08lx, buffer = %p, length = %ld\n", (DWORD)instance, (DWORD)id, buffer, (DWORD)buflen);
78 if (RtlFindMessage( instance, RT_MESSAGETABLE, lang, id, &mre ) != STATUS_SUCCESS) return 0;
80 if (mre->Flags & MESSAGE_RESOURCE_UNICODE)
81 slen = mre->Length / sizeof(WCHAR);
82 else
83 slen=mre->Length;
85 TRACE(" - strlen=%d\n",slen);
87 i = min(buflen - 1, slen);
88 if (buffer == NULL)
89 return slen;
91 if (i>0) {
92 if (mre->Flags & MESSAGE_RESOURCE_UNICODE)
93 lstrcpynW(buffer, (LPWSTR)mre->Text, i);
94 else
95 MultiByteToWideChar( CP_ACP, 0, mre->Text, -1, buffer, i);
96 buffer[i]=0;
97 } else {
98 if (buflen>1) {
99 buffer[0]=0;
100 return 0;
103 if (buffer)
104 TRACE("'%s' copied !\n", wine_dbgstr_w(buffer));
105 return i;
109 /**********************************************************************
110 * load_messageA (internal)
112 static INT load_messageA( HMODULE instance, UINT id, WORD lang,
113 LPSTR buffer, INT buflen )
115 INT ret = 0;
116 LPWSTR bufferW;
118 TRACE("instance = %08lx, id = %08lx, buffer = %p, length = %ld\n",
119 (DWORD)instance, (DWORD)id, buffer, (DWORD)buflen);
121 if (buffer == NULL)
122 return load_messageW(instance, id, lang, NULL, 0);
124 bufferW = HeapAlloc(GetProcessHeap(), 0, buflen * sizeof(WCHAR));
126 if (bufferW)
128 ret = load_messageW(instance, id, lang, bufferW, buflen);
129 if (ret > 0)
130 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, ret, NULL, NULL);
133 return ret;
137 /***********************************************************************
138 * FormatMessageA (KERNEL32.@)
139 * FIXME: missing wrap,
141 DWORD WINAPI FormatMessageA(
142 DWORD dwFlags,
143 LPCVOID lpSource,
144 DWORD dwMessageId,
145 DWORD dwLanguageId,
146 LPSTR lpBuffer,
147 DWORD nSize,
148 va_list* _args )
150 LPDWORD args=(LPDWORD)_args;
151 #if defined(__i386__) || defined(__sparc__)
152 /* This implementation is completely dependent on the format of the va_list on x86 CPUs */
153 LPSTR target,t;
154 DWORD talloced;
155 LPSTR from,f;
156 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
157 BOOL eos = FALSE;
158 INT bufsize;
159 HMODULE hmodule = (HMODULE)lpSource;
160 CHAR ch;
162 TRACE("(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
163 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
164 if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
165 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
166 || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
168 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
169 FIXME("line wrapping (%lu) not supported.\n", width);
170 from = NULL;
171 if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
173 from = HeapAlloc( GetProcessHeap(), 0, strlen((LPSTR)lpSource)+1 );
174 strcpy( from, (LPSTR)lpSource );
176 else {
177 bufsize = 0;
179 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)
181 if (!hmodule)
182 hmodule = GetModuleHandleW(NULL);
183 bufsize=load_messageA(hmodule,dwMessageId,dwLanguageId,NULL,100);
185 if ((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) && (!bufsize))
187 hmodule = kernel32_handle;
188 bufsize=load_messageA(hmodule,dwMessageId,dwLanguageId,NULL,100);
191 if (!bufsize) {
192 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND);
193 return 0;
196 from = HeapAlloc( GetProcessHeap(), 0, bufsize + 1 );
197 load_messageA(hmodule,dwMessageId,dwLanguageId,from,bufsize+1);
199 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
200 t = target;
201 talloced= 100;
203 #define ADD_TO_T(c) do { \
204 *t++=c;\
205 if (t-target == talloced) {\
206 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
207 t = target+talloced;\
208 talloced*=2;\
210 } while (0)
212 if (from) {
213 f=from;
214 if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS) {
215 while (*f && !eos)
216 ADD_TO_T(*f++);
218 else {
219 while (*f && !eos) {
220 if (*f=='%') {
221 int insertnr;
222 char *fmtstr,*x,*lastf;
223 DWORD *argliststart;
225 fmtstr = NULL;
226 lastf = f;
227 f++;
228 if (!*f) {
229 ADD_TO_T('%');
230 continue;
232 switch (*f) {
233 case '1':case '2':case '3':case '4':case '5':
234 case '6':case '7':case '8':case '9':
235 insertnr=*f-'0';
236 switch (f[1]) {
237 case '0':case '1':case '2':case '3':
238 case '4':case '5':case '6':case '7':
239 case '8':case '9':
240 f++;
241 insertnr=insertnr*10+*f-'0';
242 f++;
243 break;
244 default:
245 f++;
246 break;
248 if (*f=='!') {
249 f++;
250 if (NULL!=(x=strchr(f,'!'))) {
251 *x='\0';
252 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
253 sprintf(fmtstr,"%%%s",f);
254 f=x+1;
255 } else {
256 fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
257 sprintf(fmtstr,"%%%s",f);
258 f+=strlen(f); /*at \0*/
260 } else {
261 if(!args) break;
262 fmtstr = HeapAlloc(GetProcessHeap(),0,3);
263 strcpy( fmtstr, "%s" );
265 if (args) {
266 int sz;
267 LPSTR b;
269 if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
270 argliststart=args+insertnr-1;
271 else
272 argliststart=(*(DWORD**)args)+insertnr-1;
274 /* FIXME: precision and width components are not handled correctly */
275 if ( (strcmp(fmtstr, "%ls") == 0) || (strcmp(fmtstr,"%S") == 0) ) {
276 sz = WideCharToMultiByte( CP_ACP, 0, *(WCHAR**)argliststart, -1, NULL, 0, NULL, NULL);
277 b = HeapAlloc(GetProcessHeap(), 0, sz);
278 WideCharToMultiByte( CP_ACP, 0, *(WCHAR**)argliststart, -1, b, sz, NULL, NULL);
279 } else {
280 b = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz = 1000);
281 /* CMF - This makes a BIG assumption about va_list */
282 TRACE("A BIG assumption\n");
283 vsnprintf(b, sz, fmtstr, (va_list) argliststart);
285 for (x=b; *x; x++) ADD_TO_T(*x);
287 HeapFree(GetProcessHeap(),0,b);
288 } else {
289 /* NULL args - copy formatstr
290 * (probably wrong)
292 while ((lastf<f)&&(*lastf)) {
293 ADD_TO_T(*lastf++);
296 HeapFree(GetProcessHeap(),0,fmtstr);
297 break;
298 case 'n':
299 ADD_TO_T('\r');
300 ADD_TO_T('\n');
301 f++;
302 break;
303 case '0':
304 eos = TRUE;
305 f++;
306 break;
307 default:
308 ADD_TO_T(*f++);
309 break;
311 } else {
312 ch = *f;
313 f++;
314 if (ch == '\r') {
315 if (*f == '\n')
316 f++;
317 if(width)
318 ADD_TO_T(' ');
319 else
321 ADD_TO_T('\r');
322 ADD_TO_T('\n');
324 } else {
325 if (ch == '\n')
327 if(width)
328 ADD_TO_T(' ');
329 else
331 ADD_TO_T('\r');
332 ADD_TO_T('\n');
335 else
336 ADD_TO_T(ch);
341 *t='\0';
343 talloced = strlen(target)+1;
344 if (nSize && talloced<nSize) {
345 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
347 TRACE("-- %s\n",debugstr_a(target));
348 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
349 *((LPVOID*)lpBuffer) = (LPVOID)LocalAlloc(GMEM_ZEROINIT,max(nSize, talloced));
350 memcpy(*(LPSTR*)lpBuffer,target,talloced);
351 } else {
352 lstrcpynA(lpBuffer,target,nSize);
354 HeapFree(GetProcessHeap(),0,target);
355 if (from) HeapFree(GetProcessHeap(),0,from);
356 TRACE("-- returning %d\n", (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ? strlen(*(LPSTR*)lpBuffer):strlen(lpBuffer));
357 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
358 strlen(*(LPSTR*)lpBuffer):
359 strlen(lpBuffer);
360 #else
361 return 0;
362 #endif /* __i386__ */
364 #undef ADD_TO_T
367 /***********************************************************************
368 * FormatMessageW (KERNEL32.@)
370 DWORD WINAPI FormatMessageW(
371 DWORD dwFlags,
372 LPCVOID lpSource,
373 DWORD dwMessageId,
374 DWORD dwLanguageId,
375 LPWSTR lpBuffer,
376 DWORD nSize,
377 va_list* _args )
379 LPDWORD args=(LPDWORD)_args;
380 #if defined(__i386__) || defined(__sparc__)
381 /* This implementation is completely dependent on the format of the va_list on x86 CPUs */
382 LPWSTR target,t;
383 DWORD talloced;
384 LPWSTR from,f;
385 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
386 BOOL eos = FALSE;
387 INT bufsize;
388 HMODULE hmodule = (HMODULE)lpSource;
389 CHAR ch;
391 TRACE("(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
392 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
393 if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
394 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
395 || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
397 if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
398 FIXME("line wrapping not supported.\n");
399 from = NULL;
400 if (dwFlags & FORMAT_MESSAGE_FROM_STRING) {
401 from = HeapAlloc( GetProcessHeap(), 0, (strlenW((LPWSTR)lpSource) + 1) *
402 sizeof(WCHAR) );
403 strcpyW( from, (LPWSTR)lpSource );
405 else {
406 bufsize = 0;
408 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)
410 if (!hmodule)
411 hmodule = GetModuleHandleW(NULL);
412 bufsize=load_messageW(hmodule,dwMessageId,dwLanguageId,NULL,100);
414 if ((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) && (!bufsize))
416 hmodule = kernel32_handle;
417 bufsize=load_messageW(hmodule,dwMessageId,dwLanguageId,NULL,100);
420 if (!bufsize) {
421 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND);
422 return 0;
425 from = HeapAlloc( GetProcessHeap(), 0, (bufsize + 1) * sizeof(WCHAR) );
426 load_messageW(hmodule,dwMessageId,dwLanguageId,from,bufsize+1);
428 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100 * sizeof(WCHAR) );
429 t = target;
430 talloced= 100;
432 #define ADD_TO_T(c) do {\
433 *t++=c;\
434 if (t-target == talloced) {\
435 target = (WCHAR*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2*sizeof(WCHAR));\
436 t = target+talloced;\
437 talloced*=2;\
439 } while (0)
441 if (from) {
442 f=from;
443 if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS) {
444 while (*f && !eos)
445 ADD_TO_T(*f++);
447 else {
448 while (*f && !eos) {
449 if (*f=='%') {
450 int insertnr;
451 WCHAR *fmtstr,*sprintfbuf,*x;
452 DWORD *argliststart;
454 fmtstr = NULL;
455 f++;
456 if (!*f) {
457 ADD_TO_T('%');
458 continue;
461 switch (*f) {
462 case '1':case '2':case '3':case '4':case '5':
463 case '6':case '7':case '8':case '9':
464 insertnr=*f-'0';
465 switch (f[1]) {
466 case '0':case '1':case '2':case '3':
467 case '4':case '5':case '6':case '7':
468 case '8':case '9':
469 f++;
470 insertnr=insertnr*10+*f-'0';
471 f++;
472 break;
473 default:
474 f++;
475 break;
477 if (*f=='!') {
478 f++;
479 if (NULL!=(x=strchrW(f,'!'))) {
480 *x='\0';
481 fmtstr=HeapAlloc( GetProcessHeap(), 0,(strlenW(f)+2)*sizeof(WCHAR));
482 sprintfW(fmtstr,PCNTFMTWSTR,f);
483 f=x+1;
484 } else {
485 fmtstr=HeapAlloc(GetProcessHeap(),0,(strlenW(f)+2)*sizeof(WCHAR));
486 sprintfW(fmtstr,PCNTFMTWSTR,f);
487 f+=strlenW(f); /*at \0*/
489 } else {
490 if(!args) break;
491 fmtstr = HeapAlloc( GetProcessHeap(),0,3*sizeof(WCHAR));
492 strcpyW( fmtstr, FMTWSTR );
494 if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
495 argliststart=args+insertnr-1;
496 else
497 argliststart=(*(DWORD**)args)+insertnr-1;
499 if (fmtstr[strlenW(fmtstr)-1]=='s' && argliststart[0]) {
500 DWORD xarr[3];
502 xarr[0]=*(argliststart+0);
503 /* possible invalid pointers */
504 xarr[1]=*(argliststart+1);
505 xarr[2]=*(argliststart+2);
506 sprintfbuf=HeapAlloc(GetProcessHeap(),0,(strlenW((LPWSTR)argliststart[0])*2+1)*sizeof(WCHAR));
508 /* CMF - This makes a BIG assumption about va_list */
509 vsprintfW(sprintfbuf, fmtstr, (va_list) xarr);
510 } else {
511 sprintfbuf=HeapAlloc(GetProcessHeap(),0,100);
513 /* CMF - This makes a BIG assumption about va_list */
514 vsprintfW(sprintfbuf, fmtstr, (va_list) argliststart);
516 x=sprintfbuf;
517 while (*x) {
518 ADD_TO_T(*x++);
520 HeapFree(GetProcessHeap(),0,sprintfbuf);
521 HeapFree(GetProcessHeap(),0,fmtstr);
522 break;
523 case 'n':
524 ADD_TO_T('\r');
525 ADD_TO_T('\n');
526 f++;
527 break;
528 case '0':
529 eos = TRUE;
530 f++;
531 break;
532 default:
533 ADD_TO_T(*f++);
534 break;
536 } else {
537 ch = *f;
538 f++;
539 if (ch == '\r') {
540 if (*f == '\n')
541 f++;
542 if(width)
543 ADD_TO_T(' ');
544 else
546 ADD_TO_T('\r');
547 ADD_TO_T('\n');
549 } else {
550 if (ch == '\n')
552 if(width)
553 ADD_TO_T(' ');
554 else
556 ADD_TO_T('\r');
557 ADD_TO_T('\n');
560 else
561 ADD_TO_T(ch);
566 *t='\0';
568 talloced = strlenW(target)+1;
569 if (nSize && talloced<nSize)
570 target = (WCHAR*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize*sizeof(WCHAR));
571 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
572 /* nSize is the MINIMUM size */
573 DWORD len = strlenW(target) + 1;
574 *((LPVOID*)lpBuffer) = (LPVOID)LocalAlloc(GMEM_ZEROINIT,len*sizeof(WCHAR));
575 strcpyW(*(LPWSTR*)lpBuffer, target);
577 else lstrcpynW(lpBuffer, target, nSize);
579 HeapFree(GetProcessHeap(),0,target);
580 if (from) HeapFree(GetProcessHeap(),0,from);
581 TRACE("ret=%s\n", wine_dbgstr_w((dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
582 *(LPWSTR*)lpBuffer : lpBuffer));
583 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
584 strlenW(*(LPWSTR*)lpBuffer):
585 strlenW(lpBuffer);
586 #else
587 return 0;
588 #endif /* __i386__ */
590 #undef ADD_TO_T