VirtualBox

source: vbox/trunk/src/VBox/Runtime/win/errmsgwin.cpp@ 83743

Last change on this file since 83743 was 83743, checked in by vboxsync, 5 years ago

IPRT: Sort windows error messages so we can look them up using binary searching. Also addressed VC++ 14.1 warnings there. bugref:8489

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.3 KB
Line 
1/* $Id: errmsgwin.cpp 83743 2020-04-17 11:25:08Z vboxsync $ */
2/** @file
3 * IPRT - Status code messages, Windows.
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/win/windows.h>
32
33#include <iprt/errcore.h>
34#include <iprt/asm.h>
35#include <iprt/string.h>
36
37
38/*********************************************************************************************************************************
39* Global Variables *
40*********************************************************************************************************************************/
41/** Array of messages.
42 * The data is generated by a sed script.
43 */
44static const RTWINERRMSG g_aStatusMsgs[] =
45{
46#include "errmsgwindata-sorted.h"
47};
48
49
50/** Temporary buffers to format unknown messages in.
51 * @{
52 */
53static char g_aszUnknownStr[8][64];
54static RTWINERRMSG g_aUnknownMsgs[8] =
55{
56 { &g_aszUnknownStr[0][0], &g_aszUnknownStr[0][0], 0 },
57 { &g_aszUnknownStr[1][0], &g_aszUnknownStr[1][0], 0 },
58 { &g_aszUnknownStr[2][0], &g_aszUnknownStr[2][0], 0 },
59 { &g_aszUnknownStr[3][0], &g_aszUnknownStr[3][0], 0 },
60 { &g_aszUnknownStr[4][0], &g_aszUnknownStr[4][0], 0 },
61 { &g_aszUnknownStr[5][0], &g_aszUnknownStr[5][0], 0 },
62 { &g_aszUnknownStr[6][0], &g_aszUnknownStr[6][0], 0 },
63 { &g_aszUnknownStr[7][0], &g_aszUnknownStr[7][0], 0 },
64};
65/** Last used index in g_aUnknownMsgs. */
66static volatile uint32_t g_iUnknownMsgs;
67/** @} */
68
69
70/**
71 * Get the message corresponding to a given status code.
72 *
73 * @returns Pointer to read-only message description.
74 * @param rc The status code.
75 */
76RTDECL(PCRTWINERRMSG) RTErrWinGet(long rc)
77{
78 /*
79 * Perform binary search.
80 */
81 size_t iStart = 0;
82 size_t iEnd = RT_ELEMENTS(g_aStatusMsgs);
83 for (;;)
84 {
85 size_t i = iStart + (iEnd - iStart) / 2;
86 long const iCode = g_aStatusMsgs[i].iCode;
87 if (rc < iCode)
88 {
89 if (iStart < i)
90 iEnd = i;
91 else
92 break;
93 }
94 else if (rc > iCode)
95 {
96 i++;
97 if (i < iEnd)
98 iStart = i;
99 else
100 break;
101 }
102 else
103 return &g_aStatusMsgs[i];
104 }
105
106#ifdef RT_STRICT
107 for (size_t i = 0; i < RT_ELEMENTS(g_aStatusMsgs); i++)
108 Assert(g_aStatusMsgs[i].iCode != rc);
109#endif
110
111 /*
112 * If FACILITY_WIN32 kind of status, look up the win32 code.
113 */
114 if (SCODE_FACILITY(rc) == FACILITY_WIN32)
115 {
116 long const rcWin32 = HRESULT_CODE(rc);
117 iStart = 0;
118 iEnd = RT_ELEMENTS(g_aStatusMsgs);
119 for (;;)
120 {
121 size_t i = iStart + (iEnd - iStart) / 2;
122 long const iCode = g_aStatusMsgs[i].iCode;
123 if (rcWin32 < iCode)
124 {
125 if (iStart < i)
126 iEnd = i;
127 else
128 break;
129 }
130 else if (rcWin32 > iCode)
131 {
132 i++;
133 if (i < iEnd)
134 iStart = i;
135 else
136 break;
137 }
138 else
139 {
140 /* Append the incoming rc, so we know it's not a regular WIN32 status: */
141 int32_t iMsg = (ASMAtomicIncU32(&g_iUnknownMsgs) - 1) % RT_ELEMENTS(g_aUnknownMsgs);
142 RTStrPrintf(&g_aszUnknownStr[iMsg][0], sizeof(g_aszUnknownStr[iMsg]), "%s/0x%x", g_aStatusMsgs[i].pszDefine, rc);
143 return &g_aUnknownMsgs[iMsg];
144 }
145 }
146
147#ifdef RT_STRICT
148 for (size_t i = 0; i < RT_ELEMENTS(g_aStatusMsgs); i++)
149 Assert(g_aStatusMsgs[i].iCode != rcWin32);
150#endif
151 }
152
153 /*
154 * Need to use the temporary stuff.
155 */
156 int32_t iMsg = (ASMAtomicIncU32(&g_iUnknownMsgs) - 1) % RT_ELEMENTS(g_aUnknownMsgs);
157 RTStrPrintf(&g_aszUnknownStr[iMsg][0], sizeof(g_aszUnknownStr[iMsg]), "Unknown Status 0x%X", rc);
158 return &g_aUnknownMsgs[iMsg];
159}
160
161
162RTDECL(PCRTCOMERRMSG) RTErrCOMGet(uint32_t rc)
163{
164 return RTErrWinGet((long)rc);
165}
166
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette