/**********************************************************************
 *
 * gsdlconf.h -- system specific includes
 * Copyright (C) 1999  The New Zealand Digital Library Project
 *
 * A component of the Greenstone digital library software
 * from the New Zealand Digital Library Project at the
 * University of Waikato, New Zealand.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *********************************************************************/


#ifndef GSDLCONF_H
#define GSDLCONF_H

#define GSDL_VERSION "svn-trunk"

#if defined(__WIN32__) || defined(_WIN32) || defined(_WIN32_) || defined(WIN32)
#ifndef __WIN32__
#define __WIN32__ 1
#endif
#endif

#if defined(_MSC_VER) 
#include "../../win32cfg.h"
#else
#include "../../config.h"
#endif

// Note: all macros used in the gsdl software (except the _H macros) should
// now start with GSDL_

// GSDL_NEED_STRINGS_H should be defined if some string functions
// needed (like strcasecmp) are in strings.h
#if defined(__IBMCPP__)
#  define GSDL_NEED_STRINGS_H
#endif

// GSDL_USE_IOS_H should be defined if the .h extension needs to
// be used for the io stream headers
#if (defined(__GNUG__)  && __GNUG__ + 0 < 3) || defined(__IBMCPP__)
#  define GSDL_USE_IOS_H 1
#endif

// GSDL_USE_STL_H should be defined if the .h extension needs to
// be used for the stl header files
#if defined(__IBMCPP__)
#  define GSDL_USE_STL_H 1
#endif

// GSDL_USE_ALGO_H should be defined if algo.h should be used 
// instead of algorithm.h
#if defined(__IBMCPP__)
#  define GSDL_USE_ALGO_H 1
#endif

// GSDL_NAMESPACE_BROKEN should be defined if namespaces should
// not be used
#if (defined(__GNUG__)  && __GNUG__ + 0 < 3) || defined(__IBMCPP__)
#  define GSDL_NAMESPACE_BROKEN 1
#endif

// GSDL_USE_OBJECTSPACE should now be used instead of USE_OBJECTSPACE
#if defined(USE_OBJECTSPACE)
#  define GSDL_USE_OBJECTSPACE
#endif

// GSDL_NEED_DESTROY_USHORT should be defined if the compiler needs
// a definition for void destroy(short unsigned int *)
#if defined(__IBMCPP__)
# define GSDL_NEED_DESTROY_USHORT
#endif

// mktemp

#if !defined(__WIN32__) || defined(__GNUC__)
#include <unistd.h>
#define GSDL_MKTEMP(str) mktemp(str)
#else
#include <io.h>
#define GSDL_MKTEMP(str) _mktemp(str)
#endif



// file locking

#if defined(__WIN32__)

#if !defined(LK_UNLOCK) && defined(LK_UNLCK)
#define LK_UNLOCK LK_UNLCK
#endif

#include <io.h>
#include <sys/locking.h>

#define GSDL_UNLOCK_FILE(fd) _locking(fd, LK_UNLCK, 200)
#define GSDL_LOCK_FILE(fd) lock_val = _locking(fd, LK_NBLCK, 200)

#else     /* not WIN32 */

#if GSDL_USE_FLOCK
#include <sys/lock.h>

#ifndef LOCK_SH
#define LOCK_SH	1
#endif

#ifndef LOCK_EX
#define LOCK_EX	2
#endif

#ifndef LOCK_NB
#define LOCK_NB 4
#endif

#ifndef LOCK_UN
#define LOCK_UN 8
#endif

#define GSDL_UNLOCK_FILE(fd) flock (fd, LOCK_UN)
#define GSDL_LOCK_FILE(fd) lock_val = flock (fd, LOCK_EX + LOCK_NB)

#else  /* ! GSDL_USE_FLOCK */
// use fcntl
#include <fcntl.h>
#define GSDL_UNLOCK_FILE(fd) \
	{					\
	  struct flock flock;			\
	  flock.l_type = F_UNLCK;		\
	  flock.l_whence = SEEK_SET;		\
	  flock.l_start = flock.l_len = 0L;	\
	  fcntl (fd, F_SETLK, &flock);	\
	}
#define GSDL_LOCK_FILE(fd) \
	{					\
	  struct flock flock;			\
	  flock.l_type = F_WRLCK;		\
	  flock.l_whence = SEEK_SET;			\
	  flock.l_start = flock.l_len = 0L;	\
	  lock_val = fcntl (fd, F_SETLK, &flock);	\
	}

#endif
#endif

#endif
