⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlreader.cpp

📁 Pegasus is an open-source implementationof the DMTF CIM and WBEM standards. It is designed to be por
💻 CPP
📖 第 1 页 / 共 5 页
字号:
// stringToSignedInteger////      [ "+" | "-" ] ( positiveDecimalDigit *decimalDigit | "0" )//    or//      [ "+" | "-" ] ( "0x" | "0X" ) 1*hexDigit////------------------------------------------------------------------------------Boolean XmlReader::stringToSignedInteger(    const char* stringValue,    Sint64& x){    x = 0;    const char* p = stringValue;    if (!p || !*p)        return false;    // Skip optional sign:    Boolean negative = *p == '-';    if (negative || *p == '+')        p++;    if (*p == '0')    {        if ( (p[1] == 'x') || (p[1] == 'X') )        {            // Convert a hexadecimal string            // Skip over the "0x"            p+=2;            // At least one hexadecimal digit is required            if (!isxdigit(*p))                return false;            // Build the Sint64 as a negative number, regardless of the            // eventual sign (negative numbers can be bigger than positive ones)            // Add on each digit, checking for overflow errors            while (isxdigit(*p))            {                // Make sure we won't overflow when we multiply by 16                if (x < PEGASUS_SINT64_MIN/16)                {                    return false;                }                x = x << 4;                // Make sure we don't overflow when we add the next digit                Sint64 newDigit = Sint64(_xmlReader_hexCharToNumeric(*p++));                if (PEGASUS_SINT64_MIN - x > -newDigit)                {                    return false;                }                x = x - newDigit;            }            // If we found a non-hexadecimal digit, report an error            if (*p)                return false;            // Return the integer to positive, if necessary, checking for an            // overflow error            if (!negative)            {                if (x == PEGASUS_SINT64_MIN)                {                    return false;                }                x = -x;            }            return true;        }        else        {            // A decimal string that starts with '0' must be exactly "0".            return p[1] == '\0';        }    }    // Expect a positive decimal digit:    // At least one decimal digit is required    if (!isdigit(*p))        return false;    // Build the Sint64 as a negative number, regardless of the    // eventual sign (negative numbers can be bigger than positive ones)    // Add on each digit, checking for overflow errors    while (isdigit(*p))    {        // Make sure we won't overflow when we multiply by 10        if (x < PEGASUS_SINT64_MIN/10)        {            return false;        }        x = 10 * x;        // Make sure we won't overflow when we add the next digit        Sint64 newDigit = (*p++ - '0');        if (PEGASUS_SINT64_MIN - x > -newDigit)        {            return false;        }        x = x - newDigit;    }    // If we found a non-decimal digit, report an error    if (*p)        return false;    // Return the integer to positive, if necessary, checking for an    // overflow error    if (!negative)    {        if (x == PEGASUS_SINT64_MIN)        {            return false;        }        x = -x;    }    return true;}//------------------------------------------------------------------------------//// stringToUnsignedInteger////      ( positiveDecimalDigit *decimalDigit | "0" )//    or//      ( "0x" | "0X" ) 1*hexDigit////------------------------------------------------------------------------------Boolean XmlReader::stringToUnsignedInteger(    const char* stringValue,    Uint64& x){    x = 0;    const char* p = stringValue;    if (!p || !*p)        return false;    if (*p == '0')    {        if ( (p[1] == 'x') || (p[1] == 'X') )        {            // Convert a hexadecimal string            // Skip over the "0x"            p+=2;            // At least one hexadecimal digit is required            if (!*p)                return false;            // Add on each digit, checking for overflow errors            while (isxdigit(*p))            {                // Make sure we won't overflow when we multiply by 16                if (x > PEGASUS_UINT64_MAX/16)                {                    return false;                }                x = x << 4;                // We can't overflow when we add the next digit                Uint64 newDigit = Uint64(_xmlReader_hexCharToNumeric(*p++));                if (PEGASUS_UINT64_MAX - x < newDigit)                {                    return false;                }                x = x + newDigit;            }            // If we found a non-hexadecimal digit, report an error            if (*p)                return false;            return true;        }        else        {            // A decimal string that starts with '0' must be exactly "0".            return p[1] == '\0';        }    }    // Expect a positive decimal digit:    // Add on each digit, checking for overflow errors    while (isdigit(*p))    {        // Make sure we won't overflow when we multiply by 10        if (x > PEGASUS_UINT64_MAX/10)        {            return false;        }        x = 10 * x;        // Make sure we won't overflow when we add the next digit        Uint64 newDigit = (*p++ - '0');        if (PEGASUS_UINT64_MAX - x < newDigit)        {            return false;        }        x = x + newDigit;    }    // If we found a non-decimal digit, report an error    if (*p)        return false;    return true;}//------------------------------------------------------------------------------//// stringToValue()//// Return: CIMValue. If the string input is zero length creates a CIMValue//         with value defined by the type.  Else the value is inserted.////         Note that this does not set the CIMValue Null if the string is empty.////------------------------------------------------------------------------------CIMValue XmlReader::stringToValue(    Uint32 lineNumber,    const char* valueString,    CIMType type){    // ATTN-B: accepting only UTF-8 for now! (affects string and char16):    // Create value per type    switch (type)    {        case CIMTYPE_BOOLEAN:        {            if (System::strcasecmp(valueString, "TRUE") == 0)                return CIMValue(true);            else if (System::strcasecmp(valueString, "FALSE") == 0)                return CIMValue(false);            else            {                MessageLoaderParms mlParms(                    "Common.XmlReader.INVALID_BOOLEAN_VALUE",                    "Invalid boolean value");                throw XmlSemanticError(lineNumber, mlParms);            }        }        case CIMTYPE_STRING:        {            return CIMValue(String(valueString));        }        case CIMTYPE_CHAR16:        {// remove this test, utf-8 can be up to 6 bytes per char/*            if (strlen(valueString) != 1)            {                MessageLoaderParms mlParms(                    "Common.XmlReader.INVALID_CHAR16_VALUE",                    "Invalid char16 value");                throw XmlSemanticError(lineNumber, mlParms);            }*/            // Converts UTF-8 to UTF-16            String tmp(valueString);            if (tmp.size() != 1)            {                MessageLoaderParms mlParms(                    "Common.XmlReader.INVALID_CHAR16_VALUE",                    "Invalid char16 value");                throw XmlSemanticError(lineNumber, mlParms);            }            return CIMValue(tmp[0]);        }        case CIMTYPE_UINT8:        case CIMTYPE_UINT16:        case CIMTYPE_UINT32:        case CIMTYPE_UINT64:        {            Uint64 x;            if (!stringToUnsignedInteger(valueString, x))            {                MessageLoaderParms mlParms(                    "Common.XmlReader.INVALID_UI_VALUE",                    "Invalid unsigned integer value");                throw XmlSemanticError(lineNumber, mlParms);            }            switch (type)            {                case CIMTYPE_UINT8:                {                    if (x >= (Uint64(1)<<8))                    {                        MessageLoaderParms mlParms(                            "Common.XmlReader.U8_VALUE_OUT_OF_RANGE",                            "Uint8 value out of range");                        throw XmlSemanticError(lineNumber, mlParms);                    }                    return CIMValue(Uint8(x));                }                case CIMTYPE_UINT16:                {                    if (x >= (Uint64(1)<<16))                    {                        MessageLoaderParms mlParms(                            "Common.XmlReader.U16_VALUE_OUT_OF_RANGE",                            "Uint16 value out of range");                        throw XmlSemanticError(lineNumber, mlParms);                    }                    return CIMValue(Uint16(x));                }                case CIMTYPE_UINT32:                {                    if (x >= (Uint64(1)<<32))                    {                        MessageLoaderParms mlParms(                            "Common.XmlReader.U32_VALUE_OUT_OF_RANGE",                            "Uint32 value out of range");                        throw XmlSemanticError(lineNumber, mlParms);                    }                    return CIMValue(Uint32(x));                }                case CIMTYPE_UINT64: return CIMValue(Uint64(x));                default: break;            }        }        case CIMTYPE_SINT8:        case CIMTYPE_SINT16:        case CIMTYPE_SINT32:        case CIMTYPE_SINT64:        {            Sint64 x;            if (!stringToSignedInteger(valueString, x))            {                MessageLoaderParms mlParms(                    "Common.XmlReader.INVALID_SI_VALUE",                    "Invalid signed integer value");                throw XmlSemanticError(lineNumber, mlParms);            }            switch (type)            {                case CIMTYPE_SINT8:                {                    if ((x >= (Sint64(1)<<7)) || (x < (-(Sint64(1)<<7))))                    {                        MessageLoaderParms mlParms(                            "Common.XmlReader.S8_VALUE_OUT_OF_RANGE",                            "Sint8 value out of range");                        throw XmlSemanticError(lineNumber, mlParms);                    }                    return CIMValue(Sint8(x));                }                case CIMTYPE_SINT16:                {                    if ((x >= (Sint64(1)<<15)) || (x < (-(Sint64(1)<<15))))                    {                        MessageLoaderParms mlParms(                            "Common.XmlReader.S16_VALUE_OUT_OF_RANGE",                            "Sint16 value out of range");                        throw XmlSemanticError(lineNumber, mlParms);                    }                    return CIMValue(Sint16(x));                }                case CIMTYPE_SINT32:                {                    if ((x >= (Sint64(1)<<31)) || (x < (-(Sint64(1)<<31))))                    {                        MessageLoaderParms mlParms(                            "Common.XmlReader.S32_VALUE_OUT_OF_RANGE",                            "Sint32 value out of range");                        throw XmlSemanticError(lineNumber, mlParms);                    }                    return CIMValue(Sint32(x));                }                case CIMTYPE_SINT64: return CIMValue(Sint64(x));                default: break;            }        }        case CIMTYPE_DATETIME:        {            CIMDateTime tmp;            try            {                // KS 20021002 - Exception if no datetime value. Test for                // zero length and leave the NULL value in the variable                // Bugzilla 137  Adds the following if line.                // Expect this to become permanent but test only for now#ifdef PEGASUS_SNIA_INTEROP_TEST                if (strlen(valueString) != 0)#endif                    tmp.set(valueString);            }            catch (InvalidDateTimeFormatException&)            {                MessageLoaderParms mlParms(                    "Common.XmlReader.INVALID_DATETIME_VALUE",                    "Invalid datetime value");                throw XmlSemanticError(lineNumber, mlParms);            }            return CIMValue(tmp);        }        case CIMTYPE_REAL32:        {            Real64 x;            if (!stringToReal(valueString, x))            {                MessageLoaderParms mlParms(                    "Common.XmlReader.INVALID_RN_VALUE",                    "Invalid real number value");                throw XmlSemanticError(lineNumber, mlParms);            }            return CIMValue(Real32(x));        }        case CIMTYPE_REAL64:        {            Real64 x;            if (!stringToReal(valueString, x))            {                MessageLoaderParms mlParms(                    "Common.XmlReader.INVALID_RN_VALUE",                    "Invalid real number value");                throw XmlSemanticError(lineNumber, mlParms);            }            return CIMValue(x);        }//  PEP 194://  Note that "object" (ie. CIMTYPE_OBJECT) is not a real CIM datatype, but

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -