]> git.saurik.com Git - wxWidgets.git/blame - src/html/helpdata.cpp
Avoid an assert in wxFrame::SetTitle
[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 8// Copyright: (c) Harm van der Heijden and Vaclav Slavik
65571936 9// Licence: wxWindows licence
8ec2b484
HH
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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
401eb3de
RR
32#include <ctype.h>
33
8ec2b484
HH
34#include "wx/html/helpdata.h"
35#include "wx/tokenzr.h"
36#include "wx/wfstream.h"
37#include "wx/busyinfo.h"
f890e2d4
VS
38#include "wx/encconv.h"
39#include "wx/fontmap.h"
f3c82859 40#include "wx/log.h"
69941f05 41#include "wx/html/htmlpars.h"
8ec2b484 42#include "wx/html/htmldefs.h"
2b5f62a0 43#include "wx/html/htmlfilt.h"
057b55b0 44#include "wx/filename.h"
8ec2b484
HH
45
46#include "wx/arrimpl.cpp"
47WX_DEFINE_OBJARRAY(wxHtmlBookRecArray)
91fa114d 48WX_DEFINE_OBJARRAY(wxHtmlHelpDataItems)
8ec2b484
HH
49
50//-----------------------------------------------------------------------------
51// static helper functions
52//-----------------------------------------------------------------------------
53
54// Reads one line, stores it into buf and returns pointer to new line or NULL.
1a1dac18 55static const wxChar* ReadLine(const wxChar *line, wxChar *buf, size_t bufsize)
8ec2b484 56{
1a1dac18
MB
57 wxChar *writeptr = buf;
58 wxChar *endptr = buf + bufsize - 1;
59 const wxChar *readptr = line;
8ec2b484 60
1a1dac18 61 while (*readptr != 0 && *readptr != _T('\r') && *readptr != _T('\n') &&
3dde6c72
VS
62 writeptr != endptr)
63 *(writeptr++) = *(readptr++);
8ec2b484 64 *writeptr = 0;
1a1dac18 65 while (*readptr == _T('\r') || *readptr == _T('\n'))
3dde6c72
VS
66 readptr++;
67 if (*readptr == 0)
68 return NULL;
69 else
70 return readptr;
8ec2b484
HH
71}
72
73
8ec2b484 74
91fa114d
VS
75static int
76wxHtmlHelpIndexCompareFunc(wxHtmlHelpDataItem **a, wxHtmlHelpDataItem **b)
8ec2b484 77{
91fa114d
VS
78 wxHtmlHelpDataItem *ia = *a;
79 wxHtmlHelpDataItem *ib = *b;
3912b3f8 80
91fa114d
VS
81 if (ia->parent == ib->parent)
82 {
83 return ia->name.CmpNoCase(ib->name);
84 }
85 else if (ia->level == ib->level)
86 {
87 return wxHtmlHelpIndexCompareFunc(&ia->parent, &ib->parent);
88 }
89 else
90 {
91 wxHtmlHelpDataItem *ia2 = ia;
92 wxHtmlHelpDataItem *ib2 = ib;
93
94 while (ia2->level > ib2->level)
95 {
96 ia2 = ia2->parent;
97 }
98 while (ib2->level > ia2->level)
99 {
100 ib2 = ib2->parent;
101 }
102
103 wxASSERT(ia2);
104 wxASSERT(ib2);
105 int res = wxHtmlHelpIndexCompareFunc(&ia2, &ib2);
106 if (res != 0)
107 return res;
108 else if (ia->level > ib->level)
109 return 1;
110 else
111 return -1;
112 }
8ec2b484
HH
113}
114
8ec2b484
HH
115//-----------------------------------------------------------------------------
116// HP_Parser
117//-----------------------------------------------------------------------------
118
119class HP_Parser : public wxHtmlParser
120{
211dfedd 121public:
67c276bd
VS
122 HP_Parser()
123 {
124 GetEntitiesParser()->SetEncoding(wxFONTENCODING_ISO8859_1);
125 }
fc7a2a60 126
211dfedd 127 wxObject* GetProduct() { return NULL; }
fc7a2a60 128
211dfedd
VS
129protected:
130 virtual void AddText(const wxChar* WXUNUSED(txt)) {}
fc7a2a60
VZ
131
132 DECLARE_NO_COPY_CLASS(HP_Parser)
8ec2b484
HH
133};
134
135
136//-----------------------------------------------------------------------------
137// HP_TagHandler
138//-----------------------------------------------------------------------------
139
140class HP_TagHandler : public wxHtmlTagHandler
141{
142 private:
91fa114d
VS
143 wxString m_name, m_page;
144 int m_level;
145 int m_id;
146 int m_index;
147 int m_count;
148 wxHtmlHelpDataItem *m_parentItem;
149 wxHtmlBookRecord *m_book;
150
151 wxHtmlHelpDataItems *m_data;
8ec2b484
HH
152
153 public:
04dbb646 154 HP_TagHandler(wxHtmlBookRecord *b) : wxHtmlTagHandler()
91fa114d
VS
155 {
156 m_data = NULL;
157 m_book = b;
158 m_name = m_page = wxEmptyString;
159 m_level = 0;
160 m_id = -1;
161 m_count = 0;
162 m_parentItem = NULL;
163 }
66a77a74 164 wxString GetSupportedTags() { return wxT("UL,OBJECT,PARAM"); }
8ec2b484 165 bool HandleTag(const wxHtmlTag& tag);
91fa114d
VS
166
167 void Reset(wxHtmlHelpDataItems& data)
168 {
169 m_data = &data;
170 m_count = 0;
171 m_level = 0;
172 m_parentItem = NULL;
173 }
22f3361e
VZ
174
175 DECLARE_NO_COPY_CLASS(HP_TagHandler)
8ec2b484
HH
176};
177
178
179bool HP_TagHandler::HandleTag(const wxHtmlTag& tag)
180{
04dbb646 181 if (tag.GetName() == wxT("UL"))
4f9297b0 182 {
91fa114d
VS
183 wxHtmlHelpDataItem *oldparent = m_parentItem;
184 m_level++;
185 m_parentItem = (m_count > 0) ? &(*m_data)[m_data->size()-1] : NULL;
8ec2b484 186 ParseInner(tag);
91fa114d
VS
187 m_level--;
188 m_parentItem = oldparent;
189 return true;
8ec2b484 190 }
04dbb646 191 else if (tag.GetName() == wxT("OBJECT"))
4f9297b0 192 {
91fa114d 193 m_name = m_page = wxEmptyString;
8ec2b484 194 ParseInner(tag);
50494a55 195
daa084c2 196#if 0
91fa114d 197 if (!page.IsEmpty())
7df9fbc3 198 /* Valid HHW's file may contain only two object tags:
04dbb646 199
4157f43f
VS
200 <OBJECT type="text/site properties">
201 <param name="ImageType" value="Folder">
202 </OBJECT>
04dbb646 203
4157f43f 204 or
04dbb646
VZ
205
206 <OBJECT type="text/sitemap">
207 <param name="Name" value="main page">
208 <param name="Local" value="another.htm">
209 </OBJECT>
210
91fa114d 211 We're interested in the latter. !page.IsEmpty() is valid
4157f43f
VS
212 condition because text/site properties does not contain Local param
213 */
daa084c2
VS
214#endif
215 if (tag.GetParam(wxT("TYPE")) == wxT("text/sitemap"))
4157f43f 216 {
91fa114d
VS
217 wxHtmlHelpDataItem *item = new wxHtmlHelpDataItem();
218 item->parent = m_parentItem;
219 item->level = m_level;
220 item->id = m_id;
221 item->page = m_page;
222 item->name = m_name;
223
224 item->book = m_book;
225 m_data->Add(item);
226 m_count++;
4157f43f 227 }
50494a55 228
91fa114d 229 return true;
8ec2b484 230 }
04dbb646 231 else
4f9297b0 232 { // "PARAM"
91fa114d
VS
233 if (m_name.empty() && tag.GetParam(wxT("NAME")) == wxT("Name"))
234 m_name = tag.GetParam(wxT("VALUE"));
04dbb646 235 if (tag.GetParam(wxT("NAME")) == wxT("Local"))
91fa114d 236 m_page = tag.GetParam(wxT("VALUE"));
04dbb646 237 if (tag.GetParam(wxT("NAME")) == wxT("ID"))
91fa114d
VS
238 tag.GetParamAsInt(wxT("VALUE"), &m_id);
239 return false;
8ec2b484
HH
240 }
241}
242
243
8ec2b484
HH
244//-----------------------------------------------------------------------------
245// wxHtmlHelpData
246//-----------------------------------------------------------------------------
247
468ae730
VS
248wxString wxHtmlBookRecord::GetFullPath(const wxString &page) const
249{
250 if (wxIsAbsolutePath(page))
251 return page;
252 else
253 return m_BasePath + page;
254}
255
91fa114d
VS
256wxString wxHtmlHelpDataItem::GetIndentedName() const
257{
258 wxString s;
259 for (int i = 1; i < level; i++)
260 s << _T(" ");
261 s << name;
262 return s;
263}
468ae730
VS
264
265
8ec2b484
HH
266IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpData, wxObject)
267
f42b1601 268wxHtmlHelpData::wxHtmlHelpData()
8ec2b484 269{
91fa114d
VS
270#if WXWIN_COMPATIBILITY_2_4
271 m_cacheContents = NULL;
272 m_cacheIndex = NULL;
273#endif
8ec2b484
HH
274}
275
276wxHtmlHelpData::~wxHtmlHelpData()
277{
91fa114d
VS
278#if WXWIN_COMPATIBILITY_2_4
279 CleanCompatibilityData();
280#endif
8ec2b484
HH
281}
282
67c276bd
VS
283bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord *book, wxFileSystem& fsys,
284 const wxString& indexfile,
285 const wxString& contentsfile)
8ec2b484
HH
286{
287 wxFSFile *f;
2b5f62a0 288 wxHtmlFilterHTML filter;
eb37e1d2 289 wxString buf;
8ec2b484 290 wxString string;
f42b1601 291
8ec2b484
HH
292 HP_Parser parser;
293 HP_TagHandler *handler = new HP_TagHandler(book);
294 parser.AddTagHandler(handler);
295
dc1efb1d 296 f = ( contentsfile.IsEmpty() ? (wxFSFile*) NULL : fsys.OpenFile(contentsfile) );
04dbb646 297 if (f)
4f9297b0 298 {
eb37e1d2 299 buf.clear();
2b5f62a0 300 buf = filter.ReadFile(*f);
8ec2b484 301 delete f;
91fa114d 302 handler->Reset(m_contents);
8ec2b484 303 parser.Parse(buf);
8ec2b484 304 }
f3c82859 305 else
2b5f62a0 306 {
f6bcfd97 307 wxLogError(_("Cannot open contents file: %s"), contentsfile.c_str());
2b5f62a0 308 }
8ec2b484 309
dc1efb1d 310 f = ( indexfile.IsEmpty() ? (wxFSFile*) NULL : fsys.OpenFile(indexfile) );
04dbb646 311 if (f)
4f9297b0 312 {
eb37e1d2 313 buf.clear();
2b5f62a0 314 buf = filter.ReadFile(*f);
8ec2b484 315 delete f;
91fa114d 316 handler->Reset(m_index);
8ec2b484 317 parser.Parse(buf);
8ec2b484 318 }
b19d5e56 319 else if (!indexfile.IsEmpty())
2b5f62a0 320 {
f6bcfd97 321 wxLogError(_("Cannot open index file: %s"), indexfile.c_str());
2b5f62a0 322 }
8ec2b484
HH
323 return TRUE;
324}
325
4fd5055c
VS
326inline static void CacheWriteInt32(wxOutputStream *f, wxInt32 value)
327{
328 wxInt32 x = wxINT32_SWAP_ON_BE(value);
329 f->Write(&x, sizeof(x));
330}
f35822af 331
4fd5055c
VS
332inline static wxInt32 CacheReadInt32(wxInputStream *f)
333{
334 wxInt32 x;
335 f->Read(&x, sizeof(x));
336 return wxINT32_SWAP_ON_BE(x);
337}
f35822af 338
3912b3f8 339inline static void CacheWriteString(wxOutputStream *f, const wxString& str)
2b5f62a0 340{
3912b3f8
VS
341 const wxWX2MBbuf mbstr = str.mb_str(wxConvUTF8);
342 size_t len = strlen((const char*)mbstr)+1;
4fd5055c 343 CacheWriteInt32(f, len);
3912b3f8 344 f->Write((const char*)mbstr, len);
4fd5055c 345}
f35822af 346
3912b3f8 347inline static wxString CacheReadString(wxInputStream *f)
4fd5055c 348{
4fd5055c 349 size_t len = (size_t)CacheReadInt32(f);
3912b3f8
VS
350 wxCharBuffer str(len-1);
351 f->Read(str.data(), len);
352 return wxString(str, wxConvUTF8);
4fd5055c 353}
f35822af 354
91fa114d 355#define CURRENT_CACHED_BOOK_VERSION 5
d1a9c82b
VS
356
357// Additional flags to detect incompatibilities of the runtime environment:
358#define CACHED_BOOK_FORMAT_FLAGS \
359 (wxUSE_UNICODE << 0)
360
f35822af 361
8ec2b484
HH
362bool wxHtmlHelpData::LoadCachedBook(wxHtmlBookRecord *book, wxInputStream *f)
363{
91fa114d 364 int i, st, newsize;
f35822af
VS
365 wxInt32 version;
366
367 /* load header - version info : */
4fd5055c 368 version = CacheReadInt32(f);
2b5f62a0 369
04dbb646 370 if (version != CURRENT_CACHED_BOOK_VERSION)
f3c82859 371 {
4fd5055c
VS
372 // NB: We can just silently return FALSE here and don't worry about
373 // it anymore, because AddBookParam will load the MS project in
374 // absence of (properly versioned) .cached file and automatically
375 // create new .cached file immediately afterward.
91fa114d 376 return false;
f3c82859 377 }
04dbb646 378
d1a9c82b 379 if (CacheReadInt32(f) != CACHED_BOOK_FORMAT_FLAGS)
91fa114d 380 return false;
d1a9c82b 381
8ec2b484 382 /* load contents : */
91fa114d
VS
383 st = m_contents.size();
384 newsize = st + CacheReadInt32(f);
385 m_contents.Alloc(newsize);
386 for (i = st; i < newsize; i++)
4f9297b0 387 {
91fa114d
VS
388 wxHtmlHelpDataItem *item = new wxHtmlHelpDataItem;
389 item->level = CacheReadInt32(f);
390 item->id = CacheReadInt32(f);
391 item->name = CacheReadString(f);
392 item->page = CacheReadString(f);
393 item->book = book;
394 m_contents.Add(item);
8ec2b484
HH
395 }
396
397 /* load index : */
91fa114d
VS
398 st = m_index.size();
399 newsize = st + CacheReadInt32(f);
400 m_index.Alloc(newsize);
401 for (i = st; i < newsize; i++)
4f9297b0 402 {
91fa114d
VS
403 wxHtmlHelpDataItem *item = new wxHtmlHelpDataItem;
404 item->name = CacheReadString(f);
405 item->page = CacheReadString(f);
406 item->level = CacheReadInt32(f);
407 item->book = book;
408 int parentShift = CacheReadInt32(f);
409 if (parentShift != 0)
410 item->parent = &m_index[m_index.size() - parentShift];
411 m_index.Add(item);
8ec2b484 412 }
91fa114d 413 return true;
8ec2b484
HH
414}
415
416
417bool wxHtmlHelpData::SaveCachedBook(wxHtmlBookRecord *book, wxOutputStream *f)
418{
419 int i;
4fd5055c 420 wxInt32 cnt;
f35822af
VS
421
422 /* save header - version info : */
4fd5055c 423 CacheWriteInt32(f, CURRENT_CACHED_BOOK_VERSION);
d1a9c82b 424 CacheWriteInt32(f, CACHED_BOOK_FORMAT_FLAGS);
8ec2b484
HH
425
426 /* save contents : */
91fa114d
VS
427 int len = m_contents.size();
428 for (cnt = 0, i = 0; i < len; i++)
429 if (m_contents[i].book == book && m_contents[i].level > 0)
4fd5055c
VS
430 cnt++;
431 CacheWriteInt32(f, cnt);
8ec2b484 432
91fa114d 433 for (i = 0; i < len; i++)
4f9297b0 434 {
91fa114d 435 if (m_contents[i].book != book || m_contents[i].level == 0)
4fd5055c 436 continue;
91fa114d
VS
437 CacheWriteInt32(f, m_contents[i].level);
438 CacheWriteInt32(f, m_contents[i].id);
439 CacheWriteString(f, m_contents[i].name);
440 CacheWriteString(f, m_contents[i].page);
8ec2b484
HH
441 }
442
443 /* save index : */
91fa114d
VS
444 len = m_index.size();
445 for (cnt = 0, i = 0; i < len; i++)
446 if (m_index[i].book == book && m_index[i].level > 0)
4fd5055c
VS
447 cnt++;
448 CacheWriteInt32(f, cnt);
8ec2b484 449
91fa114d 450 for (i = 0; i < len; i++)
4f9297b0 451 {
91fa114d 452 if (m_index[i].book != book || m_index[i].level == 0)
4fd5055c 453 continue;
91fa114d
VS
454 CacheWriteString(f, m_index[i].name);
455 CacheWriteString(f, m_index[i].page);
456 CacheWriteInt32(f, m_index[i].level);
457 // save distance to parent item, if any:
458 if (m_index[i].parent == NULL)
459 {
460 CacheWriteInt32(f, 0);
461 }
462 else
463 {
464 int cnt = 0;
465 wxHtmlHelpDataItem *parent = m_index[i].parent;
466 for (int j = i-1; j >= 0; j--)
467 {
468 if (m_index[j].book == book && m_index[j].level > 0)
469 cnt++;
470 if (&m_index[j] == parent)
471 break;
472 }
473 wxASSERT(cnt > 0);
474 CacheWriteInt32(f, cnt);
475 }
8ec2b484 476 }
91fa114d 477 return true;
8ec2b484
HH
478}
479
480
481void wxHtmlHelpData::SetTempDir(const wxString& path)
482{
91fa114d
VS
483 if (path.empty())
484 m_tempPath = path;
04dbb646 485 else
4f9297b0 486 {
91fa114d
VS
487 if (wxIsAbsolutePath(path)) m_tempPath = path;
488 else m_tempPath = wxGetCwd() + _T("/") + path;
8ec2b484 489
91fa114d
VS
490 if (m_tempPath[m_tempPath.Length() - 1] != _T('/'))
491 m_tempPath << _T('/');
8ec2b484
HH
492 }
493}
494
495
29e60597
VS
496
497static wxString SafeFileName(const wxString& s)
498{
499 wxString res(s);
500 res.Replace(wxT("#"), wxT("_"));
501 res.Replace(wxT(":"), wxT("_"));
502 res.Replace(wxT("\\"), wxT("_"));
503 res.Replace(wxT("/"), wxT("_"));
504 return res;
505}
506
f35822af 507bool wxHtmlHelpData::AddBookParam(const wxFSFile& bookfile,
f890e2d4 508 wxFontEncoding encoding,
f35822af 509 const wxString& title, const wxString& contfile,
d5bb85a0
VS
510 const wxString& indexfile, const wxString& deftopic,
511 const wxString& path)
8ec2b484
HH
512{
513 wxFileSystem fsys;
514 wxFSFile *fi;
515 wxHtmlBookRecord *bookr;
04dbb646 516
91fa114d
VS
517 int IndexOld = m_index.size(),
518 ContentsOld = m_contents.size();
f42b1601 519
5ecdcaa7 520 if (!path.IsEmpty())
d5bb85a0 521 fsys.ChangePathTo(path, TRUE);
8ec2b484 522
91fa114d 523 size_t booksCnt = m_bookRecords.GetCount();
5ecdcaa7
VS
524 for (size_t i = 0; i < booksCnt; i++)
525 {
91fa114d
VS
526 if ( m_bookRecords[i].GetBookFile() == bookfile.GetLocation() )
527 return true; // book is (was) loaded
5ecdcaa7
VS
528 }
529
530 bookr = new wxHtmlBookRecord(bookfile.GetLocation(), fsys.GetPath(), title, deftopic);
fc799548 531
91fa114d
VS
532 wxHtmlHelpDataItem *bookitem = new wxHtmlHelpDataItem;
533 bookitem->level = 0;
534 bookitem->id = 0;
535 bookitem->page = deftopic;
536 bookitem->name = title;
537 bookitem->book = bookr;
8ec2b484
HH
538
539 // store the contents index for later
91fa114d
VS
540 int cont_start = m_contents.size();
541
542 m_contents.Add(bookitem);
8ec2b484
HH
543
544 // Try to find cached binary versions:
f35822af
VS
545 // 1. save file as book, but with .hhp.cached extension
546 // 2. same as 1. but in temp path
547 // 3. otherwise or if cache load failed, load it from MS.
04dbb646 548
f35822af 549 fi = fsys.OpenFile(bookfile.GetLocation() + wxT(".cached"));
04dbb646
VZ
550
551 if (fi == NULL ||
e2b87f38 552#if wxUSE_DATETIME
04dbb646 553 fi->GetModificationTime() < bookfile.GetModificationTime() ||
e2b87f38 554#endif // wxUSE_DATETIME
4f9297b0 555 !LoadCachedBook(bookr, fi->GetStream()))
f35822af
VS
556 {
557 if (fi != NULL) delete fi;
91fa114d
VS
558 fi = fsys.OpenFile(m_tempPath + wxFileNameFromPath(bookfile.GetLocation()) + wxT(".cached"));
559 if (m_tempPath.empty() || fi == NULL ||
e2b87f38 560#if wxUSE_DATETIME
04dbb646 561 fi->GetModificationTime() < bookfile.GetModificationTime() ||
e2b87f38 562#endif // wxUSE_DATETIME
4f9297b0 563 !LoadCachedBook(bookr, fi->GetStream()))
f35822af
VS
564 {
565 LoadMSProject(bookr, fsys, indexfile, contfile);
91fa114d 566 if (!m_tempPath.empty())
f35822af 567 {
91fa114d 568 wxFileOutputStream *outs = new wxFileOutputStream(m_tempPath +
29e60597 569 SafeFileName(wxFileNameFromPath(bookfile.GetLocation())) + wxT(".cached"));
f35822af
VS
570 SaveCachedBook(bookr, outs);
571 delete outs;
572 }
d5bb85a0 573 }
8ec2b484 574 }
04dbb646 575
f35822af 576 if (fi != NULL) delete fi;
8ec2b484
HH
577
578 // Now store the contents range
91fa114d 579 bookr->SetContentsRange(cont_start, m_contents.size());
04dbb646 580
3912b3f8
VS
581#if wxUSE_WCHAR_T
582 // MS HTML Help files [written by MS HTML Help Workshop] are broken
583 // in that the data are iso-8859-1 (including HTML entities), but must
584 // be interpreted as being in language's windows charset. Correct the
585 // differences here and also convert to wxConvLocal in ANSI build
67c276bd 586 if (encoding != wxFONTENCODING_SYSTEM)
3912b3f8
VS
587 {
588 #if wxUSE_UNICODE
589 #define CORRECT_STR(str, conv) \
590 str = wxString((str).mb_str(wxConvISO8859_1), conv)
591 #else
592 #define CORRECT_STR(str, conv) \
593 str = wxString((str).wc_str(conv), wxConvLocal)
594 #endif
67c276bd 595 wxCSConv conv(encoding);
91fa114d
VS
596 size_t IndexCnt = m_index.size();
597 size_t ContentsCnt = m_contents.size();
598 size_t i;
599 for (i = IndexOld; i < IndexCnt; i++)
f890e2d4 600 {
91fa114d 601 CORRECT_STR(m_index[i].name, conv);
f890e2d4 602 }
91fa114d 603 for (i = ContentsOld; i < ContentsCnt; i++)
3912b3f8 604 {
91fa114d 605 CORRECT_STR(m_contents[i].name, conv);
3912b3f8
VS
606 }
607 #undef CORRECT_STR
f890e2d4 608 }
0b4f47a3
DS
609#else
610 wxUnusedVar(IndexOld);
611 wxUnusedVar(ContentsOld);
67c276bd 612 wxASSERT_MSG(encoding == wxFONTENCODING_SYSTEM, wxT("Help files need charset conversion, but wxUSE_WCHAR_T is 0"));
3912b3f8 613#endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
8ec2b484 614
91fa114d
VS
615 m_bookRecords.Add(bookr);
616 if (!m_index.empty())
617 {
618 m_index.Sort(wxHtmlHelpIndexCompareFunc);
619 }
f42b1601 620
91fa114d 621 return true;
8ec2b484
HH
622}
623
624
625bool wxHtmlHelpData::AddBook(const wxString& book)
626{
3527f29c
VS
627 wxString extension(book.Right(4).Lower());
628 if (extension == wxT(".zip") ||
629#if wxUSE_LIBMSPACK
630 extension == wxT(".chm") /*compressed html help book*/ ||
631#endif
632 extension == wxT(".htb") /*html book*/)
68364659
VS
633 {
634 wxFileSystem fsys;
635 wxString s;
636 bool rt = FALSE;
637
3527f29c
VS
638#if wxUSE_LIBMSPACK
639 if (extension == wxT(".chm"))
640 s = fsys.FindFirst(book + wxT("#chm:*.hhp"), wxFILE);
641 else
642#endif
643 s = fsys.FindFirst(book + wxT("#zip:*.hhp"), wxFILE);
644
04dbb646 645 while (!s.IsEmpty())
68364659
VS
646 {
647 if (AddBook(s)) rt = TRUE;
648 s = fsys.FindNext();
649 }
04dbb646 650
68364659
VS
651 return rt;
652 }
68364659 653
a509f830
VZ
654 wxFSFile *fi;
655 wxFileSystem fsys;
3dde6c72 656
a509f830
VZ
657 wxString title = _("noname"),
658 safetitle,
659 start = wxEmptyString,
660 contents = wxEmptyString,
661 index = wxEmptyString,
662 charset = wxEmptyString;
68364659 663
a509f830
VZ
664 fi = fsys.OpenFile(book);
665 if (fi == NULL)
666 {
667 wxLogError(_("Cannot open HTML help book: %s"), book.c_str());
668 return FALSE;
68364659 669 }
a509f830
VZ
670 fsys.ChangePathTo(book);
671
672 const wxChar *lineptr;
673 wxChar linebuf[300];
674 wxString tmp;
675 wxHtmlFilterPlainText filter;
676 tmp = filter.ReadFile(*fi);
677 lineptr = tmp.c_str();
678
679 do
680 {
681 lineptr = ReadLine(lineptr, linebuf, 300);
682
683 for (wxChar *ch = linebuf; *ch != wxT('\0') && *ch != wxT('='); ch++)
684 *ch = tolower(*ch);
685
686 if (wxStrstr(linebuf, _T("title=")) == linebuf)
687 title = linebuf + wxStrlen(_T("title="));
688 if (wxStrstr(linebuf, _T("default topic=")) == linebuf)
689 start = linebuf + wxStrlen(_T("default topic="));
690 if (wxStrstr(linebuf, _T("index file=")) == linebuf)
691 index = linebuf + wxStrlen(_T("index file="));
692 if (wxStrstr(linebuf, _T("contents file=")) == linebuf)
693 contents = linebuf + wxStrlen(_T("contents file="));
694 if (wxStrstr(linebuf, _T("charset=")) == linebuf)
695 charset = linebuf + wxStrlen(_T("charset="));
696 } while (lineptr != NULL);
67c276bd
VS
697
698 wxFontEncoding enc;
699 if (charset == wxEmptyString) enc = wxFONTENCODING_SYSTEM;
700 else enc = wxFontMapper::Get()->CharsetToEncoding(charset);
a509f830 701
67c276bd 702 bool rtval = AddBookParam(*fi, enc,
a509f830
VZ
703 title, contents, index, start, fsys.GetPath());
704 delete fi;
91fa114d
VS
705
706#if WXWIN_COMPATIBILITY_2_4
707 CleanCompatibilityData();
708#endif
709
a509f830 710 return rtval;
8ec2b484
HH
711}
712
713wxString wxHtmlHelpData::FindPageByName(const wxString& x)
714{
715 int cnt;
716 int i;
717 wxFileSystem fsys;
718 wxFSFile *f;
8ec2b484
HH
719
720 /* 1. try to open given file: */
721
91fa114d 722 cnt = m_bookRecords.GetCount();
04dbb646 723 for (i = 0; i < cnt; i++)
4f9297b0 724 {
91fa114d 725 f = fsys.OpenFile(m_bookRecords[i].GetFullPath(x));
04dbb646
VZ
726 if (f)
727 {
91fa114d 728 wxString url = m_bookRecords[i].GetFullPath(x);
8ec2b484
HH
729 delete f;
730 return url;
731 }
732 }
733
734
735 /* 2. try to find a book: */
736
04dbb646 737 for (i = 0; i < cnt; i++)
4f9297b0 738 {
91fa114d
VS
739 if (m_bookRecords[i].GetTitle() == x)
740 return m_bookRecords[i].GetFullPath(m_bookRecords[i].GetStart());
8ec2b484
HH
741 }
742
743 /* 3. try to find in contents: */
744
91fa114d 745 cnt = m_contents.size();
04dbb646 746 for (i = 0; i < cnt; i++)
4f9297b0 747 {
91fa114d
VS
748 if (m_contents[i].name == x)
749 return m_contents[i].GetFullPath();
8ec2b484
HH
750 }
751
752
753 /* 4. try to find in index: */
754
91fa114d 755 cnt = m_index.size();
04dbb646 756 for (i = 0; i < cnt; i++)
4f9297b0 757 {
91fa114d
VS
758 if (m_index[i].name == x)
759 return m_index[i].GetFullPath();
8ec2b484
HH
760 }
761
91fa114d 762 return wxEmptyString;
8ec2b484
HH
763}
764
765wxString wxHtmlHelpData::FindPageById(int id)
f42b1601 766{
91fa114d
VS
767 size_t cnt = m_contents.size();
768 for (size_t i = 0; i < cnt; i++)
4f9297b0 769 {
91fa114d 770 if (m_contents[i].id == id)
04dbb646 771 {
91fa114d 772 return m_contents[i].GetFullPath();
8ec2b484
HH
773 }
774 }
775
91fa114d
VS
776 return wxEmptyString;
777}
778
779#if WXWIN_COMPATIBILITY_2_4
780wxHtmlContentsItem::wxHtmlContentsItem()
781 : m_Level(0), m_ID(-1), m_Name(NULL), m_Page(NULL), m_Book(NULL),
782 m_autofree(false)
783{
784}
785
786wxHtmlContentsItem::wxHtmlContentsItem(const wxHtmlHelpDataItem& d)
787{
788 m_autofree = true;
789 m_Level = d.level;
790 m_ID = d.id;
791 m_Name = wxStrdup(d.name.c_str());
792 m_Page = wxStrdup(d.page.c_str());
793 m_Book = d.book;
794}
795
796wxHtmlContentsItem& wxHtmlContentsItem::operator=(const wxHtmlContentsItem& d)
797{
798 if (m_autofree)
799 {
800 free(m_Name);
801 free(m_Page);
802 }
803 m_autofree = true;
804 m_Level = d.m_Level;
805 m_ID = d.m_ID;
806 m_Name = d.m_Name ? wxStrdup(d.m_Name) : NULL;
807 m_Page = d.m_Page ? wxStrdup(d.m_Page) : NULL;
808 m_Book = d.m_Book;
809 return *this;
8ec2b484
HH
810}
811
91fa114d
VS
812wxHtmlContentsItem::~wxHtmlContentsItem()
813{
814 if (m_autofree)
815 {
816 free(m_Name);
817 free(m_Page);
818 }
819}
820
821wxHtmlContentsItem* wxHtmlHelpData::GetContents()
822{
823 if (!m_cacheContents && !m_contents.empty())
824 {
825 size_t len = m_contents.size();
826 m_cacheContents = new wxHtmlContentsItem[len];
827 for (size_t i = 0; i < len; i++)
828 m_cacheContents[i] = m_contents[i];
829 }
830 return m_cacheContents;
831}
832
833int wxHtmlHelpData::GetContentsCnt()
834{
835 return m_contents.size();
836}
837
838wxHtmlContentsItem* wxHtmlHelpData::GetIndex()
839{
840 if (!m_cacheContents && !m_index.empty())
841 {
842 size_t len = m_index.size();
843 m_cacheContents = new wxHtmlContentsItem[len];
844 for (size_t i = 0; i < len; i++)
845 m_cacheContents[i] = m_index[i];
846 }
847 return m_cacheContents;
848}
849
850int wxHtmlHelpData::GetIndexCnt()
851{
852 return m_index.size();
853}
854
855void wxHtmlHelpData::CleanCompatibilityData()
856{
857 delete[] m_cacheContents;
858 m_cacheContents = NULL;
859 delete[] m_cacheIndex;
860 m_cacheIndex = NULL;
861}
862#endif // WXWIN_COMPATIBILITY_2_4
863
8ec2b484
HH
864//----------------------------------------------------------------------------------
865// wxHtmlSearchStatus functions
866//----------------------------------------------------------------------------------
867
868wxHtmlSearchStatus::wxHtmlSearchStatus(wxHtmlHelpData* data, const wxString& keyword,
c4971147 869 bool case_sensitive, bool whole_words_only,
d5bb85a0 870 const wxString& book)
8ec2b484
HH
871{
872 m_Data = data;
873 m_Keyword = keyword;
874 wxHtmlBookRecord* bookr = NULL;
04dbb646 875 if (book != wxEmptyString)
4f9297b0 876 {
d5bb85a0 877 // we have to search in a specific book. Find it first
91fa114d 878 int i, cnt = data->m_bookRecords.GetCount();
d5bb85a0 879 for (i = 0; i < cnt; i++)
91fa114d 880 if (data->m_bookRecords[i].GetTitle() == book)
04dbb646 881 {
91fa114d 882 bookr = &(data->m_bookRecords[i]);
d5bb85a0
VS
883 m_CurIndex = bookr->GetContentsStart();
884 m_MaxIndex = bookr->GetContentsEnd();
885 break;
886 }
887 // check; we won't crash if the book doesn't exist, but it's Bad Anyway.
888 wxASSERT(bookr);
8ec2b484 889 }
04dbb646 890 if (! bookr)
4f9297b0 891 {
d5bb85a0
VS
892 // no book specified; search all books
893 m_CurIndex = 0;
91fa114d 894 m_MaxIndex = m_Data->m_contents.size();
8ec2b484 895 }
c4971147 896 m_Engine.LookFor(keyword, case_sensitive, whole_words_only);
8ec2b484 897 m_Active = (m_CurIndex < m_MaxIndex);
8ec2b484
HH
898}
899
91fa114d
VS
900#if WXWIN_COMPATIBILITY_2_4
901wxHtmlContentsItem* wxHtmlSearchStatus::GetContentsItem()
902{
903 static wxHtmlContentsItem it;
904 it = wxHtmlContentsItem(*m_CurItem);
905 return &it;
906}
907#endif
908
8ec2b484
HH
909bool wxHtmlSearchStatus::Search()
910{
8ec2b484 911 wxFSFile *file;
d5bb85a0 912 int i = m_CurIndex; // shortcut
91fa114d 913 bool found = false;
3912b3f8 914 wxString thepage;
8ec2b484 915
04dbb646 916 if (!m_Active)
4f9297b0 917 {
f35822af 918 // sanity check. Illegal use, but we'll try to prevent a crash anyway
50494a55 919 wxASSERT(m_Active);
91fa114d 920 return false;
8ec2b484
HH
921 }
922
8ec2b484 923 m_Name = wxEmptyString;
91fa114d
VS
924 m_CurItem = NULL;
925 thepage = m_Data->m_contents[i].page;
8ec2b484 926
b5a7b000
VS
927 m_Active = (++m_CurIndex < m_MaxIndex);
928 // check if it is same page with different anchor:
3912b3f8 929 if (!m_LastPage.empty())
b5a7b000 930 {
3912b3f8
VS
931 const wxChar *p1, *p2;
932 for (p1 = thepage.c_str(), p2 = m_LastPage.c_str();
b5a7b000
VS
933 *p1 != 0 && *p1 != _T('#') && *p1 == *p2; p1++, p2++) {}
934
935 m_LastPage = thepage;
936
937 if (*p1 == 0 || *p1 == _T('#'))
3912b3f8 938 return false;
b5a7b000
VS
939 }
940 else m_LastPage = thepage;
04dbb646 941
f35822af 942 wxFileSystem fsys;
91fa114d 943 file = fsys.OpenFile(m_Data->m_contents[i].book->GetFullPath(thepage));
04dbb646 944 if (file)
b5a7b000 945 {
2b5f62a0 946 if (m_Engine.Scan(*file))
468ae730 947 {
91fa114d
VS
948 m_Name = m_Data->m_contents[i].name;
949 m_CurItem = &m_Data->m_contents[i];
950 found = true;
d5bb85a0
VS
951 }
952 delete file;
8ec2b484 953 }
8ec2b484
HH
954 return found;
955}
956
d5bb85a0
VS
957
958
959
960
961
962
963
964//--------------------------------------------------------------------------------
2b5f62a0 965// wxHtmlSearchEngine
d5bb85a0
VS
966//--------------------------------------------------------------------------------
967
2b5f62a0 968void wxHtmlSearchEngine::LookFor(const wxString& keyword, bool case_sensitive, bool whole_words_only)
d5bb85a0 969{
c4971147
VS
970 m_CaseSensitive = case_sensitive;
971 m_WholeWords = whole_words_only;
3912b3f8 972 m_Keyword = keyword;
04dbb646 973
3912b3f8
VS
974 if (m_CaseSensitive)
975 m_Keyword.LowerCase();
d5bb85a0
VS
976}
977
978
1a1dac18
MB
979static inline bool WHITESPACE(wxChar c)
980{
981 return c == _T(' ') || c == _T('\n') || c == _T('\r') || c == _T('\t');
982}
c4971147 983
2b5f62a0 984bool wxHtmlSearchEngine::Scan(const wxFSFile& file)
d5bb85a0 985{
3912b3f8 986 wxASSERT_MSG(!m_Keyword.empty(), wxT("wxHtmlSearchEngine::LookFor must be called before scanning!"));
d5bb85a0
VS
987
988 int i, j;
3912b3f8 989 int wrd = m_Keyword.Length();
d5bb85a0 990 bool found = FALSE;
2b5f62a0
VZ
991 wxHtmlFilterHTML filter;
992 wxString tmp = filter.ReadFile(file);
eb37e1d2 993 int lng = tmp.length();
1a1dac18 994 const wxChar *buf = tmp.c_str();
d5bb85a0 995
c4971147 996 if (!m_CaseSensitive)
3912b3f8 997 tmp.LowerCase();
d5bb85a0 998
3912b3f8
VS
999 const wxChar *kwd = m_Keyword.c_str();
1000
c4971147
VS
1001 if (m_WholeWords)
1002 {
04dbb646
VZ
1003 for (i = 0; i < lng - wrd; i++)
1004 {
c4971147
VS
1005 if (WHITESPACE(buf[i])) continue;
1006 j = 0;
3912b3f8
VS
1007 while ((j < wrd) && (buf[i + j] == kwd[j])) j++;
1008 if (j == wrd && WHITESPACE(buf[i + j])) { found = true; break; }
c4971147
VS
1009 }
1010 }
04dbb646 1011
c4971147
VS
1012 else
1013 {
04dbb646
VZ
1014 for (i = 0; i < lng - wrd; i++)
1015 {
c4971147 1016 j = 0;
3912b3f8
VS
1017 while ((j < wrd) && (buf[i + j] == kwd[j])) j++;
1018 if (j == wrd) { found = true; break; }
c4971147 1019 }
d5bb85a0
VS
1020 }
1021
d5bb85a0
VS
1022 return found;
1023}
1024
d5bb85a0
VS
1025
1026
8ec2b484 1027#endif