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