Move 'round()' / 'trunc()' etc so that they won't conflict with any versions already...
[ardour.git] / libs / pbd / msvc / msvc_pbd.cc
index 45137da0f3162e68fe9cb88489955eea162dac89..20065fff260215bce241f56f691311303a7720a0 100644 (file)
@@ -29,6 +29,7 @@ CreateHardLinkA( LPCSTR lpFileName,
 #include <algorithm>
 #include <string>
 #include <io.h>
+#include <math.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <stdlib.h>
@@ -48,7 +49,7 @@ struct timezone
        int  tz_dsttime;     /* type of dst correction */
 };
 
-PBD_API int PBD_APICALLTYPE
+LIBPBD_API int PBD_APICALLTYPE
 gettimeofday(struct timeval *__restrict tv, __timezone_ptr_t tz) // Does this need to be exported ?
 {
 FILETIME ft;
@@ -93,22 +94,22 @@ static int tzflag = 0;
 #ifndef PTHREAD_H   // Defined by PTW32 (Linux and other versions define _PTHREAD_H)
 #error "An incompatible version of 'pthread.h' is #included. Use only the Windows (ptw32) version!"
 #else
-bool operator>  (const pthread_t& lhs, const pthread_t& rhs)
+LIBPBD_API bool operator>  (const pthread_t& lhs, const pthread_t& rhs)
 {
        return (std::greater<void*>()(lhs.p, rhs.p));
 }
 
-bool operator<  (const pthread_t& lhs, const pthread_t& rhs)
+LIBPBD_API bool operator<  (const pthread_t& lhs, const pthread_t& rhs)
 {
        return (std::less<void*>()(lhs.p, rhs.p));
 }
 
-bool operator!= (const pthread_t& lhs, const pthread_t& rhs)
+LIBPBD_API bool operator!= (const pthread_t& lhs, const pthread_t& rhs)
 {
        return (std::not_equal_to<void*>()(lhs.p, rhs.p));
 }
 
-bool operator== (const pthread_t& lhs, const pthread_t& rhs)
+LIBPBD_API bool operator== (const pthread_t& lhs, const pthread_t& rhs)
 {
        return (!(lhs != rhs));
 }
@@ -165,7 +166,7 @@ char invert_forwardslash(char character)
 //    On Success: The number of bytes read from the file
 //    On Failure: -1
 //
-PBD_API ssize_t PBD_APICALLTYPE
+LIBPBD_API ssize_t PBD_APICALLTYPE
 pread(int handle, void *buf, size_t nbytes, off_t offset)
 {
 int old_errno;
@@ -200,7 +201,7 @@ ssize_t ret;
 //    On Success: The number of bytes written to the file
 //    On Failure: -1
 //
-PBD_API ssize_t PBD_APICALLTYPE
+LIBPBD_API ssize_t PBD_APICALLTYPE
 pwrite(int handle, const void *buf, size_t nbytes, off_t offset)
 {
 int old_errno;
@@ -222,6 +223,124 @@ ssize_t ret;
        return (ret);
 }
 
+#if defined(_MSC_VER) && (_MSC_VER < 1800)
+//***************************************************************
+//
+//     expm1()
+//
+// Emulates C99 expm1() using exp().
+//
+//     Returns:
+//
+//    On Success: (('e' raised to the power of 'x') - 1)
+//                (e.g. expm1(1) == 1.7182818).
+//    On Failure: None, except that calling exp(x) should generate
+//                an appropriate error for us (such as INF etc).
+//
+LIBPBD_API double PBD_APICALLTYPE
+expm1(double x)
+{
+       return (exp(x) - (double)1.0);
+}
+
+//***************************************************************
+//
+//     log1p()
+//
+// Emulates C99 log1p() using log().
+//
+//     Returns:
+//
+//    On Success: The natural logarithm of (1 + x)
+//                (e.g. log1p(1) == 0.69314718).
+//    On Failure: None, except that calling log(x) should generate
+//                an appropriate error for us (such as ERANGE etc).
+//
+LIBPBD_API double PBD_APICALLTYPE
+log1p(double x)
+{
+       return (log(x + (double)1.0));
+}
+
+//***************************************************************
+//
+//     roundf()
+//
+// Emulates roundf() using floorf().
+//
+//     Returns:
+//
+//    On Success: The largest integer that is less than or
+//                equal to 'x'.
+//    On Failure: None
+//
+LIBPBD_API float PBD_APICALLTYPE
+roundf(float x)
+{
+       return (floorf(x));
+}
+
+//***************************************************************
+//
+//     round()
+//
+// Emulates round() using floor().
+//
+//     Returns:
+//
+//    On Success: The largest integer that is less than or
+//                equal to 'x'.
+//    On Failure: None
+//
+LIBPBD_API double PBD_APICALLTYPE
+round(double x)
+{
+       return (floor(x));
+}
+#endif
+
+#if defined(_MSC_VER) && (_MSC_VER < 1900)
+//***************************************************************
+//
+//     log2()
+//
+// Emulates C99 log2() using log().
+//
+//     Returns:
+//
+//    On Success: The binary (base-2) logarithm of 'x'
+//                (e.g. log2(1024) == 10).
+//    On Failure: None, except that calling log(x) should generate
+//                an appropriate error for us (such as ERANGE etc).
+//
+LIBPBD_API double PBD_APICALLTYPE
+log2(double x)
+{
+       return (log(x) / log((double)2.0));
+}
+
+//***************************************************************
+//
+//     trunc()
+//
+// Emulates trunc() using floor() and ceil().
+//
+//     Returns:
+//
+//    On Success: The largest integer whose magnitude is less
+//                than or equal to 'x' (regardless of sign).
+//    On Failure: None
+//
+LIBPBD_API double PBD_APICALLTYPE
+trunc(double x)
+{
+       if (x < 0)
+               return (ceil(x));
+
+       return (floor(x));
+}
+#endif
+
 namespace PBD {
 
 //***************************************************************
@@ -236,7 +355,7 @@ namespace PBD {
 //    On Success: TRUE (if the user's OS matches the minimum spec)
 //    On Failure: FALSE otherwise
 //
-PBD_API bool PBD_APICALLTYPE
+LIBPBD_API bool PBD_APICALLTYPE
 TestForMinimumSpecOS(char *revision /* currently ignored */)
 {
 bool bRet = true;
@@ -270,7 +389,7 @@ bool bRet = true;
 //    On Success: A pointer to the resolved (absolute) path
 //    On Failure: NULL
 //
-PBD_API char* PBD_APICALLTYPE
+LIBPBD_API char* PBD_APICALLTYPE
 realpath (const char *original_path, char resolved_path[_MAX_PATH+1])
 {
 char *pRet = NULL;
@@ -313,7 +432,7 @@ bool bIsSymLink = 0; // We'll probably need to test the incoming path
 //    On Success: Pointer to a (heap based) DIR structure
 //    On Failure: NULL
 //
-PBD_API DIR* PBD_APICALLTYPE
+LIBPBD_API DIR* PBD_APICALLTYPE
 opendir (const char *szPath)
 {
 wchar_t wpath[PATH_MAX+1];
@@ -405,7 +524,7 @@ DIR *pDir = 0;
 //    On Success: A pointer to the supplied DIR's 'dirent' struct
 //    On Failure: NULL
 //
-PBD_API struct dirent* PBD_APICALLTYPE
+LIBPBD_API struct dirent* PBD_APICALLTYPE
 readdir (DIR* pDir)
 {
 int old_errno = 0;
@@ -483,7 +602,7 @@ errno = 0;
 //    On Success: 0
 //    On Failure: -1
 //
-PBD_API int PBD_APICALLTYPE
+LIBPBD_API int PBD_APICALLTYPE
 closedir (DIR *pDir)
 {
 int rc = 0;
@@ -517,7 +636,7 @@ int rc = 0;
 //    On Success: A file descriptor for the opened file.
 //    On Failure: (-1)
 //
-PBD_API int PBD_APICALLTYPE
+LIBPBD_API int PBD_APICALLTYPE
 mkstemp (char *template_name)
 {
 int ret = (-1);
@@ -548,7 +667,7 @@ char szTempPath[PATH_MAX+100]; // Just ensure we have plenty of buffer space
 //    On Success: Non-zero.
 //    On Failure: Zero (call 'GetLastError()' to retrieve info)
 //
-PBD_API int PBD_APICALLTYPE
+LIBPBD_API int PBD_APICALLTYPE
 ntfs_link (const char *existing_filepath, const char *link_filepath)
 {
 int ret = 1; // 'ERROR_INVALID_FUNCTION'
@@ -678,7 +797,7 @@ bool bValidPath = false;
 //    On Success: Non-zero.
 //    On Failure: Zero (call 'GetLastError()' to retrieve info)
 //
-PBD_API int PBD_APICALLTYPE
+LIBPBD_API int PBD_APICALLTYPE
 ntfs_unlink (const char *link_filepath)
 {
 int ret = 1; // 'ERROR_INVALID_FUNCTION'
@@ -801,7 +920,7 @@ bool bValidPath = false;
 //    On Success: A handle to the opened DLL
 //    On Failure: NULL
 //
-PBD_API void* PBD_APICALLTYPE
+LIBPBD_API void* PBD_APICALLTYPE
 dlopen (const char *file_name, int mode)
 {
        // Note that 'mode' is ignored in Win32
@@ -820,7 +939,7 @@ dlopen (const char *file_name, int mode)
 //    On Success: A non-zero number
 //    On Failure: 0
 //
-PBD_API int PBD_APICALLTYPE
+LIBPBD_API int PBD_APICALLTYPE
 dlclose (void *handle)
 {
        return (::FreeLibrary((HMODULE)handle));
@@ -838,7 +957,7 @@ dlclose (void *handle)
 //    On Success: A pointer to the found function or symbol
 //    On Failure: NULL
 //
-PBD_API void* PBD_APICALLTYPE
+LIBPBD_API void* PBD_APICALLTYPE
 dlsym (void *handle, const char *symbol_name)
 {
        // First test for RTLD_DEFAULT and RTLD_NEXT
@@ -864,7 +983,7 @@ static char szLastWinError[LOCAL_ERROR_BUF_SIZE];
 //                last error
 //    On Failure: NULL (if the last error was ERROR_SUCCESS)
 //
-PBD_API char* PBD_APICALLTYPE
+LIBPBD_API char* PBD_APICALLTYPE
 dlerror ()
 {
        DWORD dwLastErrorId = GetLastError();