metaforce/hecl/driver/main.cpp

163 lines
4.6 KiB
C++
Raw Normal View History

2015-05-15 22:39:43 +00:00
#include <stdio.h>
#include <string.h>
2015-05-20 05:22:32 +00:00
#include <regex>
#include <stdexcept>
2015-05-17 04:55:29 +00:00
#include <HECLDatabase.hpp>
2015-05-19 21:01:32 +00:00
#include "CToolBase.hpp"
#include "CToolInit.hpp"
#include "CToolAdd.hpp"
2015-05-20 05:22:32 +00:00
#include "CToolRemove.hpp"
#include "CToolGroup.hpp"
#include "CToolCook.hpp"
#include "CToolClean.hpp"
#include "CToolPackage.hpp"
#include "CToolHelp.hpp"
2015-05-15 22:39:43 +00:00
2015-05-20 05:22:32 +00:00
/* Main usage message */
static void printHelp(const char* pname)
2015-05-15 22:39:43 +00:00
{
2015-05-20 05:22:32 +00:00
#if HECL_GIT
printf("HECL Commit " #HECL_GIT " (" #HECL_BRANCH ")\n"
"Usage: %s init|add|remove|group|cook|clean|package|help\n", pname);
#elif HECL_VER
printf("HECL Version " #HECL_VER "\n"
"Usage: %s init|add|remove|group|cook|clean|package|help\n", pname);
#else
printf("HECL\n"
"Usage: %s init|add|remove|group|cook|clean|package|help\n", pname);
#endif
}
/* Regex patterns */
static const std::regex regOPEN("-o\\s*(\\S+)", std::regex::ECMAScript|std::regex::optimize);
static const std::regex regVERBOSE("-v(v*)", std::regex::ECMAScript|std::regex::optimize);
static const std::regex regFORCE("-f", std::regex::ECMAScript|std::regex::optimize);
2015-05-21 02:33:05 +00:00
static const std::regex regNOWS("\\S+", std::regex::ECMAScript|std::regex::optimize);
2015-05-20 05:22:32 +00:00
/* Iterates string segments around matched arguments and
* filters args string accordingly */
static void whiddleArgs(std::string& args, const std::regex& regex)
{
std::string remArgs;
for (std::sregex_token_iterator it(args.begin(), args.end(), regex, -1);
it != std::sregex_token_iterator() ; ++it)
{
const std::string& str = *it;
remArgs += str;
}
args = remArgs;
}
int main(int argc, const char** argv)
{
2015-05-20 05:22:32 +00:00
/* Basic usage check */
if (argc == 1)
{
printHelp(argv[0]);
return 0;
}
else if (argc == 0)
{
printHelp("hecl");
return 0;
}
2015-05-20 05:22:32 +00:00
/* Assemble common tool pass info */
SToolPassInfo info;
info.pname = argv[0];
2015-05-20 05:22:32 +00:00
/* Concatenate args */
std::string args;
for (int i=2 ; i<argc ; ++i)
args += std::string(argv[i]) + " ";
if (!args.empty())
{
/* Extract output argument */
std::sregex_token_iterator openIt(args.begin(), args.end(), regOPEN, 1);
if (openIt != std::sregex_token_iterator())
{
if (info.output.empty())
info.output = *openIt;
whiddleArgs(args, regOPEN);
}
/* Count verbosity */
for (std::sregex_token_iterator it(args.begin(), args.end(), regVERBOSE, 1);
it != std::sregex_token_iterator() ; ++it)
{
const std::string& str = *it;
++info.verbosityLevel;
info.verbosityLevel += str.length();
}
whiddleArgs(args, regVERBOSE);
/* Check force argument */
if (std::regex_search(args, regFORCE))
{
info.force = true;
whiddleArgs(args, regFORCE);
}
/* Gather remaining args */
2015-05-21 02:33:05 +00:00
for (std::sregex_token_iterator it(args.begin(), args.end(), regNOWS);
2015-05-20 05:22:32 +00:00
it != std::sregex_token_iterator() ; ++it)
{
const std::string& str = *it;
info.args.push_back(str);
}
}
/* Construct selected tool */
std::string toolName(argv[1]);
std::transform(toolName.begin(), toolName.end(), toolName.begin(), tolower);
CToolBase* tool = NULL;
try
{
if (toolName == "init")
tool = new CToolInit(info);
else if (toolName == "add")
tool = new CToolAdd(info);
else if (toolName == "remove" || toolName == "rm")
tool = new CToolRemove(info);
else if (toolName == "group")
tool = new CToolGroup(info);
else if (toolName == "cook")
tool = new CToolCook(info);
else if (toolName == "clean")
tool = new CToolClean(info);
else if (toolName == "package")
tool = new CToolPackage(info);
else if (toolName == "help")
tool = new CToolHelp(info);
else
throw std::invalid_argument("unrecognized tool '" + toolName + "'");
}
catch (std::exception& ex)
{
fprintf(stderr, "Unable to construct HECL tool '%s':\n%s\n", toolName.c_str(), ex.what());
delete tool;
return -1;
}
if (info.verbosityLevel)
printf("Constructed tool '%s' %d\n", tool->toolName().c_str(), info.verbosityLevel);
/* Run tool */
int retval;
try
{
retval = tool->run();
}
catch (std::exception& ex)
{
fprintf(stderr, "Error running HECL tool '%s':\n%s\n", toolName.c_str(), ex.what());
delete tool;
return -1;
}
2015-05-20 05:22:32 +00:00
delete tool;
return retval;
2015-05-15 22:39:43 +00:00
}