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