/* * mp3plot - Bitrate analysis tool * * Copyright (C) 2007 Toni Corvera * * 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 */ // $Id: main.cc 755 2007-05-26 15:02:28Z $ #include #include #include #include #include #include #include #include #include "mp3_file.h" #include "main.h" #define E_HEADER_READ 70 #define E_BAD_HEADER 71 namespace mp3 = net_outlyer::mp3; namespace po = boost::program_options; void print_signature(std::ostream &); void print_usage(std::ostream &); static inline bool file_exists(const std::string & filename) { std::ifstream ifs(filename.c_str()); ifs.close(); return ! ifs.fail(); } static std::string progname; // Clone of the libc error() () function static inline void error(int status, int errnum, const std::string & msg) { using std::cerr; using std::endl; cerr << progname << ": "; cerr << msg; if (0 != errnum) { cerr << ": " << strerror(errnum); } cerr << endl; if (0 != status) exit(status); } int main(int argc, char * argv[], char * envp[]) { progname = argv[0]; REGISTER_PLOTTERS(); using namespace std; string inputfile; bool image_output; po::options_description desc("Available options"); desc.add_options() ("input,i", po::value(&inputfile), "input file") ("image,g", "output to an image" #ifndef HAVE_MAGICK " (disabled)" #endif ) ("help,h", "show this text") ; // all positional = unnamed args... po::positional_options_description pos; pos.add("input", -1); // ... are equivalent to --input po::variables_map vm; try { po::store(po::command_line_parser(argc, argv) .options(desc) .positional(pos).run(), vm); } catch (po::unknown_option & e) { exit(EX_USAGE); } catch (po::invalid_command_line_syntax & e) { exit(EX_USAGE); } catch (po::multiple_occurrences & e) { exit(EX_USAGE); } po::notify(vm); image_output = vm.count("image"); if (vm.count("help")) { print_signature(cout); print_usage(cout); cout << desc << endl; exit(EX_OK); } if (! vm.count("input")) { print_signature(cerr); cerr << "No input file provided" << endl; print_usage(cerr); cerr << desc; exit(EX_USAGE); } print_signature(cout); if (!file_exists(inputfile)) { cerr << "Failed to read from input file " << inputfile << endl; return EX_NOINPUT; } try { mp3::mp3file file(inputfile); file.read_info(); file.dump_meta_info(); file.plot( (image_output ? mp3::bitrate_plotter::IMAGE : mp3::bitrate_plotter::TEXT ) ); } catch (mp3::e_error & e) { error(e.error_code, 0, e.get_msg().c_str()); } return 0; } inline void print_signature(std::ostream & os) { os << PACKAGE " " VERSION " (c) " COPYRIGHT_YEAR " " AUTHOR " <" UPSTREAM_URL ">\n\n"; } inline void print_usage(std::ostream & os) { os << "Usage: mp3plot " << std::endl; } /* vim:set ts=4 et ai: */