wined3d: Spelling fixes and downgrade a FIXME to a TRACE.
[wine/wine-kai.git] / dlls / hhctrl.ocx / chm.c
blob56d9e4a21d9c7d19bee67bc2672f927c5fab2ef8
1 /*
2 * CHM Utility API
4 * Copyright 2005 James Hawkins
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <stdarg.h>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winnls.h"
29 #include "winreg.h"
30 #include "ole2.h"
31 #include "htmlhelp.h"
33 #include "initguid.h"
34 #include "chm.h"
36 static LPWSTR CHM_ANSIToUnicode(LPCSTR ansi)
38 LPWSTR unicode;
39 int count;
41 count = MultiByteToWideChar(CP_ACP, 0, ansi, -1, NULL, 0);
42 unicode = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
43 MultiByteToWideChar(CP_ACP, 0, ansi, -1, unicode, count);
45 return unicode;
48 /* Reads a string from the #STRINGS section in the CHM file */
49 LPWSTR CHM_ReadString(CHMInfo *pChmInfo, DWORD dwOffset)
51 LARGE_INTEGER liOffset;
52 IStorage *pStorage = pChmInfo->pStorage;
53 IStream *pStream;
54 DWORD cbRead;
55 ULONG iPos;
56 DWORD dwSize;
57 LPSTR szString;
58 LPWSTR stringW;
60 const int CB_READ_BLOCK = 64;
61 static const WCHAR stringsW[] = {'#','S','T','R','I','N','G','S',0};
63 dwSize = CB_READ_BLOCK;
64 szString = HeapAlloc(GetProcessHeap(), 0, dwSize);
66 if (FAILED(IStorage_OpenStream(pStorage, stringsW, NULL, STGM_READ, 0, &pStream)))
67 return NULL;
69 liOffset.QuadPart = dwOffset;
71 if (FAILED(IStream_Seek(pStream, liOffset, STREAM_SEEK_SET, NULL)))
73 IStream_Release(pStream);
74 return NULL;
77 while (SUCCEEDED(IStream_Read(pStream, szString, CB_READ_BLOCK, &cbRead)))
79 if (!cbRead)
80 return NULL;
82 for (iPos = 0; iPos < cbRead; iPos++)
84 if (!szString[iPos])
86 stringW = CHM_ANSIToUnicode(szString);
87 HeapFree(GetProcessHeap(), 0, szString);
88 return stringW;
92 dwSize *= 2;
93 szString = HeapReAlloc(GetProcessHeap(), 0, szString, dwSize);
94 szString += cbRead;
97 /* didn't find a string */
98 return NULL;
101 /* Loads the HH_WINTYPE data from the CHM file
103 * FIXME: There may be more than one window type in the file, so
104 * add the ability to choose a certain window type
106 BOOL CHM_LoadWinTypeFromCHM(CHMInfo *pChmInfo, HH_WINTYPEW *pHHWinType)
108 LARGE_INTEGER liOffset;
109 IStorage *pStorage = pChmInfo->pStorage;
110 IStream *pStream;
111 HRESULT hr;
112 DWORD cbRead;
114 static const WCHAR windowsW[] = {'#','W','I','N','D','O','W','S',0};
116 hr = IStorage_OpenStream(pStorage, windowsW, NULL, STGM_READ, 0, &pStream);
117 if (FAILED(hr))
118 return FALSE;
120 /* jump past the #WINDOWS header */
121 liOffset.QuadPart = sizeof(DWORD) * 2;
123 hr = IStream_Seek(pStream, liOffset, STREAM_SEEK_SET, NULL);
124 if (FAILED(hr)) goto done;
126 /* read the HH_WINTYPE struct data */
127 hr = IStream_Read(pStream, pHHWinType, sizeof(*pHHWinType), &cbRead);
128 if (FAILED(hr)) goto done;
130 /* convert the #STRINGS offsets to actual strings */
131 pHHWinType->pszType = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszType);
132 pHHWinType->pszCaption = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszCaption);
133 pHHWinType->pszToc = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszToc);
134 pHHWinType->pszIndex = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszIndex);
135 pHHWinType->pszFile = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszFile);
136 pHHWinType->pszHome = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszHome);
137 pHHWinType->pszJump1 = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszJump1);
138 pHHWinType->pszJump2 = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszJump2);
139 pHHWinType->pszUrlJump1 = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszUrlJump1);
140 pHHWinType->pszUrlJump2 = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszUrlJump2);
142 /* FIXME: pszCustomTabs is a list of multiple zero-terminated strings so ReadString won't
143 * work in this case
145 #if 0
146 pHHWinType->pszCustomTabs = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszCustomTabs);
147 #endif
149 done:
150 IStream_Release(pStream);
152 return SUCCEEDED(hr);
155 /* Opens the CHM file for reading */
156 BOOL CHM_OpenCHM(CHMInfo *pChmInfo, LPCWSTR szFile)
158 pChmInfo->szFile = szFile;
160 if (FAILED(CoCreateInstance(&CLSID_ITStorage, NULL, CLSCTX_INPROC_SERVER,
161 &IID_IITStorage, (void **) &pChmInfo->pITStorage)))
162 return FALSE;
164 if (FAILED(IITStorage_StgOpenStorage(pChmInfo->pITStorage, szFile, NULL,
165 STGM_READ | STGM_SHARE_DENY_WRITE,
166 NULL, 0, &pChmInfo->pStorage)))
167 return FALSE;
169 return TRUE;
172 void CHM_CloseCHM(CHMInfo *pCHMInfo)
174 IITStorage_Release(pCHMInfo->pITStorage);
175 IStorage_Release(pCHMInfo->pStorage);
178 /* Creates a Url of a CHM file that can be used with WB_Navigate */
179 void CHM_CreateITSUrl(CHMInfo *pChmInfo, LPCWSTR szIndex, LPWSTR szUrl)
181 static const WCHAR formatW[] = {
182 'i','t','s',':','%','s',':',':','%','s',0
185 wsprintfW(szUrl, formatW, pChmInfo->szFile, szIndex);