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