You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
4.5 KiB

9 years ago
//
// 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
9 years ago
#if defined(__CYGWIN__) || defined(__linux__) || defined(__MINGW32__)
#ifndef _LARGEFILE64_SOURCE
#define _LARGEFILE64_SOURCE 1
#endif
#endif
9 years ago
#ifdef __cplusplus
///////////////////////////////////////////// Headers
////////////// Platform independent
#include<string>
#include<vector>
#include<errno.h>
9 years ago
9 years ago
#define BEGIN_TURBOLINECOUNT_NAMESPACE namespace TURBOLINECOUNT {
#define END_TURBOLINECOUNT_NAMESPACE }
////////////// Platform specific
#if defined(__APPLE__) || defined(__linux__) || defined(__CYGWIN__)
9 years ago
#define TLC_COMPATIBLE_UNIX 1
#endif
9 years ago
#ifdef _WIN32 // Windows
9 years ago
#include<Windows.h>
#include<tchar.h>
9 years ago
typedef errno_t tlc_error_t;
9 years ago
#elif defined(TLC_COMPATIBLE_UNIX)
#include<unistd.h>
#include<pthread.h>
#define _T(x) x
#define TCHAR char
#ifdef _ERRNO_T
typedef errno_t tlc_error_t;
#elif defined(__error_t_defined)
9 years ago
typedef error_t tlc_error_t;
#else
typedef int tlc_error_t;
#endif
9 years ago
#else
#error Unsupported operating system.
#endif
///////////////////////////////////////////// Line Count Class
9 years ago
BEGIN_TURBOLINECOUNT_NAMESPACE;
////////////// Platform specific
#ifdef _WIN32 // Windows
#ifdef _UNICODE
9 years ago
typedef std::wstring tlc_string_t;
#else
9 years ago
typedef std::string tlc_string_t;
#endif
9 years ago
9 years ago
typedef HANDLE tlc_filehandle_t;
typedef long long tlc_fileoffset_t;
typedef tlc_fileoffset_t tlc_linecount_t;
#define TLC_LINECOUNT_FMT "%I64d"
9 years ago
#elif defined(TLC_COMPATIBLE_UNIX) // Unix
9 years ago
typedef std::string tlc_string_t;
typedef int tlc_filehandle_t;
#if (defined (__APPLE__) && defined (__MACH__))
9 years ago
typedef off_t tlc_fileoffset_t;
#define TLC_LINECOUNT_FMT "%lld"
9 years ago
#elif defined(_LARGEFILE64_SOURCE)
9 years ago
#if defined(__CYGWIN__)
typedef _off64_t tlc_fileoffset_t;
#else
typedef off64_t tlc_fileoffset_t;
#endif
9 years ago
#define TLC_LINECOUNT_FMT "%lld"
#else
9 years ago
typedef off_t tlc_fileoffset_t;
#define TLC_LINECOUNT_FMT "%d"
#endif
9 years ago
typedef tlc_fileoffset_t tlc_linecount_t;
#endif
class CLineCount
{
public:
struct PARAMETERS
{
size_t buffersize;
int threadcount;
};
private:
bool m_opened;
bool m_auto_close;
9 years ago
tlc_filehandle_t m_fh;
tlc_error_t m_lasterror;
tlc_string_t m_lasterrorstring;
tlc_fileoffset_t m_filesize;
PARAMETERS m_parameters;
int m_actual_thread_count;
#ifdef _WIN32
std::vector<HANDLE> m_threads;
HANDLE m_filemapping;
9 years ago
#elif defined(TLC_COMPATIBLE_UNIX)
std::vector<pthread_t> m_threads;
#endif
9 years ago
std::vector<tlc_linecount_t> m_threadlinecounts;
bool m_thread_fail;
private:
9 years ago
void setLastError(tlc_error_t error, tlc_string_t lasterrorstring);
void init();
bool createThread(int thread_number);
#ifdef _WIN32
friend DWORD WINAPI threadProc(LPVOID ctx);
9 years ago
#elif defined(TLC_COMPATIBLE_UNIX)
friend void *threadProc(void *ctx);
#endif
unsigned int countThread(int thread_number);
public:
CLineCount(PARAMETERS *parameters=NULL);
~CLineCount();
bool isOpened() const;
9 years ago
tlc_error_t lastError() const;
tlc_string_t lastErrorString() const;
9 years ago
bool open(tlc_filehandle_t fhandle, bool auto_close = false);
bool open(const TCHAR * filename);
bool close();
9 years ago
bool countLines(tlc_linecount_t &linecount);
public:
// Static utility functions
9 years ago
static tlc_linecount_t LineCount(tlc_filehandle_t fhandle, tlc_error_t * error = NULL, tlc_string_t * errorstring = NULL);
static tlc_linecount_t LineCount(const TCHAR *filename, tlc_error_t * error = NULL, tlc_string_t * errorstring = NULL);
};
9 years ago
END_TURBOLINECOUNT_NAMESPACE;
9 years ago
#endif
// C compatibility functions
#ifndef _NO_TURBO_LINECOUNT_C
#ifdef __cplusplus
extern "C"
{
#endif
#ifdef _WIN32
9 years ago
long long turbo_linecount_handle(HANDLE fhandle, tlc_error_t * error = NULL, TCHAR ** errorstring = NULL);
long long turbo_linecount_file(const TCHAR *filename, tlc_error_t * error = NULL, TCHAR ** errorstring = NULL);
9 years ago
#elif defined(TLC_COMPATIBLE_UNIX)
9 years ago
long long turbo_linecount_handle(int fhandle, tlc_error_t * tlc_error = NULL, char ** errorstring = NULL);
long long turbo_linecount_file(const char *filename, tlc_error_t * error = NULL, char ** errorstring = NULL);
9 years ago
#endif
#ifdef __cplusplus
}
#endif
#endif
#endif