////CSTRING.H
// Dream House World Static Model
#ifndef __CSTRING__
#define __CSTRING__
#include <iostream.h>
class StringIndexOutOfBounds { };
class CSTRING
{
public:
CSTRING( const char *cstring = "" ); // Constructor
CSTRING( const CSTRING & str ); // Copy constructor
~CSTRING( ) // Destructor
{ delete [ ] buffer; }
const CSTRING & operator+= ( const int number ); // apend number
const CSTRING & operator= ( const CSTRING & rhs ); // Copy
const CSTRING & operator+=( const CSTRING & rhs ); // Append
const char *c_str( ) const // Return C-style CSTRING
{ return buffer; }
int length( ) const // Return CSTRING length
{ return strLength; }
char operator[]( int k ) const; // Accessor operator[]
char & operator[]( int k ); // Mutator operator[]
enum { MAX_LENGTH = 1024 }; // Maximum length for input CSTRING
private:
char *buffer; // storage for characters
int strLength; // length of CSTRING (# of characters)
int bufferLength; // capacity of buffer
};
ostream & operator<<( ostream & out, const CSTRING & str ); // Output
istream & operator>>( istream & in, CSTRING & str ); // Input
istream & getline( istream & in, CSTRING & str ); // Read line
bool operator==( const CSTRING & lhs, const CSTRING & rhs ); // Compare ==
bool operator!=( const CSTRING & lhs, const CSTRING & rhs ); // Compare !=
bool operator< ( const CSTRING & lhs, const CSTRING & rhs ); // Compare <
bool operator<=( const CSTRING & lhs, const CSTRING & rhs ); // Compare <=
bool operator> ( const CSTRING & lhs, const CSTRING & rhs ); // Compare >
bool operator>=( const CSTRING & lhs, const CSTRING & rhs ); // Compare >=
#endif // __CSTRING__
////////////////////////////////////////////////////////////////////
//CSTRING.CPP
// Dream House World Static Model
#include <string.h>
#include <stdio.h>
#include "CSTRING.h"
CSTRING::CSTRING( const char * cstring )
{
if( cstring == NULL )
cstring = "";
strLength = strlen( cstring );
bufferLength = strLength + 1;
buffer = new char[ bufferLength ];
strcpy( buffer, cstring );
}
CSTRING::CSTRING( const CSTRING & str )
{
strLength = str.length( );
bufferLength = strLength + 1;
buffer = new char[ bufferLength ];
strcpy( buffer,str.buffer );
}
const CSTRING & CSTRING::operator=( const CSTRING & rhs )
{
if( this != &rhs )
{
if( bufferLength < rhs.length( ) + 1 )
{
delete [ ] buffer;
bufferLength = rhs.length( ) + 1;
buffer = new char[ bufferLength ];
}
strLength = rhs.length( );
strcpy( buffer, rhs.buffer );
}
return *this;
}
const CSTRING & CSTRING::operator+=( const CSTRING & rhs )
{
if( this == &rhs )
{
CSTRING copy( rhs );
return *this += copy;
}
int newLength = length( ) + rhs.length( );
if( newLength >= bufferLength )
{
bufferLength = 2 * ( newLength + 1 );
char *oldBuffer = buffer;
buffer = new char[ bufferLength ];
strcpy( buffer, oldBuffer );
delete [ ] oldBuffer;
}
strcpy( buffer + length( ), rhs.buffer );
strLength = newLength;
return *this;
}
const CSTRING & CSTRING::operator+=(const int number)
{
char temp[20];
sprintf(temp,"%d",number);
CSTRING num( temp );
*this += num;
return *this;
}
char & CSTRING::operator[ ]( int k )
{
if( k < 0 || k >= strLength )
throw StringIndexOutOfBounds( );
return buffer[ k ];
}
char CSTRING::operator[ ]( int k ) const
{
if( k < 0 || k >= strLength )
throw StringIndexOutOfBounds( );
return buffer[ k ];
}
ostream & operator<<( ostream & out, const CSTRING & str )
{
return out << str.c_str();
}
istream & operator>>( istream & in, CSTRING & str )
{
char buf[ CSTRING::MAX_LENGTH + 1 ];
in >> buf;
if( !in.fail( ) )
str = buf;
return in;
}
istream & getline( istream & in, CSTRING & str )
{
char buf[ CSTRING::MAX_LENGTH + 1 ];
in.getline( buf, CSTRING::MAX_LENGTH );
if( !in.fail( ) )
str = buf;
return in;
}
bool operator==( const CSTRING & lhs, const CSTRING & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) == 0;
}
bool operator!=( const CSTRING & lhs, const CSTRING & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) != 0;
}
bool operator<( const CSTRING & lhs, const CSTRING & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) < 0;
}
bool operator<=( const CSTRING & lhs, const CSTRING & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) <= 0;
}
bool operator>( const CSTRING & lhs, const CSTRING & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) > 0;
}
bool operator>=( const CSTRING & lhs, const CSTRING & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) >= 0;
}