view th_types.h @ 663:284d5b789b7c

Add th_char_t type.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 27 Jan 2020 07:47:26 +0200
parents 6d99150a8f89
children 1d4d22c862ed
line wrap: on
line source

/*
 * Type definations
 * Programmed and designed by Matti 'ccr' Hamalainen
 * (C) Copyright 2002-2020 Tecnic Software productions (TNSP)
 *
 * Please read file 'COPYING' for information on license and distribution.
 *
 * If your code uses "config.h", you need to #include
 * it before including this header.
 */
/// @file
/// @brief Various platform-specific type and macro definitions.
#ifndef TH_TYPES_H
#define TH_TYPES_H 1


// Check for system type
#if defined(__WIN64) || defined(_WIN64) || defined(__WIN32) || defined(_WIN32)
#  define TH_PLAT_WINDOWS 1
#elif defined(__DJGPP__) && __DJGPP__ >= 2
#  define TH_PLAT_DOS 1
#  undef __STRICT_ANSI__
#else
#  define TH_PLAT_UNIX 1
#endif


// Check for standard headers
#if defined(HAVE_INTTYPES_H)
#  include <inttypes.h>
#  ifndef HAVE_STDINT_H
#    define HAVE_STDINT_H 1
#  endif
#elif defined(HAVE_STDINT_H)
#  include <stdint.h>
#elif defined(HAVE_SYS_TYPES_H)
#  include <sys/types.h>
#endif


// Check for arch bitness
#if !defined(TH_ARCH) && (defined(__WIN64) || defined(_WIN64))
#  define TH_ARCH 64
#endif

#if !defined(TH_ARCH) && (defined(__WIN32) || defined(_WIN32))
#  define TH_ARCH 32
#endif

#if !defined(TH_ARCH)
#  if UINTPTR_MAX == 0xffffffff
#    define TH_ARCH 32
#  elif UINTPTR_MAX == 0xffffffffffffffff
#    define TH_ARCH 64
#  endif
#endif


#if !defined(TH_ARCH)
#  if defined(__LP64__) || defined(_LP64)
#    define TH_ARCH 64
#  else
#    define TH_ARCH 32
#  endif
#endif


// Do we have a valid arch?
// If we don't have ISO C99 inttypes.h, define PRI* macros
#ifndef HAVE_INTTYPES_H
#  if TH_ARCH == 32
#    define PRIu32        "u"
#    define PRId32        "d"
#    define PRIx32        "x"
#    define PRIi32        "i"
#    define PRIo32        "o"
#    define PRIu64        "llu"
#    define PRId64        "lld"
#    define PRIx64        "llx"
#    define PRIi64        "lli"
#    define PRIo64        "llo"
#  elif TH_ARCH == 64
#    define PRIu32        "u"
#    define PRId32        "d"
#    define PRIx32        "x"
#    define PRIi32        "i"
#    define PRIo32        "o"
#    define PRIu64        "lu"
#    define PRId64        "ld"
#    define PRIx64        "lx"
#    define PRIi64        "li"
#    define PRIo64        "lo"
#  else
#    error Could not determine architecture (32/64bit), please define TH_ARCH=32 or 64
#  endif
#endif


// Define some printf specifiers and other useful things
#if TH_ARCH == 32
#  define PRIu_SIZE_T     PRIu32
#  define PRId_SSIZE_T    PRId32
#  define PRIx_SIZE_T     PRIx32
#  define PRIu_OFF_T      PRIu32
#  define PRIx_OFF_T      PRId32
#  define PRIX_OFF_T      PRIx32
#  ifndef TH_PTRSIZE
#    define TH_PTRSIZE 32
#  endif
#  ifndef INTPTR_MIN
#    define INTPTR_MIN    (-0x7fffffffL - 1)
#    define INTPTR_MAX    ( 0x7fffffffL)
#    define UINTPTR_MAX   ( 0xffffffffUL)
#  endif
#elif TH_ARCH == 64
#  define PRIu_SIZE_T     PRIu64
#  define PRId_SSIZE_T    PRId64
#  define PRIx_SIZE_T     PRIx64
#  define PRIu_OFF_T      PRIu64
#  define PRIx_OFF_T      PRId64
#  define PRIX_OFF_T      PRIx64
#  ifndef TH_PTRSIZE
#    define TH_PTRSIZE 64
#  endif
#  ifndef INTPTR_MIN
#    define INTPTR_MIN    (-0x7fffffffffffffffL - 1)
#    define INTPTR_MAX    ( 0x7fffffffffffffffL)
#    define UINTPTR_MAX   ( 0xffffffffffffffffUL)
#  endif
#endif


/* Default assumptions for these types should be ok for most 32/64bit platforms...
 */
#if !defined(HAVE_STDINT_H) && !defined(HAVE_SYS_TYPES_H)
typedef unsigned char uint8_t;
typedef signed char int8_t;
typedef unsigned short int uint16_t;
typedef signed short int int16_t;
typedef unsigned int uint32_t;
typedef signed int int32_t;
typedef unsigned long long uint64_t;
typedef signed long long int64_t;

#if TH_ARCH == 32
typedef long long int intmax_t;
typedef unsigned long long int uintmax_t;
#elif TH_ARCH == 64
typedef long int intmax_t;
typedef unsigned long int uintmax_t;
#endif
#endif


/* Define a boolean type, if needed
 */
#if !defined(FALSE) && !defined(TRUE) && !defined(BOOL)
typedef enum { FALSE = 0, TRUE = 1 } BOOL;
#endif

#ifndef BOOL
#    ifdef bool
#        define BOOL bool
#    else
#        define BOOL int
#    endif
#endif


/** @brief th_char_t
 * Character type. Currently it is not recommended to re-define this,
 * but in distant future it may be possible to change to uint32_t for
 * Unicode 32bit handling.
 */
#ifdef TH_CHAR_TYPE
typedef TH_CHAR_TYPE th_char_t;
#else
typedef char th_char_t;
#endif


#endif // TH_TYPES_H