//
// aegis - project change supervisor
// Copyright (C) 2014 Peter Miller
//
// 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 3 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, see .
//
#include
#include
#include
#include
#include
#include
#include
struct table_t
{
const char *name;
bool (*candidate)(const output::pointer &ofp);
oformat::pointer (*create)(const output::pointer &ofp, time_t when);
};
static const table_t table[] =
{
{ "aedist", &oformat_cpio::candidate, &oformat_cpio::create },
{ "cpio", &oformat_cpio::candidate, &oformat_cpio::create },
// { "aeexport", &oformat_git_fast::candidate, &oformat_git_fast::create },
// { "git-fast-import",
// &oformat_git_fast::candidate, &oformat_git_fast::create },
// { "patch", &oformat_patch::candidate, &oformat_patch::create },
// { "diff", &oformat_patch::candidate, &oformat_patch::create },
// { "aepatch", &oformat_patch::candidate, &oformat_patch::create },
// { "revml", &oformat_revml::candidate, &oformat_revml::create },
// { "aerevml", &oformat_revml::candidate, &oformat_revml::create },
// { "tailor", &oformat_tailor::candidate, &oformat_tailor::create },
// { "aetailor", &oformat_tailor::candidate, &oformat_tailor::create },
// { "tar", &oformat_tar::candidate, &oformat_tar::create },
// { "aetar", &oformat_tar::candidate, &oformat_tar::create },
};
oformat::pointer
oformat::factory(const char *name, const output::pointer &ofp, time_t when)
{
if (!name)
name = "";
nstring name_lc = nstring(name).downcase();
if (name_lc == "list" || name_lc == "help")
{
for (const table_t *tp = table; tp < ENDOF(table); ++tp)
{
printf("%s\n", tp->name);
while ((tp + 1) < ENDOF(table))
{
// suppress duplicates
if (tp->create == tp[1].create)
++tp;
}
exit(EXIT_SUCCESS);
}
}
//
// First we attempt to choose the format based on argv[0], or a name
// given to the --output-format=name option. This means the old
// programs work, but exerything is directed into aedist(1).
//
if (!name_lc.empty())
{
for (const table_t *tp = table; tp < ENDOF(table); ++tp)
{
if (name_lc == nstring(tp->name))
{
return tp->create(ofp, when);
}
}
double best_weight = 0.6;
const table_t *best_tp = 0;
for (const table_t *tp = table; tp < ENDOF(table); ++tp)
{
double w = fstrcmp(name_lc.c_str(), tp->name);
if (w > best_weight)
{
best_weight = w;
best_tp = tp;
}
}
if (best_tp)
{
error_raw
(
"unable to determine container type of --output-format=%s "
"option, did you mean %s instead?",
nstring(name).quote_c().c_str(),
nstring(best_tp->name).quote_c().c_str()
);
}
}
//
// Guess what to do based on the output file name.
//
for (const table_t *tp = table; tp < ENDOF(table); ++tp)
{
if (tp->candidate(ofp))
{
error_raw
(
"using container type of --output-format=%s",
nstring(tp->name).quote_c().c_str()
);
return tp->create(ofp, when);
}
}
error_raw
(
"unable to determine container type of %s, "
"defaulting to --output-format=aedist",
ofp->filename().quote_c().c_str()
);
// Default to the historical format.
return oformat_cpio::create(ofp, when);
}
// vim: set ts=8 sw=4 et :