#ifndef _DWC_MACRO_H
#define _DWC_MACRO_H

/*--------------------------------------------------------------------------
 *  Copyright (c) 2001-2004 Igor Daniloff. 
 *	All rights reserved.
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the LICENSE file which can be found at this directory.
 *------------------------------------------------------------------------*/

/* $Id: dwc_macro.h,v 1.68 2007/08/27 10:08:54 akorkov Exp $ */

/* #define DW_DEBUGMALLOC */

#if defined(__UNIX_DW)
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#if defined(FreeBSD_DW)
#include <sys/param.h>
#endif /* FreeBSD */
#if defined(Solaris_DW) || defined(FreeBSD_DW)
#include <limits.h>
#endif /* Solaris or FreeBSD */
#endif /* __UNIX_DW */
#if defined(__WIN32_DW)
#include <windows.h>
#include <process.h>
#include <io.h>
#endif /* __WIN32_DW */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#if defined(EMX_DW) || defined(__WIN32_DW)
#include <limits.h>
#endif /* EMX or WIN32 */
#if defined(__UNIX_DW) && !defined(EMX_DW)
#include <regex.h>
#endif /* UNIX and not EMX */
#include "dwc_type.h"
#include "dwc_err.h"
#include "dwc_log.h"
#if defined(EMX_DW) || defined(__WIN32_DW)
#include "regex.h"
#endif /* EMX or WIN32 */

#ifdef  __cplusplus
extern "C" {
#endif

#define NOP                         do{}while(0)
#define BEGIN_MACRO                 do{
#define END_MACRO                   }while(0)

/* -------------------------------------------------------------------------- */
#define EXIT_IF_1PARAM( expr, prefix, exit_expr )\
BEGIN_MACRO\
    if( (expr) )\
    {\
        printLib( DW_LL_ERROR, "%s (%s:%d) bad parameter was passed", prefix, __FILE__, __LINE__ );\
        exit_expr;\
    }\
END_MACRO    
/* -------------------------------------------------------------------------- */
#define EXIT_IF_2PARAM( expr_1, expr_2, prefix, exit_expr )\
BEGIN_MACRO\
    if( (expr_1) || (expr_2) )\
    {\
        printLib( DW_LL_ERROR, "%s bad parameters (%d.%d) were passed (%s:%d)", prefix, (expr_1), (expr_2), __FILE__, __LINE__ );\
        exit_expr;\
    }\
END_MACRO    
/* -------------------------------------------------------------------------- */
#define EXIT_IF_3PARAM( expr_1, expr_2, expr_3, prefix, exit_expr )\
BEGIN_MACRO\
    if( (expr_1) || (expr_2) || (expr_3) )\
    {\
        printLib( DW_LL_ERROR, "%s bad parameters (%d.%d.%d) were passed (%s:%d) ", prefix, (expr_1), (expr_2), (expr_3), __FILE__, __LINE__ );\
        exit_expr;\
    }\
END_MACRO    
/* -------------------------------------------------------------------------- */
#define EXIT_IF_4PARAM( expr_1, expr_2, expr_3, expr_4, prefix, exit_expr )\
BEGIN_MACRO\
    if( (expr_1) || (expr_2) || (expr_3) || (expr_4) )\
    {\
        printLib( DW_LL_ERROR, "%s bad parameters (%d.%d.%d.%d) were passed (%s:%d)", prefix, (expr_1), (expr_2), (expr_3), (expr_4), __FILE__, __LINE__ );\
        exit_expr;\
    }\
END_MACRO    
/* -------------------------------------------------------------------------- */
#define EXIT_IF_5PARAM( expr_1, expr_2, expr_3, expr_4, expr_5, prefix, exit_expr )\
BEGIN_MACRO\
    if( (expr_1) || (expr_2) || (expr_3) || (expr_4) || (expr_5) )\
    {\
        printLib( DW_LL_ERROR, "%s bad parameters (%d.%d.%d.%d.%d) were passed (%s:%d)", prefix, (expr_1), (expr_2), (expr_3), (expr_4), __FILE__, __LINE__ );\
        exit_expr;\
    }\
END_MACRO    
  //#undef DW_DEBUGMALLOC
#if defined(DW_DEBUGMALLOC)
#define DWC_Strdup( str )           dw_strdup( (str), __FILE__, __LINE__ )
#define DWC_Malloc( type, size )    ((type) dw_malloc( (size), __FILE__, __LINE__ ))
#define DWC_Free( obj )             BEGIN_MACRO dw_free( (obj), __FILE__, __LINE__ ); (obj) = NULL; END_MACRO
#else /* not DEBUGMALLOC */
#define DWC_Strdup( str )           strdup( (str) )
#define DWC_Malloc( type, size )    ((type) malloc( (size) ))
#define DWC_Free( obj )             BEGIN_MACRO free( (obj) ); (obj) = NULL; END_MACRO
#endif /* DEBUGMALLOC */

#define DWC_Max( a, b )             ( ((a) > (b)) ? (a) : (b) )
#define DWC_Min( a, b )             ( ((a) > (b)) ? (b) : (a) )

#define DWC_strcmp( s1, s2 )        ( ((s1) && (s2)) ? strcmp( (s1), (s2) ) : -1 )
#define DWC_strncmp( s1, s2, n )    ( ((s1) && (s2)) ? strncmp( (s1), (s2), (n) ) : -1 )
#define DWC_strcasecmp( s1, s2 )    ( ((s1) && (s2)) ? strcasecmp( (s1), (s2) ) : -1 )
#define DWC_strncasecmp( s1, s2, n ) ( ((s1) && (s2)) ? strncasecmp( (s1), (s2), (n) ) : -1 )
#define DWC_strlen( s )             ( strlen(s) + 1 )

#define SEC_TO_TIMEOUT( to )        ( ( (to) > 0 ) ? (to) * 1000 : INFINITE_TIMEOUT )
#define TIMEOUT_TO_SEC( to )        ( ( (to) >= 0 ) ? (to) / 1000 : (to) )
#define VALID_TIMEOUT( to )         ( ( ( ((to) >= NOWAIT_TIMEOUT)\
                                          && ((to) <= MAX_TIMEOUT) )\
                                        || ((to) == INFINITE_TIMEOUT)\
                                        || ((to) == EXPIRED_TIMEOUT) )\
                                      ? TRUE : FALSE )

#define dw_bs_empty( b )            ( (b) = 0 )
#define dw_bs_set( b, f )           ( (b) = (f) )
#define dw_bs_mask( b, f )          ( (b) &= (f) )
#define dw_bs_add( b, f )           ( (b) |= (f) )
#define dw_bs_del( b, f )           ( (b) &= ~(f) )
#define dw_bm_contain( f, mask )    ( ((f) & (mask)) != 0 ? TRUE : FALSE )
#define dw_bm_include( f, mask )    ( ((f) & (mask)) == (mask) ? TRUE : FALSE )
#define dw_bm_equal( f, mask )      ( (f) == (mask) ? TRUE : FALSE )

#define NULLCHAR                    '\000'
#if defined(__UNIX_DW) && !defined(EMX_DW)
#define PATH_SEPARATOR              '/'
#undef DW_HAVE_SEPARATOR_2
#else /* not UNIX */
#define PATH_SEPARATOR              '\\'
#define DW_HAVE_SEPARATOR_2         1
#define PATH_SEPARATOR_2            '/'
#endif /* UNIX */

#if defined(__WIN32_DW) || defined(EMX_DW)
#define DW_NEWLINE                  "\r\n"
#endif /* WIN32 or EMX */
#if defined(__UNIX_DW) && !defined(EMX_DW)
#define DW_NEWLINE                  "\n"
#endif /* UNIX and not EMX */

#define IO_BUF_LEN                  4096
#define CHARS_IN_INT                10
#define DISABLE                     (0)
#define ENABLE                      (1)

#ifndef STDIN_FILENO
#define STDIN_FILENO                fileno(stdin)
#endif /* STDIN_FILENO */

#ifndef STDOUT_FILENO
#define STDOUT_FILENO               fileno(stdout)
#endif /* STDOUT_FILENO */

#ifndef STDERR_FILENO
#define STDERR_FILENO               fileno(stderr)
#endif /* STDERR_FILENO */

/* number of error then try to create unique filename */
#define NUM_OPEN_ERROR              10 

#if defined(__WIN32_DW) || defined(EMX_DW)
#define EX_USAGE                    64
#define EX_DATAERR                  65
#define EX_UNAVAILABLE              69
#define EX_SOFTWARE                 70
#define EX_OSERR                    71
#define EX_TEMPFAIL                 75
#define EX_CONFIG                   78
#define access_file( path, mode )   _access( (path), (mode) )
#define xopen_file( fd, fl, mode )  _open( (fd), (fl) | O_BINARY, (mode) )
#define open_file( fd, fl )         _open( (fd), (fl) | O_BINARY )
#define read_file( fd, buf, len )   _read( (fd), (buf), (len) )
#define lseek_file( fd, off, w )    _lseek( (fd), (off), (w) )
#define close_file( fd )            _close( (fd) )
#if defined(__WIN32_DW)
#define fd_to_handle( fd )          _get_osfhandle( (fd) )
#endif /* WIN32 */
#else /* !(__WIN32_DW || EMX) */
#define access_file( path, mode )   access( (path), (mode) )
#define xopen_file( fd, fl, mode )  open( (fd), (fl), (mode) )
#define open_file( fd, fl )         open( (fd), (fl) )
#define read_file( fd, buf, len )   read( (fd), (buf), (len) )
#define lseek_file( fd, off, w )    lseek( (fd), (off), (w) )
#define close_file( fd )            close( (fd) )
#endif /* __WIN32_DW || EMX */

#if defined(EMX_DW)
#define PATH_MAX                    (255)
#define realpath(p,rp)              strcpy((rp),(p))
#define strncasecmp                 strnicmp
#define strcasecmp                  stricmp
#endif /* EMX */

#if defined(__WIN32_DW)
#define fsync( fd )                 0
#define fchmod( fd, mod )           0
#define snprintf                    _snprintf
#define vsnprintf                   _vsnprintf
#define strncasecmp                 _strnicmp
#define strcasecmp                  _stricmp
#define R_OK                        (02)
#define W_OK                        (04)
#define PATH_MAX                    _MAX_PATH
#ifdef SD_BOTH
#define SHUT_RDWR                   SD_BOTH
#else /* SD_BOTH */
#define SHUT_RDWR                   (2)
#endif /* SD_BOTH */
#else /* !__WIN32_DW */
#ifndef SHUT_RDWR
#define SHUT_RDWR                   (2)
#endif /* SHUT_RDWR */
#endif /* __WIN32_DW */

#ifdef  __cplusplus
}
#endif

#endif /* _DWC_MACRO_H */
