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.

303 lines
7.8 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. // File: strings.cpp
  8. //
  9. // Useful string manipulation functions.
  10. //
  11. //--------------------------------------------------------------------------
  12. #include "pch.h"
  13. #pragma hdrstop
  14. #include <netdom.h>
  15. #define StringByteSize(sz) \
  16. ((lstrlen(sz)+1)*sizeof(TCHAR))
  17. /*-----------------------------------------------------------------------------
  18. / LocalAllocString
  19. / ------------------
  20. / Allocate a string, and initialize it with the specified contents.
  21. /
  22. / In:
  23. / ppResult -> recieves pointer to the new string
  24. / pString -> string to initialize with
  25. /
  26. / Out:
  27. / HRESULT
  28. /----------------------------------------------------------------------------*/
  29. HRESULT LocalAllocString(LPTSTR* ppResult, LPCTSTR pString)
  30. {
  31. if ( !ppResult || !pString )
  32. return E_INVALIDARG;
  33. *ppResult = (LPTSTR)LocalAlloc(LPTR, StringByteSize(pString) );
  34. if ( !*ppResult )
  35. return E_OUTOFMEMORY;
  36. memcpy (*ppResult, pString, StringByteSize(pString) );
  37. return S_OK; // success
  38. }
  39. /*----------------------------------------------------------------------------
  40. / LocalAllocStringLen
  41. / ---------------------
  42. / Given a length return a buffer of that size.
  43. /
  44. / In:
  45. / ppResult -> receives the pointer to the string
  46. / cLen = length in characters to allocate
  47. /
  48. / Out:
  49. / HRESULT
  50. /----------------------------------------------------------------------------*/
  51. HRESULT LocalAllocStringLen(LPTSTR* ppResult, UINT cLen)
  52. {
  53. if ( !ppResult || cLen == 0 )
  54. return E_INVALIDARG;
  55. *ppResult = (LPTSTR)LocalAlloc(LPTR, (cLen+1) * sizeof(TCHAR));
  56. return *ppResult ? S_OK:E_OUTOFMEMORY;
  57. }
  58. /*-----------------------------------------------------------------------------
  59. / LocalFreeString
  60. / -----------------
  61. / Release the string pointed to be *ppString (which can be null) and
  62. / then reset the pointer back to NULL.
  63. /
  64. / In:
  65. / ppString -> pointer to string pointer to be free'd
  66. /
  67. / Out:
  68. / -
  69. /----------------------------------------------------------------------------*/
  70. void LocalFreeString(LPTSTR* ppString)
  71. {
  72. if ( ppString )
  73. {
  74. if ( *ppString )
  75. LocalFree((HLOCAL)*ppString);
  76. *ppString = NULL;
  77. }
  78. }
  79. //*************************************************************
  80. //
  81. // SizeofStringResource
  82. //
  83. // Purpose: Find the length (in chars) of a string resource
  84. //
  85. // Parameters: HINSTANCE hInstance - module containing the string
  86. // UINT idStr - ID of string
  87. //
  88. //
  89. // Return: UINT - # of chars in string, not including NULL
  90. //
  91. // Notes: Based on code from user32.
  92. //
  93. //*************************************************************
  94. UINT
  95. SizeofStringResource(HINSTANCE hInstance,
  96. UINT idStr)
  97. {
  98. UINT cch = 0;
  99. HRSRC hRes = FindResource(hInstance, (LPTSTR)((LONG_PTR)(((USHORT)idStr >> 4) + 1)), RT_STRING);
  100. if (NULL != hRes)
  101. {
  102. HGLOBAL hStringSeg = LoadResource(hInstance, hRes);
  103. if (NULL != hStringSeg)
  104. {
  105. LPWSTR psz = (LPWSTR)LockResource(hStringSeg);
  106. if (NULL != psz)
  107. {
  108. idStr &= 0x0F;
  109. while(true)
  110. {
  111. cch = *psz++;
  112. if (idStr-- == 0)
  113. break;
  114. psz += cch;
  115. }
  116. }
  117. }
  118. }
  119. return cch;
  120. }
  121. //*************************************************************
  122. //
  123. // LoadStringAlloc
  124. //
  125. // Purpose: Loads a string resource into an alloc'd buffer
  126. //
  127. // Parameters: ppszResult - string resource returned here
  128. // hInstance - module to load string from
  129. // idStr - string resource ID
  130. //
  131. // Return: same as LoadString
  132. //
  133. // Notes: On successful return, the caller must
  134. // LocalFree *ppszResult
  135. //
  136. //*************************************************************
  137. int
  138. LoadStringAlloc(LPTSTR *ppszResult, HINSTANCE hInstance, UINT idStr)
  139. {
  140. int nResult = 0;
  141. UINT cch = SizeofStringResource(hInstance, idStr);
  142. if (cch)
  143. {
  144. cch++; // for NULL
  145. *ppszResult = (LPTSTR)LocalAlloc(LPTR, cch * sizeof(TCHAR));
  146. if (*ppszResult)
  147. nResult = LoadString(hInstance, idStr, *ppszResult, cch);
  148. }
  149. return nResult;
  150. }
  151. //*************************************************************
  152. //
  153. // String formatting functions
  154. //
  155. //*************************************************************
  156. DWORD
  157. FormatStringID(LPTSTR *ppszResult, HINSTANCE hInstance, UINT idStr, ...)
  158. {
  159. DWORD dwResult;
  160. va_list args;
  161. va_start(args, idStr);
  162. dwResult = vFormatStringID(ppszResult, hInstance, idStr, &args);
  163. va_end(args);
  164. return dwResult;
  165. }
  166. DWORD
  167. FormatString(LPTSTR *ppszResult, LPCTSTR pszFormat, ...)
  168. {
  169. DWORD dwResult;
  170. va_list args;
  171. va_start(args, pszFormat);
  172. dwResult = vFormatString(ppszResult, pszFormat, &args);
  173. va_end(args);
  174. return dwResult;
  175. }
  176. DWORD
  177. vFormatStringID(LPTSTR *ppszResult, HINSTANCE hInstance, UINT idStr, va_list *pargs)
  178. {
  179. DWORD dwResult = 0;
  180. LPTSTR pszFormat = NULL;
  181. if (LoadStringAlloc(&pszFormat, hInstance, idStr))
  182. {
  183. dwResult = vFormatString(ppszResult, pszFormat, pargs);
  184. LocalFree(pszFormat);
  185. }
  186. return dwResult;
  187. }
  188. DWORD
  189. vFormatString(LPTSTR *ppszResult, LPCTSTR pszFormat, va_list *pargs)
  190. {
  191. return FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
  192. pszFormat,
  193. 0,
  194. 0,
  195. (LPTSTR)ppszResult,
  196. 1,
  197. pargs);
  198. }
  199. //*************************************************************
  200. //
  201. // GetSystemErrorText
  202. //
  203. // Purpose: Retrieve error text for a win32 error value
  204. //
  205. // Parameters: ppszResult - string resource returned here
  206. // dwErr - error ID
  207. //
  208. // Return: same as FormatMessage
  209. //
  210. // Notes: On successful return, the caller must
  211. // LocalFree *ppszResult
  212. //
  213. //*************************************************************
  214. DWORD
  215. GetSystemErrorText(LPTSTR *ppszResult, DWORD dwErr)
  216. {
  217. return FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  218. NULL,
  219. dwErr,
  220. 0,
  221. (LPTSTR)ppszResult,
  222. 0,
  223. NULL);
  224. }
  225. WCHAR swzMemErr[] = L"ERROR memory allocation failure!";
  226. //+----------------------------------------------------------------------------
  227. //
  228. // Method: CSafeUnicodeString::Assign
  229. //
  230. // Synopsis: Copy the UNICODE_STRING to a null terminated string.
  231. //
  232. //-----------------------------------------------------------------------------
  233. void
  234. CSafeUnicodeString::Assign(UNICODE_STRING * pus)
  235. {
  236. if (!pus)
  237. {
  238. _fAllocFailed = true;
  239. return;
  240. }
  241. if (_pwz && _pwz != swzMemErr)
  242. {
  243. delete [] _pwz;
  244. _pwz = NULL;
  245. _cch = 0;
  246. }
  247. _pwz = new WCHAR[pus->Length + 1];
  248. if (!_pwz)
  249. {
  250. _fAllocFailed = true;
  251. _pwz = swzMemErr;
  252. _cch = sizeof(swzMemErr) / sizeof(WCHAR) - 1;
  253. return;
  254. }
  255. if (pus->Buffer && pus->Length)
  256. {
  257. wcsncpy(_pwz, pus->Buffer, pus->Length);
  258. }
  259. _pwz[pus->Length] = L'\0';
  260. _cch = pus->Length;
  261. _fAllocFailed = false;
  262. }
  263. CSafeUnicodeString::~CSafeUnicodeString()
  264. {
  265. if (_pwz && _pwz != swzMemErr)
  266. {
  267. delete [] _pwz;
  268. }
  269. }