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