// KString.cpp: implementation of the KString class.
//
//////////////////////////////////////////////////////////////////////
////////// Created by : Hadi Dayvary //////////
////////// Email : hdayvary@yahoo.com //////////
////////// Creation Date : 2005/6/17 //////////
////////// Last Update : 2006/9/5 //////////
////////// Description : String class //////////
//////////////////////////////////////////////////////////////////////
#include "KString.h"
//*****************************************************************************
// - Default constructor
//*****************************************************************************
KString::KString(void)
{
m_sString = NULL;
m_iLength = 0;
}
//*****************************************************************************
// - Constructor
//*****************************************************************************
KString::KString(LPCTSTR lpszStr)
{
m_iLength = (int)_tcslen(lpszStr);
m_sString = new TCHAR[m_iLength + 1];
_tcscpy(m_sString, lpszStr);
}
//*****************************************************************************
// - Copy constructor
//*****************************************************************************
KString::KString(const KString &sStr)
{
m_iLength = sStr.m_iLength;
m_sString = new TCHAR[m_iLength + 1];
_tcscpy(m_sString, sStr.m_sString);
}
//*****************************************************************************
// - Destructor
//*****************************************************************************
KString::~KString(void)
{
if(m_sString)
delete[] m_sString;
m_sString = NULL;
}
//*****************************************************************************
// - Operator = overloading for KString
//*****************************************************************************
KString& KString::operator =(const KString &sStr)
{
if(m_sString)
delete[] m_sString;
m_iLength = sStr.m_iLength;
m_sString = new TCHAR[m_iLength + 1];
_tcscpy(m_sString, sStr.m_sString);
return *this;
}
//*****************************************************************************
// - Operator = overloading for string
//*****************************************************************************
KString& KString::operator =(LPCTSTR lpszStr)
{
if(m_sString)
delete[] m_sString;
m_iLength = (int)_tcslen(lpszStr);
m_sString = new TCHAR[m_iLength + 1];
_tcscpy(m_sString, lpszStr);
return *this;
}
//*****************************************************************************
// - Operator = overloading for int
//*****************************************************************************
KString& KString::operator =(const int iNum)
{
if(m_sString)
delete[] m_sString;
TCHAR sStr[16];
_stprintf(sStr, _T("%d"), iNum);
m_iLength = (int)_tcslen(sStr);
m_sString = new TCHAR[m_iLength + 1];
_tcscpy(m_sString, sStr);
return *this;
}
//*****************************************************************************
// - Operator = overloading for double
//*****************************************************************************
KString& KString::operator =(const double fNum)
{
if(m_sString)
delete[] m_sString;
TCHAR sStr[16];
_stprintf(sStr, _T("%f"), fNum);
m_iLength = (int)_tcslen(sStr);
m_sString = new TCHAR[m_iLength + 1];
_tcscpy(m_sString, sStr);
return *this;
}
//*****************************************************************************
// - Operator + overloading for : KString + KString
//*****************************************************************************
KString KString::operator +(const KString &sStr)
{
KString sTemp;
sTemp.m_iLength = m_iLength + sStr.m_iLength;
sTemp.m_sString = new TCHAR[sTemp.m_iLength + 1];
_stprintf(sTemp.m_sString, _T("%s%s"), m_sString, sStr.m_sString);
return sTemp;
}
//*****************************************************************************
// - Operator + overloading for : KString + LPCTSTR
//*****************************************************************************
KString KString::operator +(LPCTSTR lpszStr)
{
KString sTemp;
sTemp.m_iLength = m_iLength + (int)_tcslen(lpszStr);
sTemp.m_sString = new TCHAR[sTemp.m_iLength + 1];
_stprintf(sTemp.m_sString, _T("%s%s"), m_sString, lpszStr);
return sTemp;
}
//*****************************************************************************
// - Operator + overloading : KString + int
//*****************************************************************************
KString KString::operator +(int iNum)
{
TCHAR sStr[16];
_stprintf(sStr, _T("%d"), iNum);
return *this + sStr;
}
//*****************************************************************************
// - Operator + overloading : KString + double
//*****************************************************************************
KString KString::operator +(double fNum)
{
TCHAR sStr[16];
_stprintf(sStr, _T("%f"), fNum);
return *this + sStr;
}
//*****************************************************************************
// - Operator += overloading for KString
//*****************************************************************************
KString& KString::operator +=(const KString &sStr)
{
if(m_sString)
return *this = *this + sStr;
else
return *this = sStr;
}
//*****************************************************************************
// - Operator += overloading for LPCTSTR
//*****************************************************************************
KString& KString::operator +=(LPCTSTR lpszStr)
{
if(m_sString)
return *this = *this + lpszStr;
else
return *this = lpszStr;
}
//*****************************************************************************
// - Operator < overloading : return TRUE if specified KString's length is less than
// this class string's length , otherwise FALSE
//*****************************************************************************
BOOL KString::operator <(const KString &sStr)
{
if(_tcscmp(m_sString, sStr.m_sString) < 0)
return TRUE;
return FALSE;
}
//*****************************************************************************
// - Operator <= overloading : return TRUE if specified KString's length is less than
// or equal to this class string's length , otherwise FALSE
//*****************************************************************************
BOOL KString::operator <=(const KString &sStr)
{
if(_tcscmp(m_sString, sStr.m_sString) <= 0)
return TRUE;
return FALSE;
}
//*****************************************************************************
// - Operator > overloading : return TRUE if specified KString's length is greater than
// this class string's length , otherwise FALSE
//*****************************************************************************
BOOL KString::operator >(const KString &sStr)
{
if(_tcscmp(m_sString, sStr.m_sString) > 0)
return TRUE;
return FALSE;
}
//*****************************************************************************
// - Operator >= overloading : return TRUE if specified KString's length is greater than
// or equal to this class string's length , otherwise FALSE
//*****************************************************************************
BOOL KString::operator >=(const KString &sStr)
{
if(_tcscmp(m_sString, sStr.m_sString) >= 0)
return TRUE;
return FALSE;
}
//*****************************************************************************
// - Operator == overloading : return TRUE if specified KString length is equal to
// this class string's length , otherwise return FALSE
//*****************************************************************************
BOOL KString::operator ==(const KString &sStr)
{
if(_tcscmp(m_sString, sStr.m_sString) == 0)
return TRUE;
return FALSE;
}
//*****************************
评论0