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.

366 lines
8.5 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: fbstr.cxx
  7. //
  8. // Contents: Wrappers around BSTR api to account for wierdness with NULL
  9. //
  10. // Functions: ADsAllocString
  11. // ADsAllocStringLen
  12. // ADsReAllocString
  13. // ADsReAllocStringLen
  14. // ADsFreeString
  15. // ADsStringLen
  16. // ADsStringByteLen
  17. // ADsAllocStringByteLen
  18. // ADsStringCmp
  19. // ADsStringNCmp
  20. // ADsStringICmp
  21. // ADsStringNICmp
  22. //
  23. // History: 25-Oct-94 krishnag
  24. //
  25. //
  26. //----------------------------------------------------------------------------
  27. #include "procs.hxx"
  28. //+---------------------------------------------------------------------------
  29. //
  30. // Function: ADsAllocString
  31. //
  32. // Synopsis: Allocs a BSTR and initializes it from a string. If the
  33. // initializer is NULL or the empty string, the resulting bstr is
  34. // NULL.
  35. //
  36. // Arguments: [pch] -- String to initialize BSTR.
  37. // [pBSTR] -- The result.
  38. //
  39. // Returns: HRESULT.
  40. //
  41. // Modifies: [pBSTR]
  42. //
  43. // History: 5-06-94 adams Created
  44. //
  45. //----------------------------------------------------------------------------
  46. STDAPI
  47. ADsAllocString(const OLECHAR * pch, BSTR * pBSTR)
  48. {
  49. HRESULT hr = S_OK;
  50. ADsAssert(pBSTR);
  51. if (!pch)
  52. {
  53. *pBSTR = NULL;
  54. return S_OK;
  55. }
  56. *pBSTR = SysAllocString(pch);
  57. hr = (*pBSTR) ? S_OK : E_OUTOFMEMORY;
  58. RRETURN(hr);
  59. }
  60. //+---------------------------------------------------------------------------
  61. //
  62. // Function: ADsAllocStringLen
  63. //
  64. // Synopsis: Allocs a BSTR of [uc] + 1 OLECHARS, and
  65. // initializes it from an optional string. If [uc] == 0, the
  66. // resulting bstr is NULL.
  67. //
  68. // Arguments: [pch] -- String to initialize.
  69. // [uc] -- Count of characters of string.
  70. // [pBSTR] -- The result.
  71. //
  72. // Returns: HRESULT.
  73. //
  74. // Modifies: [pBSTR].
  75. //
  76. // History: 5-06-94 adams Created
  77. //
  78. //----------------------------------------------------------------------------
  79. STDAPI
  80. ADsAllocStringLen(const OLECHAR * pch, UINT uc, BSTR * pBSTR)
  81. {
  82. HRESULT hr = S_OK;
  83. ADsAssert(pBSTR);
  84. if (!pch){
  85. *pBSTR = NULL;
  86. return S_OK;
  87. }
  88. *pBSTR = SysAllocStringLen(pch, uc);
  89. hr = *pBSTR ? S_OK : E_OUTOFMEMORY;
  90. RRETURN(hr);
  91. }
  92. //+---------------------------------------------------------------------------
  93. //
  94. // Function: ADsReAllocString
  95. //
  96. // Synopsis: Allocates a BSTR initialized from a string; if successful,
  97. // frees the original string and replaces it.
  98. //
  99. // Arguments: [pBSTR] -- String to reallocate.
  100. // [pch] -- Initializer.
  101. //
  102. // Returns: HRESULT.
  103. //
  104. // Modifies: [pBSTR].
  105. //
  106. // History: 5-06-94 adams Created
  107. //
  108. //----------------------------------------------------------------------------
  109. STDAPI
  110. ADsReAllocString(BSTR * pBSTR, const OLECHAR * pch)
  111. {
  112. ADsAssert(pBSTR);
  113. #if DBG == 1
  114. HRESULT hr;
  115. BSTR bstrTmp;
  116. hr = ADsAllocString(pch, &bstrTmp);
  117. if (hr)
  118. RRETURN(hr);
  119. ADsFreeString(*pBSTR);
  120. *pBSTR = bstrTmp;
  121. return S_OK;
  122. #else
  123. if (!pch){
  124. SysFreeString(*pBSTR);
  125. *pBSTR = NULL;
  126. return S_OK;
  127. }
  128. return SysReAllocString(pBSTR, pch) ? S_OK : E_OUTOFMEMORY;
  129. #endif
  130. }
  131. //+---------------------------------------------------------------------------
  132. //
  133. // Function: ADsReAllocStringLen
  134. //
  135. // Synopsis: Allocates a BSTR of [uc] + 1 OLECHARs and optionally
  136. // initializes it from a string; if successful, frees the original
  137. // string and replaces it.
  138. //
  139. // Arguments: [pBSTR] -- String to reallocate.
  140. // [pch] -- Initializer.
  141. // [uc] -- Count of characters.
  142. //
  143. // Returns: HRESULT.
  144. //
  145. // Modifies: [pBSTR].
  146. //
  147. // History: 5-06-94 adams Created
  148. //
  149. //----------------------------------------------------------------------------
  150. STDAPI
  151. ADsReAllocStringLen(BSTR * pBSTR, const OLECHAR * pch, UINT uc)
  152. {
  153. ADsAssert(pBSTR);
  154. #if DBG == 1
  155. HRESULT hr;
  156. BSTR bstrTmp;
  157. hr = ADsAllocStringLen(pch, uc, &bstrTmp);
  158. if (hr)
  159. RRETURN(hr);
  160. ADsFreeString(*pBSTR);
  161. *pBSTR = bstrTmp;
  162. return S_OK;
  163. #else
  164. if (!pch){
  165. SysFreeString(*pBSTR);
  166. *pBSTR = NULL;
  167. return S_OK;
  168. }
  169. return SysReAllocStringLen(pBSTR, pch, uc) ? S_OK : E_OUTOFMEMORY;
  170. #endif
  171. }
  172. //+---------------------------------------------------------------------------
  173. //
  174. // Function: ADsStringLen
  175. //
  176. // Synopsis: Returns the length of the BSTR.
  177. //
  178. // History: 5-06-94 adams Created
  179. //
  180. //----------------------------------------------------------------------------
  181. STDAPI_(UINT)
  182. ADsStringLen(BSTR bstr)
  183. {
  184. return bstr ? SysStringLen(bstr) : 0;
  185. }
  186. #ifdef WIN32
  187. //+---------------------------------------------------------------------------
  188. //
  189. // Function: ADsStringByteLen
  190. //
  191. // Synopsis: Returns the length of a BSTR in bytes.
  192. //
  193. // History: 5-06-94 adams Created
  194. //
  195. //----------------------------------------------------------------------------
  196. STDAPI_(UINT)
  197. ADsStringByteLen(BSTR bstr)
  198. {
  199. return bstr ? SysStringByteLen(bstr) : 0;
  200. }
  201. //+---------------------------------------------------------------------------
  202. //
  203. // Function: ADsAllocStringByteLen
  204. //
  205. // Synopsis: Allocates a BSTR of [uc] + 1 chars and optionally initializes
  206. // from a string. If [uc] = 0, the resulting bstr is NULL.
  207. //
  208. // Arguments: [pch] -- Initializer.
  209. // [uc] -- Count of chars.
  210. // [pBSTR] -- Result.
  211. //
  212. // Returns: HRESULT.
  213. //
  214. // Modifies: [pBSTR].
  215. //
  216. // History: 5-06-94 adams Created
  217. //
  218. //----------------------------------------------------------------------------
  219. STDAPI
  220. ADsAllocStringByteLen(const char * pch, UINT uc, BSTR * pBSTR)
  221. {
  222. HRESULT hr = S_OK;
  223. ADsAssert(pBSTR);
  224. if (!pch){
  225. *pBSTR = NULL;
  226. return S_OK;
  227. }
  228. *pBSTR = SysAllocStringByteLen(pch, uc);
  229. RRETURN(hr);
  230. }
  231. #endif
  232. //+---------------------------------------------------------------------------
  233. //
  234. // Function: ADsStringCmp
  235. //
  236. // Synopsis: As per wcscmp, checking for NULL bstrs.
  237. //
  238. // History: 5-06-94 adams Created
  239. // 25-Jun-94 doncl changed from _tc to wc
  240. //
  241. //----------------------------------------------------------------------------
  242. STDAPI_(int)
  243. ADsStringCmp(CBSTR bstr1, CBSTR bstr2)
  244. {
  245. return wcscmp(STRVAL(bstr1), STRVAL(bstr2));
  246. }
  247. //+---------------------------------------------------------------------------
  248. //
  249. // Function: ADsStringNCmp
  250. //
  251. // Synopsis: As per wcsncmp, checking for NULL bstrs.
  252. //
  253. // History: 5-06-94 adams Created
  254. // 25-Jun-94 doncl changed from _tc to wc
  255. //
  256. //----------------------------------------------------------------------------
  257. STDAPI_(int)
  258. ADsStringNCmp(CBSTR bstr1, CBSTR bstr2, size_t c)
  259. {
  260. return wcsncmp(STRVAL(bstr1), STRVAL(bstr2), c);
  261. }
  262. //+---------------------------------------------------------------------------
  263. //
  264. // Function: ADsStringICmp
  265. //
  266. // Synopsis: As per wcsicmp, checking for NULL bstrs.
  267. //
  268. // History: 5-06-94 adams Created
  269. // 25-Jun-94 doncl changed from _tc to wc
  270. // 15-Aug-94 doncl changed from wcsicmp to _wcsicmp
  271. //
  272. //----------------------------------------------------------------------------
  273. STDAPI_(int)
  274. ADsStringICmp(CBSTR bstr1, CBSTR bstr2)
  275. {
  276. return _wcsicmp(STRVAL(bstr1), STRVAL(bstr2));
  277. }
  278. //+---------------------------------------------------------------------------
  279. //
  280. // Function: ADsStringNICmp
  281. //
  282. // Synopsis: As per wcsnicmp, checking for NULL bstrs.
  283. //
  284. // History: 5-06-94 adams Created
  285. // 25-Jun-94 doncl changed from _tc to wc
  286. // 15-Aug-94 doncl changed from wcsnicmp to _wcsnicmp
  287. //
  288. //----------------------------------------------------------------------------
  289. STDAPI_(int)
  290. ADsStringNICmp(CBSTR bstr1, CBSTR bstr2, size_t c)
  291. {
  292. return _wcsnicmp(STRVAL(bstr1), STRVAL(bstr2), c);
  293. }