Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<cstring>
- #include<iostream>
- #include<ostream>
- #include<istream>
- #include<cstdlib>
- #include<cctype>
- class CString
- {
- private:
- // static bool m_bWide;
- char *m_pString; // the maintained string.
- // wchar_t *m_pWideString;
- // int m_nLength;
- protected:
- public:
- CString();
- CString(const CString &cString); // Copy Constructor
- CString(const char *pString); // Char ptr..
- // CString(wchar_t *pWideString); //wide string stuff...
- char* c_str() {return m_pString;}
- void Format(char *pFormatString, ...); // the c printf
- // Format(wchar_t *pFormatString, ...); // the wide version..
- // void Format(CString &cString, ...); // the CString version...
- // bool IsWide() {return m_bWide;}
- bool Empty(); // tel me this is empty..
- int Find(char cChar, int nStart = 0); // forward find starting at nStart non wide...
- int RevFind(char cChar, int nStart = 0); //Reverse find starting at nStart none wide
- // int Find(wchar_t *pString, int nStart = 0);
- // int RevFind(wchar_t *pString, int nStart = 0);
- void MakeLower(); // make lower case
- void MakeUpper(); // make Upper Case
- char* Substring(int nStart, int nEnd = 0);
- int Length() {return strlen(m_pString);}
- void ClearStr();
- CString& operator=(CString &cString);
- CString& operator=(const char *pString);
- CString& operator=(char nChar);
- CString& operator+(CString &cString);
- CString& operator+(const char *pString);
- CString& operator+(char cChar);
- friend CString operator+(char *s, CString &obj);
- CString& operator+=(CString &str);
- CString& operator+=(const char *pString);
- CString& operator+=(char cChar);
- friend CString operator+=(char *s, CString &obj);
- int operator==(CString &obj) {return !strcmp(m_pString, obj.m_pString);}
- int operator!=(CString &obj) {return strcmp(m_pString, obj.m_pString);}
- int operator< (CString &obj) {return strcmp(m_pString, obj.m_pString) < 0;}
- int operator> (CString &obj) {return strcmp(m_pString, obj.m_pString) > 0;}
- int operator<=(CString &obj) {return strcmp(m_pString, obj.m_pString) <= 0;}
- int operator>=(CString &obj) {return strcmp(m_pString, obj.m_pString) >= 0;}
- /* relational operators between CStrings object and a quoted character string.
- Note that the operators could just as easily return bool, rather than int */
- int operator==(char *s) {return !strcmp(m_pString, s);}
- int operator!=(char *s) {return strcmp(m_pString, s);}
- int operator< (char *s) {return strcmp(m_pString, s) < 0;}
- int operator> (char *s) {return strcmp(m_pString, s) > 0;}
- int operator<=(char *s) {return strcmp(m_pString, s) <= 0;}
- int operator>=(char *s) {return strcmp(m_pString, s) >= 0;}
- friend std::ostream& operator<<(std::ostream &stream, CString &obj);
- // friend istream &operator>>(istream &stream, CString &obj);
- char operator[](unsigned int nIndex)
- {
- if(nIndex <= strlen(m_pString))
- return m_pString[nIndex];
- else
- return '\0';
- }
- virtual ~CString();
- };
- /***************************************************************************************/
- CString::CString()
- {
- // default construtor
- // m_bWide = false;
- m_pString = new char[1];
- m_pString[0] = 0;
- // m_pWideString = NULL;
- }
- CString::~CString()
- {
- if(m_pString)
- delete [] m_pString;
- // if(m_pWideString)
- // delete [] m_pWideString;
- }
- CString::CString(const CString &cString)
- {
- // copy constructor
- int newlen =strlen (cString.m_pString) +1;
- std::cout << "CString Copy Constructor:" << cString.m_pString << std::cout;
- m_pString = new char[newlen];
- strcpy (m_pString, cString.m_pString);
- }
- CString::CString(const char *pString)
- {
- m_pString = new char[strlen(pString)+1];
- strcpy (m_pString, pString);
- }
- CString& CString::operator=(CString &cString)
- {
- std::cout << "operator= &cString = " << cString << std::endl;
- if(this==&cString) return *this;
- std::cout << "2 operator= &cString = " << cString << std::endl;
- delete [] m_pString;
- int nLen = strlen(cString.m_pString)+1;
- // int nLen = cString.Length();
- m_pString = new char[nLen];
- // std::cout << "3 operator= &cString.m_pString = " << cString.m_pString << "(" << nLen << ")"<< std::endl;
- strcpy(m_pString, cString.c_str());
- // std::cout << "4 operator= this.m_pString" << m_pString << std::endl;
- return *this;
- }
- CString& CString::operator=(const char *pString)
- {
- delete [] m_pString;
- m_pString = new char[strlen(pString) +1];
- strcpy (m_pString, pString);
- return *this;
- }
- CString& CString::operator=(char nChar)
- {
- delete [] m_pString;
- m_pString = new char[2];
- m_pString[0] = nChar;
- m_pString[1] = 0;
- return *this;
- }
- CString& CString::operator+(CString &cString)
- {
- char* pTmp = new char[strlen(m_pString)+1];
- strcpy(pTmp, m_pString);
- int nLen = strlen(m_pString) + strlen(cString.c_str()) +1;
- delete [] m_pString;
- m_pString = new char[nLen];
- strcpy(m_pString, pTmp);
- strcat(m_pString, cString.c_str());
- return *this;
- }
- CString& CString::operator+(char cChar)
- {
- char* pTmp = new char[strlen(m_pString)+1];
- strcpy(pTmp, m_pString);
- int nLen = strlen(m_pString) + sizeof(cChar) +1;
- delete [] m_pString;
- m_pString = new char[nLen];
- strcpy(m_pString, pTmp);
- nLen = strlen(m_pString);
- m_pString[nLen] = cChar;
- m_pString[nLen+1] = 0;
- return *this;
- }
- CString& CString::operator+(const char *pString)
- {
- char* pTmp = new char[strlen(m_pString)+1];
- strcpy(pTmp, m_pString);
- int nLen = strlen(m_pString) + strlen(pString)+1;
- delete [] m_pString;
- m_pString = new char[nLen];
- strcpy(m_pString, pTmp);
- strcat(m_pString, pString);
- return *this;
- }
- // the friend function.
- CString operator+(char *pString, CString &cStr)
- {
- // CString =
- int nLen = strlen(pString)+strlen(cStr.c_str()) +1;
- CString strTmp;
- delete [] strTmp.m_pString;
- strTmp.m_pString = new char[nLen];
- strcpy(strTmp.m_pString, pString);
- strcpy(strTmp.m_pString, cStr.c_str());
- return strTmp;
- // friend function...
- }
- bool CString::Empty()
- {
- bool bEmpty = false; // tel me this is empty..
- if(!m_pString) bEmpty = true;
- else if(m_pString[0] == '\0') bEmpty = true;
- return bEmpty;
- }
- int CString::Find(char cChar, int nStart)
- {
- // forward find
- char *pString = &m_pString[nStart];
- int nPos = nStart;
- while(pString)
- {
- if(*pString == cChar)
- return nPos;
- ++nPos;
- ++pString;
- }
- return -1;
- // forward find starting at nStart non wide...
- }
- int CString::RevFind(char cChar, int nStart )
- {
- //Reverse find starting at nStart none wide
- int nPos = nStart;
- if(nPos== 0)
- {
- nPos = strlen(m_pString);
- }
- char *pString = &m_pString[nPos-1];
- while((nPos>-1)&&(pString))
- {
- if(*pString == cChar)
- return nPos;
- --nPos;
- --pString;
- }
- return -1;
- }
- /*
- void CString::Format(CString &strInFormat, ...)
- {
- va_list marker;
- CString strOutput;
- char chr;
- CString strFormat;
- int nIndex;
- CString strSection.
- va_start(marker, strFormat);
- while(chr = strFormat[nIndex])
- {
- if(chr == '%')
- {
- // format code
- bfound = false;
- // pChar++; // increment past the '%'
- while((!bfound)&&(pChar))
- {
- chr = *pChar++;
- *pCode = chr;
- pCode++;
- switch(chr)
- {
- case 'd':
- case 'x':
- case 'o':
- case 'u':
- *pCode++ = 0;
- nValue = va_arg(marker, unsigned long);
- sprintf(pString, ptCode, nValue);
- bfound = true;
- break;
- case 'c':
- *pCode++ = 0;
- cChar = va_arg(marker, char);
- sprintf(pString, ptCode, cChar);
- bfound = true;
- break;
- case 'e':
- case 'f':
- case 'g':
- *pCode++ = 0;
- fValue = va_arg(marker, double);
- sprintf(pString, ptCode, fValue);
- bfound = true;
- break;
- case 's':
- *pCode++ = 0;
- pTmpString = va_arg(marker, char*);
- strcpy(pString, pTmpString);
- bfound = true;
- break;
- case '%':
- *pCode++ = *pChar++;
- *pCode = 0;
- pString[0] = '%';
- pString[1] = 0;
- bfound = true;
- break;
- default:
- // *pCode= chr;
- // pCode++;
- break;
- }
- }
- // std::cout << "pString =" << pString << std::endl;
- // std::cout << "pCode =" << ptCode << std::endl;
- if(bfound)
- {
- // add it to the buffer...
- strOutput+=pString;
- }
- }
- else
- {
- // add char to output buffer
- strOutput+=chr;
- //chr = *pChar++;
- }
- } // while
- va_end(marker);
- int nLen = strOutput.Length();
- delete [] m_pString;
- m_pString = new char[nLen+1];
- strcpy(m_pString, strOutput.m_pString);
- }
- */
- void CString::Format(char *pFormatString, ...)
- {
- // this Sets up a format of the string...MFC CString.Format is it
- // Should except any known to my doc of Printf/Sprintf
- va_list marker;
- CString strOutput;
- char *pChar = pFormatString;
- va_start(marker, pFormatString);
- char *pString = new char[256]; // this will be the buffer we dump into sprintfs into
- char *pCode = new char[25]; // the format code for the sprintf...
- char *ptCode = pCode;
- double fValue; // holder for floating points...
- unsigned long nValue; // holder for integers....
- char *pTmpString; // holder for string pointers...
- bool bfound;
- // char cChar;
- char chr;
- while(chr = *pChar++)
- {
- if(chr == '%')
- {
- // format code */
- pCode = ptCode;
- *pCode = chr; // put the "%" on the code..
- pCode++;
- bfound = false;
- // pChar++; // increment past the '%'
- while((!bfound)&&(pChar))
- {
- chr = *pChar++;
- *pCode = chr;
- pCode++;
- switch(chr)
- {
- case 'd':
- case 'x':
- case 'o':
- case 'u':
- case 'c':
- *pCode++ = 0;
- nValue = va_arg(marker, unsigned long long);
- sprintf(pString, ptCode, nValue);
- bfound = true;
- break;
- // case 'c':
- /* *pCode++ = 0;
- cChar = va_arg(marker, char);
- sprintf(pString, ptCode, cChar);
- bfound = true;
- break;
- */
- case 'e':
- case 'f':
- case 'g':
- *pCode++ = 0;
- fValue = va_arg(marker, double);
- sprintf(pString, ptCode, fValue);
- bfound = true;
- break;
- case 's':
- *pCode++ = 0;
- pTmpString = va_arg(marker, char*);
- strcpy(pString, pTmpString);
- bfound = true;
- break;
- case '%':
- *pCode++ = *pChar++;
- *pCode = 0;
- pString[0] = '%';
- pString[1] = 0;
- bfound = true;
- break;
- default:
- // *pCode= chr;
- // pCode++;
- break;
- }
- }
- // std::cout << "pString =" << pString << std::endl;
- // std::cout << "pCode =" << ptCode << std::endl;
- if(bfound)
- {
- // add it to the buffer...
- strOutput+=pString;
- }
- }
- else
- {
- // add char to output buffer
- strOutput+=chr;
- //chr = *pChar++;
- }
- } // while
- va_end(marker);
- int nLen = strOutput.Length();
- delete [] m_pString;
- m_pString = new char[nLen+1];
- strcpy(m_pString, strOutput.m_pString);
- }
- char* CString::Substring(int nStart, int nEnd)
- {
- // substring of the current string.
- if(!nEnd)
- {
- nEnd= strlen(m_pString);
- }
- nStart--; // subtract 1 for zero base....
- char *pStart = &m_pString[nStart];
- char *pEnd = &m_pString[nEnd];
- int nLen = nEnd - nStart;
- char *tmpString;
- tmpString = new char[nLen+1];
- char *pDest = tmpString;
- while(pStart != pEnd)
- {
- *pDest = *pStart;
- ++pDest;
- ++pStart;
- }
- tmpString[nLen] = '\0';
- // std::cout << "tmp:=" << tmpString << std::endl;;
- return tmpString;
- }
- std::ostream &operator<<(std::ostream &stream, CString &obj)
- {
- stream << obj.c_str();
- return stream;
- }
- CString& CString::operator+=(CString &str)
- {
- // Cstring += CString
- char* pTmp = new char[strlen(m_pString)+1];
- strcpy(pTmp, m_pString);
- int nLen = strlen(m_pString) + strlen(str.c_str()) +1;
- delete [] m_pString;
- m_pString = new char[nLen];
- strcpy(m_pString, pTmp);
- strcat(m_pString, str.c_str());
- return *this;
- }
- CString& CString::operator+=(const char *pString)
- {
- //CString += char *
- char* pTmp = new char[strlen(m_pString)+1];
- strcpy(pTmp, m_pString);
- int nLen = strlen(m_pString) + strlen(pString)+1;
- delete [] m_pString;
- m_pString = new char[nLen];
- strcpy(m_pString, pTmp);
- strcat(m_pString, pString);
- return *this;
- }
- CString& CString::operator+=(char cChar)
- {
- // CString += char
- char* pTmp = new char[strlen(m_pString)+1];
- strcpy(pTmp, m_pString);
- int nLen = strlen(m_pString) + sizeof(cChar) +1;
- delete [] m_pString;
- m_pString = new char[nLen];
- strcpy(m_pString, pTmp);
- nLen = strlen(m_pString);
- m_pString[nLen] = cChar;
- m_pString[nLen+1] = 0;
- return *this;
- }
- void CString::ClearStr()
- {
- delete [] m_pString;
- m_pString = new char[1];
- m_pString[0] = 0;
- }
- void CString::MakeLower() // make lower case
- {
- char* pChar = m_pString;
- while(*pChar)
- {
- if(isalpha(*pChar))
- {
- *pChar = tolower(*pChar);
- }
- pChar++;
- }
- }
- void CString::MakeUpper()
- {
- char *pChar = m_pString;
- while(*pChar)
- {
- if(isalpha(*pChar))
- {
- *pChar = toupper(*pChar);
- }
- pChar++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment