mud++0.35/etc/
mud++0.35/etc/guilds/
mud++0.35/help/propert/
mud++0.35/mudC/
mud++0.35/player/
mud++0.35/src/interface/
mud++0.35/src/os/cygwin32/
mud++0.35/src/os/win32/
mud++0.35/src/os/win32/bcppbuilder/
mud++0.35/src/osaddon/
mud++0.35/src/util/
/*
....[@@@..[@@@..............[@.................. MUD++ is a written from
....[@..[@..[@..[@..[@..[@@@@@....[@......[@.... scratch multi-user swords and
....[@..[@..[@..[@..[@..[@..[@..[@@@@@..[@@@@@.. sorcery game written in C++.
....[@......[@..[@..[@..[@..[@....[@......[@.... This server is an ongoing
....[@......[@..[@@@@@..[@@@@@.................. development project.  All 
................................................ contributions are welcome. 
....Copyright(C).1995.Melvin.Smith.............. Enjoy. 
------------------------------------------------------------------------------
Melvin Smith (aka Fusion)         msmith@hom.net 
MUD++ development mailing list    mudpp@van.ml.org
------------------------------------------------------------------------------
index.cc
*/

#include "config.h"
#include "string.h"
#include "io.h"
#include "index.h"


Index::Index( const Index & x )
:	scope(0), key(0)
{
	if( x.scope )
	{
		scope = new char[ strlen( x.scope ) + 1 ];
		strcpy( scope, x.scope );
	}	

	if( x.key )
	{
		key = new char[ strlen( x.key ) + 1 ];
		strcpy( key, x.key );
	}
}


Index::Index( const String & str )
:	scope(0), key(0)
{
	parse_index_string( str );
}


Index::Index( const char * str )
:	scope(0), key(0)
{
	parse_index_string( str );
}


Index::Index( const String & strScope, const String & strKey )
: scope( strScope.dup() ), key( strKey.dup() )
{
}


Index::Index( const char * strScope, const char * strKey )
: scope( new char[ strlen( strScope ) + 1 ] ),
  key( new char[ strlen( strKey ) + 1 ] )
{
	strcpy( scope, strScope );
	strcpy( key, strKey );
}


void Index::setScope( const char * x )
{
	if( scope )
		delete [] scope;
	scope = new char[ strlen( x ) + 1 ];
	strcpy( scope, x );
}


void Index::setScope( const String & x )
{
	if( scope )
		delete [] scope;
	scope = x.dup();
}


void Index::setKey( const char * x )
{
	if( key )
		delete [] key;
	key = new char[ strlen( x ) + 1 ];
	strcpy( key, x );
}


void Index::setKey( const String & x )
{
	if( key )
		delete [] key;
	key = x.dup();
}


const Index & Index::operator = ( const Index & x )
{ 
	if( &x != this )
	{
		if( x.scope )
		{
			if( scope )
				delete [] scope;
			scope = new char[ strlen( x.scope ) + 1 ];
			strcpy( scope, x.scope );
		}
		
		if( x.key )
		{
			if( key )
				delete [] key;
			key = new char[ strlen( x.key ) + 1 ];
			strcpy( key, x.key );
		}
	}
	return *this;
}


const Index & Index::operator = ( const String & x )
{ 
	parse_index_string( x );
	return *this;
}


const Index & Index::operator = ( const char * x )
{ 
	parse_index_string( x );
	return *this;
}


bool Index::operator == ( const Index & sIndex ) const
{
	if( scope && sIndex.scope )
		if( strcmp( scope, sIndex.scope ) ) 
			return false;

	if( !key || !sIndex.key )
		return false;

	if( strcmp( key, sIndex.key ) ) 
		return false;

	return true;
}


Output & operator << ( Output & out, const Index & x )
{
	out << ( x.scope ? x.scope : '\0' ) << ':' << ( x.key ? x.key : '\0' );
	return out;
}


const String Index::getScope() const
{
	String ret;
	if( scope )
		ret = scope;
	return ret;
}

const String Index::getKey() const
{
	String ret;
	if( key )
		ret = key;
	return ret;
}


const String Index::asString() const
{
	String ret;
	if( scope )
		ret = scope;
	ret += ':';
	if( key )
		ret += key;
	return ret;
}



// Convert a contiguous string to an Index object
// String is in format:
//   1) "<scope-name>:<key-name>
//   2) ":<key-name>"
//   3) "<key-name>"
// Colons are not checked in key name. The first colon is assumed to
// terminate the scope name. The rest of the string is copied in the
// key name. No colon assumes the the whole string is an key string

void Index::parse_index_string( const String & x )
{
	char strScope[ BUF ];
	char strKey[ BUF ];
	int i = 0;
	int j = 0;

	while( 1 )
	{
		if( x[i] == ':' )
		{
			// ':' found so terminate scope scope and break out to parse index
			strScope[j] = '\0';
			break;
		}
		else if( !x[i] )
		{
			// No ':' found so interpret as a plain index (no scope scope)
			scope = new char('\0');
			strScope[j] = '\0';
            key = new char[ strlen( strScope ) + 1 ];
			strcpy( key, strScope );
			return;
		}
		strScope[j] = x[i];
		i++; j++;
	}

	i++; j = 0;
	while( x[i] )
	{
		strKey[j] = x[i];
		i++; j++;
	}

	strKey[j] = '\0';
	key = new char[ strlen( strKey ) + 1 ];
	strcpy( key, strKey );
	scope = new char[ strlen( strScope ) + 1 ];
	strcpy( scope, strScope );
}


// Convert a contiguous char * to an Index object
// Same as above.

void Index::parse_index_string( const char * x )
{
	char strScope[ BUF ];
	char strKey[ BUF ];
	int i = 0;
	int j = 0;

	while( 1 )
	{
		if( x[i] == ':' )
		{
			// ':' found so terminate scope scope and break out to parse index
			strScope[j] = '\0';
			break;
		}
		else if( !x[i] )
		{
			// No ':' found so interpret as a plain index (no scope scope)
			scope = new char('\0');
			strScope[j] = '\0';
			key = new char[ strlen( strScope ) + 1 ];
			strcpy( key, strScope );
			return;
		}
		strScope[j] = x[i];
		i++; j++;
	}

	i++; j = 0;
	while( x[i] )
	{
		strKey[j] = x[i];
		i++; j++;
	}

	strKey[j] = '\0';
	key = new char[ strlen( strKey ) + 1 ];
	strcpy( key, strKey );
	scope = new char[ strlen( strScope ) + 1 ];
	strcpy( scope, strScope );
}