From 35ff780a3d76b0c149b16e3beac92186e08a962f Mon Sep 17 00:00:00 2001
From: Jan Schlamelcher <jan.schlamelcher@uni-oldenburg.de>
Date: Sun, 4 Aug 2013 15:23:24 +0200
Subject: [PATCH] Minor fixes required for compiling via mingw.

---
 CMake/3rdparty.cmake               | 6 +++---
 dcmimage/libsrc/diqtfs.cc          | 6 ++++++
 oflog/libsrc/strcloc.cc            | 8 ++++----
 ofstd/include/dcmtk/ofstd/offile.h | 2 +-
 ofstd/include/dcmtk/ofstd/ofstd.h  | 2 ++
 ofstd/libsrc/offname.cc            | 4 ++++
 ofstd/libsrc/ofstd.cc              | 2 ++
 7 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/CMake/3rdparty.cmake b/CMake/3rdparty.cmake
index ed9e658..f435090 100755
--- a/CMake/3rdparty.cmake
+++ b/CMake/3rdparty.cmake
@@ -1,4 +1,4 @@
-IF(WIN32)
+IF(WIN32 AND NOT MINGW)
 
   # For Windows, we don't used FIND_PACKAGE because DCMTK usually is used with its
   # own set of 3rd-party support libraries that can be downloaded from DCMTK's
@@ -139,7 +139,7 @@ IF(WIN32)
     ENDIF(WITH_LIBICONVINC)
   ENDIF(DCMTK_WITH_ICONV)
 
-ELSE(WIN32)
+ELSE(WIN32 AND NOT MINGW)
 
   # Find TIFF
   IF(DCMTK_WITH_TIFF)
@@ -266,7 +266,7 @@ ELSE(WIN32)
     ENDIF(NOT WRAP_LIBS)
   ENDIF(DCMTK_WITH_WRAP)
 
-ENDIF(WIN32)
+ENDIF(WIN32 AND NOT MINGW)
 
 # Find doxygen
 IF(DCMTK_WITH_DOXYGEN)
diff --git a/dcmimage/libsrc/diqtfs.cc b/dcmimage/libsrc/diqtfs.cc
index 2fda6be..ff73dd5 100644
--- a/dcmimage/libsrc/diqtfs.cc
+++ b/dcmimage/libsrc/diqtfs.cc
@@ -90,9 +90,15 @@ OFCondition DcmQuantFloydSteinberg::initialize(unsigned long cols)
 
   for (unsigned long col = 0; col < columns + 2; ++col)
   {
+#ifndef rand_r
       thisrerr[col] = OFStandard::rand_r(now) % ( DcmQuantFloydSteinbergScale * 2 ) - DcmQuantFloydSteinbergScale;
       thisgerr[col] = OFStandard::rand_r(now) % ( DcmQuantFloydSteinbergScale * 2 ) - DcmQuantFloydSteinbergScale;
       thisberr[col] = OFStandard::rand_r(now) % ( DcmQuantFloydSteinbergScale * 2 ) - DcmQuantFloydSteinbergScale;
+#else
+      thisrerr[col] = rand_r(now) % ( DcmQuantFloydSteinbergScale * 2 ) - DcmQuantFloydSteinbergScale;
+      thisgerr[col] = rand_r(now) % ( DcmQuantFloydSteinbergScale * 2 ) - DcmQuantFloydSteinbergScale;
+      thisberr[col] = rand_r(now) % ( DcmQuantFloydSteinbergScale * 2 ) - DcmQuantFloydSteinbergScale;
+#endif
       /* (random errors in [-1 .. 1]) */
   }
   fs_direction = 1;
diff --git a/oflog/libsrc/strcloc.cc b/oflog/libsrc/strcloc.cc
index f363e0c..e25917c 100644
--- a/oflog/libsrc/strcloc.cc
+++ b/oflog/libsrc/strcloc.cc
@@ -78,22 +78,22 @@ tostring_internal (STD_NAMESPACE string & result, wchar_t const * src, size_t si
 }
 
 
-STD_NAMESPACE string 
+OFString
 tostring (const STD_NAMESPACE wstring & src)
 {
     STD_NAMESPACE string ret;
     tostring_internal (ret, src.c_str (), src.size ());
-    return ret;
+    return ret.c_str();
 }
 
 
-STD_NAMESPACE string 
+OFString
 tostring (wchar_t const * src)
 {
     assert (src);
     STD_NAMESPACE string ret;
     tostring_internal (ret, src, wcslen (src));
-    return ret;
+    return ret.c_str();
 }
 
 
diff --git a/ofstd/include/dcmtk/ofstd/offile.h b/ofstd/include/dcmtk/ofstd/offile.h
index 5f65b55..7cf8e33 100644
--- a/ofstd/include/dcmtk/ofstd/offile.h
+++ b/ofstd/include/dcmtk/ofstd/offile.h
@@ -70,7 +70,7 @@ END_EXTERN_C
   #endif
 #endif
 
-#if defined(_WIN32) && !defined(__MINGW32__)
+#if defined(_WIN32) && (!defined(__MINGW32__) || defined(__MINGW64__))
   // On Win32 systems except MinGW (where Posix definitions are available)
   // we use Win32 specific definitions
   typedef __int64 offile_off_t;
diff --git a/ofstd/include/dcmtk/ofstd/ofstd.h b/ofstd/include/dcmtk/ofstd/ofstd.h
index 78b6d48..ba744bc 100644
--- a/ofstd/include/dcmtk/ofstd/ofstd.h
+++ b/ofstd/include/dcmtk/ofstd/ofstd.h
@@ -648,7 +648,9 @@ class DCMTK_OFSTD_EXPORT OFStandard
      *  @param seed pointer to seed of random number generator, must not be NULL.
      *  @return pseudo-random number in the range of 0 to myrandr_max.
      */
+#ifndef rand_r
     static int rand_r(unsigned int &seed);
+#endif
 
     /// maximum value that can be returned by rand_r()
     static const unsigned int rand_max;
diff --git a/ofstd/libsrc/offname.cc b/ofstd/libsrc/offname.cc
index 77872af..e254cbd 100644
--- a/ofstd/libsrc/offname.cc
+++ b/ofstd/libsrc/offname.cc
@@ -82,7 +82,11 @@ OFBool OFFilenameCreator::makeFilename(unsigned int &seed, const char *dir, cons
     }
     if (prefix) filename += prefix;
     addLongToString(creation_time, filename);
+#ifndef rand_r
     addLongToString(((OFStandard::rand_r(seed) << 16) | OFStandard::rand_r(seed)), filename);
+#else
+    addLongToString(((rand_r(seed) << 16) | rand_r(seed)), filename);
+#endif
     if (postfix) filename += postfix;
     
     // check if filename exists
diff --git a/ofstd/libsrc/ofstd.cc b/ofstd/libsrc/ofstd.cc
index d80e665..784803d 100644
--- a/ofstd/libsrc/ofstd.cc
+++ b/ofstd/libsrc/ofstd.cc
@@ -2028,6 +2028,7 @@ long OFStandard::getProcessID()
 
 const unsigned int OFStandard::rand_max = 0x7fffffff;
 
+#ifndef rand_r
 int OFStandard::rand_r(unsigned int &seed)
 {
   unsigned long val = OFstatic_cast(unsigned long, seed);
@@ -2035,6 +2036,7 @@ int OFStandard::rand_r(unsigned int &seed)
   seed = OFstatic_cast(unsigned int, val %(OFstatic_cast(unsigned long, 0x80000000)));
   return OFstatic_cast(int, seed);
 }
+#endif
 
 #define MAX_NAME 65536
 
-- 
1.8.1.msysgit.1

