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.

180 lines
3.9 KiB

  1. // Copyright (c) 1999 Microsoft Corporation
  2. extern char *__g_pszStringBlank;
  3. class CString
  4. {
  5. private:
  6. char *m_pszString;
  7. void DeleteString() { if (m_pszString != __g_pszStringBlank) delete [] m_pszString; }
  8. char *DuplicateString()
  9. {
  10. size_t pszLen = strlen(m_pszString) + 1;
  11. char *psz = new char[pszLen];
  12. if (psz == NULL)
  13. return NULL;
  14. StringCchCopyA(psz, pszLen, m_pszString);
  15. return psz;
  16. }
  17. public:
  18. CString()
  19. {
  20. m_pszString = __g_pszStringBlank;
  21. }
  22. CString (const char *psz)
  23. {
  24. size_t pszLen = strlen(psz) + 1;
  25. m_pszString = new char[pszLen];
  26. if (m_pszString == 0)
  27. {
  28. m_pszString = __g_pszStringBlank;
  29. }
  30. else
  31. {
  32. StringCchCopyA(m_pszString, pszLen, psz);
  33. }
  34. }
  35. CString (CString &sz)
  36. {
  37. m_pszString = __g_pszStringBlank;
  38. *this = sz.m_pszString;
  39. }
  40. ~CString() { DeleteString(); }
  41. size_t Length() const { return strlen(m_pszString); } //09/17//int Length() const { return strlen(m_pszString); }
  42. CString& operator +=(const char *psz)
  43. {
  44. size_t bufLen = Length() + strlen(psz) + 1;
  45. char *pszNewString = new char[bufLen];
  46. if (pszNewString == NULL)
  47. return *this;
  48. StringCchCopyA(pszNewString, bufLen, m_pszString);
  49. StringCchCatA(pszNewString, bufLen, psz);
  50. DeleteString();
  51. m_pszString = pszNewString;
  52. return *this;
  53. }
  54. CString &operator = (const char *psz)
  55. {
  56. size_t bufLen = strlen(psz) + 1;
  57. char *pszNewString = new char[bufLen];
  58. if (pszNewString == 0)
  59. return *this;
  60. StringCchCopyA(pszNewString, bufLen, psz);
  61. DeleteString();
  62. m_pszString = pszNewString;
  63. return *this;
  64. }
  65. char *Unbind()
  66. {
  67. if (m_pszString != __g_pszStringBlank)
  68. {
  69. char *psz = m_pszString;
  70. m_pszString = __g_pszStringBlank; //should always point to at least the default blank string
  71. return psz;
  72. }
  73. else
  74. return DuplicateString();
  75. }
  76. char operator[](size_t nIndex) const //09/17//char operator[](int nIndex) const
  77. {
  78. if (nIndex > Length())
  79. nIndex = Length();
  80. return m_pszString[nIndex];
  81. }
  82. operator const char *() { return m_pszString; }
  83. };
  84. class CMultiString
  85. {
  86. private:
  87. char *m_pszString;
  88. void DeleteString() { if (m_pszString != __g_pszStringBlank) delete [] m_pszString; }
  89. size_t Length(const char *psz) const //09/17//int Length(const char *psz) const
  90. {
  91. size_t nLen = 0;
  92. while (*psz != '\0')
  93. {
  94. nLen += strlen(psz) + 1;
  95. psz += strlen(psz) + 1;
  96. }
  97. return nLen;
  98. }
  99. public:
  100. CMultiString()
  101. {
  102. m_pszString = __g_pszStringBlank;
  103. }
  104. CMultiString (const char *psz)
  105. {
  106. size_t bufLen = strlen(psz) + 1;
  107. m_pszString = new char[bufLen];
  108. if (m_pszString == NULL)
  109. {
  110. m_pszString = __g_pszStringBlank;
  111. }
  112. else
  113. {
  114. StringCchCopyA(m_pszString, bufLen, psz);
  115. }
  116. }
  117. ~CMultiString() { DeleteString(); }
  118. size_t Length() const //09/17//int Length() const
  119. {
  120. return Length(m_pszString);
  121. }
  122. CMultiString& operator +=(const char *psz)
  123. {
  124. size_t nLength = Length() + strlen(psz) + 3;
  125. char *pszNewString = new char[nLength];
  126. if (pszNewString == NULL)
  127. return *this;
  128. memcpy(pszNewString, m_pszString, Length());
  129. memcpy(pszNewString + Length(), psz, strlen(psz) + 1);
  130. pszNewString[Length() + strlen(psz) + 1] = '\0';
  131. DeleteString();
  132. m_pszString = pszNewString;
  133. return *this;
  134. }
  135. void AddUnique(const char *pszNew)
  136. {
  137. bool bFound = false;
  138. const char *psz = m_pszString;
  139. while (psz && *psz)
  140. {
  141. if (_stricmp(psz, pszNew) == 0)
  142. {
  143. bFound = true;
  144. break;
  145. }
  146. psz += strlen(psz) + 1;
  147. }
  148. if (!bFound)
  149. {
  150. *this += pszNew;
  151. }
  152. }
  153. char operator[](size_t nIndex) const //09/17//char operator[](int nIndex) const
  154. {
  155. if (nIndex > Length())
  156. nIndex = Length();
  157. return m_pszString[nIndex];
  158. }
  159. operator const char *() { return m_pszString; }
  160. CMultiString& Empty()
  161. {
  162. DeleteString();
  163. m_pszString = __g_pszStringBlank;
  164. return *this;
  165. }
  166. };