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