From: Alex Henrie Subject: [PATCH v10 3/4] shell32: Implement FolderItems_Item. Message-Id: <20160826043318.26732-3-alexhenrie24@gmail.com> Date: Thu, 25 Aug 2016 22:32:47 -0600 In-Reply-To: <20160826043318.26732-1-alexhenrie24@gmail.com> References: <20160826043318.26732-1-alexhenrie24@gmail.com> Cc: Sebastian Lackner v10 minimizes final memory consumption. Signed-off-by: Alex Henrie --- dlls/shell32/shelldispatch.c | 137 +++++++++++++++++++++++++++++++++++-- dlls/shell32/tests/shelldispatch.c | 13 ---- 2 files changed, 131 insertions(+), 19 deletions(-) diff --git a/dlls/shell32/shelldispatch.c b/dlls/shell32/shelldispatch.c index ac79302..3a30b1e 100644 --- a/dlls/shell32/shelldispatch.c +++ b/dlls/shell32/shelldispatch.c @@ -68,6 +68,9 @@ typedef struct { typedef struct { FolderItems3 FolderItems3_iface; LONG ref; + VARIANT dir; + WCHAR **item_filenames; + LONG item_count; } FolderItemsImpl; typedef struct { @@ -989,11 +992,18 @@ static ULONG WINAPI FolderItemsImpl_Release(FolderItems3 *iface) { FolderItemsImpl *This = impl_from_FolderItems(iface); ULONG ref = InterlockedDecrement(&This->ref); + LONG i; TRACE("(%p), new refcount=%i\n", iface, ref); if (!ref) + { + VariantClear(&This->dir); + for (i = 0; i < This->item_count; i++) + HeapFree(GetProcessHeap(), 0, This->item_filenames[i]); + HeapFree(GetProcessHeap(), 0, This->item_filenames); HeapFree(GetProcessHeap(), 0, This); + } return ref; } @@ -1079,10 +1089,55 @@ static HRESULT WINAPI FolderItemsImpl_get_Parent(FolderItems3 *iface, IDispatch static HRESULT WINAPI FolderItemsImpl_Item(FolderItems3 *iface, VARIANT index, FolderItem **ppid) { - FIXME("(%p,%s,%p)\n", iface, debugstr_variant(&index), ppid); + FolderItemsImpl *This = impl_from_FolderItems(iface); + LONG i; + WCHAR path_str[MAX_PATH]; + VARIANT path_var; + HRESULT ret; + + TRACE("(%p,%s,%p)\n", iface, debugstr_variant(&index), ppid); *ppid = NULL; - return E_NOTIMPL; + + switch (V_VT(&index)) + { + case VT_I2: + i = V_I2(&index); + break; + + case VT_I4: + i = V_I4(&index); + break; + + case VT_BSTR: + if (!V_BSTR(&index)) + return S_FALSE; + + for (i = 0; i < This->item_count; i++) + { + if (!strcmpW(V_BSTR(&index), This->item_filenames[i])) + break; + } + break; + + case VT_ERROR: + return FolderItem_Constructor(&This->dir, ppid); + + default: + return E_NOTIMPL; + } + + if (i >= This->item_count || i < 0) + return S_FALSE; + + if (!PathCombineW(path_str, V_BSTR(&This->dir), This->item_filenames[i])) + return E_OUTOFMEMORY; + + V_VT(&path_var) = VT_BSTR; + V_BSTR(&path_var) = SysAllocString(path_str); + ret = FolderItem_Constructor(&path_var, ppid); + VariantClear(&path_var); + return ret; } static HRESULT WINAPI FolderItemsImpl__NewEnum(FolderItems3 *iface, IUnknown **ppunk) @@ -1139,21 +1194,89 @@ static const FolderItems3Vtbl FolderItemsImpl_Vtbl = { FolderItemsImpl_get_Verbs }; -static HRESULT FolderItems_Constructor(FolderItems **ppfi) +static HRESULT FolderItems_Constructor(VARIANT *dir, FolderItems **ppfi) { + static const WCHAR backslash_star[] = {'\\','*',0}; + static const WCHAR dot[] = {'.',0}; + static const WCHAR dot_dot[] = {'.','.',0}; FolderItemsImpl *This; + LONG item_size; + WCHAR glob[MAX_PATH + 2]; + HANDLE first_file; + WIN32_FIND_DATAW file_info; + WCHAR **filenames; + HRESULT ret; - TRACE("\n"); + TRACE("(%s,%p)\n", debugstr_variant(dir), ppfi); *ppfi = NULL; + if (V_VT(dir) == VT_I4) + { + FIXME("special folder constants are not supported\n"); + return E_NOTIMPL; + } + This = HeapAlloc(GetProcessHeap(), 0, sizeof(FolderItemsImpl)); if (!This) return E_OUTOFMEMORY; This->FolderItems3_iface.lpVtbl = &FolderItemsImpl_Vtbl; This->ref = 1; + VariantInit(&This->dir); + ret = VariantCopy(&This->dir, dir); + if (FAILED(ret)) + { + HeapFree(GetProcessHeap(), 0, This); + return ret; + } + + This->item_count = 0; + lstrcpyW(glob, V_BSTR(dir)); + lstrcatW(glob, backslash_star); + first_file = FindFirstFileW(glob, &file_info); + if (first_file != INVALID_HANDLE_VALUE) + { + item_size = 128; + This->item_filenames = HeapAlloc(GetProcessHeap(), 0, item_size * sizeof(WCHAR*)); + if (!This->item_filenames) + goto fail; + + do + { + if (!strcmpW(file_info.cFileName, dot) || !strcmpW(file_info.cFileName, dot_dot)) + continue; + + if (This->item_count >= item_size) + { + item_size *= 2; + filenames = HeapReAlloc(GetProcessHeap(), 0, This->item_filenames, item_size * sizeof(WCHAR*)); + if (!filenames) + goto fail; + This->item_filenames = filenames; + } + + This->item_filenames[This->item_count] = strdupW(file_info.cFileName); + if (!This->item_filenames[This->item_count]) + goto fail; + This->item_count++; + } + while (FindNextFileW(first_file, &file_info)); + + FindClose(first_file); + This->item_filenames = HeapReAlloc(GetProcessHeap(), 0, This->item_filenames, This->item_count * sizeof(WCHAR*)); + } + else + { + This->item_filenames = NULL; + } + *ppfi = (FolderItems*)&This->FolderItems3_iface; return S_OK; + +fail: + FindClose(first_file); + FolderItems3_Release(&This->FolderItems3_iface); + return E_OUTOFMEMORY; } static HRESULT WINAPI FolderImpl_QueryInterface(Folder3 *iface, REFIID riid, @@ -1308,9 +1431,11 @@ static HRESULT WINAPI FolderImpl_get_ParentFolder(Folder3 *iface, Folder **ppsf) static HRESULT WINAPI FolderImpl_Items(Folder3 *iface, FolderItems **ppid) { - FIXME("(%p,%p)\n", iface, ppid); + FolderImpl *This = impl_from_Folder(iface); + + TRACE("(%p,%p)\n", iface, ppid); - return FolderItems_Constructor(ppid); + return FolderItems_Constructor(&This->dir, ppid); } static HRESULT WINAPI FolderImpl_ParseName(Folder3 *iface, BSTR name, FolderItem **item) diff --git a/dlls/shell32/tests/shelldispatch.c b/dlls/shell32/tests/shelldispatch.c index 1d1d262..60e8bf1 100644 --- a/dlls/shell32/tests/shelldispatch.c +++ b/dlls/shell32/tests/shelldispatch.c @@ -390,7 +390,6 @@ todo_wine r = FolderItems_Item(items, var, NULL); r = FolderItems_Item(items, var, &item); -todo_wine ok(r == S_FALSE, "expected S_FALSE, got %08x\n", r); ok(!item, "item is not null\n"); @@ -470,15 +469,11 @@ todo_wine r = FolderItems_Item(items, var, &item); if (i == VT_I2 || i == VT_I4 || i == VT_ERROR) { -todo_wine ok(r == S_OK, "type %d: expected S_OK, got %08x\n", i, r); -todo_wine ok(!!item, "item is null\n"); - if (r == E_NOTIMPL) break; } else if (i == VT_BSTR) { -todo_wine ok(r == S_FALSE, "type %d: expected S_FALSE, got %08x\n", i, r); ok(!item, "item is not null\n"); } @@ -496,11 +491,8 @@ todo_wine V_VT(&var) = VT_ERROR; V_ERROR(&var) = i; r = FolderItems_Item(items, var, &item); -todo_wine ok(r == S_OK, "expected S_OK, got %08x\n", r); -todo_wine ok(!!item, "item is null\n"); - if (!item) continue; bstr = NULL; r = FolderItem_get_Path(item, &bstr); @@ -519,7 +511,6 @@ todo_wine V_I4(&var) = -1; item = (FolderItem*)0xdeadbeef; r = FolderItems_Item(items, var, &item); -todo_wine ok(r == S_FALSE, "expected S_FALSE, got %08x\n", r); ok(!item, "item is not null\n"); @@ -542,11 +533,8 @@ todo_wine item = NULL; r = FolderItems_Item(items, var, &item); -todo_wine ok(r == S_OK, "file_defs[%d], %d: FolderItems::Item failed: %08x\n", i, j, r); -todo_wine ok(!!item, "file_defs[%d], %d: item is null\n", i, j); - if (!item) continue; item2 = NULL; r = FolderItems_Item(items, var, &item2); @@ -573,7 +561,6 @@ todo_wine V_I4(&var) = sizeof(file_defs)/sizeof(file_defs[0]); item = (FolderItem*)0xdeadbeef; r = FolderItems_Item(items, var, &item); -todo_wine ok(r == S_FALSE, "expected S_FALSE, got %08x\n", r); ok(!item, "item is not null\n"); -- 2.9.3