mirror of
https://github.com/crioux/turbo-linecount.git
synced 2026-03-02 03:40:07 +00:00
refactor
This commit is contained in:
186
src/main.cpp
Normal file
186
src/main.cpp
Normal file
@@ -0,0 +1,186 @@
|
||||
//
|
||||
// Turbo Linecount
|
||||
// Copyright 2015, Christien Rioux
|
||||
//
|
||||
// MIT Licensed, see file 'LICENSE' for details
|
||||
//
|
||||
///////////////////////////////////////////////
|
||||
|
||||
#include"turbo_linecount.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
#include<tchar.h>
|
||||
|
||||
#else
|
||||
|
||||
#include<stdlib.h>
|
||||
#define _tprintf printf
|
||||
#define _ftprintf fprintf
|
||||
#define _tcscmp strcmp
|
||||
#define _tcslen strlen
|
||||
#define _ttoi atoi
|
||||
#define _tcstoui64 strtoull
|
||||
#define _T(x) x
|
||||
#define TCHAR char
|
||||
|
||||
#endif
|
||||
|
||||
using namespace TURBO_LINECOUNT;
|
||||
|
||||
//////////////////////////////////////////////////////
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////
|
||||
|
||||
|
||||
#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;
|
||||
params.buffersize = -1;
|
||||
params.threadcount = -1;
|
||||
|
||||
TCHAR *filename = NULL;
|
||||
|
||||
if(argc==1)
|
||||
{
|
||||
help(argv[0]);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
while (arg < argc)
|
||||
{
|
||||
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)
|
||||
{
|
||||
arg++;
|
||||
if (arg == argc)
|
||||
{
|
||||
_ftprintf(stderr, _T("%s: missing argument to %s\n"), argv[0], argv[arg-1]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TCHAR *wsstr = argv[arg];
|
||||
|
||||
// Check for size multipliers
|
||||
size_t multiplier = 1;
|
||||
TCHAR *lastchar = wsstr + (_tcslen(wsstr) - 1);
|
||||
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;
|
||||
}
|
||||
|
||||
TCHAR *endptr;
|
||||
params.buffersize = ((size_t)_tcstoui64(argv[arg], &endptr, 10)) * multiplier;
|
||||
|
||||
}
|
||||
else if (_tcscmp(argv[arg], _T("-t")) == 0 || _tcscmp(argv[arg], _T("--threadcount")) == 0)
|
||||
{
|
||||
arg++;
|
||||
if (arg == argc)
|
||||
{
|
||||
_ftprintf(stderr, _T("%s: Missing argument to %s\n"), argv[0], argv[arg-1]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
params.threadcount = _ttoi(argv[arg]);
|
||||
if(params.threadcount<=0)
|
||||
{
|
||||
_ftprintf(stderr, _T("%s: Invalid thread count\n"), argv[0]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (posparam == 0)
|
||||
{
|
||||
filename = argv[arg];
|
||||
}
|
||||
else
|
||||
{
|
||||
_ftprintf(stderr, _T("%s: Too many arguments\n"), argv[0]);
|
||||
return 1;
|
||||
}
|
||||
posparam++;
|
||||
}
|
||||
|
||||
arg++;
|
||||
}
|
||||
|
||||
if (posparam != 1)
|
||||
{
|
||||
_ftprintf(stderr, _T("%s: Missing required argument\n"), argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Create line count class
|
||||
CLineCount lc(¶ms);
|
||||
|
||||
if (!lc.open(filename))
|
||||
{
|
||||
errno_t err = lc.lastError();
|
||||
LCSTRING errstr = lc.lastErrorString();
|
||||
|
||||
_ftprintf(stderr, _T("%s: Error %d (%s)\n"), argv[0], err, errstr.c_str());
|
||||
return err;
|
||||
}
|
||||
|
||||
// Count lines
|
||||
LCLINECOUNT count;
|
||||
if (!lc.countLines(count))
|
||||
{
|
||||
errno_t err = lc.lastError();
|
||||
LCSTRING errstr = lc.lastErrorString();
|
||||
|
||||
_ftprintf(stderr, _T("%s: Error %d: (%s)\n"), argv[0], err, errstr.c_str());
|
||||
return err;
|
||||
}
|
||||
|
||||
// Display output
|
||||
_tprintf(_T(LCLINECOUNTFMT _T("\n")), count);
|
||||
|
||||
return 0;
|
||||
}
|
||||
589
src/turbo_linecount.cpp
Normal file
589
src/turbo_linecount.cpp
Normal file
@@ -0,0 +1,589 @@
|
||||
//
|
||||
// Turbo Linecount
|
||||
// Copyright 2015, Christien Rioux
|
||||
//
|
||||
// MIT Licensed, see file 'LICENSE' for details
|
||||
//
|
||||
///////////////////////////////////////////////
|
||||
|
||||
#include"turbo_linecount.h"
|
||||
#include<algorithm>
|
||||
#ifdef min
|
||||
#undef min
|
||||
#endif
|
||||
|
||||
///////////////////////////// Platform specific
|
||||
#ifdef _WIN32
|
||||
|
||||
// Windows
|
||||
#define LCOPENFILE(name) CreateFile(name, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)
|
||||
#define LCCLOSEFILE(handle) CloseHandle(handle)
|
||||
#define LCINVALIDHANDLE INVALID_HANDLE_VALUE
|
||||
#define LCSETREALLASTERROR(err, errstr) { setLastError((err), (errstr)); }
|
||||
#define MAP_FAILED NULL
|
||||
typedef long long LCFILEOFFSET;
|
||||
|
||||
#elif defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
|
||||
|
||||
// POSIX
|
||||
#include<sys/types.h>
|
||||
#include<sys/stat.h>
|
||||
#include<sys/fcntl.h>
|
||||
#include<sys/mman.h>
|
||||
#if (defined (__APPLE__) && defined (__MACH__))
|
||||
#include <sys/sysctl.h>
|
||||
typedef off_t LCFILEOFFSET;
|
||||
#define MMAP ::mmap
|
||||
#define FSTAT ::fstat
|
||||
#define STAT ::stat
|
||||
#elif defined(__linux__)
|
||||
typedef off64_t LCFILEOFFSET;
|
||||
#define MMAP ::mmap64
|
||||
#define FSTAT ::fstat64
|
||||
#define STAT ::stat64
|
||||
#else
|
||||
typedef off_t LCFILEOFFSET;
|
||||
#define MMAP ::mmap
|
||||
#define FSTAT ::fstat
|
||||
#define STAT ::stat
|
||||
#endif
|
||||
|
||||
#define LCOPENFILE(name) ::open(name, O_RDONLY)
|
||||
#define LCCLOSEFILE(handle) (::close(handle) != -1)
|
||||
#define LCINVALIDHANDLE -1
|
||||
#define LCSETREALLASTERROR(err, errstr) { int __err = errno; setLastError(__err, ::strerror(__err)); }
|
||||
|
||||
#endif
|
||||
|
||||
///////////////////////////// Line Count Class
|
||||
|
||||
BEGIN_TURBO_LINECOUNT_NAMESPACE;
|
||||
|
||||
struct LCTHREADCONTEXT
|
||||
{
|
||||
int thread_number;
|
||||
CLineCount *m_this;
|
||||
};
|
||||
|
||||
CLineCount::CLineCount(PARAMETERS *parameters)
|
||||
{
|
||||
|
||||
// Set line count parameter defaults
|
||||
int cpucount;
|
||||
int allocationgranularity;
|
||||
#ifdef _WIN32
|
||||
SYSTEM_INFO sysinfo;
|
||||
GetSystemInfo(&sysinfo);
|
||||
cpucount = sysinfo.dwNumberOfProcessors;
|
||||
allocationgranularity = sysinfo.dwAllocationGranularity;
|
||||
//#elif defined(__linux__)
|
||||
#else
|
||||
cpucount = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
allocationgranularity = sysconf(_SC_PAGESIZE);
|
||||
//#elif (defined (__APPLE__) && defined (__MACH__))
|
||||
// mmsize_t count_len = sizeof(cpucount);
|
||||
// sysctlbyname("hw.logicalcpu", &cpucount, &count_len, NULL, 0);
|
||||
//#else
|
||||
// cpucount = 1;
|
||||
#endif
|
||||
m_parameters.threadcount = cpucount;
|
||||
m_parameters.buffersize = (1024 * 1024);
|
||||
|
||||
// Override defaults if specified
|
||||
if (parameters)
|
||||
{
|
||||
if (parameters->buffersize != -1)
|
||||
{
|
||||
m_parameters.buffersize = parameters->buffersize;
|
||||
m_parameters.buffersize += (allocationgranularity - (m_parameters.buffersize % allocationgranularity)) % allocationgranularity;
|
||||
}
|
||||
if (parameters->threadcount != -1)
|
||||
{
|
||||
m_parameters.threadcount = parameters->threadcount;
|
||||
}
|
||||
}
|
||||
|
||||
init();
|
||||
}
|
||||
|
||||
CLineCount::~CLineCount()
|
||||
{
|
||||
if (m_auto_close && m_opened)
|
||||
{
|
||||
LCCLOSEFILE(m_fh);
|
||||
}
|
||||
}
|
||||
|
||||
void CLineCount::init(void)
|
||||
{
|
||||
m_lasterror = 0;
|
||||
m_lasterrorstring = _T("");
|
||||
m_opened = false;
|
||||
m_auto_close = false;
|
||||
m_fh = LCINVALIDHANDLE;
|
||||
m_filesize = 0;
|
||||
m_actual_thread_count = 0;
|
||||
#ifdef _WIN32
|
||||
m_filemapping = NULL;
|
||||
#endif
|
||||
m_threads.clear();
|
||||
m_threadlinecounts.clear();
|
||||
}
|
||||
|
||||
void CLineCount::setLastError(errno_t lasterror, LCSTRING lasterrorstring)
|
||||
{
|
||||
m_lasterror = lasterror;
|
||||
m_lasterrorstring = lasterrorstring;
|
||||
}
|
||||
|
||||
errno_t CLineCount::lastError() const
|
||||
{
|
||||
return m_lasterror;
|
||||
}
|
||||
|
||||
LCSTRING CLineCount::lastErrorString() const
|
||||
{
|
||||
return m_lasterrorstring;
|
||||
}
|
||||
|
||||
bool CLineCount::isOpened() const
|
||||
{
|
||||
return m_opened;
|
||||
}
|
||||
|
||||
bool CLineCount::open(LCFILEHANDLE fhandle, bool auto_close)
|
||||
{
|
||||
if (m_opened)
|
||||
{
|
||||
setLastError(EEXIST, _T("file already opened"));
|
||||
return false;
|
||||
}
|
||||
|
||||
m_fh = fhandle;
|
||||
m_opened = true;
|
||||
m_auto_close = auto_close;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CLineCount::open(const TCHAR *filename)
|
||||
{
|
||||
if (m_opened)
|
||||
{
|
||||
setLastError(EEXIST, _T("file already opened"));
|
||||
return false;
|
||||
}
|
||||
|
||||
m_fh = LCOPENFILE(filename);
|
||||
if (m_fh == LCINVALIDHANDLE)
|
||||
{
|
||||
LCSETREALLASTERROR(ENOENT, _T("file could not be opened"));
|
||||
return false;
|
||||
}
|
||||
|
||||
m_opened = true;
|
||||
m_auto_close = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CLineCount::close()
|
||||
{
|
||||
if (!m_opened)
|
||||
{
|
||||
setLastError(EBADF, _T("file not opened"));
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ok = true;
|
||||
if (!LCCLOSEFILE(m_fh))
|
||||
{
|
||||
LCSETREALLASTERROR(EBADF, _T("unable to close file"));
|
||||
ok = false;
|
||||
}
|
||||
|
||||
init();
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
|
||||
#ifdef _WIN32
|
||||
DWORD WINAPI threadProc(LPVOID ctx)
|
||||
#else
|
||||
void *threadProc(void *ctx)
|
||||
#endif
|
||||
{
|
||||
LCTHREADCONTEXT *lctctx = (LCTHREADCONTEXT*)ctx;
|
||||
lctctx->m_this->countThread(lctctx->thread_number);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
unsigned int CLineCount::countThread(int thread_number)
|
||||
{
|
||||
LCFILEOFFSET buffersize = (LCFILEOFFSET)m_parameters.buffersize;
|
||||
LCFILEOFFSET startoffset = buffersize * (LCFILEOFFSET)thread_number;
|
||||
LCFILEOFFSET stride = buffersize * m_actual_thread_count;
|
||||
LCFILEOFFSET curoffset = startoffset;
|
||||
LCFILEOFFSET lastmapsize = 0;
|
||||
LCLINECOUNT count = 0;
|
||||
void *mem = NULL;
|
||||
|
||||
while (curoffset < m_filesize)
|
||||
{
|
||||
if (m_thread_fail)
|
||||
{
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Get best file mapping window size
|
||||
size_t mapsize = (size_t)std::min((m_filesize - curoffset), buffersize);
|
||||
|
||||
// Map view of file
|
||||
#ifdef _WIN32
|
||||
if (mem)
|
||||
{
|
||||
if (!UnmapViewOfFile(mem))
|
||||
{
|
||||
setLastError(EINVAL, _T("memory unmap failed"));
|
||||
m_thread_fail = true;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
mem = MapViewOfFile(m_filemapping, FILE_MAP_READ, (DWORD)(curoffset >> 32), (DWORD)curoffset, (SIZE_T)mapsize);
|
||||
#else
|
||||
if (mem)
|
||||
{
|
||||
if(munmap(mem, lastmapsize) !=0)
|
||||
{
|
||||
LCSETREALLASTERROR(EINVAL, _T("memory unmap failed"));
|
||||
m_thread_fail = true;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
mem = MMAP(NULL, mapsize, PROT_READ, MAP_FILE | MAP_SHARED, m_fh, curoffset);
|
||||
// printf("%p %lld %lld\n",mem, mapsize, curoffset);
|
||||
#endif
|
||||
if (mem == MAP_FAILED)
|
||||
{
|
||||
LCSETREALLASTERROR(EINVAL, _T("memory map failed"));
|
||||
m_thread_fail = true;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Count newlines in buffer
|
||||
LCFILEOFFSET windowoffset = 0;
|
||||
size_t windowleft = mapsize;
|
||||
char *ptr = (char *)mem;
|
||||
while (windowleft > 0)
|
||||
{
|
||||
char *ptrnext = (char *)memchr(ptr, '\n', windowleft);
|
||||
if (ptrnext)
|
||||
{
|
||||
ptrnext++;
|
||||
count++;
|
||||
windowleft -= (ptrnext - ptr);
|
||||
ptr = ptrnext;
|
||||
}
|
||||
else
|
||||
{
|
||||
windowleft = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// See if we need to account for end of file not ending with line terminator
|
||||
if ((curoffset + mapsize) == m_filesize)
|
||||
{
|
||||
if (*((char *)mem + (mapsize - 1)) != '\n')
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
// Move to next buffer
|
||||
curoffset += stride;
|
||||
lastmapsize = mapsize;
|
||||
}
|
||||
|
||||
// Clean up memory map
|
||||
#ifdef _WIN32
|
||||
if (mem)
|
||||
{
|
||||
if (!UnmapViewOfFile(mem))
|
||||
{
|
||||
setLastError(EINVAL, _T("memory unmap failed"));
|
||||
m_thread_fail = true;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
#else
|
||||
if (mem)
|
||||
{
|
||||
if (munmap(mem, lastmapsize) != 0)
|
||||
{
|
||||
LCSETREALLASTERROR(EINVAL, _T("memory unmap failed"));
|
||||
m_thread_fail = true;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// Save count for this thread
|
||||
m_threadlinecounts[thread_number] = count;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool CLineCount::createThread(int thread_number)
|
||||
{
|
||||
LCTHREADCONTEXT * ctx = new LCTHREADCONTEXT;
|
||||
ctx->m_this = this;
|
||||
ctx->thread_number = thread_number;
|
||||
#ifdef _WIN32
|
||||
HANDLE hThread = CreateThread(NULL, 0, threadProc, ctx, 0, NULL);
|
||||
if(!hThread)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
#else
|
||||
pthread_t hThread;
|
||||
int ret = pthread_create(&hThread, NULL, threadProc, ctx);
|
||||
if (ret != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
m_threads[thread_number] = hThread;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CLineCount::countLines(LCLINECOUNT & linecount)
|
||||
{
|
||||
// Determine file size
|
||||
#ifdef _WIN32
|
||||
LARGE_INTEGER li;
|
||||
if (!GetFileSizeEx(m_fh, &li))
|
||||
{
|
||||
LCSETREALLASTERROR(EBADF, _T("unable to get file size"));
|
||||
return false;
|
||||
}
|
||||
m_filesize = li.QuadPart;
|
||||
#else
|
||||
struct STAT statbuf;
|
||||
if(FSTAT(m_fh,&statbuf)!=0)
|
||||
{
|
||||
LCSETREALLASTERROR(EBADF, _T("unable to get file size"));
|
||||
return false;
|
||||
}
|
||||
m_filesize = statbuf.st_size;
|
||||
#endif
|
||||
|
||||
// Exit now for empty files
|
||||
if (m_filesize == 0)
|
||||
{
|
||||
linecount = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Figure out actual thread count
|
||||
LCFILEOFFSET windowcount = (m_filesize + (m_parameters.buffersize - 1)) / m_parameters.buffersize;
|
||||
if (windowcount < (LCFILEOFFSET) m_parameters.threadcount)
|
||||
{
|
||||
m_actual_thread_count = (int)windowcount;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_actual_thread_count = m_parameters.threadcount;
|
||||
}
|
||||
|
||||
// printf("act: %d\n",m_actual_thread_count);
|
||||
|
||||
#ifdef _WIN32
|
||||
// Prepare file mapping
|
||||
m_filemapping = CreateFileMapping(m_fh, NULL, PAGE_READONLY, 0, 0, NULL);
|
||||
#endif
|
||||
|
||||
// Spin up threads
|
||||
m_threads.resize(m_actual_thread_count);
|
||||
m_threadlinecounts.resize(m_actual_thread_count);
|
||||
m_thread_fail = false;
|
||||
for (int i = 0; i < m_actual_thread_count; i++)
|
||||
{
|
||||
if (!createThread(i))
|
||||
{
|
||||
setLastError(ECHILD, _T("failed to create counting thread"));
|
||||
m_thread_fail = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for threads to complete
|
||||
int complete = 0;
|
||||
int errors = 0;
|
||||
for (int i = 0; i < m_actual_thread_count; i++)
|
||||
{
|
||||
bool success = false;
|
||||
if (m_threads[i] != NULL)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
success = (WaitForSingleObject(m_threads[i], INFINITE) == WAIT_OBJECT_0);
|
||||
#else
|
||||
success = pthread_join(m_threads[i], NULL) == 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
if (success)
|
||||
{
|
||||
complete++;
|
||||
}
|
||||
else
|
||||
{
|
||||
errors++;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
// Clean up file mapping
|
||||
CloseHandle(m_filemapping);
|
||||
#endif
|
||||
|
||||
if (m_thread_fail)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (complete != m_actual_thread_count)
|
||||
{
|
||||
setLastError(ECHILD, _T("thread join failed"));
|
||||
return false;
|
||||
}
|
||||
|
||||
// Sum up thread line counts and return
|
||||
linecount = 0;
|
||||
for (int i = 0; i < m_actual_thread_count; i++)
|
||||
{
|
||||
linecount += m_threadlinecounts[i];
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Static helpers
|
||||
LCLINECOUNT CLineCount::LineCount(LCFILEHANDLE fhandle, errno_t * error, LCSTRING *errorstring)
|
||||
{
|
||||
CLineCount lc;
|
||||
if (!lc.open(fhandle))
|
||||
{
|
||||
if (error)
|
||||
{
|
||||
*error = lc.lastError();
|
||||
}
|
||||
if (errorstring)
|
||||
{
|
||||
*errorstring = lc.lastErrorString();
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
LCLINECOUNT count;
|
||||
if (!lc.countLines(count))
|
||||
{
|
||||
if (error)
|
||||
{
|
||||
*error = lc.lastError();
|
||||
}
|
||||
if (errorstring)
|
||||
{
|
||||
*errorstring = lc.lastErrorString();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
*error = 0;
|
||||
*errorstring = _T("");
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
LCLINECOUNT CLineCount::LineCount(const TCHAR *filename, errno_t * error, LCSTRING *errorstring)
|
||||
{
|
||||
CLineCount lc;
|
||||
if (!lc.open(filename))
|
||||
{
|
||||
if (error)
|
||||
{
|
||||
*error = lc.lastError();
|
||||
}
|
||||
if (errorstring)
|
||||
{
|
||||
*errorstring = lc.lastErrorString();
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
LCLINECOUNT count;
|
||||
if (!lc.countLines(count))
|
||||
{
|
||||
if (error)
|
||||
{
|
||||
*error = lc.lastError();
|
||||
}
|
||||
if (errorstring)
|
||||
{
|
||||
*errorstring = lc.lastErrorString();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
*error = 0;
|
||||
*errorstring = _T("");
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
END_TURBO_LINECOUNT_NAMESPACE;
|
||||
|
||||
|
||||
///////////////////////////// C Linkage
|
||||
|
||||
#ifndef _NO_TURBO_LINECOUNT_C
|
||||
|
||||
#ifdef _WIN32
|
||||
long long turbo_linecount_handle(HANDLE fhandle, errno_t * error, TCHAR ** errorstring)
|
||||
#else
|
||||
long long turbo_linecount_handle(int fhandle, errno_t * error, char ** errorstring)
|
||||
#endif
|
||||
{
|
||||
TURBO_LINECOUNT::LCSTRING errstr;
|
||||
|
||||
long long linecount = TURBO_LINECOUNT::CLineCount::LineCount(fhandle, error, &errstr);
|
||||
|
||||
if (errorstring)
|
||||
{
|
||||
*errorstring = _tcsdup(errstr.c_str());
|
||||
}
|
||||
|
||||
return linecount;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
long long turbo_linecount_file(const TCHAR *filename, errno_t * error, TCHAR ** errorstring)
|
||||
#else
|
||||
long long turbo_linecount_file(const char *filename, errno_t * error, char ** errorstring)
|
||||
#endif
|
||||
{
|
||||
TURBO_LINECOUNT::LCSTRING errstr;
|
||||
|
||||
long long linecount = TURBO_LINECOUNT::CLineCount::LineCount(filename, error, &errstr);
|
||||
|
||||
if (errorstring)
|
||||
{
|
||||
*errorstring = _tcsdup(errstr.c_str());
|
||||
}
|
||||
|
||||
return linecount;
|
||||
}
|
||||
|
||||
#endif
|
||||
168
src/turbo_linecount.h
Normal file
168
src/turbo_linecount.h
Normal file
@@ -0,0 +1,168 @@
|
||||
//
|
||||
// Turbo Linecount
|
||||
// Copyright 2015, Christien Rioux
|
||||
//
|
||||
// MIT Licensed, see file 'LICENSE' for details
|
||||
//
|
||||
///////////////////////////////////////////////
|
||||
|
||||
#ifndef __INC_TURBO_LINECOUNT_H
|
||||
#define __INC_TURBO_LINECOUNT_H
|
||||
|
||||
#define LINECOUNT_VERSION_MAJOR 1
|
||||
#define LINECOUNT_VERSION_MINOR 0
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
///////////////////////////////////////////// Headers
|
||||
|
||||
////////////// Platform independent
|
||||
|
||||
#include<string>
|
||||
#include<vector>
|
||||
#include<errno.h>
|
||||
|
||||
#define BEGIN_TURBO_LINECOUNT_NAMESPACE namespace TURBO_LINECOUNT {
|
||||
#define END_TURBO_LINECOUNT_NAMESPACE }
|
||||
|
||||
////////////// Platform specific
|
||||
|
||||
#ifdef _WIN32 // Windows
|
||||
#include<Windows.h>
|
||||
#include<tchar.h>
|
||||
#elif defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) // POSIX
|
||||
#include<unistd.h>
|
||||
#include<pthread.h>
|
||||
#define _T(x) x
|
||||
#define TCHAR char
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////// Line Count Class
|
||||
|
||||
BEGIN_TURBO_LINECOUNT_NAMESPACE;
|
||||
|
||||
////////////// Platform specific
|
||||
#ifdef _WIN32 // Windows
|
||||
|
||||
#ifdef _UNICODE
|
||||
typedef std::wstring LCSTRING;
|
||||
#else
|
||||
typedef std::string LCSTRING;
|
||||
#endif
|
||||
|
||||
typedef HANDLE LCFILEHANDLE;
|
||||
typedef long long LCFILEOFFSET;
|
||||
typedef LCFILEOFFSET LCLINECOUNT;
|
||||
#define LCLINECOUNTFMT "%I64d"
|
||||
|
||||
#elif defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) // POSIX
|
||||
|
||||
typedef std::string LCSTRING;
|
||||
typedef int LCFILEHANDLE;
|
||||
#if (defined (__APPLE__) && defined (__MACH__))
|
||||
typedef off_t LCFILEOFFSET;
|
||||
#define LCLINECOUNTFMT "%lld"
|
||||
#elif defined(__linux__)
|
||||
typedef off64_t LCFILEOFFSET;
|
||||
#define LCLINECOUNTFMT "%lld"
|
||||
#else
|
||||
typedef off_t LCFILEOFFSET;
|
||||
#define LCLINECOUNTFMT "%d"
|
||||
#endif
|
||||
typedef LCFILEOFFSET LCLINECOUNT;
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
class CLineCount
|
||||
{
|
||||
public:
|
||||
|
||||
struct PARAMETERS
|
||||
{
|
||||
size_t buffersize;
|
||||
int threadcount;
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
bool m_opened;
|
||||
bool m_auto_close;
|
||||
LCFILEHANDLE m_fh;
|
||||
errno_t m_lasterror;
|
||||
LCSTRING m_lasterrorstring;
|
||||
LCFILEOFFSET m_filesize;
|
||||
PARAMETERS m_parameters;
|
||||
int m_actual_thread_count;
|
||||
#ifdef _WIN32
|
||||
std::vector<HANDLE> m_threads;
|
||||
HANDLE m_filemapping;
|
||||
#else
|
||||
std::vector<pthread_t> m_threads;
|
||||
#endif
|
||||
std::vector<LCLINECOUNT> m_threadlinecounts;
|
||||
bool m_thread_fail;
|
||||
|
||||
private:
|
||||
|
||||
void setLastError(errno_t error, LCSTRING lasterrorstring);
|
||||
void init();
|
||||
bool createThread(int thread_number);
|
||||
#ifdef _WIN32
|
||||
friend DWORD WINAPI threadProc(LPVOID ctx);
|
||||
#else
|
||||
friend void *threadProc(void *ctx);
|
||||
#endif
|
||||
unsigned int countThread(int thread_number);
|
||||
|
||||
public:
|
||||
|
||||
CLineCount(PARAMETERS *parameters=NULL);
|
||||
~CLineCount();
|
||||
|
||||
bool isOpened() const;
|
||||
errno_t lastError() const;
|
||||
LCSTRING lastErrorString() const;
|
||||
|
||||
bool open(LCFILEHANDLE fhandle, bool auto_close = false);
|
||||
bool open(const TCHAR * filename);
|
||||
bool close();
|
||||
|
||||
bool countLines(LCLINECOUNT &linecount);
|
||||
|
||||
public:
|
||||
|
||||
// Static utility functions
|
||||
static LCLINECOUNT LineCount(LCFILEHANDLE fhandle, errno_t * error = NULL, LCSTRING * errorstring = NULL);
|
||||
static LCLINECOUNT LineCount(const TCHAR *filename, errno_t * error = NULL, LCSTRING * errorstring = NULL);
|
||||
};
|
||||
|
||||
END_TURBO_LINECOUNT_NAMESPACE;
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
// C compatibility functions
|
||||
#ifndef _NO_TURBO_LINECOUNT_C
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
long long turbo_linecount_handle(HANDLE fhandle, errno_t * error = NULL, TCHAR ** errorstring = NULL);
|
||||
long long turbo_linecount_file(const TCHAR *filename, errno_t * error = NULL, TCHAR ** errorstring = NULL);
|
||||
#else
|
||||
long long turbo_linecount_handle(int fhandle, errno_t * error = NULL, char ** errorstring = NULL);
|
||||
long long turbo_linecount_file(const char *filename, errno_t * error = NULL, char ** errorstring = NULL);
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user