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