]> git.saurik.com Git - wxWidgets.git/blame - src/html/helpdata.cpp
added constructor for compliance with wxPython
[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
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.
3dde6c72 51static char* ReadLine(char *line, char *buf, size_t bufsize)
8ec2b484 52{
3dde6c72
VS
53 char *writeptr = buf;
54 char *endptr = buf + bufsize - 1;
55 char *readptr = line;
8ec2b484 56
3dde6c72
VS
57 while (*readptr != 0 && *readptr != '\r' && *readptr != '\n' &&
58 writeptr != endptr)
59 *(writeptr++) = *(readptr++);
8ec2b484 60 *writeptr = 0;
3dde6c72
VS
61 while (*readptr == '\r' || *readptr == '\n')
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
251bool 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;
f42b1601 257
8ec2b484
HH
258 HP_Parser parser;
259 HP_TagHandler *handler = new HP_TagHandler(book);
260 parser.AddTagHandler(handler);
261
dc1efb1d 262 f = ( contentsfile.IsEmpty() ? (wxFSFile*) NULL : fsys.OpenFile(contentsfile) );
04dbb646 263 if (f)
4f9297b0
VS
264 {
265 sz = f->GetStream()->GetSize();
d5bb85a0 266 buf = new char[sz + 1];
8ec2b484 267 buf[sz] = 0;
4f9297b0 268 f->GetStream()->Read(buf, sz);
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
HH
273 delete[] buf;
274 }
f3c82859 275 else
f6bcfd97 276 wxLogError(_("Cannot open contents file: %s"), contentsfile.c_str());
8ec2b484 277
dc1efb1d 278 f = ( indexfile.IsEmpty() ? (wxFSFile*) NULL : fsys.OpenFile(indexfile) );
04dbb646 279 if (f)
4f9297b0
VS
280 {
281 sz = f->GetStream()->GetSize();
d5bb85a0 282 buf = new char[sz + 1];
8ec2b484 283 buf[sz] = 0;
4f9297b0 284 f->GetStream()->Read(buf, sz);
8ec2b484 285 delete f;
4f9297b0 286 handler->ReadIn(m_Index, m_IndexCnt);
8ec2b484 287 parser.Parse(buf);
4f9297b0 288 handler->WriteOut(m_Index, m_IndexCnt);
8ec2b484
HH
289 delete[] buf;
290 }
b19d5e56 291 else if (!indexfile.IsEmpty())
f6bcfd97 292 wxLogError(_("Cannot open index file: %s"), indexfile.c_str());
8ec2b484
HH
293 return TRUE;
294}
295
296
f35822af
VS
297
298
299#if wxUSE_UNICODE
300
4f9297b0
VS
301#define READ_STRING(f, s, lng) { char tmpc; for (int i = 0; i < lng; i++) { f->Read(&tmpc, 1); s[i] = (wxChar)tmpc;} }
302#define WRITE_STRING(f, s, lng) { char tmpc; for (int i = 0; i < lng; i++) { tmpc = (char)s[i]; f->Write(&tmpc, 1);} }
f35822af
VS
303
304#else
305
4f9297b0
VS
306#define READ_STRING(f, s, lng) f->Read(s, lng * sizeof(char));
307#define WRITE_STRING(f, s, lng) f->Write(s, lng * sizeof(char));
f35822af
VS
308
309#endif
310
311
312#define CURRENT_CACHED_BOOK_VERSION 1
313
8ec2b484
HH
314bool wxHtmlHelpData::LoadCachedBook(wxHtmlBookRecord *book, wxInputStream *f)
315{
316 int i, st;
f35822af
VS
317 wxInt32 x;
318 wxInt32 version;
319
320 /* load header - version info : */
321
4f9297b0 322 f->Read(&x, sizeof(x));
f35822af 323 version = wxINT32_SWAP_ON_BE(x);
04dbb646
VZ
324
325 if (version != CURRENT_CACHED_BOOK_VERSION)
f3c82859
VS
326 {
327 wxLogError(_("Incorrect version of HTML help book"));
328 return FALSE;
f35822af 329 // NOTE: when adding new version, please ensure backward compatibility!
f3c82859 330 }
04dbb646 331
8ec2b484
HH
332 /* load contents : */
333
4f9297b0 334 f->Read(&x, sizeof(x));
8ec2b484 335 st = m_ContentsCnt;
f35822af 336 m_ContentsCnt += wxINT32_SWAP_ON_BE(x);
04dbb646
VZ
337 m_Contents = (wxHtmlContentsItem*) realloc(m_Contents,
338 (m_ContentsCnt / wxHTML_REALLOC_STEP + 1) *
f35822af 339 wxHTML_REALLOC_STEP * sizeof(wxHtmlContentsItem));
04dbb646 340 for (i = st; i < m_ContentsCnt; i++)
4f9297b0
VS
341 {
342 f->Read(&x, sizeof(x));
f35822af 343 m_Contents[i].m_Level = wxINT32_SWAP_ON_BE(x);
4f9297b0 344 f->Read(&x, sizeof(x));
f35822af 345 m_Contents[i].m_ID = wxINT32_SWAP_ON_BE(x);
4f9297b0 346 f->Read(&x, sizeof(x)); x = wxINT32_SWAP_ON_BE(x);
66a77a74 347 m_Contents[i].m_Name = new wxChar[x];
f35822af 348 READ_STRING(f, m_Contents[i].m_Name, x);
4f9297b0 349 f->Read(&x, sizeof(x)); x = wxINT32_SWAP_ON_BE(x);
66a77a74 350 m_Contents[i].m_Page = new wxChar[x];
f35822af 351 READ_STRING(f, m_Contents[i].m_Page, x);
8ec2b484
HH
352 m_Contents[i].m_Book = book;
353 }
354
355 /* load index : */
356
4f9297b0 357 f->Read(&x, sizeof(x));
8ec2b484 358 st = m_IndexCnt;
f35822af 359 m_IndexCnt += wxINT32_SWAP_ON_BE(x);
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
VS
363 {
364 f->Read(&x, sizeof(x)); x = wxINT32_SWAP_ON_BE(x);
66a77a74 365 m_Index[i].m_Name = new wxChar[x];
f35822af 366 READ_STRING(f, m_Index[i].m_Name, x);
4f9297b0 367 f->Read(&x, sizeof(x)); x = wxINT32_SWAP_ON_BE(x);
66a77a74 368 m_Index[i].m_Page = new wxChar[x];
f35822af 369 READ_STRING(f, m_Index[i].m_Page, x);
8ec2b484
HH
370 m_Index[i].m_Book = book;
371 }
372 return TRUE;
373}
374
375
376bool wxHtmlHelpData::SaveCachedBook(wxHtmlBookRecord *book, wxOutputStream *f)
377{
378 int i;
f35822af
VS
379 wxInt32 x;
380
381 /* save header - version info : */
382
383 x = wxINT32_SWAP_ON_BE(CURRENT_CACHED_BOOK_VERSION);
4f9297b0 384 f->Write(&x, sizeof(x));
8ec2b484
HH
385
386 /* save contents : */
387
388 x = 0;
389 for (i = 0; i < m_ContentsCnt; i++) if (m_Contents[i].m_Book == book && m_Contents[i].m_Level > 0) x++;
f35822af 390 x = wxINT32_SWAP_ON_BE(x);
4f9297b0 391 f->Write(&x, sizeof(x));
04dbb646 392 for (i = 0; i < m_ContentsCnt; i++)
4f9297b0 393 {
8ec2b484 394 if (m_Contents[i].m_Book != book || m_Contents[i].m_Level == 0) continue;
f35822af 395 x = wxINT32_SWAP_ON_BE(m_Contents[i].m_Level);
4f9297b0 396 f->Write(&x, sizeof(x));
f35822af 397 x = wxINT32_SWAP_ON_BE(m_Contents[i].m_ID);
4f9297b0 398 f->Write(&x, sizeof(x));
f35822af 399 x = wxINT32_SWAP_ON_BE(wxStrlen(m_Contents[i].m_Name) + 1);
4f9297b0 400 f->Write(&x, sizeof(x));
f35822af
VS
401 WRITE_STRING(f, m_Contents[i].m_Name, x);
402 x = wxINT32_SWAP_ON_BE(wxStrlen(m_Contents[i].m_Page) + 1);
4f9297b0 403 f->Write(&x, sizeof(x));
f35822af 404 WRITE_STRING(f, m_Contents[i].m_Page, x);
8ec2b484
HH
405 }
406
407 /* save index : */
408
409 x = 0;
410 for (i = 0; i < m_IndexCnt; i++) if (m_Index[i].m_Book == book && m_Index[i].m_Level > 0) x++;
f35822af 411 x = wxINT32_SWAP_ON_BE(x);
4f9297b0 412 f->Write(&x, sizeof(x));
04dbb646 413 for (i = 0; i < m_IndexCnt; i++)
4f9297b0 414 {
8ec2b484 415 if (m_Index[i].m_Book != book || m_Index[i].m_Level == 0) continue;
f35822af 416 x = wxINT32_SWAP_ON_BE(wxStrlen(m_Index[i].m_Name) + 1);
4f9297b0 417 f->Write(&x, sizeof(x));
f35822af
VS
418 WRITE_STRING(f, m_Index[i].m_Name, x);
419 x = wxINT32_SWAP_ON_BE(wxStrlen(m_Index[i].m_Page) + 1);
4f9297b0 420 f->Write(&x, sizeof(x));
f35822af 421 WRITE_STRING(f, m_Index[i].m_Page, x);
8ec2b484
HH
422 }
423 return TRUE;
424}
425
426
427void wxHtmlHelpData::SetTempDir(const wxString& path)
428{
429 if (path == wxEmptyString) m_TempPath = path;
04dbb646 430 else
4f9297b0 431 {
d5bb85a0 432 if (wxIsAbsolutePath(path)) m_TempPath = path;
f35822af 433 else m_TempPath = wxGetCwd() + _T("/") + path;
8ec2b484 434
f35822af
VS
435 if (m_TempPath[m_TempPath.Length() - 1] != _T('/'))
436 m_TempPath << _T('/');
8ec2b484
HH
437 }
438}
439
440
29e60597
VS
441
442static wxString SafeFileName(const wxString& s)
443{
444 wxString res(s);
445 res.Replace(wxT("#"), wxT("_"));
446 res.Replace(wxT(":"), wxT("_"));
447 res.Replace(wxT("\\"), wxT("_"));
448 res.Replace(wxT("/"), wxT("_"));
449 return res;
450}
451
f35822af 452bool wxHtmlHelpData::AddBookParam(const wxFSFile& bookfile,
f890e2d4 453 wxFontEncoding encoding,
f35822af 454 const wxString& title, const wxString& contfile,
d5bb85a0
VS
455 const wxString& indexfile, const wxString& deftopic,
456 const wxString& path)
8ec2b484
HH
457{
458 wxFileSystem fsys;
459 wxFSFile *fi;
460 wxHtmlBookRecord *bookr;
04dbb646 461
f890e2d4
VS
462 int IndexOld = m_IndexCnt,
463 ContentsOld = m_ContentsCnt;
f42b1601 464
8ec2b484 465 if (! path.IsEmpty())
d5bb85a0 466 fsys.ChangePathTo(path, TRUE);
8ec2b484 467
68364659 468 bookr = new wxHtmlBookRecord(fsys.GetPath(), title, deftopic);
8ec2b484 469
efba2b89
VS
470 if (m_ContentsCnt % wxHTML_REALLOC_STEP == 0)
471 m_Contents = (wxHtmlContentsItem*) realloc(m_Contents, (m_ContentsCnt + wxHTML_REALLOC_STEP) * sizeof(wxHtmlContentsItem));
8ec2b484
HH
472 m_Contents[m_ContentsCnt].m_Level = 0;
473 m_Contents[m_ContentsCnt].m_ID = 0;
66a77a74
OK
474 m_Contents[m_ContentsCnt].m_Page = new wxChar[deftopic.Length() + 1];
475 wxStrcpy(m_Contents[m_ContentsCnt].m_Page, deftopic.c_str());
476 m_Contents[m_ContentsCnt].m_Name = new wxChar [title.Length() + 1];
477 wxStrcpy(m_Contents[m_ContentsCnt].m_Name, title.c_str());
8ec2b484
HH
478 m_Contents[m_ContentsCnt].m_Book = bookr;
479
480 // store the contents index for later
481 int cont_start = m_ContentsCnt++;
482
483 // Try to find cached binary versions:
f35822af
VS
484 // 1. save file as book, but with .hhp.cached extension
485 // 2. same as 1. but in temp path
486 // 3. otherwise or if cache load failed, load it from MS.
04dbb646 487
f35822af 488 fi = fsys.OpenFile(bookfile.GetLocation() + wxT(".cached"));
04dbb646
VZ
489
490 if (fi == NULL ||
491 fi->GetModificationTime() < bookfile.GetModificationTime() ||
4f9297b0 492 !LoadCachedBook(bookr, fi->GetStream()))
f35822af
VS
493 {
494 if (fi != NULL) delete fi;
495 fi = fsys.OpenFile(m_TempPath + wxFileNameFromPath(bookfile.GetLocation()) + wxT(".cached"));
04dbb646
VZ
496 if (m_TempPath == wxEmptyString || fi == NULL ||
497 fi->GetModificationTime() < bookfile.GetModificationTime() ||
4f9297b0 498 !LoadCachedBook(bookr, fi->GetStream()))
f35822af
VS
499 {
500 LoadMSProject(bookr, fsys, indexfile, contfile);
04dbb646 501 if (m_TempPath != wxEmptyString)
f35822af 502 {
04dbb646 503 wxFileOutputStream *outs = new wxFileOutputStream(m_TempPath +
29e60597 504 SafeFileName(wxFileNameFromPath(bookfile.GetLocation())) + wxT(".cached"));
f35822af
VS
505 SaveCachedBook(bookr, outs);
506 delete outs;
507 }
d5bb85a0 508 }
8ec2b484 509 }
04dbb646 510
f35822af 511 if (fi != NULL) delete fi;
8ec2b484
HH
512
513 // Now store the contents range
514 bookr->SetContentsRange(cont_start, m_ContentsCnt);
04dbb646 515
f890e2d4
VS
516 // Convert encoding, if neccessary:
517 if (encoding != wxFONTENCODING_SYSTEM)
518 {
519 wxFontEncodingArray a = wxEncodingConverter::GetPlatformEquivalents(encoding);
520 if (a.GetCount() != 0 && a[0] != encoding)
521 {
522 int i;
523 wxEncodingConverter conv;
524 conv.Init(encoding, a[0]);
04dbb646 525
f890e2d4
VS
526 for (i = IndexOld; i < m_IndexCnt; i++)
527 conv.Convert(m_Index[i].m_Name);
528 for (i = ContentsOld; i < m_ContentsCnt; i++)
529 conv.Convert(m_Contents[i].m_Name);
530 }
531 }
8ec2b484
HH
532
533 m_BookRecords.Add(bookr);
534 if (m_IndexCnt > 0)
90350682 535 qsort(m_Index, m_IndexCnt, sizeof(wxHtmlContentsItem), wxHtmlHelpIndexCompareFunc);
f42b1601 536
8ec2b484
HH
537 return TRUE;
538}
539
540
541bool wxHtmlHelpData::AddBook(const wxString& book)
542{
68364659 543 if (book.Right(4).Lower() == wxT(".zip") ||
04dbb646 544 book.Right(4).Lower() == wxT(".htb") /*html book*/)
68364659
VS
545
546 {
547 wxFileSystem fsys;
548 wxString s;
549 bool rt = FALSE;
550
551 s = fsys.FindFirst(book + wxT("#zip:") + wxT("*.hhp"), wxFILE);
04dbb646 552 while (!s.IsEmpty())
68364659
VS
553 {
554 if (AddBook(s)) rt = TRUE;
555 s = fsys.FindNext();
556 }
04dbb646 557
68364659
VS
558 return rt;
559 }
8ec2b484 560
68364659 561
04dbb646
VZ
562 else
563 {
68364659
VS
564 wxFSFile *fi;
565 wxFileSystem fsys;
566 wxInputStream *s;
567 wxString bookFull;
568
68364659 569 wxString title = _("noname"),
f890e2d4
VS
570 safetitle,
571 start = wxEmptyString,
04dbb646 572 contents = wxEmptyString,
f890e2d4
VS
573 index = wxEmptyString,
574 charset = wxEmptyString;
68364659 575
b2d1bc21 576#if defined(__WXMAC__) && !defined(__DARWIN__)
53e46b61
RR
577 if (wxIsAbsolutePath(book)) bookFull = book;
578 else bookFull = wxGetCwd() + book; // no slash or dot
057b55b0
RR
579 wxFileName fn( bookFull );
580 bookFull = fn.GetFullPath( wxPATH_UNIX );
581#else
68364659
VS
582 if (wxIsAbsolutePath(book)) bookFull = book;
583 else bookFull = wxGetCwd() + "/" + book;
057b55b0 584#endif
68364659
VS
585
586 fi = fsys.OpenFile(bookFull);
04dbb646 587 if (fi == NULL)
f6bcfd97
BP
588 {
589 wxLogError(_("Cannot open HTML help book: %s"), bookFull.c_str());
590 return FALSE;
591 }
68364659 592 fsys.ChangePathTo(bookFull);
4f9297b0 593 s = fi->GetStream();
3dde6c72
VS
594
595 int sz;
596 char *buff, *lineptr;
597 char linebuf[300];
598
4f9297b0 599 sz = s->GetSize();
68364659
VS
600 buff = new char[sz + 1];
601 buff[sz] = 0;
4f9297b0 602 s->Read(buff, sz);
68364659 603 lineptr = buff;
68364659 604
3dde6c72
VS
605 do
606 {
607 lineptr = ReadLine(lineptr, linebuf, 300);
608
609 for (char *ch = linebuf; *ch != '\0' && *ch != '='; ch++)
610 *ch = tolower(*ch);
611
612 if (strstr(linebuf, "title=") == linebuf)
613 title = linebuf + strlen("title=");
614 if (strstr(linebuf, "default topic=") == linebuf)
615 start = linebuf + strlen("default topic=");
616 if (strstr(linebuf, "index file=") == linebuf)
617 index = linebuf + strlen("index file=");
618 if (strstr(linebuf, "contents file=") == linebuf)
619 contents = linebuf + strlen("contents file=");
620 if (strstr(linebuf, "charset=") == linebuf)
621 charset = linebuf + strlen("charset=");
68364659
VS
622 } while (lineptr != NULL);
623 delete[] buff;
04dbb646 624
f890e2d4
VS
625 wxFontEncoding enc;
626 if (charset == wxEmptyString) enc = wxFONTENCODING_SYSTEM;
4f9297b0 627 else enc = wxTheFontMapper->CharsetToEncoding(charset);
04dbb646 628 bool rtval = AddBookParam(*fi, enc,
f890e2d4 629 title, contents, index, start, fsys.GetPath());
f35822af
VS
630 delete fi;
631 return rtval;
68364659 632 }
8ec2b484
HH
633}
634
635wxString wxHtmlHelpData::FindPageByName(const wxString& x)
636{
637 int cnt;
638 int i;
639 wxFileSystem fsys;
640 wxFSFile *f;
641 wxString url(wxEmptyString);
642
643 /* 1. try to open given file: */
644
645 cnt = m_BookRecords.GetCount();
04dbb646 646 for (i = 0; i < cnt; i++)
4f9297b0 647 {
468ae730 648 f = fsys.OpenFile(m_BookRecords[i].GetFullPath(x));
04dbb646
VZ
649 if (f)
650 {
468ae730 651 url = m_BookRecords[i].GetFullPath(x);
8ec2b484
HH
652 delete f;
653 return url;
654 }
655 }
656
657
658 /* 2. try to find a book: */
659
04dbb646 660 for (i = 0; i < cnt; i++)
4f9297b0 661 {
04dbb646
VZ
662 if (m_BookRecords[i].GetTitle() == x)
663 {
468ae730 664 url = m_BookRecords[i].GetFullPath(m_BookRecords[i].GetStart());
8ec2b484
HH
665 return url;
666 }
667 }
668
669 /* 3. try to find in contents: */
670
671 cnt = m_ContentsCnt;
04dbb646 672 for (i = 0; i < cnt; i++)
4f9297b0 673 {
04dbb646
VZ
674 if (wxStrcmp(m_Contents[i].m_Name, x) == 0)
675 {
468ae730 676 url = m_Contents[i].GetFullPath();
8ec2b484
HH
677 return url;
678 }
679 }
680
681
682 /* 4. try to find in index: */
683
684 cnt = m_IndexCnt;
04dbb646 685 for (i = 0; i < cnt; i++)
4f9297b0 686 {
04dbb646
VZ
687 if (wxStrcmp(m_Index[i].m_Name, x) == 0)
688 {
468ae730 689 url = m_Index[i].GetFullPath();
8ec2b484
HH
690 return url;
691 }
692 }
693
694 return url;
695}
696
697wxString wxHtmlHelpData::FindPageById(int id)
f42b1601 698{
8ec2b484
HH
699 int i;
700 wxString url(wxEmptyString);
701
04dbb646 702 for (i = 0; i < m_ContentsCnt; i++)
4f9297b0 703 {
04dbb646
VZ
704 if (m_Contents[i].m_ID == id)
705 {
468ae730 706 url = m_Contents[i].GetFullPath();
8ec2b484
HH
707 return url;
708 }
709 }
710
711 return url;
712}
713
714//----------------------------------------------------------------------------------
715// wxHtmlSearchStatus functions
716//----------------------------------------------------------------------------------
717
718wxHtmlSearchStatus::wxHtmlSearchStatus(wxHtmlHelpData* data, const wxString& keyword,
c4971147 719 bool case_sensitive, bool whole_words_only,
d5bb85a0 720 const wxString& book)
8ec2b484
HH
721{
722 m_Data = data;
723 m_Keyword = keyword;
724 wxHtmlBookRecord* bookr = NULL;
04dbb646 725 if (book != wxEmptyString)
4f9297b0 726 {
d5bb85a0
VS
727 // we have to search in a specific book. Find it first
728 int i, cnt = data->m_BookRecords.GetCount();
729 for (i = 0; i < cnt; i++)
04dbb646
VZ
730 if (data->m_BookRecords[i].GetTitle() == book)
731 {
d5bb85a0
VS
732 bookr = &(data->m_BookRecords[i]);
733 m_CurIndex = bookr->GetContentsStart();
734 m_MaxIndex = bookr->GetContentsEnd();
735 break;
736 }
737 // check; we won't crash if the book doesn't exist, but it's Bad Anyway.
738 wxASSERT(bookr);
8ec2b484 739 }
04dbb646 740 if (! bookr)
4f9297b0 741 {
d5bb85a0
VS
742 // no book specified; search all books
743 m_CurIndex = 0;
744 m_MaxIndex = m_Data->m_ContentsCnt;
8ec2b484 745 }
c4971147 746 m_Engine.LookFor(keyword, case_sensitive, whole_words_only);
8ec2b484 747 m_Active = (m_CurIndex < m_MaxIndex);
b5a7b000 748 m_LastPage = NULL;
8ec2b484
HH
749}
750
751bool wxHtmlSearchStatus::Search()
752{
8ec2b484 753 wxFSFile *file;
d5bb85a0 754 int i = m_CurIndex; // shortcut
8ec2b484 755 bool found = FALSE;
b5a7b000 756 wxChar *thepage;
8ec2b484 757
04dbb646 758 if (!m_Active)
4f9297b0 759 {
f35822af 760 // sanity check. Illegal use, but we'll try to prevent a crash anyway
50494a55 761 wxASSERT(m_Active);
50494a55 762 return FALSE;
8ec2b484
HH
763 }
764
8ec2b484 765 m_Name = wxEmptyString;
b5a7b000
VS
766 m_ContentsItem = NULL;
767 thepage = m_Data->m_Contents[i].m_Page;
8ec2b484 768
b5a7b000
VS
769 m_Active = (++m_CurIndex < m_MaxIndex);
770 // check if it is same page with different anchor:
771 if (m_LastPage != NULL)
772 {
773 wxChar *p1, *p2;
04dbb646 774 for (p1 = thepage, p2 = m_LastPage;
b5a7b000
VS
775 *p1 != 0 && *p1 != _T('#') && *p1 == *p2; p1++, p2++) {}
776
777 m_LastPage = thepage;
778
779 if (*p1 == 0 || *p1 == _T('#'))
780 return FALSE;
781 }
782 else m_LastPage = thepage;
04dbb646 783
f35822af 784 wxFileSystem fsys;
468ae730 785 file = fsys.OpenFile(m_Data->m_Contents[i].m_Book->GetFullPath(thepage));
04dbb646 786 if (file)
b5a7b000 787 {
04dbb646 788 if (m_Engine.Scan(file->GetStream()))
468ae730 789 {
b5a7b000
VS
790 m_Name = m_Data->m_Contents[i].m_Name;
791 m_ContentsItem = m_Data->m_Contents + i;
792 found = TRUE;
d5bb85a0
VS
793 }
794 delete file;
8ec2b484 795 }
8ec2b484
HH
796 return found;
797}
798
d5bb85a0
VS
799
800
801
802
803
804
805
806//--------------------------------------------------------------------------------
807// wxSearchEngine
808//--------------------------------------------------------------------------------
809
c4971147 810void wxSearchEngine::LookFor(const wxString& keyword, bool case_sensitive, bool whole_words_only)
d5bb85a0 811{
c4971147
VS
812 m_CaseSensitive = case_sensitive;
813 m_WholeWords = whole_words_only;
d5bb85a0 814 if (m_Keyword) delete[] m_Keyword;
66a77a74
OK
815 m_Keyword = new wxChar[keyword.Length() + 1];
816 wxStrcpy(m_Keyword, keyword.c_str());
04dbb646 817
c4971147 818 if (!m_CaseSensitive)
4f9297b0 819 {
c4971147 820 for (int i = wxStrlen(m_Keyword) - 1; i >= 0; i--)
04dbb646 821 {
c4971147
VS
822 if ((m_Keyword[i] >= wxT('A')) && (m_Keyword[i] <= wxT('Z')))
823 m_Keyword[i] += wxT('a') - wxT('A');
04dbb646
VZ
824 }
825 }
d5bb85a0
VS
826}
827
828
829
c4971147
VS
830#define WHITESPACE(c) (c == ' ' || c == '\n' || c == '\r' || c == '\t')
831
d5bb85a0
VS
832bool wxSearchEngine::Scan(wxInputStream *stream)
833{
50494a55 834 wxASSERT_MSG(m_Keyword != NULL, wxT("wxSearchEngine::LookFor must be called before scanning!"));
d5bb85a0
VS
835
836 int i, j;
837 int lng = stream ->GetSize();
66a77a74 838 int wrd = wxStrlen(m_Keyword);
d5bb85a0
VS
839 bool found = FALSE;
840 char *buf = new char[lng + 1];
4f9297b0 841 stream->Read(buf, lng);
d5bb85a0
VS
842 buf[lng] = 0;
843
c4971147
VS
844 if (!m_CaseSensitive)
845 for (i = 0; i < lng; i++)
846 if ((buf[i] >= 'A') && (buf[i] <= 'Z')) buf[i] += 'a' - 'A';
d5bb85a0 847
c4971147
VS
848 if (m_WholeWords)
849 {
04dbb646
VZ
850 for (i = 0; i < lng - wrd; i++)
851 {
c4971147
VS
852 if (WHITESPACE(buf[i])) continue;
853 j = 0;
854 while ((j < wrd) && (buf[i + j] == m_Keyword[j])) j++;
4f9297b0 855 if (j == wrd && WHITESPACE(buf[i + j])) { found = TRUE; break; }
c4971147
VS
856 }
857 }
04dbb646 858
c4971147
VS
859 else
860 {
04dbb646
VZ
861 for (i = 0; i < lng - wrd; i++)
862 {
c4971147
VS
863 j = 0;
864 while ((j < wrd) && (buf[i + j] == m_Keyword[j])) j++;
4f9297b0 865 if (j == wrd) { found = TRUE; break; }
c4971147 866 }
d5bb85a0
VS
867 }
868
869 delete[] buf;
870 return found;
871}
872
d5bb85a0
VS
873
874
8ec2b484 875#endif