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