Split up MyString

Andrew Turner <[email protected]>
Newsgroups gmane.comp.security.passwordsafe.devel
Message-ID <[email protected]>
Hello

I have been working on porting Password safe to FreeBSD to achieve this 
I can't use any MFC classes. I have started by removing CString from 
CMyString.

To make the having two different string classes within CMyString easier 
I would like corelib/MyString.cpp to be split in to two. One will 
contain the common methods. The other file will have the methods that 
require calling m_mystring.

I have attached a patch to achieve this. I would appreciate if it could 
be tested as I have no way of doing it myself. You will need to copy 
corelib/MyString.cpp to corelib/MyStringMFC.cpp to apply the patch.

Andrew
pws_corelib_mystring_split.diff (text/x-patch, 11.3 KB)
Index: corelib/MyString.h
===================================================================
--- corelib/MyString.h	(revision 754)
+++ corelib/MyString.h	(working copy)
@@ -8,7 +8,14 @@
 
 #ifndef _MYSTRING_H_
 #define _MYSTRING_H_
+
+#ifdef MFCString
+#undef MFCString
+#endif
+
 #include <AFX.H>
+#define StrType CString
+#define MFCString
 //-----------------------------------------------------------------------------
 class CMyString
 {
@@ -17,13 +24,13 @@
    CMyString(LPCTSTR lpsz);
    CMyString(LPCTSTR lpsz, int nLength);
    CMyString(const CMyString& stringSrc);
-   CMyString(const CString& stringSrc);
+   CMyString(const StrType& stringSrc);
    ~CMyString();
 
    TCHAR operator[](int nIndex) const;
    void SetAt(int nIndex, TCHAR ch);
-   operator CString() const;
-   operator CString&();
+   operator StrType() const;
+   operator StrType&();
    operator LPCTSTR() const;
    BOOL IsEmpty() const;
 
@@ -62,18 +69,19 @@
    int Replace(TCHAR chOld, TCHAR chNew) ;
    int Replace(LPCTSTR lpszOld, LPCTSTR lpszNew) ;
    int Remove(TCHAR ch) ;
-   CString Left(int nCount) const;
-   CString Right(int nCount) const;
-   CString Mid(int nFirst) const;
-   CString Mid(int nFirst, int nCount) const;
-  void TrimRight() {m_mystring.TrimRight();}
-  void TrimLeft() {m_mystring.TrimLeft();}
-  void MakeLower() {m_mystring.MakeLower();}
 
+   StrType Left(int nCount) const;
+   StrType Right(int nCount) const;
+   StrType Mid(int nFirst) const;
+   StrType Mid(int nFirst, int nCount) const;
+   void TrimRight();
+   void TrimLeft();
+   void MakeLower();
+
   void Trash() {trashstring();}
 
 private:
-  CString m_mystring;
+  StrType m_mystring;
   void trashstring();
 };
 //-----------------------------------------------------------------------------
Index: corelib/MyString.cpp
===================================================================
--- corelib/MyString.cpp	(revision 754)
+++ corelib/MyString.cpp	(working copy)
@@ -1,6 +1,7 @@
 /// \file MyString.cpp
 //-----------------------------------------------------------------------------
 
+#include "PwsPlatform.h"
 #include "Util.h"
 
 #include "MyString.h"
@@ -28,7 +29,7 @@
 {
 }
 
-CMyString::CMyString(const CString& stringSrc) : m_mystring(stringSrc)
+CMyString::CMyString(const StrType& stringSrc) : m_mystring(stringSrc)
 {
 }
 
@@ -37,33 +38,6 @@
    trashstring();
 }
 
-
-void
-CMyString::trashstring()
-{
-   trashMemory((unsigned char*)m_mystring.GetBuffer(m_mystring.GetLength()),
-               m_mystring.GetLength());
-}
-
-
-LPTSTR
-CMyString::GetBuffer(int nMinBufLength)
-{
-   return m_mystring.GetBuffer(nMinBufLength);
-}
-
-void
-CMyString::ReleaseBuffer(int nNewLength)
-{
-   m_mystring.ReleaseBuffer(nNewLength);
-}
-
-int
-CMyString::GetLength() const
-{
-   return m_mystring.GetLength();
-}
-
 const CMyString&
 CMyString::operator=(const CMyString& stringSrc)
 {
@@ -93,7 +67,7 @@
 CMyString::operator=(const unsigned char* psz)
 {
    trashstring();
-   m_mystring = psz;
+   m_mystring = (const char *)psz;
    return *this;
 }
 #endif
@@ -165,33 +139,16 @@
    return m_mystring[nIndex];
 }
 
-void
-CMyString::SetAt(int nIndex, TCHAR ch)
+CMyString::operator StrType() const
 {
-   m_mystring.SetAt(nIndex,ch);
-}
-
-CMyString::operator CString() const
-{
    return m_mystring;
 }
 
-CMyString::operator CString&()
+CMyString::operator StrType&()
 {
    return m_mystring;
 }
 
-CMyString::operator LPCTSTR() const
-{
-   return (LPCTSTR)m_mystring;
-}
-
-BOOL
-CMyString::IsEmpty() const
-{
-   return m_mystring.IsEmpty();
-}
-
 int
 CMyString::FindByte(char ch) const
 {
@@ -216,110 +173,40 @@
   return nRetVal;
 }
 
-int
-CMyString::Find(TCHAR ch) const
-{
-   return m_mystring.Find(ch);
-}
-
-int
-CMyString::Find(LPCTSTR lpszSub) const
-{
-   return m_mystring.Find(lpszSub);
-}
-
-int
-CMyString::Find(TCHAR ch, int nstart) const
-{
-   return m_mystring.Find(ch, nstart);
-}
-
-int
-CMyString::Find(LPCTSTR lpszSub, int nstart) const
-{
-   return m_mystring.Find(lpszSub, nstart);
-}
-
-int
-CMyString::Replace(TCHAR chOld, TCHAR chNew) 
-{
-   return m_mystring.Replace(chOld,chNew);
-}
-
-int
-CMyString::Replace(LPCTSTR lpszOld, LPCTSTR lpszNew) 
-{
-   return m_mystring.Replace(lpszOld,lpszNew);
-}
-
-int
-CMyString::Remove(TCHAR ch) 
-{
-   return m_mystring.Remove(ch);
-}
-
-//Can't properly trash the memory here, so it is better to just return a CString
-CString
-CMyString::Left(int nCount) const
-{
-   return m_mystring.Left(nCount);
-}
-
-//Can't properly trash the memory here, so it is better to just return a CString
-CString
-CMyString::Right(int nCount) const
-{
-   return m_mystring.Right(nCount);
-}
-
-//Can't properly trash the memory here, so it is better to just return a CString
-CString
-CMyString::Mid(int nFirst) const
-{
-   return m_mystring.Mid(nFirst);
-}
-
-//Can't properly trash the memory here, so it is better to just return a CString
-CString
-CMyString::Mid(int nFirst, int nCount) const
-{
-   return m_mystring.Mid(nFirst, nCount);
-}
-
 bool
 operator==(const CMyString& s1, const CMyString& s2)
 {
-   return (const CString)s1 == (const CString)s2;
+   return (const StrType)s1 == (const StrType)s2;
 }
 
 bool
 operator==(const CMyString& s1, LPCTSTR s2)
 {
-   return (const CString)s1==s2;
+   return (const StrType)s1==s2;
 }
 
 bool
 operator==(LPCTSTR s1, const CMyString& s2)
 {
-   return s1==(const CString)s2;
+   return s1==(const StrType)s2;
 }
 
 bool
 operator!=(const CMyString& s1, const CMyString& s2)
 {
-   return (const CString)s1 != (const CString)s2;
+   return (const StrType)s1 != (const StrType)s2;
 }
 
 bool
 operator!=(const CMyString& s1, LPCTSTR s2)
 {
-   return (const CString)s1 != s2;
+   return (const StrType)s1 != s2;
 }
 
 bool
 operator!=(LPCTSTR s1, const CMyString& s2)
 {
-   return s1 != (const CString)s2;
+   return s1 != (const StrType)s2;
 }
 
 //-----------------------------------------------------------------------------
Index: corelib/MyStringMFC.cpp
===================================================================
--- corelib/MyStringMFC.cpp	(revision 742)
+++ corelib/MyStringMFC.cpp	(working copy)
@@ -5,39 +5,13 @@
 
 #include "MyString.h"
 
+#ifdef MFCString
 #ifdef _DEBUG
 #define new DEBUG_NEW
 #undef THIS_FILE
 static char THIS_FILE[] = __FILE__;
 #endif
 
-
-CMyString::CMyString() : m_mystring(_T(""))
-{
-}
-
-CMyString::CMyString(LPCTSTR lpsz) : m_mystring(lpsz)
-{
-}
-
-CMyString::CMyString(LPCTSTR lpsz, int nLength) : m_mystring(lpsz, nLength)
-{
-}
-
-CMyString::CMyString(const CMyString& stringSrc) : m_mystring(stringSrc.m_mystring)
-{
-}
-
-CMyString::CMyString(const CString& stringSrc) : m_mystring(stringSrc)
-{
-}
-
-CMyString::~CMyString()
-{
-   trashstring();
-}
-
-
 void
 CMyString::trashstring()
 {
@@ -45,7 +19,6 @@
                m_mystring.GetLength());
 }
 
-
 LPTSTR
 CMyString::GetBuffer(int nMinBufLength)
 {
@@ -64,123 +37,12 @@
    return m_mystring.GetLength();
 }
 
-const CMyString&
-CMyString::operator=(const CMyString& stringSrc)
-{
-   trashstring();
-   m_mystring = stringSrc.m_mystring;
-   return *this;
-}
-
-const CMyString&
-CMyString::operator=(TCHAR ch)
-{
-   trashstring();
-   m_mystring = ch;
-   return *this;
-}
-
-const CMyString&
-CMyString::operator=(LPCTSTR lpsz)
-{
-   trashstring();
-   m_mystring = lpsz;
-   return *this;
-}
-
-#ifndef UNICODE // do we need this at all?
-const CMyString&
-CMyString::operator=(const unsigned char* psz)
-{
-   trashstring();
-   m_mystring = psz;
-   return *this;
-}
-#endif
-
-const CMyString&
-CMyString::operator+=(const CMyString& string)
-{
-   m_mystring += string.m_mystring;
-   return *this;
-}
-
-const CMyString&
-CMyString::operator+=(TCHAR ch)
-{
-   m_mystring += ch;
-   return *this;
-}
-
-const CMyString&
-CMyString::operator+=(LPCTSTR lpsz)
-{
-   m_mystring += lpsz;
-   return *this;
-}
-
-CMyString AFXAPI
-operator+(const CMyString& string1,const CMyString& string2)
-{
-   CMyString s;
-   s = (CMyString)(string1.m_mystring+string2.m_mystring);
-   return s;
-}
-
-CMyString AFXAPI
-operator+(const CMyString& string, TCHAR ch)
-{
-   CMyString s;
-   s = (CMyString)(string.m_mystring + ch);
-   return s;
-}
-
-CMyString AFXAPI
-operator+(TCHAR ch, const CMyString& string)
-{
-   CMyString s;
-   s = (CMyString)(ch + string.m_mystring);
-   return s;
-}
-
-CMyString AFXAPI
-operator+(const CMyString& string, LPCTSTR lpsz)
-{
-   CMyString s;
-   s = (CMyString)(string.m_mystring + lpsz);
-   return s;
-}
-
-CMyString AFXAPI
-operator+(LPCTSTR lpsz, const CMyString& string)
-{
-   CMyString s;
-   s = (CMyString)(lpsz + string.m_mystring);
-   return s;
-}
-
-TCHAR
-CMyString::operator[](int nIndex) const
-{
-   return m_mystring[nIndex];
-}
-
 void
 CMyString::SetAt(int nIndex, TCHAR ch)
 {
    m_mystring.SetAt(nIndex,ch);
 }
 
-CMyString::operator CString() const
-{
-   return m_mystring;
-}
-
-CMyString::operator CString&()
-{
-   return m_mystring;
-}
-
 CMyString::operator LPCTSTR() const
 {
    return (LPCTSTR)m_mystring;
@@ -193,30 +55,6 @@
 }
 
 int
-CMyString::FindByte(char ch) const
-{
-  int		nRetVal = -1;	// default to not found
-  int		nIndex	= 0;;
-
-  LPCTSTR pszString = LPCTSTR(m_mystring);
-
-  while ( pszString[nIndex] ) {
-#ifndef UNICODE
-    if ( pszString[nIndex] == ch ) {
-#else
-    if ( LOBYTE(pszString[nIndex]) == ch ) {
-#endif
-      nRetVal = nIndex;
-      break;
-    }
-
-    ++nIndex;
-  }
-
-  return nRetVal;
-}
-
-int
 CMyString::Find(TCHAR ch) const
 {
    return m_mystring.Find(ch);
@@ -258,70 +96,52 @@
    return m_mystring.Remove(ch);
 }
 
-//Can't properly trash the memory here, so it is better to just return a CString
-CString
+//Can't properly trash the memory here, so it is better to just return a StrType
+StrType
 CMyString::Left(int nCount) const
 {
    return m_mystring.Left(nCount);
 }
 
-//Can't properly trash the memory here, so it is better to just return a CString
-CString
+//Can't properly trash the memory here, so it is better to just return a StrType
+StrType
 CMyString::Right(int nCount) const
 {
    return m_mystring.Right(nCount);
 }
 
-//Can't properly trash the memory here, so it is better to just return a CString
-CString
+//Can't properly trash the memory here, so it is better to just return a StrType
+StrType
 CMyString::Mid(int nFirst) const
 {
    return m_mystring.Mid(nFirst);
 }
 
-//Can't properly trash the memory here, so it is better to just return a CString
-CString
+//Can't properly trash the memory here, so it is better to just return a StrType
+StrType
 CMyString::Mid(int nFirst, int nCount) const
 {
    return m_mystring.Mid(nFirst, nCount);
 }
 
-bool
-operator==(const CMyString& s1, const CMyString& s2)
+void
+CMyString::TrimRight()
 {
-   return (const CString)s1 == (const CString)s2;
+   m_mystring.TrimRight();
 }
 
-bool
-operator==(const CMyString& s1, LPCTSTR s2)
+void
+CMyString::TrimLeft()
 {
-   return (const CString)s1==s2;
+   m_mystring.TrimLeft();
 }
 
-bool
-operator==(LPCTSTR s1, const CMyString& s2)
+void
+CMyString::MakeLower()
 {
-   return s1==(const CString)s2;
+   m_mystring.MakeLower();
 }
 
-bool
-operator!=(const CMyString& s1, const CMyString& s2)
-{
-   return (const CString)s1 != (const CString)s2;
-}
-
-bool
-operator!=(const CMyString& s1, LPCTSTR s2)
-{
-   return (const CString)s1 != s2;
-}
-
-bool
-operator!=(LPCTSTR s1, const CMyString& s2)
-{
-   return s1 != (const CString)s2;
-}
-
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-
+#endif
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.