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