metaforce/hecl/driver/ToolBase.hpp

206 lines
4.8 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 <HECLDatabase.hpp>
2015-05-26 04:42:20 +00:00
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string.h>
2015-05-20 05:22:32 +00:00
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;
std::vector<HECL::SystemString> args;
HECL::SystemString output;
2015-06-11 04:55:06 +00:00
HECL::Database::Project* project = NULL;
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;
public:
2015-06-10 02:40:03 +00:00
ToolBase(const ToolPassInfo& info)
2015-05-20 05:22:32 +00:00
: m_info(info) {}
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-05-26 04:42:20 +00:00
#define RED "\033[0;31m"
#define GREEN "\033[0;32m"
#define CYAN "\033[0;36m"
#define BOLD "\033[1m"
#define NORMAL "\033[0m"
#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-06-10 02:40:03 +00:00
it = string.insert(it, WRAP_INDENT, _S(' ')) + WRAP_INDENT;
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-05-26 04:42:20 +00:00
: m_sout(NULL), m_helpFunc(helpFunc)
{
2015-06-10 02:40:03 +00:00
#if _WIN32
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
m_lineWidth = info.dwSize.X;
#else
2015-05-26 04:42:20 +00:00
struct winsize w;
m_lineWidth = 80;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1)
m_lineWidth = w.ws_col;
2015-06-10 02:40:03 +00:00
#endif
2015-05-26 04:42:20 +00:00
if (m_lineWidth < 10)
m_lineWidth = 10;
}
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-05-19 21:01:32 +00:00
#endif // CTOOL_BASE