From 72fac2b901ebf63dcfac537e1849ea8f8e4a7829 Mon Sep 17 00:00:00 2001 From: tchernobog Date: Wed, 25 Jan 2006 17:50:36 +0000 Subject: [PATCH] - Added support for command line option parsing - Creates a vector with filenames to be opened; not actually useful now, it's already in place when it'll be needed. git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@191 3ecf2c5c-341e-0410-92b4-d18e462d057c --- po/POTFILES.in | 3 +- src/Makefile.am | 8 +++- src/main.cc | 50 +++++++++++++------- src/parseopts.cc | 117 +++++++++++++++++++++++++++++++++++++++++++++++ src/parseopts.hh | 40 ++++++++++++++++ 5 files changed, 197 insertions(+), 21 deletions(-) create mode 100644 src/parseopts.cc create mode 100644 src/parseopts.hh diff --git a/po/POTFILES.in b/po/POTFILES.in index 535e849..bfc2bc0 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -1,6 +1,5 @@ # List of source files which contain translatable strings. -# src/pippo.cc -# src/pluto.cc +src/parseopts.cc # ... diff --git a/src/Makefile.am b/src/Makefile.am index eec3a57..39968a9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -48,9 +48,13 @@ sgpemv2_LDADD = $(PYTHON_LDFLAGS) \ sgpemv2_LDFLAGS = $(PYTHON_EXTRA_LDFLAGS) # Please keep this in sorted order: -sgpemv2_SOURCES = main.cc +sgpemv2_SOURCES = \ + main.cc \ + parseopts.cc -noinst_HEADERS = # main.hh ... +noinst_HEADERS = \ + main.hh \ + parseopts.hh # loadable python modules mod_PYTHON = # policy.py diff --git a/src/main.cc b/src/main.cc index e438960..adedaf0 100644 --- a/src/main.cc +++ b/src/main.cc @@ -21,31 +21,47 @@ #include "config.h" #include "gettext.h" +#include "parseopts.hh" + +#include +#include #include +#include +#include +#include -// Static functions declarations: -static void print_license(); -int main(int argc, char* argv[]) { +// Functor to be used by for_each() +struct create_fnames_vect : public std::unary_function +{ + typedef std::vector& vect_type; +public: + create_fnames_vect(vect_type v) : _v(v) {} + void operator()(const char* str) {_v.push_back(str);} +private: + vect_type _v; +}; + + +int +main(int argc, char* argv[]) +{ + using namespace std; + // Set up gettext support setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); - // Print out license informations - print_license(); + // Parses options and prepares vector with + // filenames of documents to be opened + vector filenames; + { + int a_count = argc; + char** a_ptr = argv; + parse_options(a_count, a_ptr); + for_each(a_ptr, a_ptr+a_count, create_fnames_vect(filenames)); + } return 0; } - -void print_license() { - // Do _NOT_ translate this text. - std::cerr << - "SGPEMv2, Copyright (C) 2005, 2006 University of Padova,\n" - " dept. of Pure and Applied Mathematics.\n" - "SGPEMv2 comes with ABSOLUTELY NO WARRANTY. This is free \n" - "software, and you are welcome to redistribute it under \n" - "the terms of the GNU General Public License; for details\n" - "see file COPYING contained in the source package. \n" - << std::endl; -} diff --git a/src/parseopts.cc b/src/parseopts.cc new file mode 100644 index 0000000..4c5eb25 --- /dev/null +++ b/src/parseopts.cc @@ -0,0 +1,117 @@ +// src/parseopts.cc - Copyright 2005, 2006, University +// of Padova, dept. of Pure and Applied +// Mathematics +// +// This file is part of SGPEMv2. +// +// This 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. +// +// SGPEMv2 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 SGPEMv2; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include "config.h" +#include "gettext.h" + +#include "main.hh" +#include "parseopts.hh" + +#ifdef _GNU_SOURCE +#include +#endif + +#include +#include +#include +#include + +// Static declarations: +static void display_help(); + +void +parse_options(int& argc, char**& argv) +{ + print_license(); + + static const char* short_options = "nh"; + + #ifdef _GNU_SOURCE + // Initialize the array for GNU long options + static struct option long_options[] = + { + {"no-gui", no_argument, NULL, 'n' }, + {"help", no_argument, NULL, 'h' }, + }; + int option_index = 0; + #endif + + int opt; + do + { + #ifdef _GNU_SOURCE + opt = getopt_long(argc, argv, short_options, + long_options, &option_index); + #else + opt = getopt(argc, argv, short_options); + #endif + + switch(opt) + { + case -1: + // We have finished normally + break; + case 'n' : + // We don't return to main, instead we + // initialize the command line version + // of sgpemv2 (?) + + // FIXME : to be written! + break; + case 'h' : + default : + display_help(); + } + } while( opt != -1 ); + + argc -= optind; + argv += optind; +} + + +void +display_help() { + printf( _("SGPEMv2 is an educational software acting as a process scheduling simulator\n" + "\n\nUsage : stradivari [options]" // filenames" + "\n\nOptions:\n" + "\t-h, --help this help you're reading\n" + "\t-n, --no-gui starts the program in command line mode\n" + // "\nFilenames:\n" + // "\t a list of any valid SGPEMv2 XML file\n" + // "\t to be opened, space-separated.\n" + "\nLong options are available only on GNU systems.\n\n" ) ); + exit(0); +} + + +void +print_license() +{ + // Do _NOT_ translate this text. + std::cerr << + "SGPEMv2, Copyright (C) 2005, 2006 University of Padova,\n" + " dept. of Pure and Applied Mathematics.\n" + "SGPEMv2 comes with ABSOLUTELY NO WARRANTY. This is free \n" + "software, and you are welcome to redistribute it under \n" + "the terms of the GNU General Public License; for details\n" + "see file COPYING contained in the source package. \n" + << std::endl; +} + diff --git a/src/parseopts.hh b/src/parseopts.hh new file mode 100644 index 0000000..4870b4d --- /dev/null +++ b/src/parseopts.hh @@ -0,0 +1,40 @@ +// src/parseopts.hh - Copyright 2005, 2006, University +// of Padova, dept. of Pure and Applied +// Mathematics +// +// This file is part of SGPEMv2. +// +// This 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. +// +// SGPEMv2 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 SGPEMv2; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef PARSEOPTS_HH +#define PARSEOPTS_HH 1 + +//! Parses command line options +/** Parses command line options and sets argv to the first filename + * given to the cmdline. + * If no filename is given, in the end argc will be 0 + * Else argc will contain the length of the remaining argv[]. + * If you still need the initial values of the global argc and argv + * passed to the main routine, remember to store them somewhere before + * calling this function. + * \param argc The number of elements of the argv array + * \param argv The array of command line parameter strings + */ +void parse_options(int& argc, char**& argv); + +//! Prints license notice text to stderr +void print_license(); + +#endif