ieframe: Simplify IPersist_Load, make error handling more straightforward.

Signed-off-by: Nikolay Sivov <nsivov@codeweavers.com>
Signed-off-by: Jacek Caban <jacek@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Nikolay Sivov 2016-04-11 22:14:54 +03:00 committed by Alexandre Julliard
parent 17379dcd08
commit 9de5b11430
1 changed files with 84 additions and 105 deletions

View File

@ -420,17 +420,19 @@ static HRESULT WINAPI PersistFile_IsDirty(IPersistFile *pFile)
return This->isDirty ? S_OK : S_FALSE;
}
/* A helper function: Allocate and fill rString. Return number of bytes read. */
static DWORD get_profile_string(LPCWSTR lpAppName, LPCWSTR lpKeyName,
/* Returns allocated profile string and a standard return code. */
static HRESULT get_profile_string(LPCWSTR lpAppName, LPCWSTR lpKeyName,
LPCWSTR lpFileName, WCHAR **rString )
{
DWORD r = 0;
DWORD len = 128;
WCHAR *buffer;
*rString = NULL;
buffer = CoTaskMemAlloc(len * sizeof(*buffer));
if (buffer != NULL)
{
if (!buffer)
return E_OUTOFMEMORY;
r = GetPrivateProfileStringW(lpAppName, lpKeyName, NULL, buffer, len, lpFileName);
while (r == len-1)
{
@ -441,72 +443,59 @@ static DWORD get_profile_string(LPCWSTR lpAppName, LPCWSTR lpKeyName,
if (realloc_buf == NULL)
{
CoTaskMemFree(buffer);
*rString = NULL;
return 0;
return E_OUTOFMEMORY;
}
buffer = realloc_buf;
r = GetPrivateProfileStringW(lpAppName, lpKeyName, NULL, buffer, len, lpFileName);
}
}
*rString = buffer;
return r;
return r ? S_OK : E_FAIL;
}
static HRESULT WINAPI PersistFile_Load(IPersistFile *pFile, LPCOLESTR pszFileName, DWORD dwMode)
{
WCHAR str_header[] = {'I','n','t','e','r','n','e','t','S','h','o','r','t','c','u','t',0};
WCHAR str_URL[] = {'U','R','L',0};
WCHAR str_iconfile[] = {'i','c','o','n','f','i','l','e',0};
WCHAR str_iconindex[] = {'i','c','o','n','i','n','d','e','x',0};
WCHAR *filename = NULL;
HRESULT hr;
InternetShortcut *This = impl_from_IPersistFile(pFile);
static WCHAR str_header[] = {'I','n','t','e','r','n','e','t','S','h','o','r','t','c','u','t',0};
static WCHAR str_URL[] = {'U','R','L',0};
static WCHAR str_iconfile[] = {'i','c','o','n','f','i','l','e',0};
static WCHAR str_iconindex[] = {'i','c','o','n','i','n','d','e','x',0};
WCHAR *filename = NULL;
WCHAR *url;
HRESULT hr;
IPropertyStorage *pPropStg;
WCHAR *iconfile;
WCHAR *iconindexstring;
TRACE("(%p, %s, 0x%x)\n", pFile, debugstr_w(pszFileName), dwMode);
if (dwMode != 0)
FIXME("ignoring unimplemented mode 0x%x\n", dwMode);
filename = co_strdupW(pszFileName);
if (filename != NULL)
{
DWORD r;
WCHAR *url;
if (!filename)
return E_OUTOFMEMORY;
r = get_profile_string(str_header, str_URL, pszFileName, &url);
if (FAILED(hr = get_profile_string(str_header, str_URL, pszFileName, &url)))
{
CoTaskMemFree(filename);
return hr;
}
if (url == NULL)
{
hr = E_OUTOFMEMORY;
CoTaskMemFree(filename);
}
else if (r == 0)
{
hr = E_FAIL;
CoTaskMemFree(filename);
}
else
{
hr = S_OK;
CoTaskMemFree(This->currentFile);
This->currentFile = filename;
CoTaskMemFree(This->url);
This->url = url;
This->isDirty = FALSE;
}
/* Now we're going to read in the iconfile and iconindex.
If we don't find them, that's not a failure case -- it's possible
that they just aren't in there. */
if (SUCCEEDED(hr))
{
IPropertyStorage *pPropStg;
WCHAR *iconfile;
WCHAR *iconindexstring;
hr = IPropertySetStorage_Open(This->property_set_storage, &FMTID_Intshcut,
STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
&pPropStg);
STGM_READWRITE | STGM_SHARE_EXCLUSIVE, &pPropStg);
if (get_profile_string(str_header, str_iconfile, pszFileName, &iconfile))
if (get_profile_string(str_header, str_iconfile, pszFileName, &iconfile) == S_OK)
{
PROPSPEC ps;
PROPVARIANT pv;
@ -516,13 +505,11 @@ static HRESULT WINAPI PersistFile_Load(IPersistFile *pFile, LPCOLESTR pszFileNam
pv.u.pwszVal = iconfile;
hr = IPropertyStorage_WriteMultiple(pPropStg, 1, &ps, &pv, 0);
if (FAILED(hr))
{
TRACE("Failed to store the iconfile to our property storage. hr = 0x%x\n", hr);
}
}
CoTaskMemFree(iconfile);
if (get_profile_string(str_header, str_iconindex, pszFileName, &iconindexstring))
if (get_profile_string(str_header, str_iconindex, pszFileName, &iconindexstring) == S_OK)
{
int iconindex;
PROPSPEC ps;
@ -536,19 +523,11 @@ static HRESULT WINAPI PersistFile_Load(IPersistFile *pFile, LPCOLESTR pszFileNam
pv.u.iVal = iconindex;
hr = IPropertyStorage_WriteMultiple(pPropStg, 1, &ps, &pv, 0);
if (FAILED(hr))
{
TRACE("Failed to store the iconindex to our property storage. hr = 0x%x\n", hr);
}
}
CoTaskMemFree(iconindexstring);
IPropertyStorage_Release(pPropStg);
}
else
hr = E_OUTOFMEMORY;
}
else
hr = E_OUTOFMEMORY;
return hr;
}