Source code of Windows XP (NT5)
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.

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