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