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