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.

280 lines
4.2 KiB

  1. // ExtendString.cpp: implementation of the CStringExt class.
  2. //
  3. // Copyright (c) 2001 Microsoft Corporation, All Rights Reserved
  4. //
  5. //////////////////////////////////////////////////////////////////////
  6. #include "precomp.h"
  7. #include "ExtendString.h"
  8. CStringExt::CStringExt ( DWORD dwSize ) :
  9. m_dwSize ( 0 ),
  10. m_wszString ( NULL )
  11. {
  12. if ( !dwSize )
  13. {
  14. dwSize = BUFF_SIZE_EXT;
  15. }
  16. try
  17. {
  18. if ( ( m_wszString = new TCHAR [ dwSize ] ) != NULL )
  19. {
  20. * m_wszString = 0;
  21. m_dwSize = dwSize;
  22. }
  23. else
  24. {
  25. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  26. }
  27. }
  28. catch ( ... )
  29. {
  30. if ( m_wszString )
  31. {
  32. delete [] m_wszString;
  33. m_wszString = NULL;
  34. }
  35. m_dwSize = 0L;
  36. throw;
  37. }
  38. }
  39. CStringExt::CStringExt ( LPCTSTR wsz ) :
  40. m_dwSize ( 0 ),
  41. m_wszString ( NULL )
  42. {
  43. try
  44. {
  45. DWORD dwSize = 0L;
  46. if ( wsz )
  47. {
  48. dwSize = lstrlen ( wsz ) + 1;
  49. if (dwSize < BUFF_SIZE_EXT)
  50. {
  51. dwSize = BUFF_SIZE_EXT;
  52. }
  53. }
  54. else
  55. {
  56. dwSize = BUFF_SIZE_EXT;
  57. }
  58. if ( ( m_wszString = new TCHAR [ dwSize ] ) != NULL )
  59. {
  60. if ( wsz )
  61. {
  62. lstrcpy ( m_wszString, wsz );
  63. }
  64. else
  65. {
  66. * m_wszString = 0;
  67. }
  68. m_dwSize = dwSize;
  69. }
  70. else
  71. {
  72. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  73. }
  74. }
  75. catch ( ... )
  76. {
  77. if ( m_wszString )
  78. {
  79. delete [] m_wszString;
  80. m_wszString = NULL;
  81. }
  82. m_dwSize = 0L;
  83. throw;
  84. }
  85. }
  86. CStringExt::~CStringExt ()
  87. {
  88. if ( m_wszString )
  89. {
  90. delete [] m_wszString;
  91. m_wszString = NULL;
  92. }
  93. m_dwSize = 0L;
  94. }
  95. HRESULT CStringExt::Clear ()
  96. {
  97. HRESULT hr = S_FALSE;
  98. if ( m_wszString )
  99. {
  100. *m_wszString = 0;
  101. hr = S_OK;
  102. }
  103. return hr;
  104. }
  105. HRESULT CStringExt::Copy ( LPCTSTR wsz )
  106. {
  107. HRESULT hr = E_FAIL;
  108. if ( wsz )
  109. {
  110. Clear ();
  111. hr = Append ( 1, wsz );
  112. }
  113. else
  114. {
  115. hr = S_FALSE;
  116. }
  117. return hr;
  118. }
  119. HRESULT CStringExt::Append ( DWORD dwCount, ... )
  120. {
  121. HRESULT hr = E_FAIL;
  122. if ( dwCount )
  123. {
  124. va_list argList;
  125. va_start ( argList, dwCount );
  126. hr = AppendList ( 0, NULL, dwCount, argList );
  127. va_end ( argList );
  128. }
  129. else
  130. {
  131. hr = S_FALSE;
  132. }
  133. return hr;
  134. }
  135. HRESULT CStringExt::AppendList ( DWORD dwConstantSize, LPCWSTR wszConstant, DWORD dwCount, va_list & argList )
  136. {
  137. HRESULT hr = S_OK;
  138. DWORD dwsz = 0L;
  139. va_list argListSave = argList;
  140. try
  141. {
  142. LPCTSTR wszc = NULL;
  143. for ( DWORD dw = 0; dw < dwCount; dw++ )
  144. {
  145. if ( ( wszc = va_arg ( argList, LPCTSTR ) ) != NULL )
  146. {
  147. dwsz += lstrlen ( wszc );
  148. }
  149. }
  150. //reuse dw for offset into buffer for start
  151. if ( dwConstantSize && wszConstant )
  152. {
  153. dw = dwConstantSize;
  154. }
  155. else
  156. {
  157. dw = _tcslen ( m_wszString );
  158. }
  159. if ( dw + dwsz + 1 < m_dwSize )
  160. {
  161. LPTSTR wsz = NULL;
  162. //reuse dw for start of append
  163. dw = 0L;
  164. if ( dwConstantSize && wszConstant )
  165. {
  166. wsz = & ( m_wszString [ dwConstantSize ] );
  167. wszc = va_arg ( argListSave, LPCTSTR );
  168. _tcscpy ( wsz, wszc );
  169. dw = 1;
  170. }
  171. else
  172. {
  173. wsz = m_wszString;
  174. }
  175. for ( DWORD dwLoop = dw; dwLoop < dwCount; dwLoop++ )
  176. {
  177. if ( ( wszc = va_arg ( argListSave, LPCTSTR ) ) != NULL )
  178. {
  179. _tcscat ( wsz, wszc );
  180. }
  181. }
  182. }
  183. else
  184. {
  185. LPTSTR wszHelp = NULL;
  186. try
  187. {
  188. if ( ( wszHelp = new TCHAR [ dw + dwsz + 1 ] ) != NULL )
  189. {
  190. if ( dwConstantSize && wszConstant )
  191. {
  192. _tcscpy ( wszHelp, wszConstant );
  193. }
  194. else
  195. {
  196. _tcscpy ( wszHelp, m_wszString );
  197. }
  198. for ( dw = 0; dw < dwCount; dw++ )
  199. {
  200. LPCTSTR wszc = NULL;
  201. wszc = va_arg ( argListSave, LPCTSTR );
  202. if ( wszc )
  203. {
  204. _tcscat ( wszHelp, wszc );
  205. }
  206. }
  207. }
  208. else
  209. {
  210. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  211. }
  212. }
  213. catch ( ... )
  214. {
  215. if ( wszHelp )
  216. {
  217. delete [] wszHelp;
  218. wszHelp = NULL;
  219. }
  220. throw;
  221. }
  222. if ( m_wszString )
  223. {
  224. delete [] m_wszString;
  225. m_wszString = NULL;
  226. }
  227. m_wszString = wszHelp;
  228. m_dwSize = dw + dwsz + 1;
  229. }
  230. }
  231. catch ( ... )
  232. {
  233. throw;
  234. }
  235. return hr;
  236. }