master
Christien Rioux 9 years ago
parent 87f6d879ee
commit 16143f4b04

@ -26,7 +26,7 @@
#endif #endif
using namespace TURBO_LINECOUNT; using namespace TURBOLINECOUNT;
////////////////////////////////////////////////////// //////////////////////////////////////////////////////
@ -161,26 +161,26 @@ int main(int argc, char **argv)
if (!lc.open(filename)) if (!lc.open(filename))
{ {
errno_t err = lc.lastError(); tlc_error_t err = lc.lastError();
LCSTRING errstr = lc.lastErrorString(); tlc_string_t errstr = lc.lastErrorString();
_ftprintf(stderr, _T("%s: Error %d (%s)\n"), argv[0], err, errstr.c_str()); _ftprintf(stderr, _T("%s: Error %d (%s)\n"), argv[0], err, errstr.c_str());
return err; return err;
} }
// Count lines // Count lines
LCLINECOUNT count; tlc_linecount_t count;
if (!lc.countLines(count)) if (!lc.countLines(count))
{ {
errno_t err = lc.lastError(); tlc_error_t err = lc.lastError();
LCSTRING errstr = lc.lastErrorString(); tlc_string_t errstr = lc.lastErrorString();
_ftprintf(stderr, _T("%s: Error %d: (%s)\n"), argv[0], err, errstr.c_str()); _ftprintf(stderr, _T("%s: Error %d: (%s)\n"), argv[0], err, errstr.c_str());
return err; return err;
} }
// Display output // Display output
_tprintf(_T(LCLINECOUNTFMT _T("\n")), count); _tprintf(_T(TLC_LINECOUNT_FMT _T("\n")), count);
return 0; return 0;
} }

@ -21,7 +21,7 @@
#define LCINVALIDHANDLE INVALID_HANDLE_VALUE #define LCINVALIDHANDLE INVALID_HANDLE_VALUE
#define LCSETREALLASTERROR(err, errstr) { setLastError((err), (errstr)); } #define LCSETREALLASTERROR(err, errstr) { setLastError((err), (errstr)); }
#define MAP_FAILED NULL #define MAP_FAILED NULL
typedef long long LCFILEOFFSET; typedef long long tlc_fileoffset_t;
#elif defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) #elif defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
@ -32,17 +32,17 @@ typedef long long LCFILEOFFSET;
#include<sys/mman.h> #include<sys/mman.h>
#if (defined (__APPLE__) && defined (__MACH__)) #if (defined (__APPLE__) && defined (__MACH__))
#include <sys/sysctl.h> #include <sys/sysctl.h>
typedef off_t LCFILEOFFSET; typedef off_t tlc_fileoffset_t;
#define MMAP ::mmap #define MMAP ::mmap
#define FSTAT ::fstat #define FSTAT ::fstat
#define STAT ::stat #define STAT ::stat
#elif defined(__linux__) #elif defined(__linux__)
typedef off64_t LCFILEOFFSET; typedef off64_t tlc_fileoffset_t;
#define MMAP ::mmap64 #define MMAP ::mmap64
#define FSTAT ::fstat64 #define FSTAT ::fstat64
#define STAT ::stat64 #define STAT ::stat64
#else #else
typedef off_t LCFILEOFFSET; typedef off_t tlc_fileoffset_t;
#define MMAP ::mmap #define MMAP ::mmap
#define FSTAT ::fstat #define FSTAT ::fstat
#define STAT ::stat #define STAT ::stat
@ -58,7 +58,7 @@ typedef off_t LCFILEOFFSET;
///////////////////////////// Line Count Class ///////////////////////////// Line Count Class
BEGIN_TURBO_LINECOUNT_NAMESPACE; BEGIN_TURBOLINECOUNT_NAMESPACE;
struct LCTHREADCONTEXT struct LCTHREADCONTEXT
{ {
@ -131,18 +131,18 @@ void CLineCount::init(void)
m_threadlinecounts.clear(); m_threadlinecounts.clear();
} }
void CLineCount::setLastError(errno_t lasterror, LCSTRING lasterrorstring) void CLineCount::setLastError(tlc_error_t lasterror, tlc_string_t lasterrorstring)
{ {
m_lasterror = lasterror; m_lasterror = lasterror;
m_lasterrorstring = lasterrorstring; m_lasterrorstring = lasterrorstring;
} }
errno_t CLineCount::lastError() const tlc_error_t CLineCount::lastError() const
{ {
return m_lasterror; return m_lasterror;
} }
LCSTRING CLineCount::lastErrorString() const tlc_string_t CLineCount::lastErrorString() const
{ {
return m_lasterrorstring; return m_lasterrorstring;
} }
@ -152,7 +152,7 @@ bool CLineCount::isOpened() const
return m_opened; return m_opened;
} }
bool CLineCount::open(LCFILEHANDLE fhandle, bool auto_close) bool CLineCount::open(tlc_filehandle_t fhandle, bool auto_close)
{ {
if (m_opened) if (m_opened)
{ {
@ -222,12 +222,12 @@ void *threadProc(void *ctx)
unsigned int CLineCount::countThread(int thread_number) unsigned int CLineCount::countThread(int thread_number)
{ {
LCFILEOFFSET buffersize = (LCFILEOFFSET)m_parameters.buffersize; tlc_fileoffset_t buffersize = (tlc_fileoffset_t)m_parameters.buffersize;
LCFILEOFFSET startoffset = buffersize * (LCFILEOFFSET)thread_number; tlc_fileoffset_t startoffset = buffersize * (tlc_fileoffset_t)thread_number;
LCFILEOFFSET stride = buffersize * m_actual_thread_count; tlc_fileoffset_t stride = buffersize * m_actual_thread_count;
LCFILEOFFSET curoffset = startoffset; tlc_fileoffset_t curoffset = startoffset;
LCFILEOFFSET lastmapsize = 0; tlc_fileoffset_t lastmapsize = 0;
LCLINECOUNT count = 0; tlc_linecount_t count = 0;
void *mem = NULL; void *mem = NULL;
while (curoffset < m_filesize) while (curoffset < m_filesize)
@ -274,7 +274,7 @@ unsigned int CLineCount::countThread(int thread_number)
} }
// Count newlines in buffer // Count newlines in buffer
LCFILEOFFSET windowoffset = 0; tlc_fileoffset_t windowoffset = 0;
size_t windowleft = mapsize; size_t windowleft = mapsize;
char *ptr = (char *)mem; char *ptr = (char *)mem;
while (windowleft > 0) while (windowleft > 0)
@ -359,7 +359,7 @@ bool CLineCount::createThread(int thread_number)
return true; return true;
} }
bool CLineCount::countLines(LCLINECOUNT & linecount) bool CLineCount::countLines(tlc_linecount_t & linecount)
{ {
// Determine file size // Determine file size
#ifdef _WIN32 #ifdef _WIN32
@ -388,8 +388,8 @@ bool CLineCount::countLines(LCLINECOUNT & linecount)
} }
// Figure out actual thread count // Figure out actual thread count
LCFILEOFFSET windowcount = (m_filesize + (m_parameters.buffersize - 1)) / m_parameters.buffersize; tlc_fileoffset_t windowcount = (m_filesize + (m_parameters.buffersize - 1)) / m_parameters.buffersize;
if (windowcount < (LCFILEOFFSET) m_parameters.threadcount) if (windowcount < (tlc_fileoffset_t) m_parameters.threadcount)
{ {
m_actual_thread_count = (int)windowcount; m_actual_thread_count = (int)windowcount;
} }
@ -470,7 +470,7 @@ bool CLineCount::countLines(LCLINECOUNT & linecount)
} }
// Static helpers // Static helpers
LCLINECOUNT CLineCount::LineCount(LCFILEHANDLE fhandle, errno_t * error, LCSTRING *errorstring) tlc_linecount_t CLineCount::LineCount(tlc_filehandle_t fhandle, tlc_error_t * error, tlc_string_t *errorstring)
{ {
CLineCount lc; CLineCount lc;
if (!lc.open(fhandle)) if (!lc.open(fhandle))
@ -487,7 +487,7 @@ LCLINECOUNT CLineCount::LineCount(LCFILEHANDLE fhandle, errno_t * error, LCSTRIN
return -1; return -1;
} }
LCLINECOUNT count; tlc_linecount_t count;
if (!lc.countLines(count)) if (!lc.countLines(count))
{ {
if (error) if (error)
@ -507,7 +507,7 @@ LCLINECOUNT CLineCount::LineCount(LCFILEHANDLE fhandle, errno_t * error, LCSTRIN
return count; return count;
} }
LCLINECOUNT CLineCount::LineCount(const TCHAR *filename, errno_t * error, LCSTRING *errorstring) tlc_linecount_t CLineCount::LineCount(const TCHAR *filename, tlc_error_t * error, tlc_string_t *errorstring)
{ {
CLineCount lc; CLineCount lc;
if (!lc.open(filename)) if (!lc.open(filename))
@ -524,7 +524,7 @@ LCLINECOUNT CLineCount::LineCount(const TCHAR *filename, errno_t * error, LCSTRI
return -1; return -1;
} }
LCLINECOUNT count; tlc_linecount_t count;
if (!lc.countLines(count)) if (!lc.countLines(count))
{ {
if (error) if (error)
@ -544,22 +544,22 @@ LCLINECOUNT CLineCount::LineCount(const TCHAR *filename, errno_t * error, LCSTRI
return count; return count;
} }
END_TURBO_LINECOUNT_NAMESPACE; END_TURBOLINECOUNT_NAMESPACE;
///////////////////////////// C Linkage ///////////////////////////// C Linkage
#ifndef _NO_TURBO_LINECOUNT_C #ifndef _NO_TURBOLINECOUNT_C
#ifdef _WIN32 #ifdef _WIN32
long long turbo_linecount_handle(HANDLE fhandle, errno_t * error, TCHAR ** errorstring) long long turbo_linecount_handle(HANDLE fhandle, tlc_error_t * error, TCHAR ** errorstring)
#else #else
long long turbo_linecount_handle(int fhandle, errno_t * error, char ** errorstring) long long turbo_linecount_handle(int fhandle, tlc_error_t * error, char ** errorstring)
#endif #endif
{ {
TURBO_LINECOUNT::LCSTRING errstr; TURBOLINECOUNT::tlc_string_t errstr;
long long linecount = TURBO_LINECOUNT::CLineCount::LineCount(fhandle, error, &errstr); long long linecount = TURBOLINECOUNT::CLineCount::LineCount(fhandle, error, &errstr);
if (errorstring) if (errorstring)
{ {
@ -570,14 +570,14 @@ long long turbo_linecount_handle(int fhandle, errno_t * error, char ** errorstri
} }
#ifdef _WIN32 #ifdef _WIN32
long long turbo_linecount_file(const TCHAR *filename, errno_t * error, TCHAR ** errorstring) long long turbo_linecount_file(const TCHAR *filename, tlc_error_t * error, TCHAR ** errorstring)
#else #else
long long turbo_linecount_file(const char *filename, errno_t * error, char ** errorstring) long long turbo_linecount_file(const char *filename, tlc_error_t * error, char ** errorstring)
#endif #endif
{ {
TURBO_LINECOUNT::LCSTRING errstr; TURBOLINECOUNT::tlc_string_t errstr;
long long linecount = TURBO_LINECOUNT::CLineCount::LineCount(filename, error, &errstr); long long linecount = TURBOLINECOUNT::CLineCount::LineCount(filename, error, &errstr);
if (errorstring) if (errorstring)
{ {

@ -22,54 +22,56 @@
#include<vector> #include<vector>
#include<errno.h> #include<errno.h>
#define BEGIN_TURBO_LINECOUNT_NAMESPACE namespace TURBO_LINECOUNT { #define BEGIN_TURBOLINECOUNT_NAMESPACE namespace TURBOLINECOUNT {
#define END_TURBO_LINECOUNT_NAMESPACE } #define END_TURBOLINECOUNT_NAMESPACE }
////////////// Platform specific ////////////// Platform specific
#ifdef _WIN32 // Windows #ifdef _WIN32 // Windows
#include<Windows.h> #include<Windows.h>
#include<tchar.h> #include<tchar.h>
typedef errno_t tlc_error_t;
#elif defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) // POSIX #elif defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) // POSIX
#include<unistd.h> #include<unistd.h>
#include<pthread.h> #include<pthread.h>
#define _T(x) x #define _T(x) x
#define TCHAR char #define TCHAR char
typedef tlc_error_t tlc_error_t;
#endif #endif
///////////////////////////////////////////// Line Count Class ///////////////////////////////////////////// Line Count Class
BEGIN_TURBO_LINECOUNT_NAMESPACE; BEGIN_TURBOLINECOUNT_NAMESPACE;
////////////// Platform specific ////////////// Platform specific
#ifdef _WIN32 // Windows #ifdef _WIN32 // Windows
#ifdef _UNICODE #ifdef _UNICODE
typedef std::wstring LCSTRING; typedef std::wstring tlc_string_t;
#else #else
typedef std::string LCSTRING; typedef std::string tlc_string_t;
#endif #endif
typedef HANDLE LCFILEHANDLE; typedef HANDLE tlc_filehandle_t;
typedef long long LCFILEOFFSET; typedef long long tlc_fileoffset_t;
typedef LCFILEOFFSET LCLINECOUNT; typedef tlc_fileoffset_t tlc_linecount_t;
#define LCLINECOUNTFMT "%I64d" #define TLC_LINECOUNT_FMT "%I64d"
#elif defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) // POSIX #elif defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) // POSIX
typedef std::string LCSTRING; typedef std::string tlc_string_t;
typedef int LCFILEHANDLE; typedef int tlc_filehandle_t;
#if (defined (__APPLE__) && defined (__MACH__)) #if (defined (__APPLE__) && defined (__MACH__))
typedef off_t LCFILEOFFSET; typedef off_t tlc_fileoffset_t;
#define LCLINECOUNTFMT "%lld" #define TLC_LINECOUNT_FMT "%lld"
#elif defined(__linux__) #elif defined(__linux__)
typedef off64_t LCFILEOFFSET; typedef off64_t tlc_fileoffset_t;
#define LCLINECOUNTFMT "%lld" #define TLC_LINECOUNT_FMT "%lld"
#else #else
typedef off_t LCFILEOFFSET; typedef off_t tlc_fileoffset_t;
#define LCLINECOUNTFMT "%d" #define TLC_LINECOUNT_FMT "%d"
#endif #endif
typedef LCFILEOFFSET LCLINECOUNT; typedef tlc_fileoffset_t tlc_linecount_t;
#endif #endif
@ -88,10 +90,10 @@ private:
bool m_opened; bool m_opened;
bool m_auto_close; bool m_auto_close;
LCFILEHANDLE m_fh; tlc_filehandle_t m_fh;
errno_t m_lasterror; tlc_error_t m_lasterror;
LCSTRING m_lasterrorstring; tlc_string_t m_lasterrorstring;
LCFILEOFFSET m_filesize; tlc_fileoffset_t m_filesize;
PARAMETERS m_parameters; PARAMETERS m_parameters;
int m_actual_thread_count; int m_actual_thread_count;
#ifdef _WIN32 #ifdef _WIN32
@ -100,12 +102,12 @@ private:
#else #else
std::vector<pthread_t> m_threads; std::vector<pthread_t> m_threads;
#endif #endif
std::vector<LCLINECOUNT> m_threadlinecounts; std::vector<tlc_linecount_t> m_threadlinecounts;
bool m_thread_fail; bool m_thread_fail;
private: private:
void setLastError(errno_t error, LCSTRING lasterrorstring); void setLastError(tlc_error_t error, tlc_string_t lasterrorstring);
void init(); void init();
bool createThread(int thread_number); bool createThread(int thread_number);
#ifdef _WIN32 #ifdef _WIN32
@ -121,23 +123,23 @@ public:
~CLineCount(); ~CLineCount();
bool isOpened() const; bool isOpened() const;
errno_t lastError() const; tlc_error_t lastError() const;
LCSTRING lastErrorString() const; tlc_string_t lastErrorString() const;
bool open(LCFILEHANDLE fhandle, bool auto_close = false); bool open(tlc_filehandle_t fhandle, bool auto_close = false);
bool open(const TCHAR * filename); bool open(const TCHAR * filename);
bool close(); bool close();
bool countLines(LCLINECOUNT &linecount); bool countLines(tlc_linecount_t &linecount);
public: public:
// Static utility functions // Static utility functions
static LCLINECOUNT LineCount(LCFILEHANDLE fhandle, errno_t * error = NULL, LCSTRING * errorstring = NULL); static tlc_linecount_t LineCount(tlc_filehandle_t fhandle, tlc_error_t * error = NULL, tlc_string_t * errorstring = NULL);
static LCLINECOUNT LineCount(const TCHAR *filename, errno_t * error = NULL, LCSTRING * errorstring = NULL); static tlc_linecount_t LineCount(const TCHAR *filename, tlc_error_t * error = NULL, tlc_string_t * errorstring = NULL);
}; };
END_TURBO_LINECOUNT_NAMESPACE; END_TURBOLINECOUNT_NAMESPACE;
#endif #endif
@ -151,11 +153,11 @@ extern "C"
#endif #endif
#ifdef _WIN32 #ifdef _WIN32
long long turbo_linecount_handle(HANDLE fhandle, errno_t * error = NULL, TCHAR ** errorstring = NULL); long long turbo_linecount_handle(HANDLE fhandle, tlc_error_t * error = NULL, TCHAR ** errorstring = NULL);
long long turbo_linecount_file(const TCHAR *filename, errno_t * error = NULL, TCHAR ** errorstring = NULL); long long turbo_linecount_file(const TCHAR *filename, tlc_error_t * error = NULL, TCHAR ** errorstring = NULL);
#else #else
long long turbo_linecount_handle(int fhandle, errno_t * error = NULL, char ** errorstring = NULL); long long turbo_linecount_handle(int fhandle, tlc_error_t * tlc_error = NULL, char ** errorstring = NULL);
long long turbo_linecount_file(const char *filename, errno_t * error = NULL, char ** errorstring = NULL); long long turbo_linecount_file(const char *filename, tlc_error_t * error = NULL, char ** errorstring = NULL);
#endif #endif

Loading…
Cancel
Save