Skip to content

Commit

Permalink
libmythbase changes to allow compiling with MSVC.
Browse files Browse the repository at this point in the history
* Added some new #include & #define Statements
* Removed some inline functions and #defines from the main source tree.
* Cleaned up the VERBOSE macros so they work in both GCC & MSVC using same code
  • Loading branch information
dblain committed Feb 24, 2011
1 parent 6fe46da commit e0d8047
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 108 deletions.
93 changes: 25 additions & 68 deletions mythtv/libs/libmythbase/compat.h
Expand Up @@ -11,9 +11,15 @@
#endif

#ifdef _MSC_VER
typedef __int64 int64_t; // Define it from MSVC's internal type

#define uint64_t __int64
#ifdef restrict
#undef restrict
#endif

#include <inttypes.h>
#include <direct.h>
#include <process.h>

#define strtoll _strtoi64
#define strncasecmp _strnicmp
#define snprintf _snprintf
Expand All @@ -24,6 +30,22 @@
typedef int ssize_t;
#endif

// Check for execute, only checking existance in MSVC
#define X_OK 0

#define rint( x ) floor(x + 0.5)
#define round( x ) floor(x + 0.5)
#define getpid() _getpid()
#define ftruncate( fd, fsize ) _chsize( fd, fsize )

#ifndef S_ISCHR
# ifdef S_IFCHR
# define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
# else
# define S_ISCHR(m) 0
# endif
#endif /* !S_ISCHR */

#endif

#ifdef _WIN32
Expand Down Expand Up @@ -75,30 +97,6 @@ typedef unsigned int uint;
#undef DeleteFile
#endif

// Dealing with Microsoft min/max mess:
// assume that under Windows the code is compiled with NOMINMAX defined
// which disables #define's for min/max.
// however, Microsoft violates the C++ standard even with
// NOMINMAX on, and defines templates _cpp_min and _cpp_max
// instead of templates min/max
// define the correct templates here

#if defined(__cplusplus) && defined(_WIN32) && !defined(USING_MINGW)
template<class _Ty> inline
const _Ty& max(const _Ty& _X, const _Ty& _Y)
{return (_X < _Y ? _Y : _X); }
template<class _Ty, class _Pr> inline
const _Ty& max(const _Ty& _X, const _Ty& _Y, _Pr _P)
{return (_P(_X, _Y) ? _Y : _X); }

template<class _Ty> inline
const _Ty& min(const _Ty& _X, const _Ty& _Y)
{return (_Y < _X ? _Y : _X); }
template<class _Ty, class _Pr> inline
const _Ty& min(const _Ty& _X, const _Ty& _Y, _Pr _P)
{return (_P(_Y, _X) ? _Y : _X); }
#endif // defined(__cplusplus) && defined(_WIN32)

#ifdef _WIN32
#undef M_PI
#define M_PI 3.14159265358979323846
Expand All @@ -123,25 +121,6 @@ inline int random(void)
#define unsetenv(x) 0
#endif

#if defined(__cplusplus) && defined(_MSC_VER)
inline unsigned int usleep( unsigned int us )
{
Sleep( (us + 999) / 1000 );
return 0;
}

inline int close( int fd )
{
return _close( fd );
}

inline int write( int fd, const void *buffer, unsigned int count )
{
return _write( fd, buffer, count );
}

#endif

#if defined(__cplusplus) && defined(USING_MINGW)
inline unsigned sleep(unsigned int x)
{
Expand Down Expand Up @@ -315,29 +294,7 @@ inline const char *dlerror(void)
#endif

#if defined(_MSC_VER)
#define EPOCHFILETIME (116444736000000000i64)

#define gettimeofday( tv, tz ) \
{ \
FILETIME ft; \
LARGE_INTEGER li; \
__int64 t; \
static int tzflag; \
\
if (tv) \
{ \
GetSystemTimeAsFileTime(&ft); \
li.LowPart = ft.dwLowDateTime; \
li.HighPart = ft.dwHighDateTime; \
t = li.QuadPart; \
t -= EPOCHFILETIME; \
t /= 10; \
tv->tv_sec = (long)(t / 1000000); \
tv->tv_usec = (long)(t % 1000000); \
} \
\
}

# define S_IRUSR _S_IREAD
#endif

#ifdef USING_MINGW
Expand Down
11 changes: 5 additions & 6 deletions mythtv/libs/libmythbase/mythcorecontext.cpp
Expand Up @@ -13,6 +13,11 @@
#include <algorithm>
using namespace std;

#ifdef USING_MINGW
#include <winsock2.h>
#include <unistd.h>
#endif

#include "compat.h"
#include "mythconfig.h" // for CONFIG_DARWIN
#include "mythcorecontext.h"
Expand All @@ -22,12 +27,6 @@ using namespace std;

#include "mythversion.h"

#ifdef USING_MINGW
#include <winsock2.h>
#include <unistd.h>
#include "compat.h"
#endif

#define LOC QString("MythCoreContext: ")
#define LOC_WARN QString("MythCoreContext, Warning: ")
#define LOC_ERR QString("MythCoreContext, Error: ")
Expand Down
52 changes: 18 additions & 34 deletions mythtv/libs/libmythbase/mythverbose.h
Expand Up @@ -131,34 +131,18 @@ extern MBASE_PUBLIC unsigned int print_verbose_messages;

#ifndef MYTHCONTEXT_H_
#ifdef __cplusplus
#ifdef WIN32
#define VERBOSE(mask, ...) \
do { \
if (VERBOSE_LEVEL_CHECK(mask)) \
{ \
QDateTime dtmp = QDateTime::currentDateTime(); \
QString dtime = dtmp.toString("yyyy-MM-dd hh:mm:ss.zzz"); \
verbose_mutex.lock(); \
std::cout << dtime.toLocal8Bit().constData() << " " \
<< QString(__VA_ARGS__).toLocal8Bit().constData() << std::endl; \
verbose_mutex.unlock(); \
} \
} while (0)

#else
#define VERBOSE(mask,args...) \
do { \
if (VERBOSE_LEVEL_CHECK(mask)) \
{ \
QDateTime dtmp = QDateTime::currentDateTime(); \
QString dtime = dtmp.toString("yyyy-MM-dd hh:mm:ss.zzz"); \
verbose_mutex.lock(); \
std::cout << dtime.toLocal8Bit().constData() << " " \
<< QString(args).toLocal8Bit().constData() << std::endl; \
verbose_mutex.unlock(); \
} \
} while (0)
#endif
#define VERBOSE(mask, ...) \
do { \
if (VERBOSE_LEVEL_CHECK(mask)) \
{ \
QDateTime dtmp = QDateTime::currentDateTime(); \
QString dtime = dtmp.toString("yyyy-MM-dd hh:mm:ss.zzz"); \
verbose_mutex.lock(); \
std::cout << dtime.toLocal8Bit().constData() << " " \
<< QString(__VA_ARGS__).toLocal8Bit().constData() << std::endl; \
verbose_mutex.unlock(); \
} \
} while (0)
#else
#if HAVE_GETTIMEOFDAY
#define VERBOSEDATE \
Expand All @@ -175,12 +159,12 @@ extern MBASE_PUBLIC unsigned int print_verbose_messages;
#else
#define VERBOSEDATE ;
#endif
#define VERBOSE(mask,args...) \
#define VERBOSE(mask, ...) \
do { \
if (VERBOSE_LEVEL_CHECK(mask)) \
{ \
VERBOSEDATE \
printf(args); \
printf(__VA_ARGS__); \
putchar('\n'); \
} \
} while (0)
Expand All @@ -194,15 +178,15 @@ extern MBASE_PUBLIC unsigned int print_verbose_messages;
// VERBOSE macro, since those threads may wish to use the VERBOSE macro
// and this will cause a deadlock.

#define VERBOSE(mask,args...) \
#define VERBOSE(mask, ...) \
do { \
if (VERBOSE_LEVEL_CHECK(mask)) \
{ \
QDateTime dtmp = QDateTime::currentDateTime(); \
QString dtime = dtmp.toString("yyyy-MM-dd hh:mm:ss.zzz"); \
verbose_mutex.lock(); \
std::cout << dtime.toLocal8Bit().constData() << " " \
<< QString(args).toLocal8Bit().constData() << std::endl; \
<< QString(__VA_ARGS__).toLocal8Bit().constData() << std::endl; \
verbose_mutex.unlock(); \
} \
} while (0)
Expand All @@ -211,14 +195,14 @@ extern MBASE_PUBLIC unsigned int print_verbose_messages;

// use a slower non-deadlockable version in release builds

#define VERBOSE(mask,args...) \
#define VERBOSE(mask, ...) \
do { \
if (VERBOSE_LEVEL_CHECK(mask)) \
{ \
QDateTime dtmp = QDateTime::currentDateTime(); \
QString dtime = dtmp.toString("yyyy-MM-dd hh:mm:ss.zzz"); \
QTextStream ssMsg(&dtime); \
ssMsg << " " << args; \
ssMsg << " " << __VA_ARGS__; \
verbose_mutex.lock(); \
std::cout << ssMsg.string()->toLocal8Bit().constData() << \
std::endl; \
Expand Down

0 comments on commit e0d8047

Please sign in to comment.