Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
2.2 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. TSTRING.CPP
  5. Abstract:
  6. History:
  7. --*/
  8. #include "precomp.h"
  9. TString::TString()
  10. {
  11. m_empty = 0;
  12. m_pString = &m_empty;
  13. m_Size = 0;
  14. }
  15. TString::TString(const TCHAR *pSrc)
  16. {
  17. m_empty = 0;
  18. m_pString = &m_empty;
  19. m_Size = 0;
  20. assign(pSrc);
  21. }
  22. void TString::assign(const TCHAR * pSrc)
  23. {
  24. if(pSrc)
  25. {
  26. int iLen = lstrlen(pSrc)+1;
  27. TCHAR * pNew = new TCHAR[iLen];
  28. if(pNew)
  29. {
  30. m_pString = pNew;
  31. m_Size = iLen;
  32. StringCchCopyW(m_pString, iLen, pSrc);
  33. }
  34. }
  35. }
  36. #ifndef UNICODE
  37. TString& TString::operator =(WCHAR * pwcSrc)
  38. {
  39. Empty();
  40. long len = 2*(wcslen(pwcSrc)+1);
  41. TCHAR * pNew = new TCHAR[len];
  42. if(pNew)
  43. {
  44. wcstombs(pNew, pwcSrc, len);
  45. assign(pNew);
  46. delete pNew;
  47. }
  48. return *this;
  49. }
  50. #endif
  51. TString& TString::operator =(LPTSTR pSrc)
  52. {
  53. Empty();
  54. assign(pSrc);
  55. return *this;
  56. }
  57. TString& TString::operator =(const TString &Src)
  58. {
  59. Empty();
  60. assign(Src.m_pString);
  61. return *this;
  62. }
  63. //TString::operator=(class TString const &)
  64. void TString::Empty()
  65. {
  66. m_Size = 0;
  67. if(m_pString != &m_empty)
  68. delete m_pString;
  69. m_pString = &m_empty;
  70. }
  71. TString& TString::operator +=(TCHAR * pSrc)
  72. {
  73. if(pSrc)
  74. {
  75. int iLen = lstrlen(m_pString) + lstrlen(pSrc)+1;
  76. TCHAR * pNew = new TCHAR[iLen];
  77. if(pNew)
  78. {
  79. StringCchCopyW(pNew, iLen, m_pString);
  80. StringCchCatW(pNew, iLen, pSrc);
  81. Empty();
  82. m_Size = iLen;
  83. m_pString = pNew;
  84. }
  85. }
  86. return *this;
  87. }
  88. TString& TString::operator +=(TCHAR Src)
  89. {
  90. if(lstrlen(m_pString) + 2 > m_Size)
  91. {
  92. int iLen = lstrlen(m_pString) + 32;
  93. TCHAR * pNew = new TCHAR[iLen];
  94. if(pNew == NULL)
  95. return *this;
  96. StringCchCopyW(pNew, iLen, m_pString);
  97. Empty();
  98. m_pString = pNew;
  99. m_Size = iLen;
  100. }
  101. TCHAR temp[2];
  102. temp[0] = Src;
  103. temp[1] = 0;
  104. StringCchCatW(m_pString, m_Size, temp);
  105. return *this;
  106. }
  107. TCHAR TString::GetAt(int iIndex)
  108. {
  109. if(iIndex < 0 || iIndex >= lstrlen(m_pString))
  110. return -1;
  111. else
  112. return m_pString[iIndex];
  113. }
  114. int TString::Find(TCHAR cFind)
  115. {
  116. int iCnt, iLen = lstrlen(m_pString);
  117. for(iCnt = 0 ;iCnt < iLen; iCnt++)
  118. if(cFind == m_pString[iCnt])
  119. return iCnt;
  120. return -1;
  121. }