VirtualBox

Ignore:
Timestamp:
Nov 13, 2009 4:09:36 PM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
54800
Message:

IPRT: Added RTStrVersionToUInt32() with testcase.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/string/strtonum.cpp

    r21337 r24639  
    3333*   Header Files                                                               *
    3434*******************************************************************************/
     35#include <iprt/mem.h>
    3536#include <iprt/string.h>
    3637#include "internal/iprt.h"
    3738
     39#include <iprt/ctype.h> /* needed for RT_C_IS_DIGIT */
    3840#include <iprt/err.h>
    3941
     
    9092*/
    9193
     94
     95/**
     96 * Converts a string representation of a version number to an unsigned number.
     97 *
     98 * @returns iprt status code.
     99 *          Warnings are used to indicate convertion problems.
     100 * @retval  VWRN_NUMBER_TOO_BIG
     101 * @retval  VWRN_TRAILING_CHARS
     102 * @retval  VWRN_TRAILING_SPACES
     103 * @retval  VINF_SUCCESS
     104 * @retval  VERR_NO_MEMORY
     105 * @retval  VERR_NO_DIGITS
     106 *
     107 * @param   pszValue    Pointer to the string value.
     108 * @param   pu32        Where to store the converted number.
     109 */
     110int RTStrVersionToUInt32(const char *pszVer, uint32_t *pu32)
     111{
     112    const char *str = pszVer;
     113    AssertPtr(pu32);
     114    AssertPtr(str);
     115
     116    char *strNew = (char*)RTMemAllocZ((strlen(pszVer) + 1) * sizeof(char));
     117    if (strNew == NULL)
     118        return VERR_NO_MEMORY;
     119
     120    int rc = VERR_NO_DIGITS;
     121    uint16_t c = 0;
     122    bool fLastInvalid = false;
     123    while (    str
     124           && *str != '\0')
     125    {
     126        if (fLastInvalid)
     127        {
     128            if (   *str == '-'
     129                || *str == '_')
     130            {
     131                fLastInvalid = false;
     132            }
     133        }
     134        else
     135        {
     136            if (RT_C_IS_DIGIT(*str))
     137            {
     138                strNew[c++] = *str;
     139            }
     140            else if (   *str != '.'
     141                     && c == 0)
     142            {
     143                fLastInvalid = true;
     144            }
     145        }
     146        str++;
     147    }
     148    strNew[c] = '\0';
     149
     150    /* Convert final number string to number */
     151    if (fLastInvalid)
     152    {
     153        *pu32 = 0;
     154        rc = VERR_NO_DIGITS;
     155    }
     156    else
     157    {
     158        rc = RTStrToUInt32Ex(strNew,
     159                             NULL,       /* Next pointer, not used */
     160                             10          /* Number base */,
     161                             pu32);
     162        if (rc != VINF_SUCCESS)
     163            *pu32 = 0;
     164    }
     165    RTStrFree(strNew);
     166    return rc;
     167}
    92168
    93169
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