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

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.

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

#include "SctsPlatformLib.h"

/******************************************************************
Memory Functions
******************************************************************/

/* Win32 String Functions */
#ifdef __SCTS_PLATFORM_WIN32__

#include "string.h"
#include "stdlib.h"

void sctsFree(void *pObject)
{
	if (!pObject) return;
	free(pObject);
}

void *sctsRealloc(void *pObject, MemSize_t constSize)
{
	return realloc(pObject, constSize);
}

void *sctsMemset(void *pObject, int value, MemSize_t count)
{
	return memset(pObject, value, count);
}

void *sctsMemcpy(void *pTarget, const void *pSource, MemSize_t count)
{
	return memcpy(pTarget, pSource, count);
}

void *sctsMemmove(void *pTarget, const void *pSource, MemSize_t count)
{
	return memmove(pTarget, pSource, count);
}

int sctsMemcmp(const void *pTarget, const void *pSource, MemSize_t count)
{
	return memcmp(pTarget, pSource, count);
}

void *sctsMalloc(MemSize_t size)
{
	return (void *)malloc(size);
}

#endif

/******************************************************************
String Functions
******************************************************************/

/* Win32 String Functions */
#ifdef __SCTS_PLATFORM_WIN32__
#include "time.h"
#include "stdio.h"

char *sctsStrdup(const char *constStringP)
{
	char *_new_str;
	if (!constStringP) return 0;

	// allocate memory for new copy
	_new_str = (char *)sctsMalloc(sctsStrlen(constStringP) + 1);
	
	// Copy the string into the new memory
	if (_new_str != NULL)
		sctsStrcpy(_new_str, constStringP);
	   
	return _new_str;
}


char *sctsStrcpy(const char *pTarget, const char *pSource) 
{
	return strcpy((char *)pTarget, (char *)pSource);
}

char *sctsStrncpy(const char *pTarget, const char *pSource, int count)
{
	return strncpy((char *)pTarget, (char *)pSource, count);
}

char *sctsStrcat(const char *pTarget, const char *pSource)
{
	return strcat((char *)pTarget, (char *)pSource);
}

char *sctsStrncat(const char *pTarget, const char *pSource, int count)
{
	return strncat((char *)pTarget, (char *)pSource, count);
}

char *sctsStrchr(const char *pString, char character)
{
	return strchr((char *)pString, character);
}

char *sctsStrstr(const char *pTarget, const char *pSource)
{
	return strstr((char *)pTarget, (char *)pSource);
}

int sctsStrcspn(const char *pTarget, const char *pSource)
{
	return strcspn((char *)pTarget, (char *)pSource);
}

int sctsStrcmp(const char *pTarget, const char *pSource)
{
	return strcmp((char *)pTarget, (char *)pSource);
}

int sctsStrncmp(const char *pTarget, const char *pSource, int count)
{
	return strncmp((char *)pTarget, (char *)pSource, count);
}

int sctsStrlen(const char *pString)
{
	return strlen((char *)pString);
}

char *sctsInt2Str(int i,char *buffer,int radix)
{
	return itoa(i,buffer,radix);
}

char *sctsLong2Str(long l,char *buffer,int radix)
{
	return ltoa(l,buffer,radix);
}

int sctsStr2Int(const char *pString)
{
	if (!pString) return 0;
	return atoi(pString);
}

long sctsStr2Long(const char *pString)
{
	if (!pString) return 0;
	return atol(pString);
}

char *sctsGetTimeStamp(bool local,long t)
{
	char buffer[20];
	time_t current;
	struct tm *gm;
	if (t)
		current=t;
	else
		time(&current);
	if (local)
		gm=localtime(&current);
	else
		gm=gmtime(&current);
	sprintf(buffer,"%d%02d%02dT%02d%02d%02d%c",gm->tm_year+1900,gm->tm_mon+1,gm->tm_mday,gm->tm_hour,gm->tm_min,gm->tm_sec,local?'L':'Z');
	return sctsStrdup(buffer);
}

long sctsGetTime()
{
	return time(0);
}
	
char *sctsFReadString(FILE *fp)
{
	int len;
	char *str;
	fread(&len,sizeof(int),1,fp);
	if (!len) return 0;
	str=(char *)sctsMalloc(len+sizeof(char));
	str[len]=0;
	fread(str,sizeof(char),len,fp);
	return str;
}

void sctsFWriteString(const char *str,FILE *fp)
{
	int len=0;
	if (!str)
	{
		fwrite(&len,sizeof(int),1,fp);
		return;
	}
	len=sctsStrlen(str);
	fwrite(&len,sizeof(int),1,fp);
	fwrite(str,sizeof(char),len,fp);
}

#ifdef __SCTS_USING_RTK__
#include "sml.h"
#include "wsm.h"

/* Initialize the toolkit */
static bool sctsInitializeRTK()
{
	SmlOptions_t smlOptions;
	smlOptions.defaultPrintFunc=0;
	smlOptions.maxWorkspaceAvailMem=MAX_WORKSPACE*MAX_WORKSPACE_SIZE;
	return (smlInit(&smlOptions)==0);
}

bool sctsRTKInitialized=sctsInitializeRTK();

#endif

#endif
