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.

224 lines
4.6 KiB

  1. //---------------------------------------------------------------------------
  2. // SimpStr.h - defines simple string classes
  3. //---------------------------------------------------------------------------
  4. #ifndef _SIMPSTR_H_
  5. #define _SIMPSTR_H_
  6. //---------------------------------------------------------------------------
  7. class CWideString // simplified version of CString
  8. {
  9. protected:
  10. void Init(LPCWSTR pszString = NULL)
  11. {
  12. _pWideString = NULL;
  13. if (pszString)
  14. {
  15. int len = lstrlen(pszString) + 1;
  16. if (len > 0)
  17. {
  18. _pWideString = new WCHAR[len];
  19. if (_pWideString)
  20. {
  21. lstrcpyn(_pWideString, pszString, len);
  22. }
  23. }
  24. }
  25. }
  26. public:
  27. //--------------------------------------------------------------
  28. CWideString(LPCWSTR pszString=NULL)
  29. {
  30. Init(pszString);
  31. }
  32. //--------------------------------------------------------------
  33. CWideString(CWideString &wsCopy)
  34. {
  35. Init(wsCopy._pWideString);
  36. }
  37. //--------------------------------------------------------------
  38. ~CWideString()
  39. {
  40. if (_pWideString)
  41. delete [] _pWideString;
  42. }
  43. //--------------------------------------------------------------
  44. CWideString & operator = (CWideString &wsNew)
  45. {
  46. //---- delete our old string ----
  47. if (_pWideString)
  48. {
  49. delete [] _pWideString;
  50. }
  51. Init(wsNew._pWideString);
  52. return *this;
  53. }
  54. //--------------------------------------------------------------
  55. CWideString & operator = (LPCWSTR pNewString)
  56. {
  57. //---- delete our old string ----
  58. if (_pWideString)
  59. {
  60. delete [] _pWideString;
  61. }
  62. Init(pNewString);
  63. return *this;
  64. }
  65. //--------------------------------------------------------------
  66. operator LPCWSTR() const
  67. {
  68. return _pWideString;
  69. }
  70. //--------------------------------------------------------------
  71. int GetLength()
  72. {
  73. int iLen = 0;
  74. if (_pWideString)
  75. {
  76. iLen = lstrlen(_pWideString);
  77. }
  78. return iLen;
  79. }
  80. //--------------------------------------------------------------
  81. protected:
  82. LPWSTR _pWideString;
  83. };
  84. //---------------------------------------------------------------------------
  85. template <class T>
  86. class CSimpleArray
  87. {
  88. public:
  89. T* m_aT;
  90. int m_nSize;
  91. int m_nAllocSize;
  92. // Construction/destruction
  93. CSimpleArray() : m_aT(NULL), m_nSize(0), m_nAllocSize(0)
  94. { }
  95. ~CSimpleArray()
  96. {
  97. RemoveAll();
  98. }
  99. // Operations
  100. int GetSize() const
  101. {
  102. return m_nSize;
  103. }
  104. BOOL Add(T& t)
  105. {
  106. if(m_nSize == m_nAllocSize)
  107. {
  108. T* aT;
  109. int nNewAllocSize = (m_nAllocSize == 0) ? 1 : (m_nSize * 2);
  110. aT = (T*)realloc(m_aT, nNewAllocSize * sizeof(T));
  111. if(aT == NULL)
  112. return FALSE;
  113. m_nAllocSize = nNewAllocSize;
  114. m_aT = aT;
  115. }
  116. m_nSize++;
  117. SetAtIndex(m_nSize - 1, t);
  118. return TRUE;
  119. }
  120. BOOL Remove(T& t)
  121. {
  122. int nIndex = Find(t);
  123. if(nIndex == -1)
  124. return FALSE;
  125. return RemoveAt(nIndex);
  126. }
  127. BOOL RemoveAt(int nIndex)
  128. {
  129. //---- always call the dtr ----
  130. #if _MSC_VER >= 1200
  131. m_aT[nIndex].~T();
  132. #else
  133. T* MyT;
  134. MyT = &m_aT[nIndex];
  135. MyT->~T();
  136. #endif
  137. if(nIndex != (m_nSize - 1))
  138. {
  139. memmove((void*)&m_aT[nIndex], (void*)&m_aT[nIndex + 1], (m_nSize - (nIndex + 1)) * sizeof(T));
  140. }
  141. m_nSize--;
  142. return TRUE;
  143. }
  144. void RemoveAll()
  145. {
  146. if(m_aT != NULL)
  147. {
  148. for(int i = 0; i < m_nSize; i++) {
  149. #if _MSC_VER >= 1200
  150. m_aT[i].~T();
  151. #else
  152. T* MyT;
  153. MyT = &m_aT[i];
  154. MyT->~T();
  155. #endif
  156. }
  157. free(m_aT);
  158. m_aT = NULL;
  159. }
  160. m_nSize = 0;
  161. m_nAllocSize = 0;
  162. }
  163. T& operator[] (int nIndex) const
  164. {
  165. ATLASSERT(nIndex >= 0 && nIndex < m_nSize);
  166. return m_aT[nIndex];
  167. }
  168. T* GetData() const
  169. {
  170. return m_aT;
  171. }
  172. // Implementation
  173. class Wrapper
  174. {
  175. public:
  176. Wrapper(T& _t) : t(_t)
  177. {
  178. }
  179. template <class _Ty>
  180. void *operator new(size_t, _Ty* p)
  181. {
  182. return p;
  183. }
  184. T t;
  185. };
  186. void SetAtIndex(int nIndex, T& t)
  187. {
  188. ATLASSERT(nIndex >= 0 && nIndex < m_nSize);
  189. new(m_aT + nIndex) Wrapper(t);
  190. }
  191. int Find(T& t) const
  192. {
  193. for(int i = 0; i < m_nSize; i++)
  194. {
  195. if(m_aT[i] == t)
  196. return i;
  197. }
  198. return -1; // not found
  199. }
  200. };
  201. #endif // _SIMPSTR_H_
  202. //---------------------------------------------------------------------------