more fixes to HTML entities parsing when loading .hhk and .hhc
[wxWidgets.git] / src / html / helpdata.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: helpdata.cpp
3 // Purpose: wxHtmlHelpData
4 // Notes: Based on htmlhelp.cpp, implementing a monolithic
5 // HTML Help controller class, by Vaclav Slavik
6 // Author: Harm van der Heijden and Vaclav Slavik
7 // RCS-ID: $Id$
8 // Copyright: (c) Harm van der Heijden and Vaclav Slavik
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "helpdata.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #include "wx/defs.h"
24
25 #if wxUSE_HTML && wxUSE_STREAMS
26
27 #ifndef WXPRECOMP
28 #include "wx/intl.h"
29 #include "wx/log.h"
30 #endif
31
32 #include <ctype.h>
33
34 #include "wx/html/helpdata.h"
35 #include "wx/tokenzr.h"
36 #include "wx/wfstream.h"
37 #include "wx/busyinfo.h"
38 #include "wx/encconv.h"
39 #include "wx/fontmap.h"
40 #include "wx/log.h"
41 #include "wx/html/htmlpars.h"
42 #include "wx/html/htmldefs.h"
43 #include "wx/html/htmlfilt.h"
44 #include "wx/filename.h"
45
46 #include "wx/arrimpl.cpp"
47 WX_DEFINE_OBJARRAY(wxHtmlBookRecArray)
48
49 //-----------------------------------------------------------------------------
50 // static helper functions
51 //-----------------------------------------------------------------------------
52
53 // Reads one line, stores it into buf and returns pointer to new line or NULL.
54 static const wxChar* ReadLine(const wxChar *line, wxChar *buf, size_t bufsize)
55 {
56 wxChar *writeptr = buf;
57 wxChar *endptr = buf + bufsize - 1;
58 const wxChar *readptr = line;
59
60 while (*readptr != 0 && *readptr != _T('\r') && *readptr != _T('\n') &&
61 writeptr != endptr)
62 *(writeptr++) = *(readptr++);
63 *writeptr = 0;
64 while (*readptr == _T('\r') || *readptr == _T('\n'))
65 readptr++;
66 if (*readptr == 0)
67 return NULL;
68 else
69 return readptr;
70 }
71
72
73
74 extern "C" int LINKAGEMODE
75 wxHtmlHelpIndexCompareFunc(const void *a, const void *b)
76 {
77 return ((wxHtmlContentsItem*)a)->m_Name.CmpNoCase(((wxHtmlContentsItem*)b)->m_Name);
78 }
79
80
81 template<typename T>
82 static T* ReallocArray(T *arr, size_t oldsize, size_t newsize)
83 {
84 T *newarr = new T[newsize];
85 for (size_t i = 0; i < oldsize; i++)
86 newarr[i] = arr[i];
87 return newarr;
88 }
89
90
91 //-----------------------------------------------------------------------------
92 // HP_Parser
93 //-----------------------------------------------------------------------------
94
95 class HP_Parser : public wxHtmlParser
96 {
97 public:
98 HP_Parser()
99 {
100 GetEntitiesParser()->SetEncoding(wxFONTENCODING_ISO8859_1);
101 }
102
103 wxObject* GetProduct() { return NULL; }
104
105 protected:
106 virtual void AddText(const wxChar* WXUNUSED(txt)) {}
107
108 DECLARE_NO_COPY_CLASS(HP_Parser)
109 };
110
111
112 //-----------------------------------------------------------------------------
113 // HP_TagHandler
114 //-----------------------------------------------------------------------------
115
116 class HP_TagHandler : public wxHtmlTagHandler
117 {
118 private:
119 wxString m_Name, m_Page;
120 int m_Level;
121 int m_ID;
122 int m_Index;
123 wxHtmlContentsItem *m_Items;
124 int m_ItemsCnt;
125 wxHtmlBookRecord *m_Book;
126
127 public:
128 HP_TagHandler(wxHtmlBookRecord *b) : wxHtmlTagHandler()
129 { m_Book = b; m_Items = NULL; m_ItemsCnt = 0; m_Name = m_Page = wxEmptyString;
130 m_Level = 0; m_ID = -1; }
131 wxString GetSupportedTags() { return wxT("UL,OBJECT,PARAM"); }
132 bool HandleTag(const wxHtmlTag& tag);
133 void WriteOut(wxHtmlContentsItem*& array, int& size);
134 void ReadIn(wxHtmlContentsItem* array, int size);
135
136 DECLARE_NO_COPY_CLASS(HP_TagHandler)
137 };
138
139
140 bool HP_TagHandler::HandleTag(const wxHtmlTag& tag)
141 {
142 if (tag.GetName() == wxT("UL"))
143 {
144 m_Level++;
145 ParseInner(tag);
146 m_Level--;
147 return TRUE;
148 }
149 else if (tag.GetName() == wxT("OBJECT"))
150 {
151 m_Name = m_Page = wxEmptyString;
152 ParseInner(tag);
153
154 #if 0
155 if (!m_Page.IsEmpty())
156 /* Valid HHW's file may contain only two object tags:
157
158 <OBJECT type="text/site properties">
159 <param name="ImageType" value="Folder">
160 </OBJECT>
161
162 or
163
164 <OBJECT type="text/sitemap">
165 <param name="Name" value="main page">
166 <param name="Local" value="another.htm">
167 </OBJECT>
168
169 We're interested in the latter. !m_Page.IsEmpty() is valid
170 condition because text/site properties does not contain Local param
171 */
172 #endif
173 if (tag.GetParam(wxT("TYPE")) == wxT("text/sitemap"))
174 {
175 if (m_ItemsCnt % wxHTML_REALLOC_STEP == 0)
176 m_Items = ReallocArray(m_Items, m_ItemsCnt,
177 m_ItemsCnt + wxHTML_REALLOC_STEP);
178
179 m_Items[m_ItemsCnt].m_Level = m_Level;
180 m_Items[m_ItemsCnt].m_ID = m_ID;
181 m_Items[m_ItemsCnt].m_Page = m_Page;
182 m_Items[m_ItemsCnt].m_Name = m_Name;
183 m_Items[m_ItemsCnt].m_Book = m_Book;
184 m_ItemsCnt++;
185 }
186
187 return TRUE;
188 }
189 else
190 { // "PARAM"
191 if (m_Name == wxEmptyString && tag.GetParam(wxT("NAME")) == wxT("Name"))
192 m_Name = tag.GetParam(wxT("VALUE"));
193 if (tag.GetParam(wxT("NAME")) == wxT("Local"))
194 m_Page = tag.GetParam(wxT("VALUE"));
195 if (tag.GetParam(wxT("NAME")) == wxT("ID"))
196 tag.GetParamAsInt(wxT("VALUE"), &m_ID);
197 return FALSE;
198 }
199 }
200
201
202
203 void HP_TagHandler::WriteOut(wxHtmlContentsItem*& array, int& size)
204 {
205 array = m_Items;
206 size = m_ItemsCnt;
207 m_Items = NULL;
208 m_ItemsCnt = 0;
209 }
210
211 void HP_TagHandler::ReadIn(wxHtmlContentsItem* array, int size)
212 {
213 m_Items = array;
214 m_ItemsCnt = size;
215 }
216
217
218
219
220 //-----------------------------------------------------------------------------
221 // wxHtmlHelpData
222 //-----------------------------------------------------------------------------
223
224 wxString wxHtmlBookRecord::GetFullPath(const wxString &page) const
225 {
226 if (wxIsAbsolutePath(page))
227 return page;
228 else
229 return m_BasePath + page;
230 }
231
232
233
234 IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpData, wxObject)
235
236 wxHtmlHelpData::wxHtmlHelpData()
237 {
238 m_TempPath = wxEmptyString;
239
240 m_Contents = NULL;
241 m_ContentsCnt = 0;
242 m_Index = NULL;
243 m_IndexCnt = 0;
244 }
245
246 wxHtmlHelpData::~wxHtmlHelpData()
247 {
248 m_BookRecords.Empty();
249 delete[] m_Contents;
250 delete[] m_Index;
251 }
252
253 bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord *book, wxFileSystem& fsys,
254 const wxString& indexfile,
255 const wxString& contentsfile)
256 {
257 wxFSFile *f;
258 wxHtmlFilterHTML filter;
259 wxString buf;
260 wxString string;
261
262 HP_Parser parser;
263 HP_TagHandler *handler = new HP_TagHandler(book);
264 parser.AddTagHandler(handler);
265
266 f = ( contentsfile.IsEmpty() ? (wxFSFile*) NULL : fsys.OpenFile(contentsfile) );
267 if (f)
268 {
269 buf.clear();
270 buf = filter.ReadFile(*f);
271 delete f;
272 handler->ReadIn(m_Contents, m_ContentsCnt);
273 parser.Parse(buf);
274 handler->WriteOut(m_Contents, m_ContentsCnt);
275 }
276 else
277 {
278 wxLogError(_("Cannot open contents file: %s"), contentsfile.c_str());
279 }
280
281 f = ( indexfile.IsEmpty() ? (wxFSFile*) NULL : fsys.OpenFile(indexfile) );
282 if (f)
283 {
284 buf.clear();
285 buf = filter.ReadFile(*f);
286 delete f;
287 handler->ReadIn(m_Index, m_IndexCnt);
288 parser.Parse(buf);
289 handler->WriteOut(m_Index, m_IndexCnt);
290 }
291 else if (!indexfile.IsEmpty())
292 {
293 wxLogError(_("Cannot open index file: %s"), indexfile.c_str());
294 }
295 return TRUE;
296 }
297
298 inline static void CacheWriteInt32(wxOutputStream *f, wxInt32 value)
299 {
300 wxInt32 x = wxINT32_SWAP_ON_BE(value);
301 f->Write(&x, sizeof(x));
302 }
303
304 inline static wxInt32 CacheReadInt32(wxInputStream *f)
305 {
306 wxInt32 x;
307 f->Read(&x, sizeof(x));
308 return wxINT32_SWAP_ON_BE(x);
309 }
310
311 inline static void CacheWriteString(wxOutputStream *f, const wxString& str)
312 {
313 const wxWX2MBbuf mbstr = str.mb_str(wxConvUTF8);
314 size_t len = strlen((const char*)mbstr)+1;
315 CacheWriteInt32(f, len);
316 f->Write((const char*)mbstr, len);
317 }
318
319 inline static wxString CacheReadString(wxInputStream *f)
320 {
321 size_t len = (size_t)CacheReadInt32(f);
322 wxCharBuffer str(len-1);
323 f->Read(str.data(), len);
324 return wxString(str, wxConvUTF8);
325 }
326
327 #define CURRENT_CACHED_BOOK_VERSION 4
328
329 // Additional flags to detect incompatibilities of the runtime environment:
330 #define CACHED_BOOK_FORMAT_FLAGS \
331 (wxUSE_UNICODE << 0)
332
333
334 bool wxHtmlHelpData::LoadCachedBook(wxHtmlBookRecord *book, wxInputStream *f)
335 {
336 int i, st;
337 wxInt32 version;
338
339 /* load header - version info : */
340 version = CacheReadInt32(f);
341
342 if (version != CURRENT_CACHED_BOOK_VERSION)
343 {
344 // NB: We can just silently return FALSE here and don't worry about
345 // it anymore, because AddBookParam will load the MS project in
346 // absence of (properly versioned) .cached file and automatically
347 // create new .cached file immediately afterward.
348 return FALSE;
349 }
350
351 if (CacheReadInt32(f) != CACHED_BOOK_FORMAT_FLAGS)
352 return FALSE;
353
354 /* load contents : */
355 st = m_ContentsCnt;
356 m_ContentsCnt += CacheReadInt32(f);
357 m_Contents = ReallocArray(m_Contents, st,
358 (m_ContentsCnt / wxHTML_REALLOC_STEP + 1) *
359 wxHTML_REALLOC_STEP);
360 for (i = st; i < m_ContentsCnt; i++)
361 {
362 m_Contents[i].m_Level = CacheReadInt32(f);
363 m_Contents[i].m_ID = CacheReadInt32(f);
364 m_Contents[i].m_Name = CacheReadString(f);
365 m_Contents[i].m_Page = CacheReadString(f);
366 m_Contents[i].m_Book = book;
367 }
368
369 /* load index : */
370 st = m_IndexCnt;
371 m_IndexCnt += CacheReadInt32(f);
372 m_Index = ReallocArray(m_Index, st,
373 (m_IndexCnt / wxHTML_REALLOC_STEP + 1) *
374 wxHTML_REALLOC_STEP);
375 for (i = st; i < m_IndexCnt; i++)
376 {
377 m_Index[i].m_Name = CacheReadString(f);
378 m_Index[i].m_Page = CacheReadString(f);
379 m_Index[i].m_Book = book;
380 }
381 return TRUE;
382 }
383
384
385 bool wxHtmlHelpData::SaveCachedBook(wxHtmlBookRecord *book, wxOutputStream *f)
386 {
387 int i;
388 wxInt32 cnt;
389
390 /* save header - version info : */
391 CacheWriteInt32(f, CURRENT_CACHED_BOOK_VERSION);
392 CacheWriteInt32(f, CACHED_BOOK_FORMAT_FLAGS);
393
394 /* save contents : */
395 for (cnt = 0, i = 0; i < m_ContentsCnt; i++)
396 if (m_Contents[i].m_Book == book && m_Contents[i].m_Level > 0)
397 cnt++;
398 CacheWriteInt32(f, cnt);
399
400 for (i = 0; i < m_ContentsCnt; i++)
401 {
402 if (m_Contents[i].m_Book != book || m_Contents[i].m_Level == 0)
403 continue;
404 CacheWriteInt32(f, m_Contents[i].m_Level);
405 CacheWriteInt32(f, m_Contents[i].m_ID);
406 CacheWriteString(f, m_Contents[i].m_Name);
407 CacheWriteString(f, m_Contents[i].m_Page);
408 }
409
410 /* save index : */
411 for (cnt = 0, i = 0; i < m_IndexCnt; i++)
412 if (m_Index[i].m_Book == book && m_Index[i].m_Level > 0)
413 cnt++;
414 CacheWriteInt32(f, cnt);
415
416 for (i = 0; i < m_IndexCnt; i++)
417 {
418 if (m_Index[i].m_Book != book || m_Index[i].m_Level == 0)
419 continue;
420 CacheWriteString(f, m_Index[i].m_Name);
421 CacheWriteString(f, m_Index[i].m_Page);
422 }
423 return TRUE;
424 }
425
426
427 void wxHtmlHelpData::SetTempDir(const wxString& path)
428 {
429 if (path == wxEmptyString) m_TempPath = path;
430 else
431 {
432 if (wxIsAbsolutePath(path)) m_TempPath = path;
433 else m_TempPath = wxGetCwd() + _T("/") + path;
434
435 if (m_TempPath[m_TempPath.Length() - 1] != _T('/'))
436 m_TempPath << _T('/');
437 }
438 }
439
440
441
442 static wxString SafeFileName(const wxString& s)
443 {
444 wxString res(s);
445 res.Replace(wxT("#"), wxT("_"));
446 res.Replace(wxT(":"), wxT("_"));
447 res.Replace(wxT("\\"), wxT("_"));
448 res.Replace(wxT("/"), wxT("_"));
449 return res;
450 }
451
452 bool wxHtmlHelpData::AddBookParam(const wxFSFile& bookfile,
453 wxFontEncoding encoding,
454 const wxString& title, const wxString& contfile,
455 const wxString& indexfile, const wxString& deftopic,
456 const wxString& path)
457 {
458 wxFileSystem fsys;
459 wxFSFile *fi;
460 wxHtmlBookRecord *bookr;
461
462 int IndexOld = m_IndexCnt,
463 ContentsOld = m_ContentsCnt;
464
465 if (!path.IsEmpty())
466 fsys.ChangePathTo(path, TRUE);
467
468 size_t booksCnt = m_BookRecords.GetCount();
469 for (size_t i = 0; i < booksCnt; i++)
470 {
471 if ( m_BookRecords[i].GetBookFile() == bookfile.GetLocation() )
472 return TRUE; // book is (was) loaded
473 }
474
475 bookr = new wxHtmlBookRecord(bookfile.GetLocation(), fsys.GetPath(), title, deftopic);
476
477 if (m_ContentsCnt % wxHTML_REALLOC_STEP == 0)
478 m_Contents = ReallocArray(m_Contents, m_ContentsCnt,
479 m_ContentsCnt + wxHTML_REALLOC_STEP);
480 m_Contents[m_ContentsCnt].m_Level = 0;
481 m_Contents[m_ContentsCnt].m_ID = 0;
482 m_Contents[m_ContentsCnt].m_Page = deftopic;
483 m_Contents[m_ContentsCnt].m_Name = title;
484 m_Contents[m_ContentsCnt].m_Book = bookr;
485
486 // store the contents index for later
487 int cont_start = m_ContentsCnt++;
488
489 // Try to find cached binary versions:
490 // 1. save file as book, but with .hhp.cached extension
491 // 2. same as 1. but in temp path
492 // 3. otherwise or if cache load failed, load it from MS.
493
494 fi = fsys.OpenFile(bookfile.GetLocation() + wxT(".cached"));
495
496 if (fi == NULL ||
497 #if wxUSE_DATETIME
498 fi->GetModificationTime() < bookfile.GetModificationTime() ||
499 #endif // wxUSE_DATETIME
500 !LoadCachedBook(bookr, fi->GetStream()))
501 {
502 if (fi != NULL) delete fi;
503 fi = fsys.OpenFile(m_TempPath + wxFileNameFromPath(bookfile.GetLocation()) + wxT(".cached"));
504 if (m_TempPath == wxEmptyString || fi == NULL ||
505 #if wxUSE_DATETIME
506 fi->GetModificationTime() < bookfile.GetModificationTime() ||
507 #endif // wxUSE_DATETIME
508 !LoadCachedBook(bookr, fi->GetStream()))
509 {
510 LoadMSProject(bookr, fsys, indexfile, contfile);
511 if (m_TempPath != wxEmptyString)
512 {
513 wxFileOutputStream *outs = new wxFileOutputStream(m_TempPath +
514 SafeFileName(wxFileNameFromPath(bookfile.GetLocation())) + wxT(".cached"));
515 SaveCachedBook(bookr, outs);
516 delete outs;
517 }
518 }
519 }
520
521 if (fi != NULL) delete fi;
522
523 // Now store the contents range
524 bookr->SetContentsRange(cont_start, m_ContentsCnt);
525
526 #if wxUSE_WCHAR_T
527 // MS HTML Help files [written by MS HTML Help Workshop] are broken
528 // in that the data are iso-8859-1 (including HTML entities), but must
529 // be interpreted as being in language's windows charset. Correct the
530 // differences here and also convert to wxConvLocal in ANSI build
531 if (encoding != wxFONTENCODING_SYSTEM)
532 {
533 #if wxUSE_UNICODE
534 #define CORRECT_STR(str, conv) \
535 str = wxString((str).mb_str(wxConvISO8859_1), conv)
536 #else
537 #define CORRECT_STR(str, conv) \
538 str = wxString((str).wc_str(conv), wxConvLocal)
539 #endif
540 wxCSConv conv(encoding);
541 int i;
542 for (i = IndexOld; i < m_IndexCnt; i++)
543 {
544 CORRECT_STR(m_Index[i].m_Name, conv);
545 }
546 for (i = ContentsOld; i < m_ContentsCnt; i++)
547 {
548 CORRECT_STR(m_Contents[i].m_Name, conv);
549 }
550 #undef CORRECT_STR
551 }
552 #else
553 wxUnusedVar(IndexOld);
554 wxUnusedVar(ContentsOld);
555 wxASSERT_MSG(encoding == wxFONTENCODING_SYSTEM, wxT("Help files need charset conversion, but wxUSE_WCHAR_T is 0"));
556 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
557
558 m_BookRecords.Add(bookr);
559 if (m_IndexCnt > 0)
560 qsort(m_Index, m_IndexCnt, sizeof(wxHtmlContentsItem), wxHtmlHelpIndexCompareFunc);
561
562 return TRUE;
563 }
564
565
566 bool wxHtmlHelpData::AddBook(const wxString& book)
567 {
568 wxString extension(book.Right(4).Lower());
569 if (extension == wxT(".zip") ||
570 #if wxUSE_LIBMSPACK
571 extension == wxT(".chm") /*compressed html help book*/ ||
572 #endif
573 extension == wxT(".htb") /*html book*/)
574 {
575 wxFileSystem fsys;
576 wxString s;
577 bool rt = FALSE;
578
579 #if wxUSE_LIBMSPACK
580 if (extension == wxT(".chm"))
581 s = fsys.FindFirst(book + wxT("#chm:*.hhp"), wxFILE);
582 else
583 #endif
584 s = fsys.FindFirst(book + wxT("#zip:*.hhp"), wxFILE);
585
586 while (!s.IsEmpty())
587 {
588 if (AddBook(s)) rt = TRUE;
589 s = fsys.FindNext();
590 }
591
592 return rt;
593 }
594
595 wxFSFile *fi;
596 wxFileSystem fsys;
597
598 wxString title = _("noname"),
599 safetitle,
600 start = wxEmptyString,
601 contents = wxEmptyString,
602 index = wxEmptyString,
603 charset = wxEmptyString;
604
605 fi = fsys.OpenFile(book);
606 if (fi == NULL)
607 {
608 wxLogError(_("Cannot open HTML help book: %s"), book.c_str());
609 return FALSE;
610 }
611 fsys.ChangePathTo(book);
612
613 const wxChar *lineptr;
614 wxChar linebuf[300];
615 wxString tmp;
616 wxHtmlFilterPlainText filter;
617 tmp = filter.ReadFile(*fi);
618 lineptr = tmp.c_str();
619
620 do
621 {
622 lineptr = ReadLine(lineptr, linebuf, 300);
623
624 for (wxChar *ch = linebuf; *ch != wxT('\0') && *ch != wxT('='); ch++)
625 *ch = tolower(*ch);
626
627 if (wxStrstr(linebuf, _T("title=")) == linebuf)
628 title = linebuf + wxStrlen(_T("title="));
629 if (wxStrstr(linebuf, _T("default topic=")) == linebuf)
630 start = linebuf + wxStrlen(_T("default topic="));
631 if (wxStrstr(linebuf, _T("index file=")) == linebuf)
632 index = linebuf + wxStrlen(_T("index file="));
633 if (wxStrstr(linebuf, _T("contents file=")) == linebuf)
634 contents = linebuf + wxStrlen(_T("contents file="));
635 if (wxStrstr(linebuf, _T("charset=")) == linebuf)
636 charset = linebuf + wxStrlen(_T("charset="));
637 } while (lineptr != NULL);
638
639 wxFontEncoding enc;
640 if (charset == wxEmptyString) enc = wxFONTENCODING_SYSTEM;
641 else enc = wxFontMapper::Get()->CharsetToEncoding(charset);
642
643 bool rtval = AddBookParam(*fi, enc,
644 title, contents, index, start, fsys.GetPath());
645 delete fi;
646 return rtval;
647 }
648
649 wxString wxHtmlHelpData::FindPageByName(const wxString& x)
650 {
651 int cnt;
652 int i;
653 wxFileSystem fsys;
654 wxFSFile *f;
655 wxString url(wxEmptyString);
656
657 /* 1. try to open given file: */
658
659 cnt = m_BookRecords.GetCount();
660 for (i = 0; i < cnt; i++)
661 {
662 f = fsys.OpenFile(m_BookRecords[i].GetFullPath(x));
663 if (f)
664 {
665 url = m_BookRecords[i].GetFullPath(x);
666 delete f;
667 return url;
668 }
669 }
670
671
672 /* 2. try to find a book: */
673
674 for (i = 0; i < cnt; i++)
675 {
676 if (m_BookRecords[i].GetTitle() == x)
677 {
678 url = m_BookRecords[i].GetFullPath(m_BookRecords[i].GetStart());
679 return url;
680 }
681 }
682
683 /* 3. try to find in contents: */
684
685 cnt = m_ContentsCnt;
686 for (i = 0; i < cnt; i++)
687 {
688 if (m_Contents[i].m_Name == x)
689 {
690 url = m_Contents[i].GetFullPath();
691 return url;
692 }
693 }
694
695
696 /* 4. try to find in index: */
697
698 cnt = m_IndexCnt;
699 for (i = 0; i < cnt; i++)
700 {
701 if (m_Index[i].m_Name == x)
702 {
703 url = m_Index[i].GetFullPath();
704 return url;
705 }
706 }
707
708 return url;
709 }
710
711 wxString wxHtmlHelpData::FindPageById(int id)
712 {
713 int i;
714 wxString url(wxEmptyString);
715
716 for (i = 0; i < m_ContentsCnt; i++)
717 {
718 if (m_Contents[i].m_ID == id)
719 {
720 url = m_Contents[i].GetFullPath();
721 return url;
722 }
723 }
724
725 return url;
726 }
727
728 //----------------------------------------------------------------------------------
729 // wxHtmlSearchStatus functions
730 //----------------------------------------------------------------------------------
731
732 wxHtmlSearchStatus::wxHtmlSearchStatus(wxHtmlHelpData* data, const wxString& keyword,
733 bool case_sensitive, bool whole_words_only,
734 const wxString& book)
735 {
736 m_Data = data;
737 m_Keyword = keyword;
738 wxHtmlBookRecord* bookr = NULL;
739 if (book != wxEmptyString)
740 {
741 // we have to search in a specific book. Find it first
742 int i, cnt = data->m_BookRecords.GetCount();
743 for (i = 0; i < cnt; i++)
744 if (data->m_BookRecords[i].GetTitle() == book)
745 {
746 bookr = &(data->m_BookRecords[i]);
747 m_CurIndex = bookr->GetContentsStart();
748 m_MaxIndex = bookr->GetContentsEnd();
749 break;
750 }
751 // check; we won't crash if the book doesn't exist, but it's Bad Anyway.
752 wxASSERT(bookr);
753 }
754 if (! bookr)
755 {
756 // no book specified; search all books
757 m_CurIndex = 0;
758 m_MaxIndex = m_Data->m_ContentsCnt;
759 }
760 m_Engine.LookFor(keyword, case_sensitive, whole_words_only);
761 m_Active = (m_CurIndex < m_MaxIndex);
762 }
763
764 bool wxHtmlSearchStatus::Search()
765 {
766 wxFSFile *file;
767 int i = m_CurIndex; // shortcut
768 bool found = FALSE;
769 wxString thepage;
770
771 if (!m_Active)
772 {
773 // sanity check. Illegal use, but we'll try to prevent a crash anyway
774 wxASSERT(m_Active);
775 return FALSE;
776 }
777
778 m_Name = wxEmptyString;
779 m_ContentsItem = NULL;
780 thepage = m_Data->m_Contents[i].m_Page;
781
782 m_Active = (++m_CurIndex < m_MaxIndex);
783 // check if it is same page with different anchor:
784 if (!m_LastPage.empty())
785 {
786 const wxChar *p1, *p2;
787 for (p1 = thepage.c_str(), p2 = m_LastPage.c_str();
788 *p1 != 0 && *p1 != _T('#') && *p1 == *p2; p1++, p2++) {}
789
790 m_LastPage = thepage;
791
792 if (*p1 == 0 || *p1 == _T('#'))
793 return false;
794 }
795 else m_LastPage = thepage;
796
797 wxFileSystem fsys;
798 file = fsys.OpenFile(m_Data->m_Contents[i].m_Book->GetFullPath(thepage));
799 if (file)
800 {
801 if (m_Engine.Scan(*file))
802 {
803 m_Name = m_Data->m_Contents[i].m_Name;
804 m_ContentsItem = m_Data->m_Contents + i;
805 found = TRUE;
806 }
807 delete file;
808 }
809 return found;
810 }
811
812
813
814
815
816
817
818
819 //--------------------------------------------------------------------------------
820 // wxHtmlSearchEngine
821 //--------------------------------------------------------------------------------
822
823 void wxHtmlSearchEngine::LookFor(const wxString& keyword, bool case_sensitive, bool whole_words_only)
824 {
825 m_CaseSensitive = case_sensitive;
826 m_WholeWords = whole_words_only;
827 m_Keyword = keyword;
828
829 if (m_CaseSensitive)
830 m_Keyword.LowerCase();
831 }
832
833
834 static inline bool WHITESPACE(wxChar c)
835 {
836 return c == _T(' ') || c == _T('\n') || c == _T('\r') || c == _T('\t');
837 }
838
839 bool wxHtmlSearchEngine::Scan(const wxFSFile& file)
840 {
841 wxASSERT_MSG(!m_Keyword.empty(), wxT("wxHtmlSearchEngine::LookFor must be called before scanning!"));
842
843 int i, j;
844 int wrd = m_Keyword.Length();
845 bool found = FALSE;
846 wxHtmlFilterHTML filter;
847 wxString tmp = filter.ReadFile(file);
848 int lng = tmp.length();
849 const wxChar *buf = tmp.c_str();
850
851 if (!m_CaseSensitive)
852 tmp.LowerCase();
853
854 const wxChar *kwd = m_Keyword.c_str();
855
856 if (m_WholeWords)
857 {
858 for (i = 0; i < lng - wrd; i++)
859 {
860 if (WHITESPACE(buf[i])) continue;
861 j = 0;
862 while ((j < wrd) && (buf[i + j] == kwd[j])) j++;
863 if (j == wrd && WHITESPACE(buf[i + j])) { found = true; break; }
864 }
865 }
866
867 else
868 {
869 for (i = 0; i < lng - wrd; i++)
870 {
871 j = 0;
872 while ((j < wrd) && (buf[i + j] == kwd[j])) j++;
873 if (j == wrd) { found = true; break; }
874 }
875 }
876
877 return found;
878 }
879
880
881
882 #endif