VirtualBox

Changeset 92937 in vbox for trunk


Ignore:
Timestamp:
Dec 15, 2021 3:29:06 PM (3 years ago)
Author:
vboxsync
Message:

VBoxManage: Generate const tables that doesn't require runtime initialization (copying help entry copy). Fixed buggy capitalization function. Coding style. bugref:1909

Location:
trunk/src/VBox/Frontends/VBoxManage
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VBoxManage/Makefile.kmk

    r92842 r92937  
    307307                'RT_C_DECLS_END' \
    308308                '' \
    309                 'HELP_LANG_ENTRY g_apHelpLangEntries[] = ' \
     309                'HELP_LANG_ENTRY_T const g_aHelpLangEntries[] = ' \
    310310                '{' \
    311                 '    {"en_US", 5, &g_apHelpEntries_en_US[0], g_cHelpEntries_en_US},'
     311                '    { "en_US", 5, &g_apHelpEntries_en_US[0], &g_cHelpEntries_en_US },'
    312312ifdef VBOX_WITH_VBOXMANAGE_NLS
    313313        $(foreach lang,$(VBOX_APPROVED_VBOXMANAGE_DOCBOOK_LANGUAGES) \
    314         ,$(NLTAB)$(QUIET)$(APPEND) "$@" '    {"$(lang)", $(length $(lang)), &g_apHelpEntries_$(lang)[0], g_cHelpEntries_$(lang)},' )
     314        ,$(NLTAB)$(QUIET)$(APPEND) "$@" '    { "$(lang)", $(length $(lang)), &g_apHelpEntries_$(lang)[0], &g_cHelpEntries_$(lang) },' )
    315315endif
    316316        $(QUIET)$(APPEND) -n "$@" \
    317317                '};' \
    318318                '' \
    319                 'const uint32_t g_cHelpLangEntries = RT_ELEMENTS(g_apHelpLangEntries);' \
    320                 '' \
    321                 'PHELP_LANG_ENTRY volatile g_pHelpLangEntry = &g_apHelpLangEntries[0];'\
     319                'uint32_t const g_cHelpLangEntries = RT_ELEMENTS(g_aHelpLangEntries);' \
     320                '' \
     321                'PCHELP_LANG_ENTRY_T volatile g_pHelpLangEntry = &g_aHelpLangEntries[0];'\
    322322                ''
    323323        $(QUIET)$(CP) --changed -- "$@" "$(patsubst %.ts,%,$@)"
     
    354354        $(QUIET)$(APPEND) -n "$@" \
    355355                '' \
    356                 'typedef struct HELP_LANG_ENTRY' \
     356                'typedef struct HELP_LANG_ENTRY_T' \
    357357                '{' \
    358358                '    const char      *pszLang;' \
    359                 '    size_t           cbLang;' \
     359                '    size_t           cchLang;' \
    360360                '    PCRTMSGREFENTRY *papHelpEntries;' \
    361                 '    const uint32_t   cHelpEntries;' \
    362                 '} HELP_LANG_ENTRY;' \
    363                 'typedef HELP_LANG_ENTRY *PHELP_LANG_ENTRY;' \
    364                 '' \
    365                 'extern HELP_LANG_ENTRY g_apHelpLangEntries[];' \
    366                 'extern const uint32_t  g_cHelpLangEntries;' \
    367                 '' \
    368                 'extern PHELP_LANG_ENTRY volatile g_pHelpLangEntry;' \
     361                '    uint32_t const  *pcHelpEntries;' \
     362                '} HELP_LANG_ENTRY_T;' \
     363                'typedef HELP_LANG_ENTRY_T const *PCHELP_LANG_ENTRY_T;' \
     364                '' \
     365                'extern HELP_LANG_ENTRY_T const g_aHelpLangEntries[];' \
     366                'extern const uint32_t          g_cHelpLangEntries;' \
     367                '' \
     368                'extern PCHELP_LANG_ENTRY_T volatile g_pHelpLangEntry;' \
    369369                '' \
    370370                'RT_C_DECLS_END' \
  • trunk/src/VBox/Frontends/VBoxManage/VBoxManage.cpp

    r92842 r92937  
    509509{
    510510#ifdef VBOX_WITH_VBOXMANAGE_NLS
    511     if (pszLang == NULL || pszLang[0] == 0 || (pszLang[0] == 'C' && pszLang[1] == 0))
     511    if (pszLang == NULL || pszLang[0] == '\0' || (pszLang[0] == 'C' && pszLang[1] == '\0'))
    512512        pszLang = "en_US";
    513     PHELP_LANG_ENTRY pHelpLangEntry = NULL;
     513
    514514    /* find language entry matching exactly pszLang */
     515    PCHELP_LANG_ENTRY_T pHelpLangEntry = NULL;
    515516    for (uint32_t i = 0; i < g_cHelpLangEntries; i++)
    516517    {
    517         if (strcmp(g_apHelpLangEntries[i].pszLang, pszLang) == 0)
    518         {
    519             pHelpLangEntry = &g_apHelpLangEntries[i];
     518        if (strcmp(g_aHelpLangEntries[i].pszLang, pszLang) == 0)
     519        {
     520            pHelpLangEntry = &g_aHelpLangEntries[i];
    520521            break;
    521522        }
    522523    }
     524
    523525    /* find first entry containing language specified if pszLang contains only language */
    524526    if (pHelpLangEntry == NULL)
    525527    {
    526         size_t cbLang = strlen(pszLang);
     528        size_t const cchLang = strlen(pszLang);
    527529        for (uint32_t i = 0; i < g_cHelpLangEntries; i++)
    528530        {
    529             if (   cbLang < g_apHelpLangEntries[i].cbLang
    530                 && memcmp(g_apHelpLangEntries[i].pszLang, pszLang, cbLang) == 0)
    531             {
    532                 pHelpLangEntry = &g_apHelpLangEntries[i];
     531            if (   cchLang < g_aHelpLangEntries[i].cchLang
     532                && memcmp(g_aHelpLangEntries[i].pszLang, pszLang, cchLang) == 0)
     533            {
     534                pHelpLangEntry = &g_aHelpLangEntries[i];
    533535                break;
    534536            }
    535537        }
    536538    }
     539
    537540    /* set to en_US (i.e. untranslated) if not found */
    538541    if (pHelpLangEntry == NULL)
    539         pHelpLangEntry = &g_apHelpLangEntries[0];
     542        pHelpLangEntry = &g_aHelpLangEntries[0];
    540543
    541544    ASMAtomicWritePtr(&g_pHelpLangEntry, pHelpLangEntry);
     
    585588        if (RT_SUCCESS(vrc))
    586589            vrc = RTPathAppend(szNlsPath, sizeof(szNlsPath), "nls" RTPATH_SLASH_STR "VBoxManageNls");
    587 
    588590        if (RT_SUCCESS(vrc))
    589591        {
  • trunk/src/VBox/Frontends/VBoxManage/VBoxManageHelp.cpp

    r92842 r92937  
    3131#include <iprt/stream.h>
    3232#include <iprt/message.h>
     33#include <iprt/uni.h>
    3334
    3435#include "VBoxManage.h"
     
    8586 *
    8687 * @returns pointer to string starting from next char.
    87  * @param   pszSrc            Source string.
    88  * @param   pszFirstCharBuffer Pointer to buffer to place first char uppercase
    89  */
    90 static const char *firstCharUppercase(const char *pszSrc, char *pszFirstCharBuffer)
    91 {
    92     RTUNICP Cp;
    93     RTStrGetCpEx(&pszSrc, &Cp);
    94     char *pszBuffer = RTStrPutCp(pszFirstCharBuffer, Cp);
    95     RTStrToUpper(pszBuffer);
    96     pszBuffer[0] = 0;
    97     return pszSrc;
     88 * @param   pszSrc      Source string.
     89 * @param   pszDst      Pointer to buffer to place first char uppercase.
     90 */
     91static const char *captialize(const char *pszSrc, char *pszDst)
     92{
     93    *RTStrPutCp(pszDst, RTUniCpToUpper(RTStrGetCp(pszSrc))) = '\0';
     94    return RTStrNextCp(pszSrc);
    9895}
    9996
     
    110107static uint32_t printBriefCommandOrSubcommandHelp(enum HELP_CMD_VBOXMANAGE enmCommand, uint64_t fSubcommandScope, PRTSTREAM pStrm)
    111108{
    112     uint32_t cLinesWritten = 0;
    113     uint32_t cPendingBlankLines = 0;
    114     uint32_t cFound = 0;
     109    /*
     110     * Try to find translated, falling back untranslated.
     111     */
     112    uint32_t                  cLinesWritten       = 0;
     113    uint32_t                  cPendingBlankLines  = 0;
     114    uint32_t                  cFound              = 0;
     115    PCHELP_LANG_ENTRY_T const apHelpLangEntries[] =
     116    {
     117        ASMAtomicUoReadPtrT(&g_pHelpLangEntry, PCHELP_LANG_ENTRY_T),
    115118#ifdef VBOX_WITH_VBOXMANAGE_NLS
    116     PHELP_LANG_ENTRY pHelpLangEntry[2] = {ASMAtomicReadPtrT(&g_pHelpLangEntry, PHELP_LANG_ENTRY), &g_apHelpLangEntries[0] };
    117 #else
    118     PHELP_LANG_ENTRY pHelpLangEntry[1] = {(PHELP_LANG_ENTRY)g_pHelpLangEntry};
    119 #endif
    120     /* Try to find translated, then untranslated */
    121     for (uint32_t k = 0; k < RT_ELEMENTS(pHelpLangEntry) && cFound == 0; k++)
     119        &g_aHelpLangEntries[0]
     120#endif
     121    };
     122    for (uint32_t k = 0; k < RT_ELEMENTS(apHelpLangEntries) && cFound == 0; k++)
    122123    {
    123124        /* skip if english is used */
    124         if (k > 0 && pHelpLangEntry[k] == pHelpLangEntry[0])
     125        if (k > 0 && apHelpLangEntries[k] == apHelpLangEntries[0])
    125126            break;
    126         for (uint32_t i = 0; i < pHelpLangEntry[k]->cHelpEntries; i++)
     127        uint32_t const cHelpEntries = *apHelpLangEntries[k]->pcHelpEntries;
     128        for (uint32_t i = 0; i < cHelpEntries; i++)
    127129        {
    128             PCRTMSGREFENTRY pHelp = pHelpLangEntry[k]->papHelpEntries[i];
     130            PCRTMSGREFENTRY pHelp = apHelpLangEntries[k]->papHelpEntries[i];
    129131            if (pHelp->idInternal == (int64_t)enmCommand)
    130132            {
     
    134136                    if (fSubcommandScope == RTMSGREFENTRYSTR_SCOPE_GLOBAL)
    135137                    {
    136                         char szFirstChar[16] = {0};
    137                         const char *pszNext = firstCharUppercase(pHelp->pszBrief, szFirstChar);
    138                         RTStrmPrintf(pStrm, Help::tr("Usage - %s%s:\n"), szFirstChar, pszNext);
     138                        char szFirstChar[8];
     139                        RTStrmPrintf(pStrm, Help::tr("Usage - %s%s:\n"), szFirstChar, captialize(pHelp->pszBrief, szFirstChar));
    139140                    }
    140141                    else
     
    173174static void printFullCommandOrSubcommandHelp(enum HELP_CMD_VBOXMANAGE enmCommand, uint64_t fSubcommandScope, PRTSTREAM pStrm)
    174175{
    175     uint32_t cPendingBlankLines = 0;
    176     uint32_t cFound = 0;
     176    /* Try to find translated, then untranslated */
     177    uint32_t                  cPendingBlankLines  = 0;
     178    uint32_t                  cFound              = 0;
     179    PCHELP_LANG_ENTRY_T const apHelpLangEntries[] =
     180    {
     181        ASMAtomicUoReadPtrT(&g_pHelpLangEntry, PCHELP_LANG_ENTRY_T),
    177182#ifdef VBOX_WITH_VBOXMANAGE_NLS
    178     PHELP_LANG_ENTRY pHelpLangEntry[2] = {ASMAtomicReadPtrT(&g_pHelpLangEntry, PHELP_LANG_ENTRY), &g_apHelpLangEntries[0] };
    179 #else
    180     PHELP_LANG_ENTRY pHelpLangEntry[1] = {(PHELP_LANG_ENTRY)g_pHelpLangEntry};
    181 #endif
    182     /* Try to find translated, then untranslated */
    183     for (uint32_t k = 0; k < RT_ELEMENTS(pHelpLangEntry) && cFound == 0; k++)
     183        &g_aHelpLangEntries[0]
     184#endif
     185    };
     186    for (uint32_t k = 0; k < RT_ELEMENTS(apHelpLangEntries) && cFound == 0; k++)
    184187    {
    185188        /* skip if english is used */
    186         if (k > 0 && pHelpLangEntry[k] == pHelpLangEntry[0])
     189        if (k > 0 && apHelpLangEntries[k] == apHelpLangEntries[0])
    187190            break;
    188         for (uint32_t i = 0; i < pHelpLangEntry[k]->cHelpEntries; i++)
     191        uint32_t const cHelpEntries = *apHelpLangEntries[k]->pcHelpEntries;
     192        for (uint32_t i = 0; i < cHelpEntries; i++)
    189193        {
    190             PCRTMSGREFENTRY pHelp = pHelpLangEntry[k]->papHelpEntries[i];
     194            PCRTMSGREFENTRY pHelp = apHelpLangEntries[k]->papHelpEntries[i];
    191195
    192196            if (   pHelp->idInternal == (int64_t)enmCommand
     
    10341038    if (enmCommand == USAGE_S_ALL)
    10351039    {
    1036         uint32_t cPendingBlankLines = 0;
    1037 #ifdef VBOX_WITH_VBOXMANAGE_NLS
    1038         PHELP_LANG_ENTRY pHelpLangEntry = ASMAtomicReadPtrT(&g_pHelpLangEntry, PHELP_LANG_ENTRY);
    1039 #else
    1040         PHELP_LANG_ENTRY pHelpLangEntry = (PHELP_LANG_ENTRY)g_pHelpLangEntry;
    1041 #endif
    1042         for (uint32_t i = 0; i < pHelpLangEntry->cHelpEntries; i++)
     1040        uint32_t            cPendingBlankLines = 0;
     1041        PCHELP_LANG_ENTRY_T pHelpLangEntry     = ASMAtomicUoReadPtrT(&g_pHelpLangEntry, PCHELP_LANG_ENTRY_T);
     1042        uint32_t const      cHelpEntries       = *pHelpLangEntry->pcHelpEntries;
     1043        for (uint32_t i = 0; i < cHelpEntries; i++)
    10431044        {
    10441045            PCRTMSGREFENTRY pHelp = pHelpLangEntry->papHelpEntries[i];
     
    10461047            while (cPendingBlankLines-- > 0)
    10471048                RTStrmPutCh(pStrm, '\n');
    1048             char szFirstChar[16] = {0};
    1049             const char *pszNext = firstCharUppercase(pHelp->pszBrief, szFirstChar);
    1050             RTStrmPrintf(pStrm, " %s%s:\n", szFirstChar, pszNext);
     1049
     1050            char szFirstChar[8];
     1051            RTStrmPrintf(pStrm, " %s%s:\n", szFirstChar, captialize(pHelp->pszBrief, szFirstChar));
     1052
    10511053            cPendingBlankLines = 0;
    10521054            RTMsgRefEntryPrintStringTable(pStrm, &pHelp->Synopsis, RTMSGREFENTRYSTR_SCOPE_GLOBAL,
    1053                                         &cPendingBlankLines, NULL /*pcLinesWritten*/);
     1055                                          &cPendingBlankLines, NULL /*pcLinesWritten*/);
    10541056            cPendingBlankLines = RT_MAX(cPendingBlankLines, 1);
    10551057        }
Note: See TracChangeset for help on using the changeset viewer.

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