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