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.

267 lines
7.0 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. /*-----------------------------------------------------------------------------
  15. / LocalAllocString
  16. / ------------------
  17. / Allocate a string, and initialize it with the specified contents.
  18. /
  19. / In:
  20. / ppResult -> recieves pointer to the new string
  21. / pString -> string to initialize with
  22. /
  23. / Out:
  24. / HRESULT
  25. /----------------------------------------------------------------------------*/
  26. HRESULT LocalAllocString(LPTSTR* ppResult, LPCTSTR pString)
  27. {
  28. HRESULT hr;
  29. TraceEnter(TRACE_COMMON_STR, "LocalAllocString");
  30. TraceAssert(ppResult);
  31. TraceAssert(pString);
  32. if ( !ppResult || !pString )
  33. ExitGracefully(hr, E_INVALIDARG, "Bad arguments");
  34. *ppResult = (LPTSTR)LocalAlloc(LPTR, StringByteSize(pString) );
  35. if ( !*ppResult )
  36. ExitGracefully(hr, E_OUTOFMEMORY, "Failed to allocate buffer");
  37. lstrcpy(*ppResult, pString);
  38. hr = S_OK; // success
  39. exit_gracefully:
  40. TraceLeaveResult(hr);
  41. }
  42. /*----------------------------------------------------------------------------
  43. / LocalAllocStringLen
  44. / ---------------------
  45. / Given a length return a buffer of that size.
  46. /
  47. / In:
  48. / ppResult -> receives the pointer to the string
  49. / cLen = length in characters to allocate
  50. /
  51. / Out:
  52. / HRESULT
  53. /----------------------------------------------------------------------------*/
  54. HRESULT LocalAllocStringLen(LPTSTR* ppResult, UINT cLen)
  55. {
  56. HRESULT hr;
  57. TraceEnter(TRACE_COMMON_STR, "LocalAllocStringLen");
  58. TraceAssert(ppResult);
  59. if ( !ppResult || cLen == 0 )
  60. ExitGracefully(hr, E_INVALIDARG, "Bad arguments (length or buffer)");
  61. *ppResult = (LPTSTR)LocalAlloc(LPTR, (cLen+1) * SIZEOF(TCHAR));
  62. hr = *ppResult ? S_OK:E_OUTOFMEMORY;
  63. exit_gracefully:
  64. TraceLeaveResult(hr);
  65. }
  66. /*-----------------------------------------------------------------------------
  67. / LocalFreeString
  68. / -----------------
  69. / Release the string pointed to be *ppString (which can be null) and
  70. / then reset the pointer back to NULL.
  71. /
  72. / In:
  73. / ppString -> pointer to string pointer to be free'd
  74. /
  75. / Out:
  76. / -
  77. /----------------------------------------------------------------------------*/
  78. void LocalFreeString(LPTSTR* ppString)
  79. {
  80. if ( ppString )
  81. {
  82. if ( *ppString )
  83. LocalFree((HLOCAL)*ppString);
  84. *ppString = NULL;
  85. }
  86. }
  87. //*************************************************************
  88. //
  89. // SizeofStringResource
  90. //
  91. // Purpose: Find the length (in chars) of a string resource
  92. //
  93. // Parameters: HINSTANCE hInstance - module containing the string
  94. // UINT idStr - ID of string
  95. //
  96. //
  97. // Return: UINT - # of chars in string, not including NULL
  98. //
  99. // Notes: Based on code from user32.
  100. //
  101. //*************************************************************
  102. UINT
  103. SizeofStringResource(HINSTANCE hInstance,
  104. UINT idStr)
  105. {
  106. UINT cch = 0;
  107. HRSRC hRes = FindResource(hInstance, (LPTSTR)((LONG_PTR)(((USHORT)idStr >> 4) + 1)), RT_STRING);
  108. if (NULL != hRes)
  109. {
  110. HGLOBAL hStringSeg = LoadResource(hInstance, hRes);
  111. if (NULL != hStringSeg)
  112. {
  113. LPWSTR psz = (LPWSTR)LockResource(hStringSeg);
  114. if (NULL != psz)
  115. {
  116. idStr &= 0x0F;
  117. while(true)
  118. {
  119. cch = *psz++;
  120. if (idStr-- == 0)
  121. break;
  122. psz += cch;
  123. }
  124. }
  125. }
  126. }
  127. return cch;
  128. }
  129. //*************************************************************
  130. //
  131. // LoadStringAlloc
  132. //
  133. // Purpose: Loads a string resource into an alloc'd buffer
  134. //
  135. // Parameters: ppszResult - string resource returned here
  136. // hInstance - module to load string from
  137. // idStr - string resource ID
  138. //
  139. // Return: same as LoadString
  140. //
  141. // Notes: On successful return, the caller must
  142. // LocalFree *ppszResult
  143. //
  144. //*************************************************************
  145. int
  146. LoadStringAlloc(LPTSTR *ppszResult, HINSTANCE hInstance, UINT idStr)
  147. {
  148. int nResult = 0;
  149. UINT cch = SizeofStringResource(hInstance, idStr);
  150. if (cch)
  151. {
  152. cch++; // for NULL
  153. *ppszResult = (LPTSTR)LocalAlloc(LPTR, cch * sizeof(TCHAR));
  154. if (*ppszResult)
  155. nResult = LoadString(hInstance, idStr, *ppszResult, cch);
  156. }
  157. return nResult;
  158. }
  159. //*************************************************************
  160. //
  161. // String formatting functions
  162. //
  163. //*************************************************************
  164. DWORD
  165. FormatStringID(LPTSTR *ppszResult, HINSTANCE hInstance, UINT idStr, ...)
  166. {
  167. DWORD dwResult;
  168. va_list args;
  169. va_start(args, idStr);
  170. dwResult = vFormatStringID(ppszResult, hInstance, idStr, &args);
  171. va_end(args);
  172. return dwResult;
  173. }
  174. DWORD
  175. FormatString(LPTSTR *ppszResult, LPCTSTR pszFormat, ...)
  176. {
  177. DWORD dwResult;
  178. va_list args;
  179. va_start(args, pszFormat);
  180. dwResult = vFormatString(ppszResult, pszFormat, &args);
  181. va_end(args);
  182. return dwResult;
  183. }
  184. DWORD
  185. vFormatStringID(LPTSTR *ppszResult, HINSTANCE hInstance, UINT idStr, va_list *pargs)
  186. {
  187. DWORD dwResult = 0;
  188. LPTSTR pszFormat = NULL;
  189. if (LoadStringAlloc(&pszFormat, hInstance, idStr))
  190. {
  191. dwResult = vFormatString(ppszResult, pszFormat, pargs);
  192. LocalFree(pszFormat);
  193. }
  194. return dwResult;
  195. }
  196. DWORD
  197. vFormatString(LPTSTR *ppszResult, LPCTSTR pszFormat, va_list *pargs)
  198. {
  199. return FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
  200. pszFormat,
  201. 0,
  202. 0,
  203. (LPTSTR)ppszResult,
  204. 1,
  205. pargs);
  206. }
  207. //*************************************************************
  208. //
  209. // GetSystemErrorText
  210. //
  211. // Purpose: Retrieve error text for a win32 error value
  212. //
  213. // Parameters: ppszResult - string resource returned here
  214. // dwErr - error ID
  215. //
  216. // Return: same as FormatMessage
  217. //
  218. // Notes: On successful return, the caller must
  219. // LocalFree *ppszResult
  220. //
  221. //*************************************************************
  222. DWORD
  223. GetSystemErrorText(LPTSTR *ppszResult, DWORD dwErr)
  224. {
  225. return FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  226. NULL,
  227. dwErr,
  228. 0,
  229. (LPTSTR)ppszResult,
  230. 0,
  231. NULL);
  232. }