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

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 "SctsURI.h"

void SctsURI::Parse(char *_uri)
{
	char *partStart;
	char c;
	SCTSASSERT(_uri);
	SCTSASSERT(sctsStrlen(_uri)<=MAX_URI_SIZE);
	if (scheme) sctsFree(scheme);
	if (authority) sctsFree(authority);
	if (path) sctsFree(path);
	if (query) sctsFree(query);
	if (fragment) sctsFree(fragment);
	scheme=authority=path=query=fragment=0;
	if (*_uri==':') return;
	partStart=sctsStrdup(_uri);
	_uri=partStart;
	
	/* Parse for scheme */
	c=*partStart;
	while (c!=':')
	{
		if (c=='\0' || c=='/' || c=='?' || c=='#') break;
		c=*++partStart;
	}
	if (c==':')
	{
		c=*++partStart;
		*partStart=0;
		scheme=sctsStrdup(_uri);
		*partStart=c;
	}
	else
	{
		partStart=_uri;
		c=*partStart;
	}
	c=*partStart;
	/* Parse for authority */
	if (*partStart=='/' && *(partStart+1)=='/')
	{
		authority=partStart;
		++partStart;
		c=*++partStart;
		while (c!='\0' && c!='/' && c!='?' && c!='#') c=*++partStart;
		*partStart=0;
		authority=sctsStrdup(authority);
		*partStart=c;
	}
	/* Parse for path */
	path=partStart;
	while (c!='\0' && c!='?' && c!='#') c=*++partStart;
	if (path!=partStart)
	{
		*partStart=0;
		path=sctsStrdup(path);
		*partStart=c;
	}
	else
		path=0;
	/* Parse for query */
	if (c=='?')
	{
		query=partStart;
		c=*++partStart;
		while (c!='\0' && c!='#') c=*++partStart;
		*partStart=0;
		query=sctsStrdup(query);
		*partStart=c;
	}
	/* Parse for fragment */
	if (c=='#') fragment=sctsStrdup(partStart);
	sctsFree(_uri);
}

char *SctsURI::Build()
{
	char *uri;
	int size=0;
	if (scheme) size+=sctsStrlen(scheme);
	if (authority) size+=sctsStrlen(authority);
	if (path) size+=sctsStrlen(path);
	if (query) size+=sctsStrlen(query);
	if (fragment) size+=sctsStrlen(fragment);
	if (!size) return 0;
	uri=(char *)sctsMalloc(size+sizeof(char));
	uri[0]=0;
	if (scheme) sctsStrcat(uri,scheme);
	if (authority) sctsStrcat(uri,authority);
	if (path) sctsStrcat(uri,path);
	if (query) sctsStrcat(uri,query);
	if (fragment) sctsStrcat(uri,fragment);
	return uri;
}

SctsURI::SctsURI(SctsURI& _uri)
{
	if (_uri.scheme)
		scheme=sctsStrdup(_uri.scheme);
	else
		scheme=0;
	if (_uri.authority)
		authority=sctsStrdup(_uri.authority);
	else
		authority=0;
	if (_uri.path)
		path=sctsStrdup(_uri.path);
	else
		path=0;
	if (_uri.query)
		query=sctsStrdup(_uri.query);
	else
		query=0;
	if (_uri.fragment)
		fragment=sctsStrdup(_uri.fragment);
	else
		fragment=0;
}

SctsURI::~SctsURI()
{
	if (scheme) sctsFree(scheme);
	if (authority) sctsFree(authority);
	if (path) sctsFree(path);
	if (query) sctsFree(query);
	if (fragment) sctsFree(fragment);
}

void SctsURI::Save(FILE *fp)
{
	sctsFWriteString(scheme,fp);
	sctsFWriteString(authority,fp);
	sctsFWriteString(path,fp);
	sctsFWriteString(query,fp);
	sctsFWriteString(fragment,fp);
}

SctsURI::SctsURI(FILE *fp)
{
	scheme=sctsFReadString(fp);
	authority=sctsFReadString(fp);
	path=sctsFReadString(fp);
	query=sctsFReadString(fp);
	fragment=sctsFReadString(fp);
}
