📄 optionmanager.cpp
字号:
//%2006//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development// Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation, The Open Group.// Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; Symantec Corporation; The Open Group.//// Permission is hereby granted, free of charge, to any person obtaining a copy// of this software and associated documentation files (the "Software"), to// deal in the Software without restriction, including without limitation the// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or// sell copies of the Software, and to permit persons to whom the Software is// furnished to do so, subject to the following conditions:// // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.////==============================================================================////%/////////////////////////////////////////////////////////////////////////////#include <cstdlib>#include <cctype>#include <fstream>#include <cstdio>#include "OptionManager.h"#include "FileSystem.h"PEGASUS_USING_STD;PEGASUS_NAMESPACE_BEGIN//////////////////////////////////////////////////////////////////////////////////// TODO: expand variables in the configuration file. For example://// provider_dir = "${home}/providers"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Option//////////////////////////////////////////////////////////////////////////////////const Array<String>& Option::getDomain() const{ return _domain;}void Option::setDomain(const Array<String>& domain){ _domain = domain;}//////////////////////////////////////////////////////////////////////////////////// OptionManager//////////////////////////////////////////////////////////////////////////////////OptionManager::OptionManager(){}OptionManager::~OptionManager(){ // Delete all options in the list: for (Uint32 i = 0; i < _options.size(); i++) delete _options[i];}void OptionManager::registerOption(Option* option){ if (!option) throw NullPointer(); if (lookupOption(option->getOptionName())) throw OMDuplicateOption(option->getOptionName()); _options.append(option);}void OptionManager::registerOptions(OptionRow* optionRow, Uint32 numOptions){ for (Uint32 i = 0; i < numOptions; i++) { // Get option name: if (!optionRow[i].optionName) throw NullPointer(); String optionName = optionRow[i].optionName; // Get default value: String defaultValue; if (optionRow[i].defaultValue) defaultValue = optionRow[i].defaultValue; // Get the required flag: Boolean required = optionRow[i].required != 0; // Get the type: Option::Type type = optionRow[i].type; // Get the domain: Array<String> domain; if (optionRow[i].domain) { Uint32 domainSize = optionRow[i].domainSize; for (Uint32 j = 0; j < domainSize; j++) domain.append(optionRow[i].domain[j]); } // Get commandLineOptionName: String commandLineOptionName; if (optionRow[i].commandLineOptionName) commandLineOptionName = optionRow[i].commandLineOptionName; // get optionHelp Message String String optionHelpMessage; if (optionRow[i].optionHelpMessage) optionHelpMessage = optionRow[i].optionHelpMessage; // Add the option: Option* option = new Option( optionName, defaultValue, required, type, domain, commandLineOptionName, optionHelpMessage); registerOption(option); }}void OptionManager::mergeCommandLine( int& argc, char**& argv, Boolean abortOnErr){ for (int i = 0; i < argc; ) { // Check for -option: const char* arg = argv[i]; if (*arg == '-') { // Look for the option: Option* option = _lookupOptionByCommandLineOptionName(arg + 1); if (!option) { if (abortOnErr) { throw OMMBadCmdLineOption(arg); } else { i++; continue; } } // Get the option argument if any: const char* optionArgument = "true"; if (option->getType() != Option::BOOLEAN) { if (i + 1 == argc) throw OMMissingCommandLineOptionArgument(arg); optionArgument = argv[i + 1]; } // Validate the value: if (!option->isValid(optionArgument)) throw OMInvalidOptionValue(arg, optionArgument); // Set the value: option->setValue(optionArgument); // Remove the option and its argument from the command line: if (option->getType() == Option::BOOLEAN) { memmove(&argv[i], &argv[i + 1], (argc-i) * sizeof(char*)); argc--; } else { memmove(&argv[i], &argv[i + 2], (argc-i-1) * sizeof(char*)); argc -= 2; } } else i++; }}void OptionManager::mergeFile(const String& fileName){ // Open the input file:#if defined(PEGASUS_OS_OS400) ifstream is(fileName.getCString(),PEGASUS_STD(_CCSID_T(1208)));#else ifstream is(fileName.getCString());#endif if (!is) throw NoSuchFile(fileName); // For each line of the file: String line; for (Uint32 lineNumber = 1; GetLine(is, line); lineNumber++) { // -- Get the identifier and value: if (line[0] == '#') continue; // Skip leading whitespace: const Char16* p = line.getChar16Data(); while (*p && isspace(*p)) p++; if (!*p) continue; if (*p == '#') continue; // Get the identifier: String ident; if (!(isalpha(*p) || *p == '_')) throw OMConfigFileSyntaxError(fileName, lineNumber); ident.append(*p++); while (isalnum(*p) || *p == '_') ident.append(*p++); // Skip whitespace after identifier: while (*p && isspace(*p)) p++; // Expect an equal sign: if (*p != '=') throw OMConfigFileSyntaxError(fileName, lineNumber); p++; // Skip whitespace after equal sign: while (*p && isspace(*p)) p++; // Expect open quote: if (*p != '"') throw OMConfigFileSyntaxError(fileName, lineNumber); p++; // Get the value: String value; while (*p && *p != '"') { if (*p == '\\') { p++; switch (*p) { case 'n': value.append('\n'); break; case 'r': value.append('\r'); break; case 't': value.append('\t'); break; case 'f': value.append('\f');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -