2014-08-27 18:31:31 +05:30
|
|
|
#include "base/application.h"
|
|
|
|
|
2016-01-08 18:21:52 +05:30
|
|
|
#include <boost/lexical_cast.hpp>
|
2015-01-16 16:18:19 +05:30
|
|
|
#include <libgen.h>
|
2014-08-27 18:31:31 +05:30
|
|
|
#include <linux/limits.h>
|
|
|
|
#include <string.h>
|
2016-01-08 18:21:52 +05:30
|
|
|
#include <stdlib.h>
|
2014-08-27 18:31:31 +05:30
|
|
|
|
|
|
|
using namespace base;
|
2016-01-08 18:21:52 +05:30
|
|
|
using namespace boost;
|
2014-08-27 18:31:31 +05:30
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2016-01-08 18:21:52 +05:30
|
|
|
command::command(string const &name)
|
|
|
|
: name_(name) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
command::die(string const &msg)
|
|
|
|
{
|
|
|
|
cerr << msg << endl;
|
|
|
|
usage(cerr);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2020-03-25 20:28:02 +05:30
|
|
|
::uint64_t
|
2016-01-08 18:21:52 +05:30
|
|
|
command::parse_uint64(string const &str, string const &desc)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
// FIXME: check trailing garbage is handled
|
2020-03-25 20:28:02 +05:30
|
|
|
return lexical_cast<::uint64_t>(str);
|
2016-01-08 18:21:52 +05:30
|
|
|
|
|
|
|
} catch (...) {
|
|
|
|
ostringstream out;
|
|
|
|
out << "Couldn't parse " << desc << ": '" << str << "'";
|
|
|
|
die(out.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0; // never get here
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2014-08-27 18:31:31 +05:30
|
|
|
int
|
|
|
|
application::run(int argc, char **argv)
|
|
|
|
{
|
2015-01-16 16:18:19 +05:30
|
|
|
string cmd = get_basename(argv[0]);
|
2014-08-27 18:31:31 +05:30
|
|
|
|
2021-01-11 12:14:06 +05:30
|
|
|
if (cmd.find("pdata_tools") == 0) {
|
2014-08-27 18:31:31 +05:30
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
|
|
|
|
if (!argc) {
|
|
|
|
usage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = argv[0];
|
|
|
|
}
|
|
|
|
|
2016-01-08 18:21:52 +05:30
|
|
|
std::list<command::ptr>::const_iterator it;
|
2014-08-27 18:31:31 +05:30
|
|
|
for (it = cmds_.begin(); it != cmds_.end(); ++it) {
|
2016-07-11 19:23:03 +05:30
|
|
|
if (cmd == (*it)->get_name()) {
|
|
|
|
try {
|
|
|
|
return (*it)->run(argc, argv);
|
|
|
|
} catch (std::exception const &e) {
|
|
|
|
cerr << e.what() << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2014-08-27 18:31:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << "Unknown command '" << cmd << "'\n";
|
|
|
|
usage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
application::usage()
|
|
|
|
{
|
|
|
|
std::cerr << "Usage: <command> <args>\n"
|
|
|
|
<< "commands:\n";
|
|
|
|
|
2016-01-08 18:21:52 +05:30
|
|
|
std::list<command::ptr>::const_iterator it;
|
2014-08-27 18:31:31 +05:30
|
|
|
for (it = cmds_.begin(); it != cmds_.end(); ++it) {
|
|
|
|
std::cerr << " " << (*it)->get_name() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
2015-01-16 16:18:19 +05:30
|
|
|
application::get_basename(std::string const &path) const
|
2014-08-27 18:31:31 +05:30
|
|
|
{
|
|
|
|
char buffer[PATH_MAX + 1];
|
|
|
|
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
|
|
|
strncpy(buffer, path.c_str(), PATH_MAX);
|
|
|
|
|
|
|
|
return ::basename(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|