changeset 150:9bc7f60f3013

More portability stuff. :|
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 07 Jan 2015 20:21:27 +0200
parents 6a0e9980b76b
children 434c0eae87c9
files th_network.c th_types.h
diffstat 2 files changed, 58 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/th_network.c	Wed Jan 07 17:04:31 2015 +0200
+++ b/th_network.c	Wed Jan 07 20:21:27 2015 +0200
@@ -943,8 +943,8 @@
     
     fprintf(f,
     "\n--------------------------------------------------------------\n"
-    "err=%d, status=%d, got_bytes=%zd, total_bytes=%zd\n"
-    "buf=0x%p, in_ptr=0x%04zx, ptr=0x%04zx\n",
+    "err=%d, status=%d, got_bytes=%" TH_PRIu_SIZE_T ", total_bytes=%" TH_PRIu_SIZE_T "\n"
+    "buf=0x%p, in_ptr=0x%04" TH_PRIx_SIZE_T ", ptr=0x%04" TH_PRIx_SIZE_T "\n",
     conn->err, conn->status, conn->base.got_bytes, conn->base.total_bytes,
     conn->base.buf, conn->base.in_ptr - conn->base.buf, conn->base.ptr - conn->base.buf);
     
--- a/th_types.h	Wed Jan 07 17:04:31 2015 +0200
+++ b/th_types.h	Wed Jan 07 20:21:27 2015 +0200
@@ -11,18 +11,67 @@
 #ifndef _TH_TYPES_H
 #define _TH_TYPES_H
 
+// Check for standard headers
 #ifdef HAVE_STDINT_H
-#include <stdint.h>
-#ifndef HAVE_INT_TYPES
-#define HAVE_INT_TYPES 1
-#endif
+#  include <stdint.h>
+#  ifndef HAVE_INT_TYPES
+#    define HAVE_INT_TYPES 1
+#  endif
+// Check for arch bitness
+#  if UINTPTR_MAX == 0xffffffff
+#    define TH_ARCH 32
+#  elif UINTPTR_MAX == 0xffffffffffffffff
+#    define TH_ARCH 64
+#  endif
 #endif
 
 #ifdef HAVE_SYS_TYPES_H
-#include <sys/types.h>
-#ifndef HAVE_INT_TYPES
-#define HAVE_INT_TYPES 1
+#  include <sys/types.h>
+#  ifndef HAVE_INT_TYPES
+#    define HAVE_INT_TYPES 1
+#  endif
+#endif
+
+#if !defined(TH_ARCH)
+#  if defined(__LP64__) || defined(_LP64)
+#    define TH_ARCH 64
+#  else
+#    define TH_ARCH 32
+#  endif
+#endif
+
+
+#if !defined(TH_ARCH) && (defined(__WIN32) || defined(_WIN32))
+#  define TH_ARCH 32
 #endif
+
+
+// Do we have a valid arch?
+// If so, set some printf specifiers
+#if TH_ARCH == 32
+#  define TH_ARCH_32BIT    1
+#  define TH_PRIu32        "u"
+#  define TH_PRId32        "d"
+#  define TH_PRIx32        "x"
+#  define TH_PRIu64        "llu"
+#  define TH_PRId64        "lld"
+#  define TH_PRIx64        "llx"
+#  define TH_PRIu_SIZE_T   "u"
+#  define TH_PRId_SSIZE_T  "d"
+#  define TH_PRIx_SIZE_T   "x"
+#elif TH_ARCH == 64
+#  define TH_ARCH_64BIT    1
+#  define TH_PRIu32        "u"
+#  define TH_PRId32        "d"
+#  define TH_PRIx32        "x"
+#  define TH_PRIu64        "lu"
+#  define TH_PRId64        "ld"
+#  define TH_PRIx64        "lx"
+#  define TH_PRIu_SIZE_T   "lu"
+#  define TH_PRId_SSIZE_T  "ld"
+#  define TH_PRIx_SIZE_T   "lx"
+#else
+#  error Could not determine architecture (32/64bit), please define TH_ARCH=32 or 64
 #endif
 
 // Shorthand types