hlink: Split target at hash in HlinkCreateFromString.
authorAndrew Eikum <aeikum@codeweavers.com>
Tue, 16 Mar 2010 19:52:04 +0000 (14:52 -0500)
committerAlexandre Julliard <julliard@winehq.org>
Wed, 17 Mar 2010 09:59:02 +0000 (10:59 +0100)
dlls/hlink/hlink_main.c
dlls/hlink/tests/hlink.c

index d07901aea4ab3656a480f6990b5324e9c3511ad2..e96f206645f54ae4ab1e7b097f544dee7852a77c 100644 (file)
@@ -99,6 +99,8 @@ HRESULT WINAPI HlinkCreateFromString( LPCWSTR pwzTarget, LPCWSTR pwzLocation,
 {
     IHlink *hl = NULL;
     HRESULT r = S_OK;
+    WCHAR *hash, *tgt;
+    const WCHAR *loc;
 
     TRACE("%s %s %s %p %i %p %s %p\n", debugstr_w(pwzTarget),
             debugstr_w(pwzLocation), debugstr_w(pwzFriendlyName), pihlsite,
@@ -108,8 +110,44 @@ HRESULT WINAPI HlinkCreateFromString( LPCWSTR pwzTarget, LPCWSTR pwzLocation,
     if (FAILED(r))
         return r;
 
-    IHlink_SetStringReference(hl, HLINKSETF_TARGET | HLINKSETF_LOCATION,
-            pwzTarget, pwzLocation);
+    if (pwzTarget)
+    {
+        hash = strchrW(pwzTarget, '#');
+        if (hash)
+        {
+            if (hash == pwzTarget)
+                tgt = NULL;
+            else
+            {
+                int tgt_len = hash - pwzTarget;
+                tgt = heap_alloc((tgt_len + 1) * sizeof(WCHAR));
+                if (!tgt)
+                    return E_OUTOFMEMORY;
+                memcpy(tgt, pwzTarget, tgt_len * sizeof(WCHAR));
+                tgt[tgt_len] = 0;
+            }
+            if (!pwzLocation)
+                loc = hash + 1;
+            else
+                loc = pwzLocation;
+        }
+        else
+        {
+            tgt = hlink_strdupW(pwzTarget);
+            if (!tgt)
+                return E_OUTOFMEMORY;
+            loc = pwzLocation;
+        }
+    }
+    else
+    {
+        tgt = NULL;
+        loc = pwzLocation;
+    }
+
+    IHlink_SetStringReference(hl, HLINKSETF_TARGET | HLINKSETF_LOCATION, tgt, loc);
+
+    heap_free(tgt);
 
     if (pwzFriendlyName)
         IHlink_SetFriendlyName(hl, pwzFriendlyName);
index 28c0c9a0560917d85dc6ae564ad4684ea7a99f03..d454819e57c4fb4fe5a1e26bd35679bb8181ee6c 100644 (file)
@@ -1363,6 +1363,83 @@ static void test_HlinkMoniker(void)
     IHlink_Release(hlink);
 }
 
+static void test_HashLink(void)
+{
+    IHlink *hlink;
+    IMoniker *pmk;
+    const WCHAR hash_targetW[] = {'a','f','i','l','e','#','a','n','a','n','c','h','o','r',0};
+    const WCHAR two_hash_targetW[] = {'a','f','i','l','e','#','a','n','a','n','c','h','o','r','#','a','n','o','t','h','e','r',0};
+    const WCHAR hash_no_tgtW[] = {'#','a','n','a','n','c','h','o','r',0};
+    const WCHAR tgt_partW[] = {'a','f','i','l','e',0};
+    const WCHAR loc_partW[] = {'a','n','a','n','c','h','o','r',0};
+    const WCHAR two_hash_loc_partW[] = {'a','n','a','n','c','h','o','r','#','a','n','o','t','h','e','r',0};
+    const WCHAR test_locW[] = {'t','e','s','t','l','o','c',0};
+    HRESULT hres;
+
+    /* simple single hash test */
+    hres = HlinkCreateFromString(hash_targetW, NULL, NULL, NULL, 0, NULL, &IID_IHlink, (void*)&hlink);
+    ok(hres == S_OK, "HlinkCreateFromString failed: 0x%08x\n", hres);
+    ok(hlink != NULL, "Didn't get an hlink\n");
+
+    if(hlink){
+        getStringRef(hlink, tgt_partW, loc_partW);
+        pmk = getMonikerRef(hlink, (IMoniker*)0xFFFFFFFF, loc_partW);
+        ok(pmk != NULL, "Found moniker should not be NULL\n");
+        if(pmk)
+            IMoniker_Release(pmk);
+
+        setStringRef(hlink, HLINKSETF_TARGET, hash_targetW, NULL);
+        getStringRef(hlink, hash_targetW, loc_partW);
+
+        IHlink_Release(hlink);
+    }
+
+    /* two hashes in the target */
+    hres = HlinkCreateFromString(two_hash_targetW, NULL, NULL, NULL, 0, NULL, &IID_IHlink, (void*)&hlink);
+    ok(hres == S_OK, "HlinkCreateFromString failed: 0x%08x\n", hres);
+    ok(hlink != NULL, "Didn't get an hlink\n");
+
+    if(hlink){
+        getStringRef(hlink, tgt_partW, two_hash_loc_partW);
+        pmk = getMonikerRef(hlink, (IMoniker*)0xFFFFFFFF, two_hash_loc_partW);
+        ok(pmk != NULL, "Found moniker should not be NULL\n");
+        if(pmk)
+            IMoniker_Release(pmk);
+
+        IHlink_Release(hlink);
+    }
+
+    /* target with hash plus a location string */
+    hres = HlinkCreateFromString(hash_targetW, test_locW, NULL, NULL, 0, NULL, &IID_IHlink, (void*)&hlink);
+    ok(hres == S_OK, "HlinkCreateFromString failed: 0x%08x\n", hres);
+    ok(hlink != NULL, "Didn't get an hlink\n");
+
+    if(hlink){
+        getStringRef(hlink, tgt_partW, test_locW);
+        pmk = getMonikerRef(hlink, (IMoniker*)0xFFFFFFFF, test_locW);
+        ok(pmk != NULL, "Found moniker should not be NULL\n");
+        if(pmk)
+            IMoniker_Release(pmk);
+
+        IHlink_Release(hlink);
+    }
+
+    /* target with hash containing no "target part" */
+    hres = HlinkCreateFromString(hash_no_tgtW, NULL, NULL, NULL, 0, NULL, &IID_IHlink, (void*)&hlink);
+    ok(hres == S_OK, "HlinkCreateFromString failed: 0x%08x\n", hres);
+    ok(hlink != NULL, "Didn't get an hlink\n");
+
+    if(hlink){
+        getStringRef(hlink, NULL, loc_partW);
+        pmk = getMonikerRef(hlink, (IMoniker*)0xFFFFFFFF, loc_partW);
+        ok(pmk == NULL, "Found moniker should be NULL\n");
+        if(pmk)
+            IMoniker_Release(pmk);
+
+        IHlink_Release(hlink);
+    }
+}
+
 START_TEST(hlink)
 {
     CoInitialize(NULL);
@@ -1377,6 +1454,7 @@ START_TEST(hlink)
     test_HlinkGetSetMonikerReference();
     test_HlinkGetSetStringReference();
     test_HlinkMoniker();
+    test_HashLink();
 
     CoUninitialize();
 }