📄 getoopt.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.////==============================================================================//// Author: Bob Blair (bblair@bmc.com)//// Modified By: Carol Ann Krug Graves, Hewlett-Packard Company// (carolann_graves@hp.com)// David Dillard, VERITAS Software Corp.// (david.dillard@veritas.com)////%///////////////////////////////////////////////////////////////////////////////// implementation of getoopt#include <Pegasus/Common/PegasusVersion.h>#include <Pegasus/Common/MessageLoader.h> //l10n#include "getoopt.h"#include <cctype>#include <cstdlib>PEGASUS_USING_STD;//-----------------------------------------------------------------------// Implementation of class Optarg// An Optarg is created for each parameter on the command line.//-----------------------------------------------------------------------// Constructors// Default ConstructorOptarg::Optarg() : _name(""), _opttype(REGULAR), _value("") {}// All-in-oneOptarg::Optarg(const String &name, opttype type, const String &value) : _name(name), _opttype(type), _value(value) {}// DestructorOptarg::~Optarg() {};//-----------------------------------------------------------------------// Set the class members//-----------------------------------------------------------------------// Set the _name membervoidOptarg::setName(const String &name) { _name = name;}// Set the _opttype membervoidOptarg::setType(opttype type) { _opttype = type;}// Set the _value membervoidOptarg::setValue(const String &value) { _value = value;}//-----------------------------------------------------------------------// Retrieve class members//-----------------------------------------------------------------------// Get the _name memberconst String &Optarg::getName() const { return _name; }// Get the _name member using getopt() terminologyconst String &Optarg::getopt() const { return _name; }// Get the _type memberOptarg::opttypeOptarg::getType() const { return _opttype; }//-------------------------------------------------------------------// Ways to get the _value member//-------------------------------------------------------------------// Return _value as const String refconst String &Optarg::Value() const { return _value; }// Same thing as Value(), but using getopt() terminologyconst String &Optarg::optarg() const { return _value; }// Fill in a caller-provided StringvoidOptarg::Value(String &s) const { s = _value; }// Fill in a caller-provided int with the integer conversion of the value.void Optarg::Value (int &i) const{ CString cs = _value.getCString(); const char* s = cs; Boolean valid = true; Uint32 j; for (j = 0; j < strlen (s); j++) { if ((!isdigit (s [j])) && (!isspace (s [j])) && (s [j] != '+') && (s [j] != '-')) { valid = false; break; } } if (valid) { Sint64 i64; if ( !(sscanf (s, "%" PEGASUS_64BIT_CONVERSION_WIDTH "d", &i64)) || (i64 != Sint64(Sint32(i64))) ) { throw TypeMismatchException (); } i = Sint32(i64); } else { throw TypeMismatchException (); }}// Fill in a caller-provided unsigned intvoid Optarg::Value (unsigned int &i) const{ CString cs = _value.getCString(); const char* s = cs; Boolean valid = true; Uint32 j; for (j = 0; j < strlen (s); j++) { if ((!isdigit (s [j])) && (!isspace (s [j]))) { valid = false; break; } } if (valid) { Uint64 i64; if ( !(sscanf (s, "%" PEGASUS_64BIT_CONVERSION_WIDTH "u", &i64)) || (i64 > 0xFFFFFFFF)) { throw TypeMismatchException (); } i = Uint32(i64); } else { throw TypeMismatchException (); }}// Fill in a caller-provided longvoidOptarg::Value(long &l) const { l = (long)atoi(_value.getCString());}// Fill in a caller-provided unsigned longvoidOptarg::Value(unsigned long &l) const { l = (unsigned long)atoi(_value.getCString());}// Fill in a caller-provided doublevoidOptarg::Value(double &d) const { d = (double)atof(_value.getCString());}//--------------------------------------------------------------------// Provide information about the flag, if any//--------------------------------------------------------------------// Is the option value is bound to a flag?BooleanOptarg::isFlag() const { return (_opttype == FLAG || _opttype == LONGFLAG); }// Is it bound to a long-named flag?BooleanOptarg::isLongFlag() const { return (_opttype == LONGFLAG); }//-----------------------------------------------------------------------// print the members as a formatted String//-----------------------------------------------------------------------ostream &Optarg::print(ostream &os) const { os << "{name:(" << getName(); os << ") type:("; switch (getType()) { case FLAG: os << "FLAG"; break; case LONGFLAG: os << "LONGFLAG"; break; case REGULAR: os << "REGULAR"; break; } os << ") value:(" << Value() << ")}"; return os;}//---------------------------------------------------------------------// Implementation of class getoopt//---------------------------------------------------------------------// Constructors and destructor// Default constructor. The optional String is in the format of// a getopt() optstringgetoopt::getoopt(const char *optstring){ if (optstring) { addFlagspec(optstring); }}getoopt::~getoopt() {;}//----------------------------------------------------------------------// methods to register program-defined flags and their characteristics// The flags are encapsulated in the struct flagspec, an array of which// is a member of this class.// There are also methods to deregister flags. This is done by resetting// a flagspec's active flag.//----------------------------------------------------------------------// Parse through a getopt() optstring and create flagspecs from each// short flag.Booleangetoopt::addFlagspec(const String &opt) { unsigned int size = opt.size(); if (size == 0) return false; for (unsigned int i = 0; i < size; i++) { char c = static_cast<char>(opt[i]); if ( ((i + 1) < size) && (opt[i+1] == ':') ) { if (!(addFlagspec(c, true))) { return false; } ++i; } else { if (!(addFlagspec(c, false))) return false; } } return true;}// Create a filespec from a single short flag and push it onto the arrayBooleangetoopt::addFlagspec(char flag, Boolean hasarg) { if (flag == '*') { //l10n MessageLoaderParms parms("getoopt.getoopt.CANT_NAME_FLAG", "You can't have a flag named '$0'", flag); addError(MessageLoader::getMessage(parms)); //addError("You can't have a flag named '*'"); //l10n end return false; } flagspec fs; char c[2]; c[0] = flag; c[1] = 0; fs.name = c; fs.argtype = hasarg ? 1 : 0; fs.islong = false; fs.active = true; _flagspecs.append(fs); return true;}// Create a flagspec from a single long flag and push it onto the arrayBooleangetoopt::addLongFlagspec(const String &name, argtype type) { flagspec fs; // Changing "fs.name = name" to the following line masks an ugly crash // which occurs when compiling with debug option on WIN32: fs.name = name; fs.argtype = type; fs.islong = true; fs.active = true; _flagspecs.append(fs); return true;}// Unregister a flagspecBooleangetoopt::removeFlagspec(char opt) { flagspec *fs = getFlagspecForUpdate(opt); if (fs) { fs->active = false; return true; } return false;}/** In the valid option definition string, following an option, indicates that the preceding option takes a required argument. */const char getoopt::GETOPT_ARGUMENT_DESIGNATOR = ':';//--------------------------------------------------------------------// Routines for parsing the command line//--------------------------------------------------------------------//------------------------// Static functions//------------------------// Parse out the flagname and the value from a long flag option that// may be in the form// --longflag=valuestatic voidpartsFromLongOpt (const String &s, String &name, String &value) { for (unsigned int i = 0; i < s.size(); i++) { if (s[i] == '=') { name = s.subString(0, i); value = s.subString(i+1); return; } } name = s; value = "";}// Create an Optarg instance from a long flag String like// --longflag=value// (The =value is optional).static voidoptargFromLongOpt(Optarg &o, const String &arg) { String name; String value;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -