// Filename: panda_getopt_impl.cxx // Created by: drose (19Jul11) // //////////////////////////////////////////////////////////////////// // // PANDA 3D SOFTWARE // Copyright (c) Carnegie Mellon University. All rights reserved. // // All use of this software is subject to the terms of the revised BSD // license. You should have received a copy of this license along // with this source code in a file named "LICENSE." // //////////////////////////////////////////////////////////////////// #include "panda_getopt_impl.h" #include "pvector.h" #if defined(HAVE_GETOPT) && defined(HAVE_GETOPT_LONG_ONLY) // If the system provides both of these functions, we don't need to // provide our own implementation, so in that case this file does // nothing. #else // If the system does lack one or the other of these functions, then // we'll go ahead and provide it instead. char *optarg = NULL; int optind = 0; int opterr = 1; int optopt = 0; //////////////////////////////////////////////////////////////////// // Class : PandaGetopt // Description : The implementation within this file of the various // getopt() functions. This class is not visible // outside of this file; instead, the interface is via // the getopt() functions themselves. //////////////////////////////////////////////////////////////////// class PandaGetopt { public: PandaGetopt(int argc, char *const argv[], const char *optstring, const struct option *longopts, bool allow_one_hyphen_long); void permute(int argc, char **mutable_argv); int process(int opterr, int *longindex, char *&optarg, int &optind, int &optopt); private: size_t find_short_option(char short_option); size_t find_long_option(const string &long_option); void scan_options(const char *optstring, const struct option *longopts); void scan_args(int argc, char *const argv[]); // We build a list of Options, which correspond to the input defined // in optstring and/or in the longopts list. These are the short // and long options that are available, whether or not the user // tries to use any of them. This list is populated by // scan_options(). class Option { public: Option(char short_option, int has_arg); Option(const struct option *longopts, int longopts_index); char _short_option; string _long_option; int _has_arg; const struct option *_option; int _longopts_index; }; // We next build a list of Params, which are the parameter options // that are parsed out of the argv array--those options that the // user has actually specified. This list does not contain the // non-option arguments, the words that follow the options on the // command line (those end up in the _arguments list instead). This // list is populated by scan_args(). class Param { public: Param(size_t opt_index, size_t argv_index, char short_option, char *argument = NULL); size_t _opt_index; size_t _argv_index; char _short_option; char *_argument; }; // The list of available options. typedef pvector