crioux__turbo-linecount/src/turbo_linecount.h

181 lines
4.1 KiB
C
Raw Normal View History

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
2015-12-12 22:10:33 +00:00
#define BEGIN_TURBOLINECOUNT_NAMESPACE namespace TURBOLINECOUNT {
#define END_TURBOLINECOUNT_NAMESPACE }
2015-12-12 06:49:31 +00:00
////////////// Platform specific
2015-12-12 23:43:47 +00:00
#if defined(_APPLE__) || defined(__linux__) || defined(__CYGWIN__)
2015-12-12 23:09:30 +00:00
#define TLC_COMPATIBLE_UNIX 1
#endif
2015-12-12 06:49:31 +00:00
#ifdef _WIN32 // Windows
2015-12-12 23:09:30 +00:00
2015-12-12 06:49:31 +00:00
#include<Windows.h>
#include<tchar.h>
2015-12-12 22:10:33 +00:00
typedef errno_t tlc_error_t;
2015-12-12 23:09:30 +00:00
#elif defined(TLC_COMPATIBLE_UNIX)
2015-12-12 06:49:31 +00:00
#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
2015-12-12 23:43:47 +00:00
typedef error_t tlc_error_t;
2015-12-12 23:09:30 +00:00
#else
#error Unsupported operating system.
2015-12-12 06:49:31 +00:00
#endif
///////////////////////////////////////////// Line Count Class
2015-12-12 22:10:33 +00:00
BEGIN_TURBOLINECOUNT_NAMESPACE;
2015-12-12 06:49:31 +00:00
////////////// Platform specific
#ifdef _WIN32 // Windows
#ifdef _UNICODE
2015-12-12 22:10:33 +00:00
typedef std::wstring tlc_string_t;
2015-12-12 06:49:31 +00:00
#else
2015-12-12 22:10:33 +00:00
typedef std::string tlc_string_t;
2015-12-12 06:49:31 +00:00
#endif
2015-12-12 20:35:58 +00:00
2015-12-12 22:10:33 +00:00
typedef HANDLE tlc_filehandle_t;
typedef long long tlc_fileoffset_t;
typedef tlc_fileoffset_t tlc_linecount_t;
#define TLC_LINECOUNT_FMT "%I64d"
2015-12-12 06:49:31 +00:00
2015-12-12 23:09:30 +00:00
#elif defined(TLC_COMPATIBLE_UNIX) // Unix
2015-12-12 06:49:31 +00:00
2015-12-12 22:10:33 +00:00
typedef std::string tlc_string_t;
typedef int tlc_filehandle_t;
2015-12-12 08:44:11 +00:00
#if (defined (__APPLE__) && defined (__MACH__))
2015-12-12 22:10:33 +00:00
typedef off_t tlc_fileoffset_t;
#define TLC_LINECOUNT_FMT "%lld"
2015-12-12 23:09:30 +00:00
#elif defined(_LARGEFILE64_SOURCE)
2015-12-12 23:43:47 +00:00
typedef _off64_t tlc_fileoffset_t;
2015-12-12 22:10:33 +00:00
#define TLC_LINECOUNT_FMT "%lld"
2015-12-12 06:49:31 +00:00
#else
2015-12-12 22:10:33 +00:00
typedef off_t tlc_fileoffset_t;
#define TLC_LINECOUNT_FMT "%d"
2015-12-12 06:49:31 +00:00
#endif
2015-12-12 22:10:33 +00:00
typedef tlc_fileoffset_t tlc_linecount_t;
2015-12-12 06:49:31 +00:00
#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;
2015-12-12 22:10:33 +00:00
tlc_filehandle_t m_fh;
tlc_error_t m_lasterror;
tlc_string_t m_lasterrorstring;
tlc_fileoffset_t m_filesize;
2015-12-12 06:49:31 +00:00
PARAMETERS m_parameters;
int m_actual_thread_count;
#ifdef _WIN32
std::vector<HANDLE> m_threads;
HANDLE m_filemapping;
2015-12-12 23:09:30 +00:00
#elif defined(TLC_COMPATIBLE_UNIX)
2015-12-12 06:49:31 +00:00
std::vector<pthread_t> m_threads;
#endif
2015-12-12 22:10:33 +00:00
std::vector<tlc_linecount_t> m_threadlinecounts;
2015-12-12 06:49:31 +00:00
bool m_thread_fail;
private:
2015-12-12 22:10:33 +00:00
void setLastError(tlc_error_t error, tlc_string_t lasterrorstring);
2015-12-12 06:49:31 +00:00
void init();
bool createThread(int thread_number);
#ifdef _WIN32
friend DWORD WINAPI threadProc(LPVOID ctx);
2015-12-12 23:09:30 +00:00
#elif defined(TLC_COMPATIBLE_UNIX)
2015-12-12 06:49:31 +00:00
friend void *threadProc(void *ctx);
#endif
unsigned int countThread(int thread_number);
public:
CLineCount(PARAMETERS *parameters=NULL);
~CLineCount();
bool isOpened() const;
2015-12-12 22:10:33 +00:00
tlc_error_t lastError() const;
tlc_string_t lastErrorString() const;
2015-12-12 06:49:31 +00:00
2015-12-12 22:10:33 +00:00
bool open(tlc_filehandle_t fhandle, bool auto_close = false);
2015-12-12 06:49:31 +00:00
bool open(const TCHAR * filename);
bool close();
2015-12-12 22:10:33 +00:00
bool countLines(tlc_linecount_t &linecount);
2015-12-12 06:49:31 +00:00
public:
// Static utility functions
2015-12-12 22:10:33 +00:00
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);
2015-12-12 06:49:31 +00:00
};
2015-12-12 22:10:33 +00:00
END_TURBOLINECOUNT_NAMESPACE;
2015-12-12 20:35:58 +00:00
#endif
// C compatibility functions
#ifndef _NO_TURBO_LINECOUNT_C
#ifdef __cplusplus
extern "C"
{
#endif
#ifdef _WIN32
2015-12-12 22:10:33 +00:00
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);
2015-12-12 23:09:30 +00:00
#elif defined(TLC_COMPATIBLE_UNIX)
2015-12-12 22:10:33 +00:00
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);
2015-12-12 20:35:58 +00:00
#endif
#ifdef __cplusplus
}
#endif
#endif
2015-12-12 06:49:31 +00:00
#endif