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.

377 lines
7.0 KiB

  1. //---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995
  5. //
  6. // File: object.cxx
  7. //
  8. // Contents:
  9. //
  10. // History:
  11. //----------------------------------------------------------------------------
  12. #include "nwcompat.hxx"
  13. #pragma hdrstop
  14. HRESULT
  15. BuildObjectArray(
  16. VARIANT var,
  17. SAFEARRAY ** ppFilter,
  18. DWORD * pdwNumElements
  19. );
  20. HRESULT
  21. BuildDefaultObjectArray(
  22. PFILTERS pFilters,
  23. DWORD dwMaxFilters,
  24. SAFEARRAY ** ppFilter,
  25. DWORD * pdwNumElements
  26. );
  27. ObjectTypeList::ObjectTypeList()
  28. {
  29. _pObjList = NULL;
  30. _dwCurrentIndex = 0;
  31. _dwMaxElements = 0;
  32. _dwUBound = 0;
  33. _dwLBound = 0;
  34. }
  35. HRESULT
  36. ObjectTypeList::CreateObjectTypeList(
  37. VARIANT vFilter,
  38. ObjectTypeList ** ppObjectTypeList
  39. )
  40. {
  41. ObjectTypeList * pObjectTypeList = NULL;
  42. HRESULT hr = S_OK;
  43. pObjectTypeList = new ObjectTypeList;
  44. if (!pObjectTypeList) {
  45. hr = E_OUTOFMEMORY;
  46. BAIL_ON_FAILURE(hr);
  47. }
  48. hr = BuildObjectArray(
  49. vFilter,
  50. &pObjectTypeList->_pObjList,
  51. &pObjectTypeList->_dwMaxElements
  52. );
  53. if (FAILED(hr)) {
  54. hr = BuildDefaultObjectArray(
  55. gpFilters,
  56. gdwMaxFilters,
  57. &pObjectTypeList->_pObjList,
  58. &pObjectTypeList->_dwMaxElements
  59. );
  60. BAIL_ON_FAILURE(hr);
  61. }
  62. hr = SafeArrayGetUBound(
  63. pObjectTypeList->_pObjList,
  64. 1,
  65. (long FAR *)&pObjectTypeList->_dwUBound
  66. );
  67. BAIL_ON_FAILURE(hr);
  68. hr = SafeArrayGetLBound(
  69. pObjectTypeList->_pObjList,
  70. 1,
  71. (long FAR *)&pObjectTypeList->_dwLBound
  72. );
  73. BAIL_ON_FAILURE(hr);
  74. pObjectTypeList->_dwCurrentIndex = pObjectTypeList->_dwLBound;
  75. *ppObjectTypeList = pObjectTypeList;
  76. RRETURN(S_OK);
  77. error:
  78. if (pObjectTypeList) {
  79. delete pObjectTypeList;
  80. }
  81. RRETURN(hr);
  82. }
  83. ObjectTypeList::~ObjectTypeList()
  84. {
  85. HRESULT hr = S_OK;
  86. if (_pObjList) {
  87. hr = SafeArrayDestroy(_pObjList);
  88. }
  89. }
  90. HRESULT
  91. ObjectTypeList::GetCurrentObject(
  92. PDWORD pdwObject
  93. )
  94. {
  95. HRESULT hr = S_OK;
  96. if (_dwCurrentIndex > _dwUBound) {
  97. return(E_FAIL);
  98. }
  99. hr = SafeArrayGetElement(
  100. _pObjList,
  101. (long FAR *)&_dwCurrentIndex,
  102. (void *)pdwObject
  103. );
  104. RRETURN(hr);
  105. }
  106. HRESULT
  107. ObjectTypeList::Next()
  108. {
  109. HRESULT hr = S_OK;
  110. _dwCurrentIndex++;
  111. if (_dwCurrentIndex > _dwUBound) {
  112. return(E_FAIL);
  113. }
  114. return(hr);
  115. }
  116. HRESULT
  117. ObjectTypeList::Reset()
  118. {
  119. HRESULT hr = S_OK;
  120. return(hr);
  121. }
  122. HRESULT
  123. IsValidFilter(
  124. LPWSTR ObjectName,
  125. DWORD *pdwFilterId,
  126. PFILTERS pFilters,
  127. DWORD dwMaxFilters
  128. )
  129. {
  130. DWORD i = 0;
  131. for (i = 0; i < dwMaxFilters; i++) {
  132. if (!_wcsicmp(ObjectName, (pFilters + i)->szObjectName)) {
  133. *pdwFilterId = (pFilters + i)->dwFilterId;
  134. RRETURN(S_OK);
  135. }
  136. }
  137. *pdwFilterId = 0;
  138. RRETURN(E_FAIL);
  139. }
  140. HRESULT
  141. BuildDefaultObjectArray(
  142. PFILTERS pFilters,
  143. DWORD dwMaxFilters,
  144. SAFEARRAY ** ppFilter,
  145. DWORD * pdwNumElements
  146. )
  147. {
  148. DWORD i;
  149. HRESULT hr = S_OK;
  150. SAFEARRAYBOUND sabNewArray;
  151. SAFEARRAY * pFilter = NULL;
  152. sabNewArray.cElements = dwMaxFilters;
  153. sabNewArray.lLbound = 0;
  154. pFilter = SafeArrayCreate(
  155. VT_I4,
  156. 1,
  157. &sabNewArray
  158. );
  159. if (!pFilter){
  160. hr = E_OUTOFMEMORY;
  161. BAIL_ON_FAILURE(hr);
  162. }
  163. for (i = 0; i < dwMaxFilters; i++) {
  164. hr = SafeArrayPutElement(
  165. pFilter,
  166. (long *)&i,
  167. (void *)&((pFilters + i)->dwFilterId)
  168. );
  169. BAIL_ON_FAILURE(hr);
  170. }
  171. *ppFilter = pFilter;
  172. *pdwNumElements = dwMaxFilters;
  173. RRETURN(S_OK);
  174. error:
  175. if (pFilter) {
  176. SafeArrayDestroy(pFilter);
  177. }
  178. *ppFilter = NULL;
  179. *pdwNumElements = 0;
  180. RRETURN(hr);
  181. }
  182. HRESULT
  183. BuildObjectArray(
  184. VARIANT var,
  185. SAFEARRAY ** ppFilter,
  186. DWORD * pdwNumElements
  187. )
  188. {
  189. LONG uDestCount = 0;
  190. LONG dwSLBound = 0;
  191. LONG dwSUBound = 0;
  192. VARIANT v;
  193. VARIANT varDest;
  194. LONG i;
  195. HRESULT hr = S_OK;
  196. SAFEARRAYBOUND sabNewArray;
  197. DWORD dwFilterId;
  198. SAFEARRAY * pFilter = NULL;
  199. if (!(V_VT(&var) == (VT_VARIANT|VT_ARRAY))) {
  200. RRETURN(E_FAIL);
  201. }
  202. //
  203. // Check that there is only one dimension in this array
  204. //
  205. if ((V_ARRAY(&var))->cDims != 1) {
  206. hr = E_FAIL;
  207. BAIL_ON_FAILURE(hr);
  208. }
  209. //
  210. // Check that there is atleast one element in this array
  211. //
  212. if ((V_ARRAY(&var))->rgsabound[0].cElements == 0){
  213. hr = E_FAIL;
  214. BAIL_ON_FAILURE(hr);
  215. }
  216. //
  217. // We know that this is a valid single dimension array
  218. //
  219. hr = SafeArrayGetLBound(V_ARRAY(&var),
  220. 1,
  221. (long FAR *)&dwSLBound
  222. );
  223. BAIL_ON_FAILURE(hr);
  224. hr = SafeArrayGetUBound(V_ARRAY(&var),
  225. 1,
  226. (long FAR *)&dwSUBound
  227. );
  228. BAIL_ON_FAILURE(hr);
  229. sabNewArray.cElements = dwSUBound - dwSLBound + 1;
  230. sabNewArray.lLbound = dwSLBound;
  231. pFilter = SafeArrayCreate(
  232. VT_I4,
  233. 1,
  234. &sabNewArray
  235. );
  236. if (!pFilter) {
  237. hr = E_OUTOFMEMORY;
  238. BAIL_ON_FAILURE(hr);
  239. }
  240. for (i = dwSLBound; i <= dwSUBound; i++) {
  241. VariantInit(&v);
  242. hr = SafeArrayGetElement(V_ARRAY(&var),
  243. (long FAR *)&i,
  244. &v
  245. );
  246. if (FAILED(hr)) {
  247. continue;
  248. }
  249. hr = IsValidFilter(
  250. V_BSTR(&v),
  251. &dwFilterId,
  252. gpFilters,
  253. gdwMaxFilters
  254. );
  255. if (FAILED(hr)) {
  256. VariantClear(&v);
  257. continue;
  258. }
  259. VariantClear(&v);
  260. hr = SafeArrayPutElement(
  261. pFilter,
  262. (long*)&uDestCount,
  263. (void *)&dwFilterId
  264. );
  265. if(FAILED(hr)){
  266. continue;
  267. }
  268. uDestCount++;
  269. }
  270. //
  271. // There was nothing of value that could be retrieved from the
  272. // filter and vFilter do not contain entries, that means no filter is
  273. // being set. Hence, we return err in this case so that we build the
  274. // default array.
  275. //
  276. if ( !uDestCount && (( dwSUBound - dwSLBound + 1 ) == 0 )) {
  277. hr = E_FAIL;
  278. BAIL_ON_FAILURE(hr);
  279. }
  280. *pdwNumElements = uDestCount;
  281. *ppFilter = pFilter;
  282. RRETURN(S_OK);
  283. error:
  284. if (pFilter) {
  285. SafeArrayDestroy(pFilter);
  286. }
  287. *ppFilter = NULL;
  288. *pdwNumElements = 0;
  289. RRETURN(hr);
  290. }