MSCUString.h

class MSCUString : public MSCUObject
{
public:
MSCUString (unsigned long NewFixedSize );
~MSCUString();

virtual void SaveToFile( ostream& O ) const;
virtual void LoadFromFile( istream& I );
virtual int GetStorageSize() const ;

virtual const string GetAsString() const;
virtual void SetFromString( const string& Src );
virtual int Compare( const MSCUObject& Src ) const;
virtual const char * GetType() const { return( "MSCUString" ); }
void CorrectSize();
static bool SetSensitive( bool Mode );
private:
char* StrData;
unsigned long FixedSize;
static bool Sensitive;

};

MSCUString.cpp

#include "mscui.h"

bool MSCUString::Sensitive=true;

MSCUString::MSCUString (unsigned long NewFixedSize )
{
FixedSize = NewFixedSize;
StrData = new char[FixedSize+1];
if( !StrData )
throw "Out of memory";
StrData[0]='\0';
CorrectSize();
}

MSCUString::~MSCUString()
{
delete [] StrData;
}

void MSCUString::SaveToFile( ostream& O ) const
{
int i=strlen(StrData);
if( i > FixedSize )
i = FixedSize;
O.write( StrData, i );
while( i++ < FixedSize )
O.write( " ", 1 );
//if( !(O.rdstate() & ios::goodbit) )
// throw "Error writting";
}
void MSCUString::LoadFromFile( istream& I )
{
I.read( StrData, FixedSize );
CorrectSize();
}

int MSCUString::GetStorageSize() const
{
if( FixedSize )
return( FixedSize );
else if( StrData )
return( strlen(StrData) );
else
return(0);
}
const string MSCUString::GetAsString() const
{
return( StrData );
}

void MSCUString::SetFromString( const string& Src )
{
sprintf( StrData, "%-*.*s", FixedSize, FixedSize, Src.c_str() );
}

 

bool MSCUString::SetSensitive( bool Mode )
{
bool Ret=Sensitive;
Sensitive=Mode;
return(Ret);
}

int MyStrICmp( char *A, char * B ) // Works in UNIX and PC compilers
{
for( ; *A && toupper(*A)==toupper(*B) ; A++, B++ )
;
return( toupper(*A) - toupper(*B) );
}

int MSCUString::Compare( const MSCUObject& Src ) const
{
char* Str;
if( GetType()!=Src.GetType() )
throw "Bad type conversion";
Str = ((MSCUString*)&Src)->StrData;
if( Sensitive )
return( strcmp( StrData, Str ) );
else
return( MyStrICmp( StrData, Str ) );
}

void MSCUString::CorrectSize()
{
StrData[FixedSize]='\0';
while( strlen( StrData ) < FixedSize )
strcat( StrData, " " );
}