[all] Build a single executable

This commit is contained in:
Joe Thornber
2014-08-27 14:01:31 +01:00
parent c1e0799367
commit 6f8b7e2914
48 changed files with 418 additions and 496 deletions

62
base/application.cc Normal file
View File

@@ -0,0 +1,62 @@
#include "base/application.h"
#include <linux/limits.h>
#include <string.h>
using namespace base;
using namespace std;
//----------------------------------------------------------------
int
application::run(int argc, char **argv)
{
string cmd = basename(argv[0]);
if (cmd == string("pdata-tools")) {
argc--;
argv++;
if (!argc) {
usage();
return 1;
}
cmd = argv[0];
}
std::list<command const *>::const_iterator it;
for (it = cmds_.begin(); it != cmds_.end(); ++it) {
if (cmd == (*it)->get_name())
return (*it)->run(argc, argv);
}
std::cerr << "Unknown command '" << cmd << "'\n";
usage();
return 1;
}
void
application::usage()
{
std::cerr << "Usage: <command> <args>\n"
<< "commands:\n";
std::list<command const *>::const_iterator it;
for (it = cmds_.begin(); it != cmds_.end(); ++it) {
std::cerr << " " << (*it)->get_name() << "\n";
}
}
std::string
application::basename(std::string const &path) const
{
char buffer[PATH_MAX + 1];
memset(buffer, 0, sizeof(buffer));
strncpy(buffer, path.c_str(), PATH_MAX);
return ::basename(buffer);
}
//----------------------------------------------------------------

52
base/application.h Normal file
View File

@@ -0,0 +1,52 @@
#ifndef BASE_APPLICATION_H
#define BASE_APPLICATION_H
#include <iostream>
#include <list>
#include <string>
#include <stdexcept>
//----------------------------------------------------------------
namespace base {
class command {
public:
typedef int (*cmd_fn)(int, char **);
command(std::string const &name, cmd_fn fn)
: name_(name),
fn_(fn) {
}
std::string const &get_name() const {
return name_;
}
int run(int argc, char **argv) const {
return fn_(argc, argv);
}
private:
std::string name_;
cmd_fn fn_;
};
class application {
public:
void add_cmd(command const &c) {
cmds_.push_back(&c);
}
int run(int argc, char **argv);
private:
void usage();
std::string basename(std::string const &path) const;
std::list<command const *> cmds_;
};
}
//----------------------------------------------------------------
#endif