jscript: Added to_string(VT_I4) implementation.
[wine] / dlls / dxerr8 / dxerr8.c
1 /*
2  * DirectX 8 error routines
3  *
4  * Copyright 2004-2005 Robert Reif
5  *
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.
10  *
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.
15  *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdarg.h>
25 #include <stdio.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31
32 #include "mmsystem.h"
33 #include "dsound.h"
34 #include "dmerror.h"
35 #include "ddraw.h"
36 #include "dinput.h"
37 #include "vfwmsgs.h"
38
39 #include "dxerr8.h"
40
41 typedef struct {
42     HRESULT      hr;
43     const CHAR*  resultA;
44     const WCHAR* resultW;
45     const CHAR*  descriptionA;
46     const WCHAR* descriptionW;
47 } error_info;
48
49 #include "errors.h"
50
51 const char * WINAPI DXGetErrorString8A(HRESULT hr)
52 {
53     unsigned int i, j, k = 0;
54
55     for (i = sizeof(info)/sizeof(info[0]); i != 0; i /= 2) {
56         j = k + (i / 2);
57         if (hr == info[j].hr)
58             return info[j].resultA;
59         if ((unsigned int)hr > (unsigned int)info[j].hr) {
60             k = j + 1;
61             i--;
62         }
63     }
64
65     return "Unknown";
66 }
67
68 const WCHAR * WINAPI DXGetErrorString8W(HRESULT hr)
69 {
70     static const WCHAR unknown[] = { 'U', 'n', 'k', 'n', 'o', 'w', 'n', 0 };
71     unsigned int i, j, k = 0;
72
73     for (i = sizeof(info)/sizeof(info[0]); i != 0; i /= 2) {
74         j = k + (i / 2);
75         if (hr == info[j].hr)
76             return info[j].resultW;
77         if ((unsigned int)hr > (unsigned int)info[j].hr) {
78             k = j + 1;
79             i--;
80         }
81     }
82
83     return unknown;
84 }
85
86 const char * WINAPI DXGetErrorDescription8A(HRESULT hr)
87 {
88     unsigned int i, j, k = 0;
89
90     for (i = sizeof(info)/sizeof(info[0]); i != 0; i /= 2) {
91         j = k + (i / 2);
92         if (hr == info[j].hr)
93             return info[j].descriptionA;
94         if ((unsigned int)hr > (unsigned int)info[j].hr) {
95             k = j + 1;
96             i--;
97         }
98     }
99
100     return "n/a";
101 }
102
103 const WCHAR * WINAPI DXGetErrorDescription8W(HRESULT hr)
104 {
105     static const WCHAR na[] = { 'n', '/', 'a', 0 };
106     unsigned int i, j, k = 0;
107
108     for (i = sizeof(info)/sizeof(info[0]); i != 0; i /= 2) {
109         j = k + (i / 2);
110         if (hr == info[j].hr)
111             return info[j].descriptionW;
112         if ((unsigned int)hr > (unsigned int)info[j].hr) {
113             k = j + 1;
114             i--;
115         }
116     }
117
118     return na;
119 }
120
121 HRESULT WINAPI DXTraceA(const char* strFile, DWORD dwLine, HRESULT hr, const char*  strMsg, BOOL bPopMsgBox)
122 {
123     char msg[1024];
124
125     if (bPopMsgBox) {
126         snprintf(msg, sizeof(msg), "File: %s\nLine: %d\nError Code: %s (0x%08x)\nCalling: %s",
127             strFile, dwLine, DXGetErrorString8A(hr), hr, strMsg);
128         MessageBoxA(0, msg, "Unexpected error encountered", MB_OK|MB_ICONERROR);
129     } else {
130         snprintf(msg, sizeof(msg), "%s(%d): %s (hr=%s (0x%08x))", strFile,
131             dwLine, strMsg, DXGetErrorString8A(hr), hr);
132         OutputDebugStringA(msg);
133     }
134
135     return hr;
136 }
137
138 HRESULT WINAPI DXTraceW(const char* strFile, DWORD dwLine, HRESULT hr, const WCHAR* strMsg, BOOL bPopMsgBox)
139 {
140     WCHAR msg[1024];
141
142     if (bPopMsgBox) {
143         static const WCHAR format[] = { 'F','i','l','e',':',' ','%','s','\\','n','L','i','n',
144             'e',':',' ','%','l','d','\\','n','E','r','r','o','r',' ','C','o',
145             'd','e',':',' ','%','s',' ','(','0','x','%','0','8','l','x',')',
146             '\\','n','C','a','l','l','i','n','g',':',' ','%','s',0 };
147         static const WCHAR caption[] = { 'U','n','e','x','p','e','c','t','e','d',' ','e','r',
148             'r','o','r',' ','e','n','c','o','u','n','t','e','r','e','d',0 };
149         /* FIXME: should use wsnprintf */
150         wsprintfW(msg, format, strFile, dwLine,
151                   DXGetErrorString8W(hr), hr, strMsg);
152         MessageBoxW(0, msg, caption, MB_OK|MB_ICONERROR);
153     } else {
154         static const WCHAR format[] = { '%','s','(','%','l','d',')',':',' ','%','s',' ','(',
155             'h','r','=','%','s',' ','(','0','x','%','0','8','l','x',')',')',' ',
156             0 };
157         /* FIXME: should use wsnprintf */
158         wsprintfW(msg, format, strFile, dwLine, strMsg,
159                   DXGetErrorString8W(hr), hr);
160         OutputDebugStringW(msg);
161     }
162
163     return hr;
164 }