include/trace-tools/tool/tt_options.hxx

00001 /*
00002  * trace-tools - A library and a set of tools to manipulate wireless traces.
00003  * Copyright (C) 2007  Universite Pierre et Marie Curie - Paris 6
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00018  * MA  02110-1301  USA
00019  *
00020  * Author: Thomas Claveirole <thomas.claveirole@lip6.fr>
00021  */
00022 #ifndef TOOL_TT_OPTIONS_HXX_
00023 # define TOOL_TT_OPTIONS_HXX_
00024 
00025 # include <cassert>
00026 # include <unistd.h>
00027 
00028 # include "tt_options.hh"
00029 
00030 namespace tool
00031 {
00032 
00033   namespace opt
00034   {
00035 
00036     namespace internals
00037     {
00038 
00039       void
00040       usage(const std::string& progname,
00041             const std::string& available_options,
00042             unsigned ninputs, unsigned noutputs)
00043       {
00044         std::cerr << "Invocation:\n\t" << progname << " [options]";
00045         switch (ninputs)
00046           {
00047           case 0:                                       break;
00048           case 1:  std::cerr << " <input>";             break;
00049           case 2:  std::cerr << " <input-1> <input-2>"; break;
00050           default: std::cerr << " <inputs...>";         break;
00051           }
00052         switch (noutputs)
00053           {
00054           case 0:                                       break;
00055           case 1:  std::cerr << " <output>";            break;
00056           default: std::cerr << " <outputs...>";        break;
00057           }
00058         std::cerr << "\n\nOptions:\n";
00059 
00060 # define tt_help_print_(OptName) OptName
00061 # define tt_help_print(OptName)                                         \
00062         if (available_options.find(tt_opt_  ## OptName) <               \
00063             available_options.size())                                   \
00064           std::cerr << tt_help_print_(tt_help_ ## OptName)
00065 
00066         tt_help_print(802cmp);
00067         tt_help_print(attr);
00068         tt_help_print(bytescmp);
00069         tt_help_print(colheads);
00070         tt_help_print(defaultcmp);
00071         tt_help_print(help);
00072         tt_help_print(mapping);
00073         tt_help_print(endianness);
00074         tt_help_print(prism_noise);
00075         tt_help_print(quiet);
00076         tt_help_print(usec);
00077         tt_help_print(timecmp);
00078         tt_help_print(version);
00079 
00080 # undef tt_help_print
00081 
00082         if (available_options.find(tt_opt_attr) < available_options.size())
00083           std::cerr << "\nAvailable attributes:\n"
00084             "  " tt_attr_dst_tmp "\n"
00085             "  " tt_attr_src_tmp "\n"
00086             "  " tt_attr_bss_tmp "\n"
00087             "  " tt_attr_src_bss_tmp "\n"
00088             "  " tt_attr_seq_bss_tmp "\n"
00089             "  " tt_attr_seq_dst_bss_tmp "\n";
00090       }
00091 
00092       void
00093       version(const std::string& progname)
00094       {
00095         std::cerr << progname << "\n"
00096           "Trace Tools, version " << PACKAGE_VERSION << '.' << std::endl;
00097       }
00098 
00099     }
00100 
00101     inline
00102     options
00103     parse_args(int argc, char* const* argv,
00104                const std::string& available_options,
00105                int ninputs, int noutputs)
00106     {
00107       const std::string optstring =
00108         available_options + tt_opt_help tt_opt_version;
00109 
00110       options   r;
00111       int       o;
00112 
00113       r.mapping = tt_mapping_default;
00114       r.attributes = tt_attr_default;
00115       r.endianness = endian::guess;
00116 
00117       r.cmp = options::cmp_default;
00118       r.prism_noise_means_error = false;
00119       r.print_errors = false;
00120       r.print_frame_indices = true;
00121       r.print_column_headers = isatty(STDOUT_FILENO);
00122       r.print_microseconds = false;
00123       r.print_summary = true;
00124 
00125       while ((o = getopt(argc, argv, optstring.c_str())) != -1)
00126         switch (o)
00127           {
00128           case '8':
00129             r.cmp = options::cmp_802;
00130             break;
00131 
00132           case 'a':
00133             r.attributes = optarg;
00134             break;
00135 
00136           case 'b':
00137             r.cmp = options::cmp_bytes;
00138             break;
00139 
00140           case 'c':
00141             r.print_column_headers = false;
00142             break;
00143 
00144           case 'C':
00145             r.print_column_headers = true;
00146             break;
00147 
00148           case 'd':
00149             r.cmp = options::cmp_default;
00150             break;
00151 
00152           case 'e':
00153             r.print_errors = false;
00154             break;
00155 
00156           case 'E':
00157             r.print_errors = true;
00158             break;
00159 
00160           case 'h':
00161             internals::usage(argv[0], optstring, ninputs, noutputs);
00162             exit(EXIT_SUCCESS);
00163             break;
00164 
00165           case 'i':
00166             r.print_frame_indices = false;
00167             break;
00168 
00169           case 'I':
00170             r.print_frame_indices = true;
00171             break;
00172 
00173           case 'm':
00174             r.mapping = optarg;
00175             break;
00176 
00177           case 'n':
00178             r.endianness = endian::little;
00179             break;
00180 
00181           case 'N':
00182             r.endianness = endian::big;
00183             break;
00184 
00185           case 'p':
00186             r.prism_noise_means_error = false;
00187             break;
00188 
00189           case 'P':
00190             r.prism_noise_means_error = true;
00191             break;
00192 
00193           case 'q':
00194             r.print_errors = false;
00195             r.print_frame_indices = false;
00196             r.print_column_headers = false;
00197             r.print_microseconds = false;
00198             r.print_summary = false;
00199             break;
00200 
00201           case 't':
00202             r.cmp = options::cmp_time;
00203             break;
00204 
00205           case 'u':
00206             r.print_microseconds = false;
00207             break;
00208 
00209           case 'U':
00210               r.print_microseconds = true;
00211               break;
00212 
00213           case 'v':
00214             internals::version(argv[0]);
00215             exit(EXIT_SUCCESS);
00216 
00217           default: /* '?' */
00218             internals::usage(argv[0], optstring, ninputs, noutputs);
00219             exit(EXIT_FAILURE);
00220           }
00221 
00222       assert(ninputs  != options::arbitrary_number_of_inputs or
00223              noutputs != options::arbitrary_number_of_outputs);
00224 
00225       if (ninputs == options::arbitrary_number_of_inputs)
00226         ninputs = argc - optind - noutputs;
00227       if (noutputs == options::arbitrary_number_of_outputs)
00228         noutputs = argc - optind - ninputs;
00229       if (ninputs < 0 or noutputs < 0 or optind + ninputs + noutputs != argc)
00230         {
00231           std::cerr << "ERROR: Wrong number of arguments." << std::endl;
00232           internals::usage(argv[0], optstring, ninputs, noutputs);
00233           exit(EXIT_FAILURE);
00234         }
00235       for (int i = 0; i < ninputs; ++i)
00236         r.input.push_back(argv[optind + i]);
00237       for (int i = 0; i < noutputs; ++i)
00238         r.output.push_back(argv[optind + ninputs + i]);
00239 
00240       return r;
00241     }
00242 
00243   } // End of namespace tool::opt.
00244 
00245 } // End of namespace tool.
00246 
00247 #endif // TOOL_TT_OPTIONS_HXX_

Generated on Tue Nov 13 13:48:01 2007 for trace-tools by  doxygen 1.5.3-20071008