139 lines
1.9 KiB
C++
139 lines
1.9 KiB
C++
#ifndef CSTR_H
|
|
#define CSTR_H
|
|
|
|
#include <stdlib.h>
|
|
#include <memory.h>
|
|
#include <string.h>
|
|
|
|
class Str
|
|
{
|
|
public:
|
|
Str()
|
|
{
|
|
m_Text = NULL;
|
|
}
|
|
|
|
Str( const Str& source )
|
|
{
|
|
m_Text = NULL;
|
|
if ( source.m_Text != NULL )
|
|
Copy( source.m_Text );
|
|
}
|
|
|
|
Str( const char* source )
|
|
{
|
|
m_Text = NULL;
|
|
if ( source != NULL )
|
|
Copy( source );
|
|
}
|
|
|
|
~Str()
|
|
{
|
|
Free();
|
|
}
|
|
|
|
void Free()
|
|
{
|
|
if ( m_Text != NULL )
|
|
{
|
|
free( m_Text );
|
|
m_Text = NULL;
|
|
}
|
|
}
|
|
|
|
void Copy( const char* source )
|
|
{
|
|
Reserve( strlen( source ) + 1 );
|
|
strcpy( m_Text, source );
|
|
}
|
|
|
|
void Reserve( unsigned int size )
|
|
{
|
|
if (m_Text)
|
|
{
|
|
free( m_Text );
|
|
}
|
|
|
|
m_Text = (char*)malloc( size );
|
|
}
|
|
|
|
Str& operator=( const char* source )
|
|
{
|
|
Free();
|
|
|
|
if ( source != NULL )
|
|
Copy( source );
|
|
|
|
return *this;
|
|
}
|
|
|
|
int Compare( const Str& string ) const
|
|
{
|
|
if ( m_Text == NULL && string.m_Text == NULL )
|
|
return 0;
|
|
|
|
if ( m_Text == NULL || string.m_Text == NULL )
|
|
return -1;
|
|
|
|
return strcmp( m_Text, string.m_Text );
|
|
}
|
|
|
|
int Compare( const char* string ) const
|
|
{
|
|
if ( m_Text == NULL && string == NULL )
|
|
return 0;
|
|
|
|
if ( m_Text == NULL || string == NULL )
|
|
return -1;
|
|
|
|
return strcmp( m_Text, string );
|
|
}
|
|
|
|
friend bool operator==( const Str& stringA, const Str& stringB )
|
|
{
|
|
return stringA.Compare( stringB ) == 0;
|
|
}
|
|
|
|
friend bool operator==( const Str& stringA, const char* stringB )
|
|
{
|
|
return stringA.Compare( stringB ) == 0;
|
|
}
|
|
|
|
friend bool operator==( const char* stringA, const Str& stringB )
|
|
{
|
|
return stringB.Compare( stringA ) == 0;
|
|
}
|
|
|
|
char& operator[]( const size_t idx )
|
|
{
|
|
return m_Text[idx];
|
|
}
|
|
|
|
void Clear()
|
|
{
|
|
Free();
|
|
}
|
|
|
|
bool IsEmpty()
|
|
{
|
|
return GetLength() == 0;
|
|
}
|
|
|
|
unsigned int GetLength()
|
|
{
|
|
if ( m_Text == NULL )
|
|
return 0;
|
|
|
|
return strlen( m_Text );
|
|
}
|
|
|
|
const char* GetText()
|
|
{
|
|
return (const char*)m_Text;
|
|
}
|
|
|
|
private:
|
|
char* m_Text;
|
|
};
|
|
|
|
#endif // !CSTR_H
|