/*******************************************************************

Copyright (c) 2004 Open Mobile Alliance, Ltd.  All Rights Reserved.

This software is made available under the usage and distribution terms 
covered by the Common Public License 1.0 and by using this software you
are bound by the terms and conditions of the same.

EXCEPT AS EXPRESSLY SET FORTH IN THE COMMON PUBLIC LICENSE,
THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 
ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, 
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
   
Each recipient is solely responsible for determining the 
appropriateness of using and distributing this program and assumes 
all risks associated with its exercise of rights under the Common 
Public License, including but not limited to the risks and 
costs of program errors, compliance with applicable laws, 
damage to or loss of data, programs or equipment, and 
unavailability or interruption of operations.

********************************************************************/
/*************************************************************************/
/* module:          SyncML xpt portability header file                   */
/* file:            src/xpt/inc/xptport.h                                */
/* target system:   all                                                  */
/* target OS:       all                                                  */
/*************************************************************************/


/**
 * Isolate here the differences between ANSI string and memory functions and
 * Palm string and memory functions.  Note the sml tree contains similar
 * mappings, but we avoid those for two reasons:
 *
 *  1. They are all implemented as functions, which makes calling them less
 *     efficient, especially on the Palm.  Also, if they are implemented as
 *     functions, then the xpt DLL and the DLLs of all transport
 *     implementations must be linked to the sml DLL, which seems unnecessary.
 *
 *  2. Even on the Palm, the libstr.h and libmem.h header files include ANSI C
 *     header files like string.h and stdlib.h.  These shouldn't be included
 *     on the Palm, because with some compilers, the introduced typedefs
 *     conflict with typedefs from the Palm SDK, causing compilation errors.
 *
 * We start the names of these functions with a different prefix so they are
 * not confused with functions of the official xpt interface.  We use "xpp"
 * here instead of "xpt".
 */

#ifndef XPTPORT_H
#define XPTPORT_H

#include <xptdef.h>

/*
 * If we neglect some of the return values, most of the functions have simple
 * mappings.
 */
#ifdef __PALM_OS__

 /* Palm mappings */

 #include <StringMgr.h>
 #include <MemoryMgr.h>

 #define xppStrcpy(target, source)        StrCopy(target, source)
 #define xppStrncpy(target, source, cnt)  StrNCopy(target, source, cnt)
 #define xppStrcat(target, source)        StrCat(target, source)
 #define xppStrncat(target, source, cnt)  StrNCat(target, source, cnt)
 #define xppStrcmp(first, second)         StrCompare(first, second)
 #define xppStrncmp(first, second, cnt)   StrNCompare(first, second, cnt)
 #define xppStrchr(string, c)             StrChr(string, c)
 #define xppStrstr(string, substr)        StrStr(string, substr)
 #define xppStrlen(string)                StrLen(string)
 #define xppAtoi(string)                  StrAToI(string)

 #define xppMemset(s, c, n)               MemSet(s, n, c)
 #define xppMemcpy(target, source, cnt)   MemMove(target, source, cnt)
 #define xppMemmove(target, source, cnt)  MemMove(target, source, cnt)
 #define xppMemcmp(target, source, cnt)   MemCmp(target, source, cnt)
 #define xppMalloc(size)                  MemPtrNew(size)
 XPTDECLEXP1 void * XPTAPI xppRealloc(void *ptr, size_t size) XPT_SECTION;
 #define xppFree(ptr)                     MemPtrFree(ptr)

 #define xppStricmp(first, second)        StrCaselessCompare(first, second)
 #define xppMemicmp(first, second, cnt)   StrNCaselessCompare(first, second, cnt)

#endif
#if !defined(__EPOC_OS__) && !defined(__PALM_OS__)

 /* ANSI C mappings */

 #include <string.h>
 #include <stdlib.h>

 #define xppStrcpy(target, source)        strcpy(target, source)
 #define xppStrncpy(target, source, cnt)  strncpy(target, source, cnt)
 #define xppStrcat(target, source)        strcat(target, source)
 #define xppStrncat(target, source, cnt)  strncat(target, source, cnt)
 #define xppStrcmp(first, second)         strcmp(first, second)
 #define xppStrncmp(first, second, cnt)   strncmp(first, second, cnt)
 #define xppStrchr(string, c)             strchr(string, c)
 #define xppStrstr(string, substr)        strstr(string, substr)
 #define xppStrlen(string)                strlen(string)
 #define xppAtoi(string)                  atoi(string)

 #define xppMemset(s, c, n)               memset(s, c, n)
 #define xppMemcpy(target, source, cnt)   memcpy(target, source, cnt)
 #define xppMemmove(target, source, cnt)  memmove(target, source, cnt)
 #define xppMemcmp(target, source, cnt)   memcmp(target, source, cnt)
 #define xppMalloc(size)                  malloc(size)
 #define xppRealloc(ptr, size)            realloc(ptr, size)
 #define xppFree(ptr)                     free(ptr)

 /* These aren't ANSI C functions, but they're pretty common */

 #ifdef _WIN32
  #define xppStricmp(first, second)       stricmp(first, second)
 #endif

 /* Most other systems call it strcasecmp */
 #ifndef xppStricmp
  #define xppStricmp(first, second)       strcasecmp(first, second)
 #endif

 #ifdef _WIN32
  #define xppMemicmp(first, second, cnt)  memicmp(first, second, cnt)
 #endif

 #ifndef xppMemicmp
  #define xppMemicmp(first, second, cnt)  strncasecmp(first, second, cnt)
 #endif

#endif

#if defined(__EPOC_OS__)

/* EPOC mappings */

#include <string.h>
#include <stdlib.h>

#define xppStrcpy(target, source)			strcpy(target, source)
#define xppStrncpy(target, source, cnt)		strncpy(target, source, cnt)
#define xppStrcat(target, source)			strcat(target, source)
#define xppStrncat(target, source, cnt)		strncat(target, source, cnt)
#define xppStrcmp(first, second)			strcmp(first, second)
#define xppStrncmp(first, second, cnt)		strncmp(first, second, cnt)
#define xppStrchr(string, c)				strchr(string, c)
#define xppStrstr(string, substr)			strstr(string, substr)
#define xppStrlen(string)					strlen(string)
#define xppAtoi(string)						atoi(string)

#define xppMemset(s, c, n)					memset(s, c, n)
#define xppMemcpy(target, source, cnt)		memcpy(target, source, cnt)
#define xppMemmove(target, source, cnt)		memmove(target, source, cnt)
#define xppMemcmp(target, source, cnt)		memcmp(target, source, cnt)
#define xppMalloc(size)						malloc(size)
#define xppRealloc(ptr, size)				realloc(ptr, size)
#define xppFree(ptr)						free(ptr)

#define xppStricmp(first, second)			strcasecmp(first, second)
#define xppMemicmp(first, second, cnt)		strncasecmp(first, second, cnt)

#endif

#endif
