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

📄 getopt.cpp

📁 vbindiff-3.0_beta4.tar.gz是一个linux下二进制比较工具
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//--------------------------------------------------------------------// $Id: GetOpt.cpp 4570 2004-07-05 21:57:23Z cjm $//--------------------------------------------------------------------//// Free GetOpt// Copyright 2000 by Christopher J. Madsen//// Process command line arguments//// This program is free software; you can redistribute it and/or// modify it under the terms of the GNU General Public License as// published by the Free Software Foundation; either version 2 of// the License, or (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.//// As a special exception, if you link Free GetOpt with other files to// produce an executable, this does not by itself cause the resulting// executable to be covered by the GNU General Public License.  Your// use of that executable is in no way restricted on account of linking// the Free GetOpt code into it.  However, if you link a modified// version of Free GetOpt to your executable and distribute the// executable, you must make your modifications to Free GetOpt publicly// available as machine-readable source code.//// This exception does not however invalidate any other reasons why// the executable file might be covered by the GNU General Public License.//// This exception applies only to the code released under the name// Free GetOpt.  If you copy code from other programs into a copy of// Free GetOpt, as the General Public License permits, the exception// does not apply to the code that you add in this way.  To avoid// misleading anyone as to the status of such modified files, you must// delete this exception notice from them.//// If you write modifications of your own for Free GetOpt, it is your// choice whether to permit this exception to apply to your modifications.// If you do not wish that, delete this exception notice.//--------------------------------------------------------------------#ifndef GETOPT_NO_STDIO#include <stdio.h>#endif#include <stdlib.h>#include <string.h>#include "GetOpt.hpp"static const char longOptionStart[] = "--";//====================================================================// Standard argument callback functions://// An argument callback function is called when GetOpt finds the// option that specified it.  The usual behavior is to validate the// argument and copy it to the location specified by option->data.// However, the function can do anything it wants.  It should return// true if it found an argument, or false if it did not.//// To report an error, the callback should set getopt->error to true,// or call getopt->reportError, which does that automatically.  It// should then return false.//// Input://   getopt://     The GetOpt object which is calling the function//   option://     The GetOpt::Option which we are processing//   asEntered://     The option as the user entered it//   connected://     The way the argument (if any) was connected to the option://       nextArg:     The next command-line argument (or no argument)//       withEquals:  Connected to the option by an equals sign//       adjacent:    Adjacent to the option (single-char option only)//   argument://     The argument to the option (if any)//     May be NULL, which means there was no argument.//       This is because some callbacks may not care about the//       argument, but may want to do something just because the//       option was found.  connected will be nextArg in this case.//     If connected is withEquals, then argument[-1] is the equals sign.//   usedChars://     Most callback functions can ignore this parameter.  Use it only//     if you want to implement the following behavior.  If usedChars//     is not NULL, then we are processing an argument that was//     adjacent to a single-character option.  If the function would//     like to use only some of the characters in argument and allow//     the rest to be processed as more single-character options, it//     should set *usedChars to the number of characters used.//     *usedChars is always initialized to -1, which means that the//     entire argument was used.//// Return Value://   true:   The argument was processed//   false:  The argument was not used, or an error occurred//// Note://   The standard callbacks insist that an optional argument must be//   connected to the option (ie, they return false if connected is//   nextArg and the argument was not required).  You can change this//   behavior by using your own callbacks instead of the standard ones.////--------------------------------------------------------------------// Process a floating-point argument://// option->data must point to a double.bool GetOpt::isFloat(GetOpt* getopt, const Option* option,                     const char* asEntered,                     Connection connected, const char* argument,                     int* usedChars){  if (!argument ||      ((connected == nextArg) && !(option->flag & GetOpt::needArg)))    return false; // No argument or non-connected optional argument  char*  end;  *reinterpret_cast<double*>(option->data) = strtod(argument, &end);  if (*end) {    getopt->reportError(asEntered, " requires a numeric argument");    return false;  }  return true;} // end GetOpt::isFloat//--------------------------------------------------------------------// Process an integer argument://// option->data must point to a long.bool GetOpt::isLong(GetOpt* getopt, const Option* option,                   const char* asEntered,                   Connection connected, const char* argument,                   int* usedChars){  if (!argument ||      ((connected == nextArg) && !(option->flag & GetOpt::needArg)))    return false; // No argument or non-connected optional argument  char*  end;  *reinterpret_cast<long*>(option->data) = strtol(argument, &end, 0);  if (*end) {    getopt->reportError(asEntered, " requires an integer argument");    return false;  }  return true;} // end GetOpt::isLong//--------------------------------------------------------------------// Process a string argument://// option->data must point to a const char*.bool GetOpt::isString(GetOpt* getopt, const Option* option,                   const char* asEntered,                   Connection connected, const char* argument,                   int* usedChars){  if (!argument ||      ((connected == nextArg) && !(option->flag & GetOpt::needArg)))    return false; // No argument or non-connected optional argument  if (option->data)    *reinterpret_cast<const char**>(option->data) = argument;  return true;} // end GetOpt::isString//====================================================================// Class GetOpt://// Member Variables://   error://     true if an error has occurred during option processing//     false if everything is ok//   errorOutput://     A function which is called to display errors//     Set to GetOpt::printError by the GetOpt constructor.//     If NULL, errors are reported only by setting error to true.//   optionStart://     A string containing the characters that indicate options//     Set to "-" by the GetOpt constructor.//     Must contain '-' if you expect long options to work.//     This applies only to single-character options; "--" is always//     the long option indicator.//     Programs that want to accept DOS-style options should set this//     to "-/".  Note that this string is not disposed of by the//     GetOpt object.  It must continue to exist as long as the GetOpt//     object does.  (Normally, you would set it to point to a string//     literal.)//// Protected Member Variables://   optionList://     The array of GetOpt::Option objects passed to the constructor//   argc, argv://     The parameters passed to main (or similar)//   argi://     The index in argv of the argument currently being processed//   chari://     If non-zero, the index in argv[argi] of the option character//     currently being processed (for single-character option bundles).//   normalOnly://     True means that all arguments yet to be processed are not options.//   returningAll://     Points to the GetOpt::Option that corresponds to normal//     non-option arguments, or NULL if GetOpt is to process only//     options.//   shortOptionBuf://     Used when processing single-character option bundles////--------------------------------------------------------------------// Constructor://// Input://   aList://     An array of GetOpt::Option objects that define the options to//     look for.  This array is not copied, and must continue to exist//     as long as the GetOpt object does.GetOpt::GetOpt(const Option* aList): error(false),#ifdef GETOPT_NO_STDIO  errorOutput(NULL),               // No stdio, can't print errors#else  errorOutput(GetOpt::printError), // Print error messages to stderr#endif  optionStart("-"),  optionList(aList),  argc(0),  argi(0), chari(0),  argv(NULL),  normalOnly(false){  checkReturnAll();} // end GetOpt::GetOpt//--------------------------------------------------------------------// Standard callback function for printing error messages://// You should generally not call this function directly.  Use// GetOpt::reportError to report errors, which calls the errorOutput// function.//// The GetOpt constructor sets errorOutput to GetOpt::printError.//// Input://   option:   The option the user typed//   message:  The error message to display#ifndef GETOPT_NO_STDIOvoid GetOpt::printError(const char* option, const char* message){  fputs(option, stderr);  fputs(message, stderr);  putc('\n', stderr);} // end GetOpt::printError#endif // not GETOPT_NO_STDIO//--------------------------------------------------------------------// Prepare to process a command line://// This function also goes through the option list and sets all found// entries to notFound.//// Input://   theArgc://     The number of elements in theArgv//   theArgv://     The program name & command line arguments.//     theArgv[0] (the program name) is not used and may be NULL.//     This array is not copied, and must exist as long as the GetOpt//     object is in use.void GetOpt::init(int theArgc, const char** theArgv){  argc = theArgc;  argv = theArgv;  argi = chari = 0;  error = normalOnly = false;  const Option* op = optionList;  while (op->shortName || op->longName) {    if (op->found)      *(op->found) = notFound;    ++op;  }} // end GetOpt::init//--------------------------------------------------------------------// Set the returningAll member variable:void GetOpt::checkReturnAll(){  const Option* op = optionList;  while (op->shortName || op->longName) {    if (op->longName && !*(op->longName)) {      returningAll = op;      return;    }    ++op;  }  returningAll = NULL;} // end GetOpt::checkReturnAll//--------------------------------------------------------------------// Determine what Option a long option refers to://// Looks first for an exact match, then for an approximate one.//

⌨️ 快捷键说明

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