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