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