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

📄 optionmanager.cpp

📁 Pegasus is an open-source implementationof the DMTF CIM and WBEM standards. It is designed to be por
💻 CPP
📖 第 1 页 / 共 2 页
字号:
                        break;                    case '"':                        value.append('"');                        break;                    case '\0':                        throw OMConfigFileSyntaxError(fileName, lineNumber);                    default:                        value.append(*p);                }                p++;            }            else                value.append(*p++);        }        // Expect close quote:        if (*p != '"')            throw OMConfigFileSyntaxError(fileName, lineNumber);        p++;        // Skip whitespace through end of line:        while (*p && isspace(*p))            p++;        if (*p)            throw OMConfigFileSyntaxError(fileName, lineNumber);        // Now that we have the identifier and value, merge it:        Option* option = (Option*)lookupOption(ident);        if (!option)            throw OMUnrecognizedConfigFileOption(ident);        if (!option->isValid(value))            throw OMInvalidOptionValue(ident, value);        option->setValue(value);    }}void OptionManager::checkRequiredOptions() const{    for (Uint32 i = 0; i < _options.size(); i++)    {        const Option* option = _options[i];        if (option->getRequired() && !option->isResolved())            throw OMMissingRequiredOptionValue(option->getOptionName());    }}const Option* OptionManager::lookupOption(const String& name) const{    for (Uint32 i = 0; i < _options.size(); i++)    {        if (_options[i]->getOptionName() == name)            return _options[i];    }    return 0;}Boolean OptionManager::lookupValue(const String& name, String& value) const{    const Option* option = lookupOption(name);    if (!option)        return false;    value = option->getValue();    return true;}Boolean OptionManager::lookupIntegerValue(    const String& name,    Uint32& value) const{    //ATTN: KS P1 7 May 2002 - Add test for Integer type in om table.    String valueString;    if (lookupValue(name, valueString))    {        value = atol(valueString.getCString());        return true;    }    else    {        return false;    }}Boolean OptionManager::valueEquals(    const String& name,    const String& value) const{    String optionString;    return (lookupValue(name, optionString) && optionString == value);}Boolean OptionManager::isTrue(const String& name) const{    //ATTN: KS 7 May 2002 P3 Add test to confirm boolean type    return valueEquals(name, "true") ? true: false;}/*  ATTN: P3 MB 2001 Buried this one for the moment to think about it.Uint32 OptionManager::isStringInOptionMask(    const String& option,    const String& entry){    String optionString;    if (lookupValue(name, optionString) && optionString == value)        if (optionString.find(entry)            return 1;    else        return PEG_NOT_FOUND;}*/Option* OptionManager::_lookupOptionByCommandLineOptionName(const String& name){    for (Uint32 i = 0; i < _options.size(); i++)    {        if (_options[i]->getCommandLineOptionName() == name)            return _options[i];    }    return 0;}void OptionManager::print() const{    for (Uint32 i = 0; i < _options.size(); i++)    {        Option* option = _options[i];        cout << option->getOptionName() << "=\"";        cout << option->getValue() << "\" ";        cout << option->getOptionHelpMessage() << "\n";    }    cout << endl;}void OptionManager::printOptionsHelp() const{    for (Uint32 i = 0; i < _options.size(); i++)    {        Option* option = _options[i];        cout << " -";        cout << option->getCommandLineOptionName() << "  ";        cout << option->getOptionName() << ". ";        cout << option->getOptionHelpMessage();        cout << ". Default(" << option->getDefaultValue() << ")\n";    }    cout << endl;}void OptionManager::printOptionsHelpTxt(    const String& header,    const String& trailer) const{    cout << "\n" << header << "\n";    printOptionsHelp();    cout << trailer << "\n";}//////////////////////////////////////////////////////////////////////////////////// Option//////////////////////////////////////////////////////////////////////////////////Option::Option(    const String& optionName,    const String& defaultValue,    Boolean required,    Type type,    const Array<String>& domain,    const String& commandLineOptionName,    const String& optionHelpMessage)    :    _optionName(optionName),    _defaultValue(defaultValue),    _value(defaultValue),    _required(required),    _type(type),    _domain(domain),    _commandLineOptionName(commandLineOptionName),    _optionHelpMessage(optionHelpMessage),    _resolved(false){    if (!isValid(_value))        throw OMInvalidOptionValue(_optionName, _value);}Option::Option(const Option& x)    :    _optionName(x._optionName),    _defaultValue(x._defaultValue),    _value(x._value),    _required(x._required),    _type(x._type),    _domain(x._domain),    _commandLineOptionName(x._commandLineOptionName),    _optionHelpMessage(x._optionHelpMessage){}Option::~Option(){}Option& Option::operator=(const Option& x){    if (this != &x)    {        _optionName = x._optionName;        _defaultValue = x._defaultValue;        _value = x._value;        _required = x._required;        _type = x._type;        _domain = x._domain;        _commandLineOptionName = x._commandLineOptionName;        _optionHelpMessage = x._optionHelpMessage;    }    return *this;}Boolean Option::isValid(const String& value) const{    // Check to see that the value is in the domain (if a domain was given)    Uint32 domainSize = _domain.size();    if (domainSize)    {        Boolean found = false;        for (Uint32 i = 0; i < domainSize; i++)        {            if (value == _domain[i])                found = true;        }        if (!found)            return false;    }    // Check the type:    switch (_type)    {        case BOOLEAN:        {            if (value == "true" || value == "false")                return true;            else                return false;        }        case STRING:            return true;        case INTEGER:        case NATURAL_NUMBER:        case WHOLE_NUMBER:        {            CString tmp = value.getCString();            char* end = 0;            long x = strtol(tmp, &end, 10);            if (!end || *end != '\0')                return false;            switch (_type)            {                case INTEGER:                    return true;                case NATURAL_NUMBER:                    return x >= 1;                case WHOLE_NUMBER:                    return x >= 0;                default:                    break;            }        }    }    // Unreachable!    return false;}//////////////////////////////////////////////////////////////////////////////////// ConfigFileSyntaxError//////////////////////////////////////////////////////////////////////////////////String OMConfigFileSyntaxError::_formatMessage(    const String& file, Uint32 line){    char buffer[32];    sprintf(buffer, "%d", line);    MessageLoaderParms parms(        "Common.OptionManager.SYNTAX_ERR_CONFIG_FILE",        "Syntax error in configuration file: ");    String result = MessageLoader::getMessage(parms);    result.append(file);    result.append("(");    result.append(buffer);    result.append(")");    return result;}PEGASUS_NAMESPACE_END

⌨️ 快捷键说明

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