]> git.saurik.com Git - wxWidgets.git/blame - src/html/helpdata.cpp
Tabs to spaces
[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
67c276bd
VS
703 wxFontEncoding enc;
704 if (charset == wxEmptyString) enc = wxFONTENCODING_SYSTEM;
705 else enc = wxFontMapper::Get()->CharsetToEncoding(charset);
a509f830 706
67c276bd 707 bool rtval = AddBookParam(*fi, enc,
a509f830
VZ
708 title, contents, index, start, fsys.GetPath());
709 delete fi;
91fa114d
VS
710
711#if WXWIN_COMPATIBILITY_2_4
712 CleanCompatibilityData();
713#endif
714
a509f830 715 return rtval;
8ec2b484
HH
716}
717
718wxString wxHtmlHelpData::FindPageByName(const wxString& x)
719{
720 int cnt;
721 int i;
722 wxFileSystem fsys;
723 wxFSFile *f;
8ec2b484
HH
724
725 /* 1. try to open given file: */
726
91fa114d 727 cnt = m_bookRecords.GetCount();
04dbb646 728 for (i = 0; i < cnt; i++)
4f9297b0 729 {
91fa114d 730 f = fsys.OpenFile(m_bookRecords[i].GetFullPath(x));
04dbb646
VZ
731 if (f)
732 {
91fa114d 733 wxString url = m_bookRecords[i].GetFullPath(x);
8ec2b484
HH
734 delete f;
735 return url;
736 }
737 }
738
739
740 /* 2. try to find a book: */
741
04dbb646 742 for (i = 0; i < cnt; i++)
4f9297b0 743 {
91fa114d
VS
744 if (m_bookRecords[i].GetTitle() == x)
745 return m_bookRecords[i].GetFullPath(m_bookRecords[i].GetStart());
8ec2b484
HH
746 }
747
748 /* 3. try to find in contents: */
749
91fa114d 750 cnt = m_contents.size();
04dbb646 751 for (i = 0; i < cnt; i++)
4f9297b0 752 {
91fa114d
VS
753 if (m_contents[i].name == x)
754 return m_contents[i].GetFullPath();
8ec2b484
HH
755 }
756
757
758 /* 4. try to find in index: */
759
91fa114d 760 cnt = m_index.size();
04dbb646 761 for (i = 0; i < cnt; i++)
4f9297b0 762 {
91fa114d
VS
763 if (m_index[i].name == x)
764 return m_index[i].GetFullPath();
8ec2b484
HH
765 }
766
91fa114d 767 return wxEmptyString;
8ec2b484
HH
768}
769
770wxString wxHtmlHelpData::FindPageById(int id)
f42b1601 771{
91fa114d
VS
772 size_t cnt = m_contents.size();
773 for (size_t i = 0; i < cnt; i++)
4f9297b0 774 {
91fa114d 775 if (m_contents[i].id == id)
04dbb646 776 {
91fa114d 777 return m_contents[i].GetFullPath();
8ec2b484
HH
778 }
779 }
780
91fa114d
VS
781 return wxEmptyString;
782}
783
784#if WXWIN_COMPATIBILITY_2_4
785wxHtmlContentsItem::wxHtmlContentsItem()
d1da8872 786 : m_Level(0), m_ID(wxID_ANY), m_Name(NULL), m_Page(NULL), m_Book(NULL),
91fa114d
VS
787 m_autofree(false)
788{
789}
790
791wxHtmlContentsItem::wxHtmlContentsItem(const wxHtmlHelpDataItem& d)
792{
793 m_autofree = true;
794 m_Level = d.level;
795 m_ID = d.id;
796 m_Name = wxStrdup(d.name.c_str());
797 m_Page = wxStrdup(d.page.c_str());
798 m_Book = d.book;
799}
800
801wxHtmlContentsItem& wxHtmlContentsItem::operator=(const wxHtmlContentsItem& d)
802{
803 if (m_autofree)
804 {
805 free(m_Name);
806 free(m_Page);
807 }
808 m_autofree = true;
809 m_Level = d.m_Level;
810 m_ID = d.m_ID;
811 m_Name = d.m_Name ? wxStrdup(d.m_Name) : NULL;
812 m_Page = d.m_Page ? wxStrdup(d.m_Page) : NULL;
813 m_Book = d.m_Book;
814 return *this;
8ec2b484
HH
815}
816
91fa114d
VS
817wxHtmlContentsItem::~wxHtmlContentsItem()
818{
819 if (m_autofree)
820 {
821 free(m_Name);
822 free(m_Page);
823 }
824}
825
826wxHtmlContentsItem* wxHtmlHelpData::GetContents()
827{
828 if (!m_cacheContents && !m_contents.empty())
829 {
830 size_t len = m_contents.size();
831 m_cacheContents = new wxHtmlContentsItem[len];
832 for (size_t i = 0; i < len; i++)
833 m_cacheContents[i] = m_contents[i];
834 }
835 return m_cacheContents;
836}
837
838int wxHtmlHelpData::GetContentsCnt()
839{
840 return m_contents.size();
841}
842
843wxHtmlContentsItem* wxHtmlHelpData::GetIndex()
844{
845 if (!m_cacheContents && !m_index.empty())
846 {
847 size_t len = m_index.size();
848 m_cacheContents = new wxHtmlContentsItem[len];
849 for (size_t i = 0; i < len; i++)
850 m_cacheContents[i] = m_index[i];
851 }
852 return m_cacheContents;
853}
854
855int wxHtmlHelpData::GetIndexCnt()
856{
857 return m_index.size();
858}
859
860void wxHtmlHelpData::CleanCompatibilityData()
861{
862 delete[] m_cacheContents;
863 m_cacheContents = NULL;
864 delete[] m_cacheIndex;
865 m_cacheIndex = NULL;
866}
867#endif // WXWIN_COMPATIBILITY_2_4
868
8ec2b484
HH
869//----------------------------------------------------------------------------------
870// wxHtmlSearchStatus functions
871//----------------------------------------------------------------------------------
872
873wxHtmlSearchStatus::wxHtmlSearchStatus(wxHtmlHelpData* data, const wxString& keyword,
c4971147 874 bool case_sensitive, bool whole_words_only,
d5bb85a0 875 const wxString& book)
8ec2b484
HH
876{
877 m_Data = data;
878 m_Keyword = keyword;
879 wxHtmlBookRecord* bookr = NULL;
04dbb646 880 if (book != wxEmptyString)
4f9297b0 881 {
d5bb85a0 882 // we have to search in a specific book. Find it first
91fa114d 883 int i, cnt = data->m_bookRecords.GetCount();
d5bb85a0 884 for (i = 0; i < cnt; i++)
91fa114d 885 if (data->m_bookRecords[i].GetTitle() == book)
04dbb646 886 {
91fa114d 887 bookr = &(data->m_bookRecords[i]);
d5bb85a0
VS
888 m_CurIndex = bookr->GetContentsStart();
889 m_MaxIndex = bookr->GetContentsEnd();
890 break;
891 }
892 // check; we won't crash if the book doesn't exist, but it's Bad Anyway.
893 wxASSERT(bookr);
8ec2b484 894 }
04dbb646 895 if (! bookr)
4f9297b0 896 {
d5bb85a0
VS
897 // no book specified; search all books
898 m_CurIndex = 0;
91fa114d 899 m_MaxIndex = m_Data->m_contents.size();
8ec2b484 900 }
c4971147 901 m_Engine.LookFor(keyword, case_sensitive, whole_words_only);
8ec2b484 902 m_Active = (m_CurIndex < m_MaxIndex);
8ec2b484
HH
903}
904
91fa114d
VS
905#if WXWIN_COMPATIBILITY_2_4
906wxHtmlContentsItem* wxHtmlSearchStatus::GetContentsItem()
907{
908 static wxHtmlContentsItem it;
909 it = wxHtmlContentsItem(*m_CurItem);
910 return &it;
911}
912#endif
913
8ec2b484
HH
914bool wxHtmlSearchStatus::Search()
915{
8ec2b484 916 wxFSFile *file;
d5bb85a0 917 int i = m_CurIndex; // shortcut
91fa114d 918 bool found = false;
3912b3f8 919 wxString thepage;
8ec2b484 920
04dbb646 921 if (!m_Active)
4f9297b0 922 {
f35822af 923 // sanity check. Illegal use, but we'll try to prevent a crash anyway
50494a55 924 wxASSERT(m_Active);
91fa114d 925 return false;
8ec2b484
HH
926 }
927
8ec2b484 928 m_Name = wxEmptyString;
91fa114d
VS
929 m_CurItem = NULL;
930 thepage = m_Data->m_contents[i].page;
8ec2b484 931
b5a7b000
VS
932 m_Active = (++m_CurIndex < m_MaxIndex);
933 // check if it is same page with different anchor:
3912b3f8 934 if (!m_LastPage.empty())
b5a7b000 935 {
3912b3f8
VS
936 const wxChar *p1, *p2;
937 for (p1 = thepage.c_str(), p2 = m_LastPage.c_str();
b5a7b000
VS
938 *p1 != 0 && *p1 != _T('#') && *p1 == *p2; p1++, p2++) {}
939
940 m_LastPage = thepage;
941
942 if (*p1 == 0 || *p1 == _T('#'))
3912b3f8 943 return false;
b5a7b000
VS
944 }
945 else m_LastPage = thepage;
04dbb646 946
f35822af 947 wxFileSystem fsys;
91fa114d 948 file = fsys.OpenFile(m_Data->m_contents[i].book->GetFullPath(thepage));
04dbb646 949 if (file)
b5a7b000 950 {
2b5f62a0 951 if (m_Engine.Scan(*file))
468ae730 952 {
91fa114d
VS
953 m_Name = m_Data->m_contents[i].name;
954 m_CurItem = &m_Data->m_contents[i];
955 found = true;
d5bb85a0
VS
956 }
957 delete file;
8ec2b484 958 }
8ec2b484
HH
959 return found;
960}
961
d5bb85a0
VS
962
963
964
965
966
967
968
969//--------------------------------------------------------------------------------
2b5f62a0 970// wxHtmlSearchEngine
d5bb85a0
VS
971//--------------------------------------------------------------------------------
972
2b5f62a0 973void wxHtmlSearchEngine::LookFor(const wxString& keyword, bool case_sensitive, bool whole_words_only)
d5bb85a0 974{
c4971147
VS
975 m_CaseSensitive = case_sensitive;
976 m_WholeWords = whole_words_only;
3912b3f8 977 m_Keyword = keyword;
04dbb646 978
3444e4a8 979 if (!m_CaseSensitive)
3912b3f8 980 m_Keyword.LowerCase();
d5bb85a0
VS
981}
982
983
1a1dac18
MB
984static inline bool WHITESPACE(wxChar c)
985{
986 return c == _T(' ') || c == _T('\n') || c == _T('\r') || c == _T('\t');
987}
c4971147 988
2b5f62a0 989bool wxHtmlSearchEngine::Scan(const wxFSFile& file)
d5bb85a0 990{
3912b3f8 991 wxASSERT_MSG(!m_Keyword.empty(), wxT("wxHtmlSearchEngine::LookFor must be called before scanning!"));
d5bb85a0
VS
992
993 int i, j;
3912b3f8 994 int wrd = m_Keyword.Length();
d1da8872 995 bool found = false;
2b5f62a0
VZ
996 wxHtmlFilterHTML filter;
997 wxString tmp = filter.ReadFile(file);
eb37e1d2 998 int lng = tmp.length();
1a1dac18 999 const wxChar *buf = tmp.c_str();
d5bb85a0 1000
c4971147 1001 if (!m_CaseSensitive)
3912b3f8 1002 tmp.LowerCase();
d5bb85a0 1003
3912b3f8 1004 const wxChar *kwd = m_Keyword.c_str();
d1da8872 1005
c4971147
VS
1006 if (m_WholeWords)
1007 {
04dbb646
VZ
1008 for (i = 0; i < lng - wrd; i++)
1009 {
c4971147
VS
1010 if (WHITESPACE(buf[i])) continue;
1011 j = 0;
3912b3f8
VS
1012 while ((j < wrd) && (buf[i + j] == kwd[j])) j++;
1013 if (j == wrd && WHITESPACE(buf[i + j])) { found = true; break; }
c4971147
VS
1014 }
1015 }
04dbb646 1016
c4971147
VS
1017 else
1018 {
04dbb646
VZ
1019 for (i = 0; i < lng - wrd; i++)
1020 {
c4971147 1021 j = 0;
3912b3f8
VS
1022 while ((j < wrd) && (buf[i + j] == kwd[j])) j++;
1023 if (j == wrd) { found = true; break; }
c4971147 1024 }
d5bb85a0
VS
1025 }
1026
d5bb85a0
VS
1027 return found;
1028}
1029
d5bb85a0
VS
1030
1031
8ec2b484 1032#endif