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
35 static void free_content_item(ContentItem *item)
42 free_content_item(item->child);
44 heap_free(item->name);
45 heap_free(item->local);
46 heap_free(item->merge.chm_file);
47 heap_free(item->merge.chm_index);
59 static void strbuf_init(strbuf_t *buf)
63 buf->buf = heap_alloc(buf->size);
66 static void strbuf_zero(strbuf_t *buf)
71 static void strbuf_free(strbuf_t *buf)
76 static void strbuf_append(strbuf_t *buf, const char *data, int len)
78 if(buf->len+len > buf->size) {
79 buf->size = buf->len+len;
80 buf->buf = heap_realloc(buf->buf, buf->size);
83 memcpy(buf->buf+buf->len, data, len);
94 static void stream_init(stream_t *stream, IStream *str)
96 memset(stream, 0, sizeof(stream_t));
100 static BOOL stream_chr(stream_t *stream, strbuf_t *buf, char c)
106 for(i=stream->p; i<stream->size; i++) {
107 if(stream->buf[i] == c) {
113 if(buf && i > stream->p)
114 strbuf_append(buf, stream->buf+stream->p, i-stream->p);
117 if(stream->p == stream->size) {
119 IStream_Read(stream->str, stream->buf, sizeof(stream->buf), &stream->size);
125 return stream->size != 0;
128 static void get_node_name(strbuf_t *node, strbuf_t *name)
130 const char *ptr = node->buf+1;
134 while(*ptr != '>' && !isspace(*ptr))
137 strbuf_append(name, node->buf+1, ptr-node->buf-1);
138 strbuf_append(name, "", 1);
141 static BOOL next_node(stream_t *stream, strbuf_t *buf)
143 if(!stream_chr(stream, NULL, '<'))
146 if(!stream_chr(stream, buf, '>'))
149 strbuf_append(buf, ">", 2);
155 * Find the value of a named HTML attribute.
157 * Note: Attribute names are case insensitive, so it is necessary to
158 * put both the node text and the attribute name in the same case
159 * before attempting a string search.
161 static const char *get_attr(const char *node, const char *name, int *len)
163 const char *ptr, *ptr2;
164 int name_len, node_len;
169 /* Create a lower case copy of the node */
170 node_len = strlen(node)+1;
171 node_buf = heap_alloc(node_len*sizeof(char));
174 memcpy(node_buf, node, node_len);
175 for(i=0;i<node_len;i++)
176 node_buf[i] = tolower(node_buf[i]);
177 /* Create a lower case copy of the attribute name (search string) */
178 name_len = strlen(name);
179 memcpy(name_buf, name, name_len);
180 for(i=0;i<name_len;i++)
181 name_buf[i] = tolower(name_buf[i]);
182 name_buf[name_len++] = '=';
183 name_buf[name_len++] = '\"';
184 name_buf[name_len] = 0;
186 ptr = strstr(node_buf, name_buf);
188 WARN("name not found\n");
194 ptr2 = strchr(ptr, '\"');
203 /* Return the pointer offset within the original string */
204 return node+(ptr-node_buf);
207 static void parse_obj_node_param(ContentItem *item, ContentItem *hhc_root, const char *text)
210 LPWSTR *param, merge;
213 ptr = get_attr(text, "name", &len);
215 WARN("name attr not found\n");
219 if(!strncasecmp("name", ptr, len)) {
221 }else if(!strncasecmp("merge", ptr, len)) {
223 }else if(!strncasecmp("local", ptr, len)) {
224 param = &item->local;
226 WARN("unhandled param %s\n", debugstr_an(ptr, len));
230 ptr = get_attr(text, "value", &len);
232 WARN("value attr not found\n");
236 wlen = MultiByteToWideChar(CP_ACP, 0, ptr, len, NULL, 0);
237 *param = heap_alloc((wlen+1)*sizeof(WCHAR));
238 MultiByteToWideChar(CP_ACP, 0, ptr, len, *param, wlen);
241 if(param == &merge) {
242 SetChmPath(&item->merge, hhc_root->merge.chm_file, merge);
247 static ContentItem *parse_hhc(HHInfo*,IStream*,ContentItem*,insert_type_t*);
249 static ContentItem *insert_item(ContentItem *item, ContentItem *new_item, insert_type_t insert_type)
257 switch(insert_type) {
259 item->next = new_item;
263 ContentItem *iter = item->child;
266 iter->next = new_item;
268 item->child = new_item;
276 static ContentItem *parse_sitemap_object(HHInfo *info, stream_t *stream, ContentItem *hhc_root,
277 insert_type_t *insert_type)
279 strbuf_t node, node_name;
282 *insert_type = INSERT_NEXT;
285 strbuf_init(&node_name);
287 item = heap_alloc_zero(sizeof(ContentItem));
289 while(next_node(stream, &node)) {
290 get_node_name(&node, &node_name);
292 TRACE("%s\n", node.buf);
294 if(!strcasecmp(node_name.buf, "/object"))
296 if(!strcasecmp(node_name.buf, "param"))
297 parse_obj_node_param(item, hhc_root, node.buf);
303 strbuf_free(&node_name);
305 if(item->merge.chm_index) {
306 IStream *merge_stream;
308 merge_stream = GetChmStream(info->pCHMInfo, item->merge.chm_file, &item->merge);
310 item->child = parse_hhc(info, merge_stream, hhc_root, insert_type);
311 IStream_Release(merge_stream);
313 WARN("Could not get %s::%s stream\n", debugstr_w(item->merge.chm_file),
314 debugstr_w(item->merge.chm_file));
317 free_content_item(item);
327 static ContentItem *parse_ul(HHInfo *info, stream_t *stream, ContentItem *hhc_root)
329 strbuf_t node, node_name;
330 ContentItem *ret = NULL, *prev = NULL, *new_item = NULL;
334 strbuf_init(&node_name);
336 while(next_node(stream, &node)) {
337 get_node_name(&node, &node_name);
339 TRACE("%s\n", node.buf);
341 if(!strcasecmp(node_name.buf, "object")) {
345 static const char sz_text_sitemap[] = "text/sitemap";
347 ptr = get_attr(node.buf, "type", &len);
349 if(ptr && len == sizeof(sz_text_sitemap)-1
350 && !memcmp(ptr, sz_text_sitemap, len)) {
351 new_item = parse_sitemap_object(info, stream, hhc_root, &it);
352 prev = insert_item(prev, new_item, it);
356 }else if(!strcasecmp(node_name.buf, "ul")) {
357 new_item = parse_ul(info, stream, hhc_root);
358 insert_item(prev, new_item, INSERT_CHILD);
359 }else if(!strcasecmp(node_name.buf, "/ul")) {
367 strbuf_free(&node_name);
372 static ContentItem *parse_hhc(HHInfo *info, IStream *str, ContentItem *hhc_root,
373 insert_type_t *insert_type)
376 strbuf_t node, node_name;
377 ContentItem *ret = NULL, *prev = NULL;
379 *insert_type = INSERT_NEXT;
382 strbuf_init(&node_name);
384 stream_init(&stream, str);
386 while(next_node(&stream, &node)) {
387 get_node_name(&node, &node_name);
389 TRACE("%s\n", node.buf);
391 if(!strcasecmp(node_name.buf, "ul")) {
392 ContentItem *item = parse_ul(info, &stream, hhc_root);
393 prev = insert_item(prev, item, INSERT_CHILD);
396 *insert_type = INSERT_CHILD;
403 strbuf_free(&node_name);
408 static void insert_content_item(HWND hwnd, ContentItem *parent, ContentItem *item)
410 TVINSERTSTRUCTW tvis;
412 memset(&tvis, 0, sizeof(tvis));
413 tvis.u.item.mask = TVIF_TEXT|TVIF_PARAM;
414 tvis.u.item.cchTextMax = strlenW(item->name)+1;
415 tvis.u.item.pszText = item->name;
416 tvis.u.item.lParam = (LPARAM)item;
417 tvis.hParent = parent ? parent->id : 0;
418 tvis.hInsertAfter = TVI_LAST;
420 item->id = (HTREEITEM)SendMessageW(hwnd, TVM_INSERTITEMW, 0, (LPARAM)&tvis);
423 static void fill_content_tree(HWND hwnd, ContentItem *parent, ContentItem *item)
427 insert_content_item(hwnd, parent, item);
428 fill_content_tree(hwnd, item, item->child);
430 fill_content_tree(hwnd, parent, item->child);
436 static void set_item_parents(ContentItem *parent, ContentItem *item)
439 item->parent = parent;
440 set_item_parents(item, item->child);
445 void InitContent(HHInfo *info)
448 insert_type_t insert_type;
450 info->content = heap_alloc_zero(sizeof(ContentItem));
451 SetChmPath(&info->content->merge, info->pCHMInfo->szFile, info->WinType.pszToc);
453 stream = GetChmStream(info->pCHMInfo, info->pCHMInfo->szFile, &info->content->merge);
455 TRACE("Could not get content stream\n");
459 info->content->child = parse_hhc(info, stream, info->content, &insert_type);
460 IStream_Release(stream);
462 set_item_parents(NULL, info->content);
463 fill_content_tree(info->tabs[TAB_CONTENTS].hwnd, NULL, info->content);
466 void ReleaseContent(HHInfo *info)
468 free_content_item(info->content);