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