2015-12-12 06:49:31 +00:00
|
|
|
#include"linecount.h"
|
2015-12-12 08:44:11 +00:00
|
|
|
|
2015-12-12 06:49:31 +00:00
|
|
|
#ifdef _WIN32
|
2015-12-12 08:44:11 +00:00
|
|
|
|
2015-12-12 06:49:31 +00:00
|
|
|
#include<tchar.h>
|
2015-12-12 08:44:11 +00:00
|
|
|
|
2015-12-12 06:49:31 +00:00
|
|
|
#else
|
2015-12-12 08:44:11 +00:00
|
|
|
|
|
|
|
#include<stdlib.h>
|
2015-12-12 06:49:31 +00:00
|
|
|
#define _tprintf printf
|
|
|
|
#define _ftprintf fprintf
|
|
|
|
#define _tcscmp strcmp
|
2015-12-12 08:44:11 +00:00
|
|
|
#define _tcslen strlen
|
2015-12-12 06:49:31 +00:00
|
|
|
#define _ttoi atoi
|
|
|
|
#define _tcstoui64 strtoull
|
|
|
|
#define _T(x) x
|
2015-12-12 08:44:11 +00:00
|
|
|
#define TCHAR char
|
|
|
|
|
2015-12-12 06:49:31 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace LineCount;
|
|
|
|
|
2015-12-12 08:44:11 +00:00
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
void help(const TCHAR *argv0)
|
|
|
|
{
|
|
|
|
_ftprintf(stderr, _T("usage: %s [options] <file>\n"), argv0);
|
|
|
|
_ftprintf(stderr, _T(" -h --help print this usage and exit\n"));
|
|
|
|
_ftprintf(stderr, _T(" -b --buffersize <BUFFERSIZE> size of buffer per-thread to use when reading (default is 1MB)\n"));
|
|
|
|
_ftprintf(stderr, _T(" -t --threadcount <THREADCOUNT> number of threads to use (defaults to number of cpu cores)\n"));
|
|
|
|
_ftprintf(stderr, _T(" -v --version print version information and exit\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void version(void)
|
|
|
|
{
|
|
|
|
_tprintf(_T("lc (linecount) %d.%2.2d\nCopyright (c) 2015 Christien Rioux\n"), LINECOUNT_VERSION_MAJOR, LINECOUNT_VERSION_MINOR);
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2015-12-12 06:49:31 +00:00
|
|
|
#if defined(WIN32) && defined(_UNICODE)
|
|
|
|
int wmain(int argc, TCHAR **argv)
|
|
|
|
#else
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
// Parse parameters
|
|
|
|
int arg = 1;
|
|
|
|
int posparam = 0;
|
|
|
|
|
|
|
|
CLineCount::PARAMETERS params;
|
2015-12-12 08:44:11 +00:00
|
|
|
params.buffersize = -1;
|
|
|
|
params.threadcount = -1;
|
2015-12-12 06:49:31 +00:00
|
|
|
|
|
|
|
TCHAR *filename = NULL;
|
|
|
|
|
2015-12-12 08:44:11 +00:00
|
|
|
if(argc==1)
|
|
|
|
{
|
|
|
|
help(argv[0]);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2015-12-12 06:49:31 +00:00
|
|
|
while (arg < argc)
|
|
|
|
{
|
2015-12-12 08:44:11 +00:00
|
|
|
if (_tcscmp(argv[arg], _T("-h")) == 0 || _tcscmp(argv[arg], _T("--help")) == 0)
|
|
|
|
{
|
|
|
|
help(argv[0]);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
else if (_tcscmp(argv[arg], _T("-v")) == 0 || _tcscmp(argv[arg], _T("--version")) == 0)
|
|
|
|
{
|
|
|
|
version();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
else if (_tcscmp(argv[arg], _T("-b")) == 0 || _tcscmp(argv[arg], _T("--buffersize")) == 0)
|
2015-12-12 06:49:31 +00:00
|
|
|
{
|
|
|
|
arg++;
|
|
|
|
if (arg == argc)
|
|
|
|
{
|
2015-12-12 08:44:11 +00:00
|
|
|
_ftprintf(stderr, _T("%s: missing argument to %s\n"), argv[0], argv[arg-1]);
|
2015-12-12 06:49:31 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-12-12 08:44:11 +00:00
|
|
|
TCHAR *wsstr = argv[arg];
|
2015-12-12 06:49:31 +00:00
|
|
|
|
|
|
|
// Check for size multipliers
|
|
|
|
size_t multiplier = 1;
|
2015-12-12 08:44:11 +00:00
|
|
|
TCHAR *lastchar = wsstr + (_tcslen(wsstr) - 1);
|
2015-12-12 06:49:31 +00:00
|
|
|
if (*lastchar == _T('k') || *lastchar == _T('K'))
|
|
|
|
{
|
|
|
|
multiplier = 1024;
|
|
|
|
lastchar = 0;
|
|
|
|
}
|
|
|
|
else if (*lastchar == _T('m') || *lastchar == _T('M'))
|
|
|
|
{
|
|
|
|
multiplier = 1024 * 1024;
|
|
|
|
lastchar = 0;
|
|
|
|
}
|
|
|
|
else if (*lastchar == _T('g') || *lastchar == _T('G'))
|
|
|
|
{
|
|
|
|
multiplier = 1024 * 1024 * 1024;
|
|
|
|
lastchar = 0;
|
|
|
|
}
|
|
|
|
|
2015-12-12 08:44:11 +00:00
|
|
|
TCHAR *endptr;
|
|
|
|
params.buffersize = ((size_t)_tcstoui64(argv[arg], &endptr, 10)) * multiplier;
|
2015-12-12 06:49:31 +00:00
|
|
|
|
|
|
|
}
|
2015-12-12 08:44:11 +00:00
|
|
|
else if (_tcscmp(argv[arg], _T("-t")) == 0 || _tcscmp(argv[arg], _T("--threadcount")) == 0)
|
2015-12-12 06:49:31 +00:00
|
|
|
{
|
|
|
|
arg++;
|
|
|
|
if (arg == argc)
|
|
|
|
{
|
2015-12-12 08:44:11 +00:00
|
|
|
_ftprintf(stderr, _T("%s: Missing argument to %s\n"), argv[0], argv[arg-1]);
|
2015-12-12 06:49:31 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-12-12 08:44:11 +00:00
|
|
|
params.threadcount = _ttoi(argv[arg]);
|
|
|
|
if(params.threadcount<=0)
|
|
|
|
{
|
|
|
|
_ftprintf(stderr, _T("%s: Invalid thread count\n"), argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
2015-12-12 06:49:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (posparam == 0)
|
|
|
|
{
|
|
|
|
filename = argv[arg];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-12 08:44:11 +00:00
|
|
|
_ftprintf(stderr, _T("%s: Too many arguments\n"), argv[0]);
|
2015-12-12 06:49:31 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
posparam++;
|
|
|
|
}
|
|
|
|
|
|
|
|
arg++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (posparam != 1)
|
|
|
|
{
|
2015-12-12 08:44:11 +00:00
|
|
|
_ftprintf(stderr, _T("%s: Missing required argument\n"), argv[0]);
|
2015-12-12 06:49:31 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create line count class
|
|
|
|
CLineCount lc(¶ms);
|
|
|
|
|
|
|
|
if (!lc.open(filename))
|
|
|
|
{
|
|
|
|
LCERROR err = lc.lastError();
|
|
|
|
LCSTRING errstr = lc.lastErrorString();
|
|
|
|
|
2015-12-12 08:44:11 +00:00
|
|
|
_ftprintf(stderr, _T("%s: Error %d (%s)\n"), argv[0], err, errstr.c_str());
|
2015-12-12 06:49:31 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count lines
|
|
|
|
LCLINECOUNT count;
|
|
|
|
if (!lc.countLines(count))
|
|
|
|
{
|
|
|
|
LCERROR err = lc.lastError();
|
|
|
|
LCSTRING errstr = lc.lastErrorString();
|
|
|
|
|
2015-12-12 08:44:11 +00:00
|
|
|
_ftprintf(stderr, _T("%s: Error %d: (%s)\n"), argv[0], err, errstr.c_str());
|
2015-12-12 06:49:31 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display output
|
|
|
|
_tprintf(_T(LCLINECOUNTFMT _T("\n")), count);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|