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