metaforce/hecl/driver/ToolBase.hpp

321 lines
7.9 KiB
C++
Raw Normal View History

2015-05-19 21:01:32 +00:00
#ifndef CTOOL_BASE
#define CTOOL_BASE
2015-05-20 05:22:32 +00:00
#include <string>
#include <vector>
#include <list>
2015-05-26 04:42:20 +00:00
#include <stdio.h>
#include <string.h>
2015-05-20 05:22:32 +00:00
2015-07-22 19:14:50 +00:00
#ifndef _WIN32
#include <unistd.h>
#endif
#include "HECL/Database.hpp"
2015-07-05 06:27:24 +00:00
#include "LogVisor/LogVisor.hpp"
extern LogVisor::LogModule LogModule;
2015-06-10 02:40:03 +00:00
struct ToolPassInfo
2015-05-20 05:22:32 +00:00
{
2015-06-10 02:40:03 +00:00
HECL::SystemString pname;
HECL::SystemString cwd;
2015-10-04 04:35:18 +00:00
std::vector<HECL::SystemString> args;
2015-06-10 02:40:03 +00:00
HECL::SystemString output;
2015-09-30 06:23:07 +00:00
HECL::Database::Project* project = nullptr;
2015-05-20 05:22:32 +00:00
unsigned verbosityLevel = 0;
bool force = false;
};
2015-06-10 02:40:03 +00:00
class ToolBase
{
protected:
2015-06-10 02:40:03 +00:00
const ToolPassInfo& m_info;
2015-07-26 02:52:02 +00:00
bool m_good = false;
public:
2015-06-10 02:40:03 +00:00
ToolBase(const ToolPassInfo& info)
2015-08-06 19:10:12 +00:00
: m_info(info)
{
HECL::VerbosityLevel = info.verbosityLevel;
}
2015-06-10 02:40:03 +00:00
virtual ~ToolBase() {}
virtual HECL::SystemString toolName() const=0;
2015-05-20 05:22:32 +00:00
virtual int run()=0;
2015-07-26 02:52:02 +00:00
inline operator bool() const {return m_good;}
};
2015-05-26 04:42:20 +00:00
#define RED "\033[0;31m"
#define GREEN "\033[0;32m"
2015-07-10 05:28:33 +00:00
#define YELLOW "\033[0;33m"
#define BLUE "\033[0;34m"
#define MAGENTA "\033[0;35m"
2015-05-26 04:42:20 +00:00
#define CYAN "\033[0;36m"
#define BOLD "\033[1m"
#define NORMAL "\033[0m"
2015-07-20 23:27:22 +00:00
#define HIDE_CURSOR "\033[?25l"
#define SHOW_CURSOR "\033[?25h"
2015-05-26 04:42:20 +00:00
#define WRAP_INDENT 4
extern bool XTERM_COLOR;
2015-06-10 02:40:03 +00:00
class HelpOutput
2015-05-26 04:42:20 +00:00
{
public:
2015-06-10 02:40:03 +00:00
typedef void(*HelpFunc)(HelpOutput&);
2015-05-26 04:42:20 +00:00
private:
FILE* m_sout;
2015-06-10 02:40:03 +00:00
HelpFunc m_helpFunc;
2015-05-26 04:42:20 +00:00
int m_lineWidth;
2015-06-10 02:40:03 +00:00
HECL::SystemString m_wrapBuffer;
2015-05-26 04:42:20 +00:00
2015-06-10 02:40:03 +00:00
void _wrapBuf(HECL::SystemString& string)
2015-05-26 04:42:20 +00:00
{
int counter;
2015-06-10 02:40:03 +00:00
HECL::SystemString::iterator it = string.begin();
2015-05-26 04:42:20 +00:00
while (it != string.end())
{
2015-06-10 02:40:03 +00:00
HECL::SystemString::iterator v=it;
2015-05-26 04:42:20 +00:00
/* copy string until the end of the line is reached */
for (counter=WRAP_INDENT ; counter < m_lineWidth ; ++counter)
{
2015-06-10 02:40:03 +00:00
if (*it == _S('\n'))
2015-05-26 04:42:20 +00:00
{
counter = WRAP_INDENT;
++it;
}
if (counter == WRAP_INDENT)
2015-07-12 18:08:20 +00:00
{
for (int i=0 ; i<WRAP_INDENT ; ++i)
it = string.insert(it, _S(' ')) + 1;
}
2015-05-26 04:42:20 +00:00
if (it >= string.end())
return;
2015-06-10 02:40:03 +00:00
if (*it != _S('\n'))
2015-05-26 04:42:20 +00:00
++it;
}
/* check for whitespace */
if (isspace(*it))
{
2015-06-10 02:40:03 +00:00
*it = _S('\n');
2015-05-26 04:42:20 +00:00
counter = WRAP_INDENT;
++it;
}
else
{
/* check for nearest whitespace back in string */
2015-06-10 02:40:03 +00:00
for (HECL::SystemString::iterator k=it ; k!=string.begin() ; --k)
2015-05-26 04:42:20 +00:00
{
if (isspace(*k))
{
counter = WRAP_INDENT;
if (k < v)
{
k = it;
2015-06-10 02:40:03 +00:00
string.insert(k, _S('\n'));
2015-05-26 04:42:20 +00:00
}
else
2015-06-10 02:40:03 +00:00
*k = _S('\n');
2015-05-26 04:42:20 +00:00
it = k + 1;
break;
}
}
}
}
}
public:
2015-06-10 02:40:03 +00:00
HelpOutput(HelpFunc helpFunc)
2015-07-20 23:27:22 +00:00
: m_sout(NULL), m_helpFunc(helpFunc), m_lineWidth(HECL::ConsoleWidth())
{}
2015-05-26 04:42:20 +00:00
void go()
{
2015-06-10 02:40:03 +00:00
#if _WIN32
m_sout = stdout;
m_helpFunc(*this);
#else
2015-05-26 04:42:20 +00:00
m_sout = popen("less -R", "w");
if (m_sout)
{
m_helpFunc(*this);
pclose(m_sout);
}
else
{
m_sout = stdout;
m_helpFunc(*this);
}
2015-06-10 02:40:03 +00:00
#endif
2015-05-26 04:42:20 +00:00
}
2015-06-10 02:40:03 +00:00
void print(const HECL::SystemChar* str)
2015-05-26 04:42:20 +00:00
{
2015-06-10 02:40:03 +00:00
HECL::FPrintf(m_sout, _S("%s"), str);
2015-05-26 04:42:20 +00:00
}
2015-06-10 02:40:03 +00:00
void printBold(const HECL::SystemChar* str)
2015-05-26 04:42:20 +00:00
{
if (XTERM_COLOR)
2015-06-10 02:40:03 +00:00
HECL::FPrintf(m_sout, _S("" BOLD "%s" NORMAL ""), str);
2015-05-26 04:42:20 +00:00
else
2015-06-10 02:40:03 +00:00
HECL::FPrintf(m_sout, _S("%s"), str);
2015-05-26 04:42:20 +00:00
}
2015-06-10 02:40:03 +00:00
void secHead(const HECL::SystemChar* headName)
2015-05-26 04:42:20 +00:00
{
if (XTERM_COLOR)
2015-06-10 02:40:03 +00:00
HECL::FPrintf(m_sout, _S("" BOLD "%s" NORMAL "\n"), headName);
2015-05-26 04:42:20 +00:00
else
2015-06-10 02:40:03 +00:00
HECL::FPrintf(m_sout, _S("%s\n"), headName);
2015-05-26 04:42:20 +00:00
}
2015-06-10 02:40:03 +00:00
void optionHead(const HECL::SystemChar* flag, const HECL::SystemChar* synopsis)
2015-05-26 04:42:20 +00:00
{
if (XTERM_COLOR)
2015-06-10 02:40:03 +00:00
HECL::FPrintf(m_sout, _S("" BOLD "%s" NORMAL " (%s)\n"), flag, synopsis);
2015-05-26 04:42:20 +00:00
else
2015-06-10 02:40:03 +00:00
HECL::FPrintf(m_sout, _S("%s (%s)\n"), flag, synopsis);
2015-05-26 04:42:20 +00:00
}
void beginWrap()
{
m_wrapBuffer.clear();
}
2015-06-10 02:40:03 +00:00
void wrap(const HECL::SystemChar* str)
2015-05-26 04:42:20 +00:00
{
m_wrapBuffer += str;
}
2015-06-10 02:40:03 +00:00
void wrapBold(const HECL::SystemChar* str)
2015-05-26 04:42:20 +00:00
{
2015-06-10 02:40:03 +00:00
if (XTERM_COLOR)
m_wrapBuffer += _S("" BOLD "");
2015-05-26 04:42:20 +00:00
m_wrapBuffer += str;
2015-06-10 02:40:03 +00:00
if (XTERM_COLOR)
m_wrapBuffer += _S("" NORMAL "");
2015-05-26 04:42:20 +00:00
}
void endWrap()
{
_wrapBuf(m_wrapBuffer);
2015-06-10 02:40:03 +00:00
m_wrapBuffer += _S('\n');
HECL::FPrintf(m_sout, _S("%s"), m_wrapBuffer.c_str());
2015-05-26 04:42:20 +00:00
m_wrapBuffer.clear();
}
};
2015-10-02 04:06:45 +00:00
static HECL::SystemString MakePathArgAbsolute(const HECL::SystemString& arg,
const HECL::SystemString& cwd)
{
#if _WIN32
if (arg.size() >= 2 && iswalpha(arg[0]) && arg[1] == _S(':'))
return arg;
if (arg[0] == _S('\\') || arg[0] == _S('/'))
return arg;
return cwd + _S('\\') + arg;
#else
if (arg[0] == _S('/') || arg[0] == _S('\\'))
return arg;
return cwd + _S('/') + arg;
#endif
}
2015-09-30 06:23:07 +00:00
void ToolPrintProgress(const HECL::SystemChar* message, const HECL::SystemChar* submessage,
int lidx, float factor, int& lineIdx)
{
2015-10-04 04:35:18 +00:00
bool blocks = factor >= 0.0;
2015-09-30 06:23:07 +00:00
factor = std::max(0.0f, std::min(1.0f, factor));
int iFactor = factor * 100.0;
if (XTERM_COLOR)
HECL::Printf(_S("" HIDE_CURSOR ""));
if (lidx > lineIdx)
{
HECL::Printf(_S("\n "));
lineIdx = lidx;
}
else
HECL::Printf(_S(" "));
int width = HECL::ConsoleWidth();
2015-10-04 04:35:18 +00:00
int half;
if (blocks)
half = width / 2 - 2;
else
half = width - 4;
2015-09-30 06:23:07 +00:00
if (!message)
message = _S("");
size_t messageLen = HECL::StrLen(message);
if (!submessage)
submessage = _S("");
size_t submessageLen = HECL::StrLen(submessage);
if (half - messageLen < submessageLen-2)
submessageLen = 0;
if (submessageLen)
{
if (messageLen > half-submessageLen-1)
2015-10-07 01:16:54 +00:00
HECL::Printf(_S("%.*s... %s "), half-int(submessageLen)-4, message, submessage);
2015-09-30 06:23:07 +00:00
else
{
HECL::Printf(_S("%s"), message);
for (int i=half-messageLen-submessageLen-1 ; i>=0 ; --i)
HECL::Printf(_S(" "));
HECL::Printf(_S("%s "), submessage);
}
}
else
{
if (messageLen > half)
HECL::Printf(_S("%.*s... "), half-3, message);
else
{
HECL::Printf(_S("%s"), message);
for (int i=half-messageLen ; i>=0 ; --i)
HECL::Printf(_S(" "));
}
}
2015-10-04 04:35:18 +00:00
if (blocks)
2015-09-30 06:23:07 +00:00
{
2015-10-04 04:35:18 +00:00
if (XTERM_COLOR)
{
size_t blocks = half - 7;
size_t filled = blocks * factor;
size_t rem = blocks - filled;
HECL::Printf(_S("" BOLD "%3d%% ["), iFactor);
for (int b=0 ; b<filled ; ++b)
HECL::Printf(_S("#"));
for (int b=0 ; b<rem ; ++b)
HECL::Printf(_S("-"));
HECL::Printf(_S("]" NORMAL ""));
}
else
{
size_t blocks = half - 7;
size_t filled = blocks * factor;
size_t rem = blocks - filled;
HECL::Printf(_S("%3d%% ["), iFactor);
for (int b=0 ; b<filled ; ++b)
HECL::Printf(_S("#"));
for (int b=0 ; b<rem ; ++b)
HECL::Printf(_S("-"));
HECL::Printf(_S("]"));
}
2015-09-30 06:23:07 +00:00
}
HECL::Printf(_S("\r"));
if (XTERM_COLOR)
HECL::Printf(_S("" SHOW_CURSOR ""));
fflush(stdout);
}
2015-05-19 21:01:32 +00:00
#endif // CTOOL_BASE