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