// cString.cpp
// 15 de Novembro de 2000
// Versão 2 da classe cString

#include <stdlib.h>
#include <string.h>
#include <iostream.h>
#include "cString.h"


cString::cString()
{
	pStr = ( char *) malloc( sizeof(char) );
	*pStr = '\0';
}


cString::cString( const char *s )
{
	pStr = strdup(s);
}


cString::cString( cString& s)
{
	pStr = strdup(s.pStr );
}

cString::~cString()
{
	free( pStr );
}

int cString::Comprimento ()
{	
	return strlen( pStr );
}


void cString::operator=( const cString& s )
{

	free( pStr );

	pStr = strdup(s.pStr );
}


void cString::operator+=( const cString& s )
{
	int lenDest = strlen(pStr); 
	int lenSrc = strlen(s.pStr );

	pStr=(char *) realloc( pStr, ( lenDest + lenSrc + 1 ) * sizeof(char) );
	
	strcat( pStr , s.pStr );	
}


// friend functions

cString operator+(  const cString& s1,  const cString& s2 )
{
	cString res;
	
	res = s1;
	res += s2;

	return res;          
}

ostream& operator<<( ostream& os, const cString &s )
{
	return os << s.pStr ;
}
