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