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