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