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