]> git.saurik.com Git - wxWidgets.git/blame - src/html/helpdata.cpp
removed use of p2cstr
[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)
48
49//-----------------------------------------------------------------------------
50// static helper functions
51//-----------------------------------------------------------------------------
52
53// Reads one line, stores it into buf and returns pointer to new line or NULL.
1a1dac18 54static const wxChar* ReadLine(const wxChar *line, wxChar *buf, size_t bufsize)
8ec2b484 55{
1a1dac18
MB
56 wxChar *writeptr = buf;
57 wxChar *endptr = buf + bufsize - 1;
58 const wxChar *readptr = line;
8ec2b484 59
1a1dac18 60 while (*readptr != 0 && *readptr != _T('\r') && *readptr != _T('\n') &&
3dde6c72
VS
61 writeptr != endptr)
62 *(writeptr++) = *(readptr++);
8ec2b484 63 *writeptr = 0;
1a1dac18 64 while (*readptr == _T('\r') || *readptr == _T('\n'))
3dde6c72
VS
65 readptr++;
66 if (*readptr == 0)
67 return NULL;
68 else
69 return readptr;
8ec2b484
HH
70}
71
72
8ec2b484 73
90350682
VZ
74extern "C" int LINKAGEMODE
75wxHtmlHelpIndexCompareFunc(const void *a, const void *b)
8ec2b484 76{
25fd9bdf 77 return wxStricmp(((wxHtmlContentsItem*)a)->m_Name, ((wxHtmlContentsItem*)b)->m_Name);
8ec2b484
HH
78}
79
80
81//-----------------------------------------------------------------------------
82// HP_Parser
83//-----------------------------------------------------------------------------
84
85class HP_Parser : public wxHtmlParser
86{
211dfedd 87public:
fc7a2a60
VZ
88 HP_Parser() { }
89
211dfedd 90 wxObject* GetProduct() { return NULL; }
fc7a2a60 91
211dfedd
VS
92protected:
93 virtual void AddText(const wxChar* WXUNUSED(txt)) {}
fc7a2a60
VZ
94
95 DECLARE_NO_COPY_CLASS(HP_Parser)
8ec2b484
HH
96};
97
98
99//-----------------------------------------------------------------------------
100// HP_TagHandler
101//-----------------------------------------------------------------------------
102
103class HP_TagHandler : public wxHtmlTagHandler
104{
105 private:
106 wxString m_Name, m_Page;
107 int m_Level;
d5bb85a0 108 int m_ID;
8ec2b484
HH
109 int m_Index;
110 wxHtmlContentsItem *m_Items;
111 int m_ItemsCnt;
112 wxHtmlBookRecord *m_Book;
113
114 public:
04dbb646
VZ
115 HP_TagHandler(wxHtmlBookRecord *b) : wxHtmlTagHandler()
116 { m_Book = b; m_Items = NULL; m_ItemsCnt = 0; m_Name = m_Page = wxEmptyString;
117 m_Level = 0; m_ID = -1; }
66a77a74 118 wxString GetSupportedTags() { return wxT("UL,OBJECT,PARAM"); }
8ec2b484
HH
119 bool HandleTag(const wxHtmlTag& tag);
120 void WriteOut(wxHtmlContentsItem*& array, int& size);
121 void ReadIn(wxHtmlContentsItem* array, int size);
22f3361e
VZ
122
123 DECLARE_NO_COPY_CLASS(HP_TagHandler)
8ec2b484
HH
124};
125
126
127bool HP_TagHandler::HandleTag(const wxHtmlTag& tag)
128{
04dbb646 129 if (tag.GetName() == wxT("UL"))
4f9297b0 130 {
8ec2b484
HH
131 m_Level++;
132 ParseInner(tag);
133 m_Level--;
134 return TRUE;
135 }
04dbb646 136 else if (tag.GetName() == wxT("OBJECT"))
4f9297b0 137 {
8ec2b484
HH
138 m_Name = m_Page = wxEmptyString;
139 ParseInner(tag);
50494a55 140
daa084c2
VS
141#if 0
142 if (!m_Page.IsEmpty())
7df9fbc3 143 /* Valid HHW's file may contain only two object tags:
04dbb646 144
4157f43f
VS
145 <OBJECT type="text/site properties">
146 <param name="ImageType" value="Folder">
147 </OBJECT>
04dbb646 148
4157f43f 149 or
04dbb646
VZ
150
151 <OBJECT type="text/sitemap">
152 <param name="Name" value="main page">
153 <param name="Local" value="another.htm">
154 </OBJECT>
155
4157f43f
VS
156 We're interested in the latter. !m_Page.IsEmpty() is valid
157 condition because text/site properties does not contain Local param
158 */
daa084c2
VS
159#endif
160 if (tag.GetParam(wxT("TYPE")) == wxT("text/sitemap"))
4157f43f 161 {
daa084c2 162 if (m_ItemsCnt % wxHTML_REALLOC_STEP == 0)
04dbb646
VZ
163 m_Items = (wxHtmlContentsItem*) realloc(m_Items,
164 (m_ItemsCnt + wxHTML_REALLOC_STEP) *
daa084c2 165 sizeof(wxHtmlContentsItem));
e96360ef 166
4157f43f
VS
167 m_Items[m_ItemsCnt].m_Level = m_Level;
168 m_Items[m_ItemsCnt].m_ID = m_ID;
169 m_Items[m_ItemsCnt].m_Page = new wxChar[m_Page.Length() + 1];
170 wxStrcpy(m_Items[m_ItemsCnt].m_Page, m_Page.c_str());
171 m_Items[m_ItemsCnt].m_Name = new wxChar [m_Name.Length() + 1];
172 wxStrcpy(m_Items[m_ItemsCnt].m_Name, m_Name.c_str());
173 m_Items[m_ItemsCnt].m_Book = m_Book;
174 m_ItemsCnt++;
175 }
50494a55 176
8ec2b484
HH
177 return TRUE;
178 }
04dbb646 179 else
4f9297b0 180 { // "PARAM"
daa616fc 181 if (m_Name == wxEmptyString && tag.GetParam(wxT("NAME")) == wxT("Name"))
f6bcfd97 182 m_Name = tag.GetParam(wxT("VALUE"));
04dbb646 183 if (tag.GetParam(wxT("NAME")) == wxT("Local"))
daa616fc 184 m_Page = tag.GetParam(wxT("VALUE"));
04dbb646 185 if (tag.GetParam(wxT("NAME")) == wxT("ID"))
8bd72d90 186 tag.GetParamAsInt(wxT("VALUE"), &m_ID);
8ec2b484
HH
187 return FALSE;
188 }
189}
190
191
192
193void HP_TagHandler::WriteOut(wxHtmlContentsItem*& array, int& size)
194{
195 array = m_Items;
196 size = m_ItemsCnt;
197 m_Items = NULL;
198 m_ItemsCnt = 0;
199}
200
201void HP_TagHandler::ReadIn(wxHtmlContentsItem* array, int size)
202{
203 m_Items = array;
204 m_ItemsCnt = size;
205}
206
d5bb85a0
VS
207
208
209
8ec2b484
HH
210//-----------------------------------------------------------------------------
211// wxHtmlHelpData
212//-----------------------------------------------------------------------------
213
468ae730
VS
214wxString wxHtmlBookRecord::GetFullPath(const wxString &page) const
215{
216 if (wxIsAbsolutePath(page))
217 return page;
218 else
219 return m_BasePath + page;
220}
221
222
223
8ec2b484
HH
224IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpData, wxObject)
225
f42b1601 226wxHtmlHelpData::wxHtmlHelpData()
8ec2b484
HH
227{
228 m_TempPath = wxEmptyString;
229
230 m_Contents = NULL;
231 m_ContentsCnt = 0;
232 m_Index = NULL;
233 m_IndexCnt = 0;
234}
235
236wxHtmlHelpData::~wxHtmlHelpData()
237{
238 int i;
239
240 m_BookRecords.Empty();
04dbb646 241 if (m_Contents)
4f9297b0 242 {
04dbb646
VZ
243 for (i = 0; i < m_ContentsCnt; i++)
244 {
8ec2b484
HH
245 delete[] m_Contents[i].m_Page;
246 delete[] m_Contents[i].m_Name;
247 }
248 free(m_Contents);
249 }
04dbb646 250 if (m_Index)
4f9297b0 251 {
04dbb646
VZ
252 for (i = 0; i < m_IndexCnt; i++)
253 {
8ec2b484
HH
254 delete[] m_Index[i].m_Page;
255 delete[] m_Index[i].m_Name;
256 }
257 free(m_Index);
258 }
259}
260
261bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord *book, wxFileSystem& fsys, const wxString& indexfile, const wxString& contentsfile)
262{
263 wxFSFile *f;
2b5f62a0 264 wxHtmlFilterHTML filter;
eb37e1d2 265 wxString buf;
8ec2b484 266 wxString string;
f42b1601 267
8ec2b484
HH
268 HP_Parser parser;
269 HP_TagHandler *handler = new HP_TagHandler(book);
270 parser.AddTagHandler(handler);
271
dc1efb1d 272 f = ( contentsfile.IsEmpty() ? (wxFSFile*) NULL : fsys.OpenFile(contentsfile) );
04dbb646 273 if (f)
4f9297b0 274 {
eb37e1d2 275 buf.clear();
2b5f62a0 276 buf = filter.ReadFile(*f);
8ec2b484 277 delete f;
4f9297b0 278 handler->ReadIn(m_Contents, m_ContentsCnt);
8ec2b484 279 parser.Parse(buf);
4f9297b0 280 handler->WriteOut(m_Contents, m_ContentsCnt);
8ec2b484 281 }
f3c82859 282 else
2b5f62a0 283 {
f6bcfd97 284 wxLogError(_("Cannot open contents file: %s"), contentsfile.c_str());
2b5f62a0 285 }
8ec2b484 286
dc1efb1d 287 f = ( indexfile.IsEmpty() ? (wxFSFile*) NULL : fsys.OpenFile(indexfile) );
04dbb646 288 if (f)
4f9297b0 289 {
eb37e1d2 290 buf.clear();
2b5f62a0 291 buf = filter.ReadFile(*f);
8ec2b484 292 delete f;
4f9297b0 293 handler->ReadIn(m_Index, m_IndexCnt);
8ec2b484 294 parser.Parse(buf);
4f9297b0 295 handler->WriteOut(m_Index, m_IndexCnt);
8ec2b484 296 }
b19d5e56 297 else if (!indexfile.IsEmpty())
2b5f62a0 298 {
f6bcfd97 299 wxLogError(_("Cannot open index file: %s"), indexfile.c_str());
2b5f62a0 300 }
8ec2b484
HH
301 return TRUE;
302}
303
304
f35822af 305
4fd5055c
VS
306inline static void CacheWriteInt32(wxOutputStream *f, wxInt32 value)
307{
308 wxInt32 x = wxINT32_SWAP_ON_BE(value);
309 f->Write(&x, sizeof(x));
310}
f35822af 311
4fd5055c
VS
312inline static wxInt32 CacheReadInt32(wxInputStream *f)
313{
314 wxInt32 x;
315 f->Read(&x, sizeof(x));
316 return wxINT32_SWAP_ON_BE(x);
317}
f35822af 318
4fd5055c 319inline static void CacheWriteString(wxOutputStream *f, const wxChar *str)
2b5f62a0
VZ
320{
321#if wxUSE_UNICODE
322 wxWX2MBbuf mbstr(wxConvUTF8.cWX2MB(str));
323#else
324 const wxChar *mbstr = str;
325#endif
326 size_t len = strlen(mbstr)+1;
4fd5055c 327 CacheWriteInt32(f, len);
2b5f62a0 328 f->Write(mbstr, len);
4fd5055c 329}
f35822af 330
2b5f62a0 331inline static wxChar* CacheReadString(wxInputStream *f)
4fd5055c 332{
2b5f62a0 333 char *str;
4fd5055c 334 size_t len = (size_t)CacheReadInt32(f);
2b5f62a0
VZ
335 str = new char[len];
336 f->Read(str, len);
337#if !wxUSE_UNICODE
4fd5055c 338 return str;
2b5f62a0
VZ
339#else
340 wxMB2WXbuf wxstr(wxConvUTF8.cMB2WX(str));
341 wxChar *outstr = new wxChar[wxStrlen(wxstr)+1];
342 wxStrcpy(outstr, wxstr);
343 return outstr;
344#endif
4fd5055c 345}
f35822af 346
d1a9c82b
VS
347#define CURRENT_CACHED_BOOK_VERSION 4
348
349// Additional flags to detect incompatibilities of the runtime environment:
350#define CACHED_BOOK_FORMAT_FLAGS \
351 (wxUSE_UNICODE << 0)
352
f35822af 353
8ec2b484
HH
354bool wxHtmlHelpData::LoadCachedBook(wxHtmlBookRecord *book, wxInputStream *f)
355{
356 int i, st;
f35822af
VS
357 wxInt32 version;
358
359 /* load header - version info : */
4fd5055c 360 version = CacheReadInt32(f);
2b5f62a0 361
04dbb646 362 if (version != CURRENT_CACHED_BOOK_VERSION)
f3c82859 363 {
4fd5055c
VS
364 // NB: We can just silently return FALSE here and don't worry about
365 // it anymore, because AddBookParam will load the MS project in
366 // absence of (properly versioned) .cached file and automatically
367 // create new .cached file immediately afterward.
f3c82859 368 return FALSE;
f3c82859 369 }
04dbb646 370
d1a9c82b
VS
371 if (CacheReadInt32(f) != CACHED_BOOK_FORMAT_FLAGS)
372 return FALSE;
373
8ec2b484 374 /* load contents : */
8ec2b484 375 st = m_ContentsCnt;
4fd5055c 376 m_ContentsCnt += CacheReadInt32(f);
04dbb646
VZ
377 m_Contents = (wxHtmlContentsItem*) realloc(m_Contents,
378 (m_ContentsCnt / wxHTML_REALLOC_STEP + 1) *
f35822af 379 wxHTML_REALLOC_STEP * sizeof(wxHtmlContentsItem));
04dbb646 380 for (i = st; i < m_ContentsCnt; i++)
4f9297b0 381 {
4fd5055c
VS
382 m_Contents[i].m_Level = CacheReadInt32(f);
383 m_Contents[i].m_ID = CacheReadInt32(f);
384 m_Contents[i].m_Name = CacheReadString(f);
385 m_Contents[i].m_Page = CacheReadString(f);
8ec2b484
HH
386 m_Contents[i].m_Book = book;
387 }
388
389 /* load index : */
8ec2b484 390 st = m_IndexCnt;
4fd5055c 391 m_IndexCnt += CacheReadInt32(f);
04dbb646 392 m_Index = (wxHtmlContentsItem*) realloc(m_Index, (m_IndexCnt / wxHTML_REALLOC_STEP + 1) *
f35822af 393 wxHTML_REALLOC_STEP * sizeof(wxHtmlContentsItem));
04dbb646 394 for (i = st; i < m_IndexCnt; i++)
4f9297b0 395 {
4fd5055c
VS
396 m_Index[i].m_Name = CacheReadString(f);
397 m_Index[i].m_Page = CacheReadString(f);
8ec2b484
HH
398 m_Index[i].m_Book = book;
399 }
400 return TRUE;
401}
402
403
404bool wxHtmlHelpData::SaveCachedBook(wxHtmlBookRecord *book, wxOutputStream *f)
405{
406 int i;
4fd5055c 407 wxInt32 cnt;
f35822af
VS
408
409 /* save header - version info : */
4fd5055c 410 CacheWriteInt32(f, CURRENT_CACHED_BOOK_VERSION);
d1a9c82b 411 CacheWriteInt32(f, CACHED_BOOK_FORMAT_FLAGS);
8ec2b484
HH
412
413 /* save contents : */
4fd5055c
VS
414 for (cnt = 0, i = 0; i < m_ContentsCnt; i++)
415 if (m_Contents[i].m_Book == book && m_Contents[i].m_Level > 0)
416 cnt++;
417 CacheWriteInt32(f, cnt);
8ec2b484 418
04dbb646 419 for (i = 0; i < m_ContentsCnt; i++)
4f9297b0 420 {
4fd5055c
VS
421 if (m_Contents[i].m_Book != book || m_Contents[i].m_Level == 0)
422 continue;
423 CacheWriteInt32(f, m_Contents[i].m_Level);
424 CacheWriteInt32(f, m_Contents[i].m_ID);
425 CacheWriteString(f, m_Contents[i].m_Name);
426 CacheWriteString(f, m_Contents[i].m_Page);
8ec2b484
HH
427 }
428
429 /* save index : */
4fd5055c
VS
430 for (cnt = 0, i = 0; i < m_IndexCnt; i++)
431 if (m_Index[i].m_Book == book && m_Index[i].m_Level > 0)
432 cnt++;
433 CacheWriteInt32(f, cnt);
8ec2b484 434
04dbb646 435 for (i = 0; i < m_IndexCnt; i++)
4f9297b0 436 {
4fd5055c
VS
437 if (m_Index[i].m_Book != book || m_Index[i].m_Level == 0)
438 continue;
439 CacheWriteString(f, m_Index[i].m_Name);
440 CacheWriteString(f, m_Index[i].m_Page);
8ec2b484
HH
441 }
442 return TRUE;
443}
444
445
446void wxHtmlHelpData::SetTempDir(const wxString& path)
447{
448 if (path == wxEmptyString) m_TempPath = path;
04dbb646 449 else
4f9297b0 450 {
d5bb85a0 451 if (wxIsAbsolutePath(path)) m_TempPath = path;
f35822af 452 else m_TempPath = wxGetCwd() + _T("/") + path;
8ec2b484 453
f35822af
VS
454 if (m_TempPath[m_TempPath.Length() - 1] != _T('/'))
455 m_TempPath << _T('/');
8ec2b484
HH
456 }
457}
458
459
29e60597
VS
460
461static wxString SafeFileName(const wxString& s)
462{
463 wxString res(s);
464 res.Replace(wxT("#"), wxT("_"));
465 res.Replace(wxT(":"), wxT("_"));
466 res.Replace(wxT("\\"), wxT("_"));
467 res.Replace(wxT("/"), wxT("_"));
468 return res;
469}
470
f35822af 471bool wxHtmlHelpData::AddBookParam(const wxFSFile& bookfile,
f890e2d4 472 wxFontEncoding encoding,
f35822af 473 const wxString& title, const wxString& contfile,
d5bb85a0
VS
474 const wxString& indexfile, const wxString& deftopic,
475 const wxString& path)
8ec2b484
HH
476{
477 wxFileSystem fsys;
478 wxFSFile *fi;
479 wxHtmlBookRecord *bookr;
04dbb646 480
f890e2d4
VS
481 int IndexOld = m_IndexCnt,
482 ContentsOld = m_ContentsCnt;
f42b1601 483
5ecdcaa7 484 if (!path.IsEmpty())
d5bb85a0 485 fsys.ChangePathTo(path, TRUE);
8ec2b484 486
5ecdcaa7
VS
487 size_t booksCnt = m_BookRecords.GetCount();
488 for (size_t i = 0; i < booksCnt; i++)
489 {
490 if ( m_BookRecords[i].GetBookFile() == bookfile.GetLocation() )
491 return TRUE; // book is (was) loaded
492 }
493
494 bookr = new wxHtmlBookRecord(bookfile.GetLocation(), fsys.GetPath(), title, deftopic);
fc799548 495
efba2b89
VS
496 if (m_ContentsCnt % wxHTML_REALLOC_STEP == 0)
497 m_Contents = (wxHtmlContentsItem*) realloc(m_Contents, (m_ContentsCnt + wxHTML_REALLOC_STEP) * sizeof(wxHtmlContentsItem));
8ec2b484
HH
498 m_Contents[m_ContentsCnt].m_Level = 0;
499 m_Contents[m_ContentsCnt].m_ID = 0;
66a77a74
OK
500 m_Contents[m_ContentsCnt].m_Page = new wxChar[deftopic.Length() + 1];
501 wxStrcpy(m_Contents[m_ContentsCnt].m_Page, deftopic.c_str());
502 m_Contents[m_ContentsCnt].m_Name = new wxChar [title.Length() + 1];
503 wxStrcpy(m_Contents[m_ContentsCnt].m_Name, title.c_str());
8ec2b484
HH
504 m_Contents[m_ContentsCnt].m_Book = bookr;
505
506 // store the contents index for later
507 int cont_start = m_ContentsCnt++;
508
509 // Try to find cached binary versions:
f35822af
VS
510 // 1. save file as book, but with .hhp.cached extension
511 // 2. same as 1. but in temp path
512 // 3. otherwise or if cache load failed, load it from MS.
04dbb646 513
f35822af 514 fi = fsys.OpenFile(bookfile.GetLocation() + wxT(".cached"));
04dbb646
VZ
515
516 if (fi == NULL ||
e2b87f38 517#if wxUSE_DATETIME
04dbb646 518 fi->GetModificationTime() < bookfile.GetModificationTime() ||
e2b87f38 519#endif // wxUSE_DATETIME
4f9297b0 520 !LoadCachedBook(bookr, fi->GetStream()))
f35822af
VS
521 {
522 if (fi != NULL) delete fi;
523 fi = fsys.OpenFile(m_TempPath + wxFileNameFromPath(bookfile.GetLocation()) + wxT(".cached"));
04dbb646 524 if (m_TempPath == wxEmptyString || fi == NULL ||
e2b87f38 525#if wxUSE_DATETIME
04dbb646 526 fi->GetModificationTime() < bookfile.GetModificationTime() ||
e2b87f38 527#endif // wxUSE_DATETIME
4f9297b0 528 !LoadCachedBook(bookr, fi->GetStream()))
f35822af
VS
529 {
530 LoadMSProject(bookr, fsys, indexfile, contfile);
04dbb646 531 if (m_TempPath != wxEmptyString)
f35822af 532 {
04dbb646 533 wxFileOutputStream *outs = new wxFileOutputStream(m_TempPath +
29e60597 534 SafeFileName(wxFileNameFromPath(bookfile.GetLocation())) + wxT(".cached"));
f35822af
VS
535 SaveCachedBook(bookr, outs);
536 delete outs;
537 }
d5bb85a0 538 }
8ec2b484 539 }
04dbb646 540
f35822af 541 if (fi != NULL) delete fi;
8ec2b484
HH
542
543 // Now store the contents range
544 bookr->SetContentsRange(cont_start, m_ContentsCnt);
04dbb646 545
0b4f47a3 546#if wxUSE_FONTMAP
f890e2d4
VS
547 // Convert encoding, if neccessary:
548 if (encoding != wxFONTENCODING_SYSTEM)
549 {
550 wxFontEncodingArray a = wxEncodingConverter::GetPlatformEquivalents(encoding);
551 if (a.GetCount() != 0 && a[0] != encoding)
552 {
553 int i;
554 wxEncodingConverter conv;
555 conv.Init(encoding, a[0]);
04dbb646 556
f890e2d4
VS
557 for (i = IndexOld; i < m_IndexCnt; i++)
558 conv.Convert(m_Index[i].m_Name);
559 for (i = ContentsOld; i < m_ContentsCnt; i++)
560 conv.Convert(m_Contents[i].m_Name);
561 }
562 }
0b4f47a3
DS
563#else
564 wxUnusedVar(IndexOld);
565 wxUnusedVar(ContentsOld);
566 wxASSERT_MSG(encoding == wxFONTENCODING_SYSTEM, wxT("Encoding can't be converted"));
567#endif
8ec2b484
HH
568
569 m_BookRecords.Add(bookr);
570 if (m_IndexCnt > 0)
90350682 571 qsort(m_Index, m_IndexCnt, sizeof(wxHtmlContentsItem), wxHtmlHelpIndexCompareFunc);
f42b1601 572
8ec2b484
HH
573 return TRUE;
574}
575
576
577bool wxHtmlHelpData::AddBook(const wxString& book)
578{
3527f29c
VS
579 wxString extension(book.Right(4).Lower());
580 if (extension == wxT(".zip") ||
581#if wxUSE_LIBMSPACK
582 extension == wxT(".chm") /*compressed html help book*/ ||
583#endif
584 extension == wxT(".htb") /*html book*/)
68364659
VS
585 {
586 wxFileSystem fsys;
587 wxString s;
588 bool rt = FALSE;
589
3527f29c
VS
590#if wxUSE_LIBMSPACK
591 if (extension == wxT(".chm"))
592 s = fsys.FindFirst(book + wxT("#chm:*.hhp"), wxFILE);
593 else
594#endif
595 s = fsys.FindFirst(book + wxT("#zip:*.hhp"), wxFILE);
596
04dbb646 597 while (!s.IsEmpty())
68364659
VS
598 {
599 if (AddBook(s)) rt = TRUE;
600 s = fsys.FindNext();
601 }
04dbb646 602
68364659
VS
603 return rt;
604 }
68364659 605
a509f830
VZ
606 wxFSFile *fi;
607 wxFileSystem fsys;
3dde6c72 608
a509f830
VZ
609 wxString title = _("noname"),
610 safetitle,
611 start = wxEmptyString,
612 contents = wxEmptyString,
613 index = wxEmptyString,
614 charset = wxEmptyString;
68364659 615
a509f830
VZ
616 fi = fsys.OpenFile(book);
617 if (fi == NULL)
618 {
619 wxLogError(_("Cannot open HTML help book: %s"), book.c_str());
620 return FALSE;
68364659 621 }
a509f830
VZ
622 fsys.ChangePathTo(book);
623
624 const wxChar *lineptr;
625 wxChar linebuf[300];
626 wxString tmp;
627 wxHtmlFilterPlainText filter;
628 tmp = filter.ReadFile(*fi);
629 lineptr = tmp.c_str();
630
631 do
632 {
633 lineptr = ReadLine(lineptr, linebuf, 300);
634
635 for (wxChar *ch = linebuf; *ch != wxT('\0') && *ch != wxT('='); ch++)
636 *ch = tolower(*ch);
637
638 if (wxStrstr(linebuf, _T("title=")) == linebuf)
639 title = linebuf + wxStrlen(_T("title="));
640 if (wxStrstr(linebuf, _T("default topic=")) == linebuf)
641 start = linebuf + wxStrlen(_T("default topic="));
642 if (wxStrstr(linebuf, _T("index file=")) == linebuf)
643 index = linebuf + wxStrlen(_T("index file="));
644 if (wxStrstr(linebuf, _T("contents file=")) == linebuf)
645 contents = linebuf + wxStrlen(_T("contents file="));
646 if (wxStrstr(linebuf, _T("charset=")) == linebuf)
647 charset = linebuf + wxStrlen(_T("charset="));
648 } while (lineptr != NULL);
649
650 wxFontEncoding enc;
651 if (charset == wxEmptyString) enc = wxFONTENCODING_SYSTEM;
652 else enc = wxFontMapper::Get()->CharsetToEncoding(charset);
653 bool rtval = AddBookParam(*fi, enc,
654 title, contents, index, start, fsys.GetPath());
655 delete fi;
656 return rtval;
8ec2b484
HH
657}
658
659wxString wxHtmlHelpData::FindPageByName(const wxString& x)
660{
661 int cnt;
662 int i;
663 wxFileSystem fsys;
664 wxFSFile *f;
665 wxString url(wxEmptyString);
666
667 /* 1. try to open given file: */
668
669 cnt = m_BookRecords.GetCount();
04dbb646 670 for (i = 0; i < cnt; i++)
4f9297b0 671 {
468ae730 672 f = fsys.OpenFile(m_BookRecords[i].GetFullPath(x));
04dbb646
VZ
673 if (f)
674 {
468ae730 675 url = m_BookRecords[i].GetFullPath(x);
8ec2b484
HH
676 delete f;
677 return url;
678 }
679 }
680
681
682 /* 2. try to find a book: */
683
04dbb646 684 for (i = 0; i < cnt; i++)
4f9297b0 685 {
04dbb646
VZ
686 if (m_BookRecords[i].GetTitle() == x)
687 {
468ae730 688 url = m_BookRecords[i].GetFullPath(m_BookRecords[i].GetStart());
8ec2b484
HH
689 return url;
690 }
691 }
692
693 /* 3. try to find in contents: */
694
695 cnt = m_ContentsCnt;
04dbb646 696 for (i = 0; i < cnt; i++)
4f9297b0 697 {
04dbb646
VZ
698 if (wxStrcmp(m_Contents[i].m_Name, x) == 0)
699 {
468ae730 700 url = m_Contents[i].GetFullPath();
8ec2b484
HH
701 return url;
702 }
703 }
704
705
706 /* 4. try to find in index: */
707
708 cnt = m_IndexCnt;
04dbb646 709 for (i = 0; i < cnt; i++)
4f9297b0 710 {
04dbb646
VZ
711 if (wxStrcmp(m_Index[i].m_Name, x) == 0)
712 {
468ae730 713 url = m_Index[i].GetFullPath();
8ec2b484
HH
714 return url;
715 }
716 }
717
718 return url;
719}
720
721wxString wxHtmlHelpData::FindPageById(int id)
f42b1601 722{
8ec2b484
HH
723 int i;
724 wxString url(wxEmptyString);
725
04dbb646 726 for (i = 0; i < m_ContentsCnt; i++)
4f9297b0 727 {
04dbb646
VZ
728 if (m_Contents[i].m_ID == id)
729 {
468ae730 730 url = m_Contents[i].GetFullPath();
8ec2b484
HH
731 return url;
732 }
733 }
734
735 return url;
736}
737
738//----------------------------------------------------------------------------------
739// wxHtmlSearchStatus functions
740//----------------------------------------------------------------------------------
741
742wxHtmlSearchStatus::wxHtmlSearchStatus(wxHtmlHelpData* data, const wxString& keyword,
c4971147 743 bool case_sensitive, bool whole_words_only,
d5bb85a0 744 const wxString& book)
8ec2b484
HH
745{
746 m_Data = data;
747 m_Keyword = keyword;
748 wxHtmlBookRecord* bookr = NULL;
04dbb646 749 if (book != wxEmptyString)
4f9297b0 750 {
d5bb85a0
VS
751 // we have to search in a specific book. Find it first
752 int i, cnt = data->m_BookRecords.GetCount();
753 for (i = 0; i < cnt; i++)
04dbb646
VZ
754 if (data->m_BookRecords[i].GetTitle() == book)
755 {
d5bb85a0
VS
756 bookr = &(data->m_BookRecords[i]);
757 m_CurIndex = bookr->GetContentsStart();
758 m_MaxIndex = bookr->GetContentsEnd();
759 break;
760 }
761 // check; we won't crash if the book doesn't exist, but it's Bad Anyway.
762 wxASSERT(bookr);
8ec2b484 763 }
04dbb646 764 if (! bookr)
4f9297b0 765 {
d5bb85a0
VS
766 // no book specified; search all books
767 m_CurIndex = 0;
768 m_MaxIndex = m_Data->m_ContentsCnt;
8ec2b484 769 }
c4971147 770 m_Engine.LookFor(keyword, case_sensitive, whole_words_only);
8ec2b484 771 m_Active = (m_CurIndex < m_MaxIndex);
b5a7b000 772 m_LastPage = NULL;
8ec2b484
HH
773}
774
775bool wxHtmlSearchStatus::Search()
776{
8ec2b484 777 wxFSFile *file;
d5bb85a0 778 int i = m_CurIndex; // shortcut
8ec2b484 779 bool found = FALSE;
b5a7b000 780 wxChar *thepage;
8ec2b484 781
04dbb646 782 if (!m_Active)
4f9297b0 783 {
f35822af 784 // sanity check. Illegal use, but we'll try to prevent a crash anyway
50494a55 785 wxASSERT(m_Active);
50494a55 786 return FALSE;
8ec2b484
HH
787 }
788
8ec2b484 789 m_Name = wxEmptyString;
b5a7b000
VS
790 m_ContentsItem = NULL;
791 thepage = m_Data->m_Contents[i].m_Page;
8ec2b484 792
b5a7b000
VS
793 m_Active = (++m_CurIndex < m_MaxIndex);
794 // check if it is same page with different anchor:
795 if (m_LastPage != NULL)
796 {
797 wxChar *p1, *p2;
04dbb646 798 for (p1 = thepage, p2 = m_LastPage;
b5a7b000
VS
799 *p1 != 0 && *p1 != _T('#') && *p1 == *p2; p1++, p2++) {}
800
801 m_LastPage = thepage;
802
803 if (*p1 == 0 || *p1 == _T('#'))
804 return FALSE;
805 }
806 else m_LastPage = thepage;
04dbb646 807
f35822af 808 wxFileSystem fsys;
468ae730 809 file = fsys.OpenFile(m_Data->m_Contents[i].m_Book->GetFullPath(thepage));
04dbb646 810 if (file)
b5a7b000 811 {
2b5f62a0 812 if (m_Engine.Scan(*file))
468ae730 813 {
b5a7b000
VS
814 m_Name = m_Data->m_Contents[i].m_Name;
815 m_ContentsItem = m_Data->m_Contents + i;
816 found = TRUE;
d5bb85a0
VS
817 }
818 delete file;
8ec2b484 819 }
8ec2b484
HH
820 return found;
821}
822
d5bb85a0
VS
823
824
825
826
827
828
829
830//--------------------------------------------------------------------------------
2b5f62a0 831// wxHtmlSearchEngine
d5bb85a0
VS
832//--------------------------------------------------------------------------------
833
2b5f62a0 834void wxHtmlSearchEngine::LookFor(const wxString& keyword, bool case_sensitive, bool whole_words_only)
d5bb85a0 835{
c4971147
VS
836 m_CaseSensitive = case_sensitive;
837 m_WholeWords = whole_words_only;
d5bb85a0 838 if (m_Keyword) delete[] m_Keyword;
66a77a74
OK
839 m_Keyword = new wxChar[keyword.Length() + 1];
840 wxStrcpy(m_Keyword, keyword.c_str());
04dbb646 841
c4971147 842 if (!m_CaseSensitive)
4f9297b0 843 {
c4971147 844 for (int i = wxStrlen(m_Keyword) - 1; i >= 0; i--)
04dbb646 845 {
c4971147
VS
846 if ((m_Keyword[i] >= wxT('A')) && (m_Keyword[i] <= wxT('Z')))
847 m_Keyword[i] += wxT('a') - wxT('A');
04dbb646
VZ
848 }
849 }
d5bb85a0
VS
850}
851
852
1a1dac18
MB
853static inline bool WHITESPACE(wxChar c)
854{
855 return c == _T(' ') || c == _T('\n') || c == _T('\r') || c == _T('\t');
856}
c4971147 857
2b5f62a0 858bool wxHtmlSearchEngine::Scan(const wxFSFile& file)
d5bb85a0 859{
2b5f62a0 860 wxASSERT_MSG(m_Keyword != NULL, wxT("wxHtmlSearchEngine::LookFor must be called before scanning!"));
d5bb85a0
VS
861
862 int i, j;
66a77a74 863 int wrd = wxStrlen(m_Keyword);
d5bb85a0 864 bool found = FALSE;
2b5f62a0
VZ
865 wxHtmlFilterHTML filter;
866 wxString tmp = filter.ReadFile(file);
eb37e1d2 867 int lng = tmp.length();
1a1dac18 868 const wxChar *buf = tmp.c_str();
d5bb85a0 869
c4971147
VS
870 if (!m_CaseSensitive)
871 for (i = 0; i < lng; i++)
1a1dac18 872 tmp[(size_t)i] = (wxChar)wxTolower(tmp[(size_t)i]);
d5bb85a0 873
c4971147
VS
874 if (m_WholeWords)
875 {
04dbb646
VZ
876 for (i = 0; i < lng - wrd; i++)
877 {
c4971147
VS
878 if (WHITESPACE(buf[i])) continue;
879 j = 0;
880 while ((j < wrd) && (buf[i + j] == m_Keyword[j])) j++;
4f9297b0 881 if (j == wrd && WHITESPACE(buf[i + j])) { found = TRUE; break; }
c4971147
VS
882 }
883 }
04dbb646 884
c4971147
VS
885 else
886 {
04dbb646
VZ
887 for (i = 0; i < lng - wrd; i++)
888 {
c4971147
VS
889 j = 0;
890 while ((j < wrd) && (buf[i + j] == m_Keyword[j])) j++;
4f9297b0 891 if (j == wrd) { found = TRUE; break; }
c4971147 892 }
d5bb85a0
VS
893 }
894
d5bb85a0
VS
895 return found;
896}
897
d5bb85a0
VS
898
899
8ec2b484 900#endif