2015-12-12 20:35:58 +00:00
|
|
|
//
|
|
|
|
// Turbo Linecount
|
|
|
|
// Copyright 2015, Christien Rioux
|
|
|
|
//
|
|
|
|
// MIT Licensed, see file 'LICENSE' for details
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef __INC_TURBO_LINECOUNT_H
|
|
|
|
#define __INC_TURBO_LINECOUNT_H
|
2015-12-12 06:49:31 +00:00
|
|
|
|
2015-12-12 08:44:11 +00:00
|
|
|
#define LINECOUNT_VERSION_MAJOR 1
|
|
|
|
#define LINECOUNT_VERSION_MINOR 0
|
|
|
|
|
2015-12-12 20:35:58 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
2015-12-12 06:49:31 +00:00
|
|
|
///////////////////////////////////////////// Headers
|
|
|
|
|
|
|
|
////////////// Platform independent
|
|
|
|
|
|
|
|
#include<string>
|
|
|
|
#include<vector>
|
|
|
|
#include<errno.h>
|
2015-12-12 20:35:58 +00:00
|
|
|
|
|
|
|
#define BEGIN_TURBO_LINECOUNT_NAMESPACE namespace TURBO_LINECOUNT {
|
|
|
|
#define END_TURBO_LINECOUNT_NAMESPACE }
|
2015-12-12 06:49:31 +00:00
|
|
|
|
|
|
|
////////////// Platform specific
|
|
|
|
|
|
|
|
#ifdef _WIN32 // Windows
|
|
|
|
#include<Windows.h>
|
|
|
|
#include<tchar.h>
|
|
|
|
#elif defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) // POSIX
|
|
|
|
#include<unistd.h>
|
2015-12-12 08:44:11 +00:00
|
|
|
#include<pthread.h>
|
2015-12-12 06:49:31 +00:00
|
|
|
#define _T(x) x
|
|
|
|
#define TCHAR char
|
|
|
|
#endif
|
|
|
|
|
|
|
|
///////////////////////////////////////////// Line Count Class
|
|
|
|
|
2015-12-12 20:35:58 +00:00
|
|
|
BEGIN_TURBO_LINECOUNT_NAMESPACE;
|
2015-12-12 06:49:31 +00:00
|
|
|
|
|
|
|
////////////// Platform specific
|
|
|
|
#ifdef _WIN32 // Windows
|
|
|
|
|
|
|
|
#ifdef _UNICODE
|
|
|
|
typedef std::wstring LCSTRING;
|
|
|
|
#else
|
|
|
|
typedef std::string LCSTRING;
|
|
|
|
#endif
|
2015-12-12 20:35:58 +00:00
|
|
|
|
2015-12-12 06:49:31 +00:00
|
|
|
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;
|
2015-12-12 08:44:11 +00:00
|
|
|
#if (defined (__APPLE__) && defined (__MACH__))
|
2015-12-12 06:49:31 +00:00
|
|
|
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
|
|
|
|
{
|
2015-12-12 08:44:11 +00:00
|
|
|
size_t buffersize;
|
|
|
|
int threadcount;
|
2015-12-12 06:49:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
bool m_opened;
|
|
|
|
bool m_auto_close;
|
|
|
|
LCFILEHANDLE m_fh;
|
2015-12-12 20:35:58 +00:00
|
|
|
errno_t m_lasterror;
|
2015-12-12 06:49:31 +00:00
|
|
|
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:
|
|
|
|
|
2015-12-12 20:35:58 +00:00
|
|
|
void setLastError(errno_t error, LCSTRING lasterrorstring);
|
2015-12-12 06:49:31 +00:00
|
|
|
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;
|
2015-12-12 20:35:58 +00:00
|
|
|
errno_t lastError() const;
|
2015-12-12 06:49:31 +00:00
|
|
|
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
|
2015-12-12 20:35:58 +00:00
|
|
|
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);
|
2015-12-12 06:49:31 +00:00
|
|
|
};
|
|
|
|
|
2015-12-12 20:35:58 +00:00
|
|
|
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
|
2015-12-12 06:49:31 +00:00
|
|
|
|
|
|
|
#endif
|