/* Include XM compatibility mappings, used so we can support various
   (incompatible) versions of X-Midas. This file is likely to grow as the number
   of X-Midas versions increases.
   @author J. Czechowski IV / Neon Ngo
 */
#ifndef _xmcompat_h
#define _xmcompat_h

#include <midas.h>
#include "xmversion.h"
#include "tables.h"     /* for MTABLES definition */

#ifdef XM371
#include <fcinterface.h>
#endif

#ifndef XM_SYS_VERSION_AT_LEAST
/* this is copied from XM 4.4.4's xmversion.h so that we can use it for XM 3.8.9 support */
#define XM_SYS_VERSION_AT_LEAST(_maj,_min,_plvl) \
  (XM_SYS_VERSION >= MIDAS_VERSION_CODE(_maj,_min,_plvl))
#endif

#ifndef NULL
#define NULL ((void*)0)
#endif

#if defined(_IFC) || defined(_PGF77) || defined(_GFC)
#define MIDAS_COMMON_NAME m$common_
#define MIDAS_TABLES_NAME m$tables_
#define MIDAS_UNIQUE_NAME m$unique_
#else
#define MIDAS_COMMON_NAME Xm$common
#define MIDAS_TABLES_NAME Xm$tables
#define MIDAS_UNIQUE_NAME Xm$unique
#endif

#if NEW_PNAME_PTR /* (e.g. XM 4.2.1) */
#define GET_POINTER(ptr) ((ptr).p)
#define IS_NULL(ptr)     (GET_POINTER(ptr) == NULL)
#else /* (e.g. XM 3.8.3) */
#define M_PTR int_u4
#define GET_POINTER(ptr) (ptr)
#define IS_NULL(ptr)     (GET_POINTER(ptr) == 0)
#endif

extern char MIDAS_COMMON_NAME[]; /* Contains a pointer to the Mc structure. */
extern char MIDAS_UNIQUE_NAME[]; /* Contains a pointer to the Mu structure. */
extern char MIDAS_TABLES_NAME[]; /* Contains a pointer to the Mt structure. */

MCSTRUCT **mc;  /* Will set to point to the common block on initialization */
#ifdef XM371
  MUSTRUCT *mu; /* Will set to point to the unique block on initialization */
  MTSTRUCT **mt;
#else
  MUNIQUE *mu; /* Will set to point to the unique block on initialization */
  MTABLES **mt;
#endif

/* Length of a Fortran string as passed from Fortran code (64-bit support).
   This matches XM445 and XM455's typedef of fstring_len_t in fcinterface.h

   The implicit length argument for FORTRAN strings is passed as different
   sizes on different architectures and compilers.  We assume by default
   that an architecture will pass string lengths as a size_t; but some
   architectures pass string lengths as 32-bit integers.  The resulting
   typedef is used in the C++-FORTRAN translation jackets.
*/
#if XM_SYS_VERSION_AT_LEAST(4,5,5)
  /* fstring_len_t is defined in X-Midas 4.5.5+ */
#else
# if XM_SYS_VERSION_AT_LEAST(4,4,5) && !XM_SYS_VERSION_AT_LEAST(4,5,0)
  /* 2011-11-18: fstring_len_t is defined in X-Midas 4.4.5+ but not under 4.5.0 to 4.5.4 */
# else
  /* fstring_len_t is defined in X-Midas 4.4.5+ or 4.5.5 and greater */
  /* otherwise we have to define typedef for fstring_len_t for XM 4.4.4 and earlier */
#  if defined(_PGF77) || defined(_OSF) || defined(_GFC)
     typedef int fstring_len_t;
#  else
     typedef size_t fstring_len_t;
#  endif
# endif
#endif

#endif /* _xmcompat_h */
