VirtualBox

Changeset 84063 in vbox for trunk/src/VBox/Runtime/win


Ignore:
Timestamp:
Apr 28, 2020 7:32:41 PM (5 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
137642
Message:

IPRT,++: New RTErrWin* API, data source unchanged. bugref:9726

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/win/errmsgwin.cpp

    r83984 r84063  
    3939*   Defined Constants And Macros                                                                                                 *
    4040*********************************************************************************************************************************/
    41 #if defined(IPRT_ERRMSG_DEFINES_ONLY) || defined(IN_RT_STATIC) /* No message text in static builds to save space. */
     41#if !defined(IPRT_ERRMSG_DEFINES_ONLY) && defined(IN_RT_STATIC) /* No message text in static builds to save space. */
     42# define IPRT_ERRMSG_DEFINES_ONLY
     43#endif
     44
     45#ifdef IPRT_ERRMSG_DEFINES_ONLY
    4246# define ENTRY(a_pszMsg, a_pszDefine, a_iCode) \
    4347    { a_pszDefine, a_pszDefine, a_iCode }
     
    8589
    8690/**
    87  * Get the message corresponding to a given status code.
    88  *
    89  * @returns Pointer to read-only message description.
    90  * @param   rc      The status code.
    91  */
    92 RTDECL(PCRTWINERRMSG) RTErrWinGet(long rc)
    93 {
    94     /*
    95      * Perform binary search (duplicate code in RTErrGet).
     91 * Looks up the message table entry for @a rc.
     92 *
     93 * @returns index into g_aStatusMsgs on success, ~(size_t)0 if not found.
     94 * @param   rc      The status code to locate the entry for.
     95 */
     96static size_t rtErrWinLookup(long rc)
     97{
     98    /*
     99     * Perform binary search (duplicate code in rtErrLookup).
    96100     */
    97101    size_t iStart = 0;
     
    117121        }
    118122        else
    119             return &g_aStatusMsgs[i];
     123            return i;
    120124    }
    121125
     
    125129#endif
    126130
    127     /*
    128      * If FACILITY_WIN32 kind of status, look up the win32 code.
    129      */
    130     if (SCODE_FACILITY(rc) == FACILITY_WIN32)
    131     {
    132         long const rcWin32 = HRESULT_CODE(rc);
    133         iStart = 0;
    134         iEnd   = RT_ELEMENTS(g_aStatusMsgs);
    135         for (;;)
    136         {
    137             size_t i = iStart + (iEnd - iStart) / 2;
    138             long const iCode = g_aStatusMsgs[i].iCode;
    139             if (rcWin32 < iCode)
    140             {
    141                 if (iStart < i)
    142                     iEnd = i;
    143                 else
    144                     break;
    145             }
    146             else if (rcWin32 > iCode)
    147             {
    148                 i++;
    149                 if (i < iEnd)
    150                     iStart = i;
    151                 else
    152                     break;
    153             }
    154             else
    155             {
    156                 /* Append the incoming rc, so we know it's not a regular WIN32 status: */
    157                 int32_t iMsg = (ASMAtomicIncU32(&g_iUnknownMsgs) - 1) % RT_ELEMENTS(g_aUnknownMsgs);
    158                 RTStrPrintf(&g_aszUnknownStr[iMsg][0], sizeof(g_aszUnknownStr[iMsg]), "%s/0x%x", g_aStatusMsgs[i].pszDefine, rc);
    159                 return &g_aUnknownMsgs[iMsg];
    160             }
    161         }
    162 
    163 #ifdef RT_STRICT
    164         for (size_t i = 0; i < RT_ELEMENTS(g_aStatusMsgs); i++)
    165             Assert(g_aStatusMsgs[i].iCode != rcWin32);
    166 #endif
    167     }
    168 
    169     /*
    170      * Need to use the temporary stuff.
    171      */
    172     int32_t iMsg = (ASMAtomicIncU32(&g_iUnknownMsgs) - 1) % RT_ELEMENTS(g_aUnknownMsgs);
    173     RTStrPrintf(&g_aszUnknownStr[iMsg][0], sizeof(g_aszUnknownStr[iMsg]), "Unknown Status 0x%X", rc);
    174     return &g_aUnknownMsgs[iMsg];
    175 }
    176 
    177 
    178 RTDECL(PCRTCOMERRMSG) RTErrCOMGet(uint32_t rc)
    179 {
    180     return RTErrWinGet((long)rc);
    181 }
    182 
     131    return ~(size_t)0;
     132}
     133
     134
     135RTDECL(bool)    RTErrWinIsKnown(long rc)
     136{
     137    if (rtErrWinLookup(rc) != ~(size_t)0)
     138        return true;
     139    if (SCODE_FACILITY(rc) == FACILITY_WIN32)
     140    {
     141        if (rtErrWinLookup(HRESULT_CODE(rc)) != ~(size_t)0)
     142            return true;
     143    }
     144    return false;
     145}
     146
     147
     148RTDECL(ssize_t) RTErrWinQueryDefine(long rc, char *pszBuf, size_t cbBuf, bool fFailIfUnknown)
     149{
     150    size_t i = rtErrWinLookup(rc);
     151    if (i != ~(size_t)0)
     152    {
     153        size_t cch = strlen(g_aStatusMsgs[i].pszDefine);
     154        if (cch < cbBuf)
     155        {
     156            memcpy(pszBuf, g_aStatusMsgs[i].pszDefine, cch + 1);
     157            return cch;
     158        }
     159        if (cbBuf)
     160        {
     161            memcpy(pszBuf, g_aStatusMsgs[i].pszDefine, cbBuf);
     162            pszBuf[cbBuf - 1] = '\0';
     163        }
     164        return VERR_BUFFER_OVERFLOW;
     165    }
     166
     167    /*
     168     * If FACILITY_WIN32 kind of status, look up the win32 code.
     169     */
     170    if (   SCODE_FACILITY(rc) == FACILITY_WIN32
     171        && (i = rtErrWinLookup(HRESULT_CODE(rc))) != ~(size_t)0)
     172    {
     173        /* Append the incoming rc, so we know it's not a regular WIN32 status: */
     174        ssize_t cchRet = RTStrPrintf2(pszBuf, cbBuf, "%s/0x%x", g_aStatusMsgs[i].pszDefine, rc);
     175        return cchRet >= 0 ? cchRet : VERR_BUFFER_OVERFLOW;
     176    }
     177
     178    if (fFailIfUnknown)
     179        return VERR_NOT_FOUND;
     180    return RTStrFormatU32(pszBuf, cbBuf, rc, 16, 0, 0, RTSTR_F_SPECIAL);
     181}
     182
     183
     184RTDECL(size_t)  RTErrWinFormatDefine(long rc, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, char *pszTmp, size_t cbTmp)
     185{
     186    RT_NOREF(pszTmp, cbTmp);
     187    size_t i = rtErrWinLookup(rc);
     188    if (i != ~(size_t)0)
     189        return pfnOutput(pvArgOutput, g_aStatusMsgs[i].pszDefine, strlen(g_aStatusMsgs[i].pszDefine));
     190
     191    /*
     192     * If FACILITY_WIN32 kind of status, look up the win32 code.
     193     */
     194    if (SCODE_FACILITY(rc) == FACILITY_WIN32)
     195    {
     196        i = rtErrWinLookup(HRESULT_CODE(rc));
     197        if (i != ~(size_t)0)
     198            /* Append the incoming rc, so we know it's not a regular WIN32 status: */
     199            return RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL, "%s/0x%x", g_aStatusMsgs[i].pszDefine, rc);
     200    }
     201
     202    return RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL, "0x%x", rc);
     203}
     204
     205
     206RTDECL(size_t)  RTErrWinFormatMsg(   long rc, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, char *pszTmp, size_t cbTmp)
     207{
     208    RT_NOREF(pszTmp, cbTmp);
     209#ifdef IPRT_ERRMSG_DEFINES_ONLY
     210    return RTErrWinFormatDefine(rc, pfnOutput, pvArgOutput, pszTmp, cbTmp);
     211#else
     212    size_t i = rtErrWinLookup(rc);
     213    if (i != ~(size_t)0)
     214        return pfnOutput(pvArgOutput, g_aStatusMsgs[i].pszMsgFull, strlen(g_aStatusMsgs[i].pszMsgFull));
     215
     216    /*
     217     * If FACILITY_WIN32 kind of status, look up the win32 code.
     218     */
     219    if (SCODE_FACILITY(rc) == FACILITY_WIN32)
     220    {
     221        i = rtErrWinLookup(HRESULT_CODE(rc));
     222        if (i != ~(size_t)0)
     223            /* Append the incoming rc, so we know it's not a regular WIN32 status: */
     224            return RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL, "%s/0x%x", g_aStatusMsgs[i].pszDefine, rc);
     225    }
     226    return RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL, "Unknown Status 0x%x", rc);
     227#endif
     228}
     229
     230
     231RTDECL(size_t)  RTErrWinFormatMsgAll(long rc, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, char *pszTmp, size_t cbTmp)
     232{
     233    RT_NOREF(pszTmp, cbTmp);
     234    size_t i = rtErrWinLookup(rc);
     235    if (i != ~(size_t)0)
     236#ifdef IPRT_ERRMSG_DEFINES_ONLY
     237        return RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL, "%s (0x%x)", g_aStatusMsgs[i].pszDefine, rc, g_aStatusMsgs[i].pszMsgFull);
     238#else
     239        return RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL, "%s (0x%x) - %s", g_aStatusMsgs[i].pszDefine, rc, g_aStatusMsgs[i].pszMsgFull);
     240#endif
     241
     242    /*
     243     * If FACILITY_WIN32 kind of status, look up the win32 code.
     244     */
     245    if (SCODE_FACILITY(rc) == FACILITY_WIN32)
     246    {
     247        i = rtErrWinLookup(HRESULT_CODE(rc));
     248        if (i != ~(size_t)0)
     249#ifdef IPRT_ERRMSG_DEFINES_ONLY
     250            return RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL, "%s (0x%x)", g_aStatusMsgs[i].pszDefine, rc, g_aStatusMsgs[i].pszMsgFull);
     251#else
     252            return RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL, "%s (0x%x) - %s", g_aStatusMsgs[i].pszDefine, rc, g_aStatusMsgs[i].pszMsgFull);
     253#endif
     254    }
     255    return RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL, "Unknown Status 0x%x", rc);
     256}
     257
Note: See TracChangeset for help on using the changeset viewer.

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