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