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