2 * Copyright 2007 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #define NONAMELESSUNION
20 #define NONAMELESSSTRUCT
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(htmlhelp);
28 #define BLOCK_SIZE 0x1000
41 static void strbuf_init(strbuf_t *buf)
45 buf->buf = hhctrl_alloc(buf->size);
48 static void strbuf_zero(strbuf_t *buf)
53 static void strbuf_free(strbuf_t *buf)
55 hhctrl_free(buf->buf);
58 static void strbuf_append(strbuf_t *buf, const char *data, int len)
60 if(buf->len+len > buf->size) {
61 buf->size = buf->len+len;
62 buf->buf = hhctrl_realloc(buf->buf, buf->size);
65 memcpy(buf->buf+buf->len, data, len);
76 static void stream_init(stream_t *stream, IStream *str)
78 memset(stream, 0, sizeof(stream_t));
82 static BOOL stream_chr(stream_t *stream, strbuf_t *buf, char c)
88 for(i=stream->p; i<stream->size; i++) {
89 if(stream->buf[i] == c) {
95 if(buf && i > stream->p)
96 strbuf_append(buf, stream->buf+stream->p, i-stream->p);
99 if(stream->p == stream->size) {
101 IStream_Read(stream->str, stream->buf, sizeof(stream->buf), &stream->size);
107 return stream->size != 0;
110 static void get_node_name(strbuf_t *node, strbuf_t *name)
112 const char *ptr = node->buf+1;
116 while(*ptr != '>' && !isspace(*ptr))
119 strbuf_append(name, node->buf+1, ptr-node->buf-1);
120 strbuf_append(name, "", 1);
123 static BOOL next_node(stream_t *stream, strbuf_t *buf)
125 if(!stream_chr(stream, NULL, '<'))
128 if(!stream_chr(stream, buf, '>'))
131 strbuf_append(buf, ">", 2);
136 static const char *get_attr(const char *node, const char *name, int *len)
138 const char *ptr, *ptr2;
143 memcpy(name_buf, name, nlen);
144 name_buf[nlen++] = '=';
145 name_buf[nlen++] = '\"';
148 ptr = strstr(node, name_buf);
150 WARN("name not found\n");
155 ptr2 = strchr(ptr, '\"');
163 static void parse_obj_node_param(ContentItem *item, const char *text)
166 LPWSTR *param, merge;
169 ptr = get_attr(text, "name", &len);
171 WARN("name attr not found\n");
175 if(!strncasecmp("name", ptr, len)) {
177 }else if(!strncasecmp("merge", ptr, len)) {
179 }else if(!strncasecmp("local", ptr, len)) {
180 param = &item->local;
182 WARN("unhandled param %s\n", debugstr_an(ptr, len));
186 ptr = get_attr(text, "value", &len);
188 WARN("value attr not found\n");
192 wlen = MultiByteToWideChar(CP_ACP, 0, ptr, len, NULL, 0);
193 *param = hhctrl_alloc((wlen+1)*sizeof(WCHAR));
194 MultiByteToWideChar(CP_ACP, 0, ptr, len, *param, wlen);
197 if(param == &merge) {
198 SetChmPath(&item->merge, merge);
203 static ContentItem *parse_hhc(HHInfo*,IStream*,insert_type_t*);
205 static ContentItem *insert_item(ContentItem *item, ContentItem *new_item, insert_type_t insert_type)
210 switch(insert_type) {
212 item->next = new_item;
216 ContentItem *iter = item->child;
219 iter->next = new_item;
221 item->child = new_item;
229 static ContentItem *parse_sitemap_object(HHInfo *info, stream_t *stream, insert_type_t *insert_type)
231 strbuf_t node, node_name;
234 *insert_type = INSERT_NEXT;
237 strbuf_init(&node_name);
239 item = hhctrl_alloc_zero(sizeof(ContentItem));
241 while(next_node(stream, &node)) {
242 get_node_name(&node, &node_name);
244 TRACE("%s\n", node.buf);
246 if(!strcasecmp(node_name.buf, "/object"))
248 if(!strcasecmp(node_name.buf, "param"))
249 parse_obj_node_param(item, node.buf);
255 strbuf_free(&node_name);
257 if(item->merge.chm_index) {
258 IStream *merge_stream;
260 merge_stream = GetChmStream(info->pCHMInfo, item->merge.chm_file, &item->merge);
262 item->child = parse_hhc(info, merge_stream, insert_type);
263 IStream_Release(merge_stream);
265 WARN("Could not get %s::%s stream\n", debugstr_w(item->merge.chm_file),
266 debugstr_w(item->merge.chm_file));
274 static ContentItem *parse_ul(HHInfo *info, stream_t *stream)
276 strbuf_t node, node_name;
277 ContentItem *ret = NULL, *prev = NULL, *new_item = NULL;
281 strbuf_init(&node_name);
283 while(next_node(stream, &node)) {
284 get_node_name(&node, &node_name);
286 TRACE("%s\n", node.buf);
288 if(!strcasecmp(node_name.buf, "object")) {
292 static const char sz_text_sitemap[] = "text/sitemap";
294 ptr = get_attr(node.buf, "type", &len);
296 if(ptr && len == sizeof(sz_text_sitemap)-1
297 && !memcmp(ptr, sz_text_sitemap, len)) {
298 new_item = parse_sitemap_object(info, stream, &it);
299 prev = insert_item(prev, new_item, it);
303 }else if(!strcasecmp(node_name.buf, "ul")) {
304 new_item = parse_ul(info, stream);
305 insert_item(prev, new_item, INSERT_CHILD);
306 }else if(!strcasecmp(node_name.buf, "/ul")) {
314 strbuf_free(&node_name);
319 static ContentItem *parse_hhc(HHInfo *info, IStream *str, insert_type_t *insert_type)
322 strbuf_t node, node_name;
323 ContentItem *ret = NULL, *prev = NULL;
325 *insert_type = INSERT_NEXT;
328 strbuf_init(&node_name);
330 stream_init(&stream, str);
332 while(next_node(&stream, &node)) {
333 get_node_name(&node, &node_name);
335 TRACE("%s\n", node.buf);
337 if(!strcasecmp(node_name.buf, "ul")) {
338 ContentItem *item = parse_ul(info, &stream);
339 prev = insert_item(prev, item, INSERT_CHILD);
342 *insert_type = INSERT_CHILD;
349 strbuf_free(&node_name);
354 static void insert_content_item(HWND hwnd, ContentItem *parent, ContentItem *item)
356 TVINSERTSTRUCTW tvis;
358 memset(&tvis, 0, sizeof(tvis));
359 tvis.u.item.mask = TVIF_TEXT|TVIF_PARAM;
360 tvis.u.item.cchTextMax = strlenW(item->name)+1;
361 tvis.u.item.pszText = item->name;
362 tvis.u.item.lParam = (LPARAM)item;
363 tvis.hParent = parent ? parent->id : 0;
364 tvis.hInsertAfter = TVI_LAST;
366 item->id = (HTREEITEM)SendMessageW(hwnd, TVM_INSERTITEMW, 0, (LPARAM)&tvis);
369 static void fill_content_tree(HWND hwnd, ContentItem *parent, ContentItem *item)
373 insert_content_item(hwnd, parent, item);
374 fill_content_tree(hwnd, item, item->child);
376 fill_content_tree(hwnd, parent, item->child);
382 static void set_item_parents(ContentItem *parent, ContentItem *item)
385 item->parent = parent;
386 set_item_parents(item, item->child);
391 void InitContent(HHInfo *info)
394 insert_type_t insert_type;
396 info->content = hhctrl_alloc_zero(sizeof(ContentItem));
397 SetChmPath(&info->content->merge, info->WinType.pszToc);
398 if(!info->content->merge.chm_file)
399 info->content->merge.chm_file = strdupW(info->pCHMInfo->szFile);
401 stream = GetChmStream(info->pCHMInfo, info->pCHMInfo->szFile, &info->content->merge);
403 TRACE("Could not get content stream\n");
407 info->content->child = parse_hhc(info, stream, &insert_type);
408 IStream_Release(stream);
410 set_item_parents(NULL, info->content);
411 fill_content_tree(info->tabs[TAB_CONTENTS].hwnd, NULL, info->content);
414 static void free_content_item(ContentItem *item)
421 free_content_item(item->child);
423 hhctrl_free(item->name);
424 hhctrl_free(item->local);
425 hhctrl_free(item->merge.chm_file);
426 hhctrl_free(item->merge.chm_index);
432 void ReleaseContent(HHInfo *info)
434 free_content_item(info->content);