Fix/hack 1.
[wxWidgets.git] / src / html / helpdata.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: helpdata.cpp
3 // Purpose: wxHtmlHelpData
4 // Notes: Based on htmlhelp.cpp, implementing a monolithic
5 // HTML Help controller class, by Vaclav Slavik
6 // Author: Harm van der Heijden and Vaclav Slavik
7 // RCS-ID: $Id$
8 // Copyright: (c) Harm van der Heijden and Vaclav Slavik
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation
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
25 #if wxUSE_HTML && wxUSE_STREAMS
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"
35 #include "wx/encconv.h"
36 #include "wx/fontmap.h"
37 #include "wx/log.h"
38 #include "wx/html/htmlpars.h"
39 #include "wx/html/htmldefs.h"
40
41 #include "wx/arrimpl.cpp"
42 WX_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.
49 static 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
61
62 static int LINKAGEMODE IndexCompareFunc(const void *a, const void *b)
63 {
64 return wxStrcmp(((wxHtmlContentsItem*)a)->m_Name, ((wxHtmlContentsItem*)b)->m_Name);
65 }
66
67
68 //-----------------------------------------------------------------------------
69 // HP_Parser
70 //-----------------------------------------------------------------------------
71
72 class HP_Parser : public wxHtmlParser
73 {
74 public:
75 void AddText(const char* WXUNUSED(text)) { }
76 wxObject* GetProduct() { return NULL; }
77 };
78
79
80 //-----------------------------------------------------------------------------
81 // HP_TagHandler
82 //-----------------------------------------------------------------------------
83
84 class HP_TagHandler : public wxHtmlTagHandler
85 {
86 private:
87 wxString m_Name, m_Page;
88 int m_Level;
89 int m_ID;
90 int m_Index;
91 wxHtmlContentsItem *m_Items;
92 int m_ItemsCnt;
93 wxHtmlBookRecord *m_Book;
94 bool m_firstTime; // For checking if we're adding sections at level zero, so we 'delete' the first one
95
96 public:
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; m_firstTime = TRUE; }
100 wxString GetSupportedTags() { return wxT("UL,OBJECT,PARAM"); }
101 bool HandleTag(const wxHtmlTag& tag);
102 void WriteOut(wxHtmlContentsItem*& array, int& size);
103 void ReadIn(wxHtmlContentsItem* array, int size);
104 };
105
106
107 bool HP_TagHandler::HandleTag(const wxHtmlTag& tag)
108 {
109 if (tag.GetName() == wxT("UL"))
110 {
111 m_Level++;
112 ParseInner(tag);
113 m_Level--;
114 return TRUE;
115 }
116 else if (tag.GetName() == wxT("OBJECT"))
117 {
118 m_Name = m_Page = wxEmptyString;
119 ParseInner(tag);
120
121 if (tag.GetParam("TYPE") == "text/sitemap")
122
123 // if (!m_Page.IsEmpty())
124 /* Valid HHW's file may contain only two object tags:
125
126 <OBJECT type="text/site properties">
127 <param name="ImageType" value="Folder">
128 </OBJECT>
129
130 or
131
132 <OBJECT type="text/sitemap">
133 <param name="Name" value="main page">
134 <param name="Local" value="another.htm">
135 </OBJECT>
136
137 We're interested in the latter. !m_Page.IsEmpty() is valid
138 condition because text/site properties does not contain Local param
139 */
140 {
141 // We're reading in items at level zero, which must mean we want to specify
142 // our own 'books', so chuck out the first (empty) one that AddBook already
143 // created
144 if (m_firstTime && (m_Level == 0) && (m_ItemsCnt > 0))
145 {
146 m_ItemsCnt --;
147 }
148 else
149 {
150 if (m_ItemsCnt % wxHTML_REALLOC_STEP == 0)
151 m_Items = (wxHtmlContentsItem*) realloc(m_Items, (m_ItemsCnt + wxHTML_REALLOC_STEP) * sizeof(wxHtmlContentsItem));
152 }
153 m_Items[m_ItemsCnt].m_Level = m_Level;
154 m_Items[m_ItemsCnt].m_ID = m_ID;
155 m_Items[m_ItemsCnt].m_Page = new wxChar[m_Page.Length() + 1];
156 wxStrcpy(m_Items[m_ItemsCnt].m_Page, m_Page.c_str());
157 m_Items[m_ItemsCnt].m_Name = new wxChar [m_Name.Length() + 1];
158 wxStrcpy(m_Items[m_ItemsCnt].m_Name, m_Name.c_str());
159 m_Items[m_ItemsCnt].m_Book = m_Book;
160 m_ItemsCnt++;
161
162 m_firstTime = FALSE;
163 }
164
165 return TRUE;
166 }
167 else
168 { // "PARAM"
169 if (m_Name == wxEmptyString && tag.GetParam(wxT("NAME")) == wxT("Name"))
170 {
171 m_Name = tag.GetParam(wxT("VALUE"));
172 if (m_Name.Find(wxT('&')) != -1)
173 {
174 #define ESCSEQ(escape, subst) \
175 { _T("&") _T(escape) _T(";"), _T("&") _T(escape) _T(" "), _T(subst) }
176 static wxChar* substitutions[][3] =
177 {
178 ESCSEQ("quot", "\""),
179 ESCSEQ("lt", "<"),
180 ESCSEQ("gt", ">"),
181
182 ESCSEQ("nbsp", " "),
183 ESCSEQ("iexcl", "!"),
184 ESCSEQ("cent", "\242"/* ¢ */),
185
186 ESCSEQ("yen", " "),
187 ESCSEQ("brkbar", " "),
188 ESCSEQ("sect", " "),
189 ESCSEQ("uml", " "),
190
191 ESCSEQ("copy", "(c)"),
192 ESCSEQ("ordf", " "),
193 ESCSEQ("laquo", " "),
194 ESCSEQ("not", " "),
195
196 ESCSEQ("reg", "(r)"),
197
198 ESCSEQ("deg", " "),
199 ESCSEQ("plusm", " "),
200
201 ESCSEQ("acute", " "),
202 ESCSEQ("micro", " "),
203 ESCSEQ("para", " "),
204
205 ESCSEQ("ordm", " "),
206 ESCSEQ("raquo", " "),
207
208 ESCSEQ("iquest", " "),
209 ESCSEQ("Agrave", "\300"/* À */),
210
211 ESCSEQ("Acirc", "\302"/* Â */),
212 ESCSEQ("Atilde", "\303"/* Ã */),
213 ESCSEQ("Auml", "\304"/* Ä */),
214 ESCSEQ("Aring", " "),
215 ESCSEQ("AElig", " "),
216 ESCSEQ("Ccedil", "\347"/* ç */),
217 ESCSEQ("Egrave", "\310"/* È */),
218 ESCSEQ("Eacute", "\311"/* É */),
219 ESCSEQ("Ecirc", "\312"/* Ê */),
220 ESCSEQ("Euml", "\313"/* Ë */),
221 ESCSEQ("Igrave", "\314"/* Ì */),
222
223 ESCSEQ("Icirc", "\316"/* Î */),
224 ESCSEQ("Iuml", "\317"/* Ï */),
225
226 ESCSEQ("Ntilde", "\321"/* Ñ */),
227 ESCSEQ("Ograve", "\322"/* Ò */),
228
229 ESCSEQ("Ocirc", "\324"/* Ô */),
230 ESCSEQ("Otilde", "\325"/* Õ */),
231 ESCSEQ("Ouml", "\326"/* Ö */),
232
233 ESCSEQ("Oslash", " "),
234 ESCSEQ("Ugrave", "\331"/* Ù */),
235
236 ESCSEQ("Ucirc", " "),
237 ESCSEQ("Uuml", "\334"/* Ü */),
238
239 ESCSEQ("szlig", "\247"/* § */),
240 ESCSEQ("agrave;","à"),
241 ESCSEQ("aacute", "\341"/* á */),
242 ESCSEQ("acirc", "\342"/* â */),
243 ESCSEQ("atilde", "\343"/* ã */),
244 ESCSEQ("auml", "\344"/* ä */),
245 ESCSEQ("aring", "a"),
246 ESCSEQ("aelig", "ae"),
247 ESCSEQ("ccedil", "\347"/* ç */),
248 ESCSEQ("egrave", "\350"/* è */),
249 ESCSEQ("eacute", "\351"/* é */),
250 ESCSEQ("ecirc", "\352"/* ê */),
251 ESCSEQ("euml", "\353"/* ë */),
252 ESCSEQ("igrave", "\354"/* ì */),
253 ESCSEQ("iacute", "\355"/* í */),
254 ESCSEQ("icirc", " "),
255 ESCSEQ("iuml", "\357"/* ï */),
256 ESCSEQ("eth", " "),
257 ESCSEQ("ntilde", "\361"/* ñ */),
258 ESCSEQ("ograve", "\362"/* ò */),
259 ESCSEQ("oacute", "\363"/* ó */),
260 ESCSEQ("ocirc", "\364"/* ô */),
261 ESCSEQ("otilde", "\365"/* õ */),
262 ESCSEQ("ouml", "\366"/* ö */),
263 ESCSEQ("divide", " "),
264 ESCSEQ("oslash", " "),
265 ESCSEQ("ugrave", "\371"/* ù */),
266 ESCSEQ("uacute", "\372"/* ú */),
267 ESCSEQ("ucirc", "\373"/* û */),
268 ESCSEQ("uuml", "\374"/* ü */),
269
270 ESCSEQ("yuml", ""),
271
272 /* this one should ALWAYS stay the last one!!! */
273 ESCSEQ("amp", "&"),
274
275 { NULL, NULL, NULL }
276 };
277
278 for (int i = 0; substitutions[i][0] != NULL; i++)
279 {
280 m_Name.Replace(substitutions[i][0], substitutions[i][2], TRUE);
281 m_Name.Replace(substitutions[i][1], substitutions[i][2], TRUE);
282 }
283 }
284 }
285 if (tag.GetParam(wxT("NAME")) == wxT("Local")) m_Page = tag.GetParam(wxT("VALUE"));
286 if (tag.GetParam(wxT("NAME")) == wxT("ID")) tag.ScanParam(wxT("VALUE"), wxT("%i"), &m_ID);
287 return FALSE;
288 }
289 }
290
291
292
293 void HP_TagHandler::WriteOut(wxHtmlContentsItem*& array, int& size)
294 {
295 array = m_Items;
296 size = m_ItemsCnt;
297 m_Items = NULL;
298 m_ItemsCnt = 0;
299 }
300
301 void HP_TagHandler::ReadIn(wxHtmlContentsItem* array, int size)
302 {
303 m_Items = array;
304 m_ItemsCnt = size;
305 }
306
307
308
309
310 //-----------------------------------------------------------------------------
311 // wxHtmlHelpData
312 //-----------------------------------------------------------------------------
313
314 IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpData, wxObject)
315
316 wxHtmlHelpData::wxHtmlHelpData()
317 {
318 m_TempPath = wxEmptyString;
319
320 m_Contents = NULL;
321 m_ContentsCnt = 0;
322 m_Index = NULL;
323 m_IndexCnt = 0;
324 }
325
326 wxHtmlHelpData::~wxHtmlHelpData()
327 {
328 int i;
329
330 m_BookRecords.Empty();
331 if (m_Contents)
332 {
333 for (i = 0; i < m_ContentsCnt; i++)
334 {
335 delete[] m_Contents[i].m_Page;
336 delete[] m_Contents[i].m_Name;
337 }
338 free(m_Contents);
339 }
340 if (m_Index)
341 {
342 for (i = 0; i < m_IndexCnt; i++)
343 {
344 delete[] m_Index[i].m_Page;
345 delete[] m_Index[i].m_Name;
346 }
347 free(m_Index);
348 }
349 }
350
351 bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord *book, wxFileSystem& fsys, const wxString& indexfile, const wxString& contentsfile)
352 {
353 wxFSFile *f;
354 char *buf;
355 int sz;
356 wxString string;
357
358 HP_Parser parser;
359 HP_TagHandler *handler = new HP_TagHandler(book);
360 parser.AddTagHandler(handler);
361
362 f = ( contentsfile.IsEmpty() ? (wxFSFile*) NULL : fsys.OpenFile(contentsfile) );
363 if (f)
364 {
365 sz = f->GetStream()->GetSize();
366 buf = new char[sz + 1];
367 buf[sz] = 0;
368 f->GetStream()->Read(buf, sz);
369 delete f;
370 handler->ReadIn(m_Contents, m_ContentsCnt);
371 parser.Parse(buf);
372 handler->WriteOut(m_Contents, m_ContentsCnt);
373 delete[] buf;
374 }
375 else
376 wxLogError(_("Cannot open contents file: %s"), contentsfile.c_str());
377
378 f = ( indexfile.IsEmpty() ? (wxFSFile*) NULL : fsys.OpenFile(indexfile) );
379 if (f)
380 {
381 sz = f->GetStream()->GetSize();
382 buf = new char[sz + 1];
383 buf[sz] = 0;
384 f->GetStream()->Read(buf, sz);
385 delete f;
386 handler->ReadIn(m_Index, m_IndexCnt);
387 parser.Parse(buf);
388 handler->WriteOut(m_Index, m_IndexCnt);
389 delete[] buf;
390 }
391 else if (!indexfile.IsEmpty())
392 wxLogError(_("Cannot open index file: %s"), indexfile.c_str());
393 return TRUE;
394 }
395
396
397
398
399 #if wxUSE_UNICODE
400
401 #define READ_STRING(f, s, lng) { char tmpc; for (int i = 0; i < lng; i++) { f->Read(&tmpc, 1); s[i] = (wxChar)tmpc;} }
402 #define WRITE_STRING(f, s, lng) { char tmpc; for (int i = 0; i < lng; i++) { tmpc = (char)s[i]; f->Write(&tmpc, 1);} }
403
404 #else
405
406 #define READ_STRING(f, s, lng) f->Read(s, lng * sizeof(char));
407 #define WRITE_STRING(f, s, lng) f->Write(s, lng * sizeof(char));
408
409 #endif
410
411
412 #define CURRENT_CACHED_BOOK_VERSION 1
413
414 bool wxHtmlHelpData::LoadCachedBook(wxHtmlBookRecord *book, wxInputStream *f)
415 {
416 int i, st;
417 wxInt32 x;
418 wxInt32 version;
419
420 /* load header - version info : */
421
422 f->Read(&x, sizeof(x));
423 version = wxINT32_SWAP_ON_BE(x);
424
425 if (version != CURRENT_CACHED_BOOK_VERSION)
426 {
427 wxLogError(_("Incorrect version of HTML help book"));
428 return FALSE;
429 // NOTE: when adding new version, please ensure backward compatibility!
430 }
431
432 /* load contents : */
433
434 f->Read(&x, sizeof(x));
435 st = m_ContentsCnt;
436 m_ContentsCnt += wxINT32_SWAP_ON_BE(x);
437 m_Contents = (wxHtmlContentsItem*) realloc(m_Contents,
438 (m_ContentsCnt / wxHTML_REALLOC_STEP + 1) *
439 wxHTML_REALLOC_STEP * sizeof(wxHtmlContentsItem));
440 for (i = st; i < m_ContentsCnt; i++)
441 {
442 f->Read(&x, sizeof(x));
443 m_Contents[i].m_Level = wxINT32_SWAP_ON_BE(x);
444 f->Read(&x, sizeof(x));
445 m_Contents[i].m_ID = wxINT32_SWAP_ON_BE(x);
446 f->Read(&x, sizeof(x)); x = wxINT32_SWAP_ON_BE(x);
447 m_Contents[i].m_Name = new wxChar[x];
448 READ_STRING(f, m_Contents[i].m_Name, x);
449 f->Read(&x, sizeof(x)); x = wxINT32_SWAP_ON_BE(x);
450 m_Contents[i].m_Page = new wxChar[x];
451 READ_STRING(f, m_Contents[i].m_Page, x);
452 m_Contents[i].m_Book = book;
453 }
454
455 /* load index : */
456
457 f->Read(&x, sizeof(x));
458 st = m_IndexCnt;
459 m_IndexCnt += wxINT32_SWAP_ON_BE(x);
460 m_Index = (wxHtmlContentsItem*) realloc(m_Index, (m_IndexCnt / wxHTML_REALLOC_STEP + 1) *
461 wxHTML_REALLOC_STEP * sizeof(wxHtmlContentsItem));
462 for (i = st; i < m_IndexCnt; i++)
463 {
464 f->Read(&x, sizeof(x)); x = wxINT32_SWAP_ON_BE(x);
465 m_Index[i].m_Name = new wxChar[x];
466 READ_STRING(f, m_Index[i].m_Name, x);
467 f->Read(&x, sizeof(x)); x = wxINT32_SWAP_ON_BE(x);
468 m_Index[i].m_Page = new wxChar[x];
469 READ_STRING(f, m_Index[i].m_Page, x);
470 m_Index[i].m_Book = book;
471 }
472 return TRUE;
473 }
474
475
476 bool wxHtmlHelpData::SaveCachedBook(wxHtmlBookRecord *book, wxOutputStream *f)
477 {
478 int i;
479 wxInt32 x;
480
481 /* save header - version info : */
482
483 x = wxINT32_SWAP_ON_BE(CURRENT_CACHED_BOOK_VERSION);
484 f->Write(&x, sizeof(x));
485
486 /* save contents : */
487
488 x = 0;
489 for (i = 0; i < m_ContentsCnt; i++) if (m_Contents[i].m_Book == book && m_Contents[i].m_Level > 0) x++;
490 x = wxINT32_SWAP_ON_BE(x);
491 f->Write(&x, sizeof(x));
492 for (i = 0; i < m_ContentsCnt; i++)
493 {
494 if (m_Contents[i].m_Book != book || m_Contents[i].m_Level == 0) continue;
495 x = wxINT32_SWAP_ON_BE(m_Contents[i].m_Level);
496 f->Write(&x, sizeof(x));
497 x = wxINT32_SWAP_ON_BE(m_Contents[i].m_ID);
498 f->Write(&x, sizeof(x));
499 x = wxINT32_SWAP_ON_BE(wxStrlen(m_Contents[i].m_Name) + 1);
500 f->Write(&x, sizeof(x));
501 WRITE_STRING(f, m_Contents[i].m_Name, x);
502 x = wxINT32_SWAP_ON_BE(wxStrlen(m_Contents[i].m_Page) + 1);
503 f->Write(&x, sizeof(x));
504 WRITE_STRING(f, m_Contents[i].m_Page, x);
505 }
506
507 /* save index : */
508
509 x = 0;
510 for (i = 0; i < m_IndexCnt; i++) if (m_Index[i].m_Book == book && m_Index[i].m_Level > 0) x++;
511 x = wxINT32_SWAP_ON_BE(x);
512 f->Write(&x, sizeof(x));
513 for (i = 0; i < m_IndexCnt; i++)
514 {
515 if (m_Index[i].m_Book != book || m_Index[i].m_Level == 0) continue;
516 x = wxINT32_SWAP_ON_BE(wxStrlen(m_Index[i].m_Name) + 1);
517 f->Write(&x, sizeof(x));
518 WRITE_STRING(f, m_Index[i].m_Name, x);
519 x = wxINT32_SWAP_ON_BE(wxStrlen(m_Index[i].m_Page) + 1);
520 f->Write(&x, sizeof(x));
521 WRITE_STRING(f, m_Index[i].m_Page, x);
522 }
523 return TRUE;
524 }
525
526
527 void wxHtmlHelpData::SetTempDir(const wxString& path)
528 {
529 if (path == wxEmptyString) m_TempPath = path;
530 else
531 {
532 if (wxIsAbsolutePath(path)) m_TempPath = path;
533 else m_TempPath = wxGetCwd() + _T("/") + path;
534
535 if (m_TempPath[m_TempPath.Length() - 1] != _T('/'))
536 m_TempPath << _T('/');
537 }
538 }
539
540
541
542 static wxString SafeFileName(const wxString& s)
543 {
544 wxString res(s);
545 res.Replace(wxT("#"), wxT("_"));
546 res.Replace(wxT(":"), wxT("_"));
547 res.Replace(wxT("\\"), wxT("_"));
548 res.Replace(wxT("/"), wxT("_"));
549 return res;
550 }
551
552 bool wxHtmlHelpData::AddBookParam(const wxFSFile& bookfile,
553 wxFontEncoding encoding,
554 const wxString& title, const wxString& contfile,
555 const wxString& indexfile, const wxString& deftopic,
556 const wxString& path)
557 {
558 wxFileSystem fsys;
559 wxFSFile *fi;
560 wxHtmlBookRecord *bookr;
561
562 int IndexOld = m_IndexCnt,
563 ContentsOld = m_ContentsCnt;
564
565 if (! path.IsEmpty())
566 fsys.ChangePathTo(path, TRUE);
567
568 bookr = new wxHtmlBookRecord(fsys.GetPath(), title, deftopic);
569
570 if (m_ContentsCnt % wxHTML_REALLOC_STEP == 0)
571 m_Contents = (wxHtmlContentsItem*) realloc(m_Contents, (m_ContentsCnt + wxHTML_REALLOC_STEP) * sizeof(wxHtmlContentsItem));
572 m_Contents[m_ContentsCnt].m_Level = 0;
573 m_Contents[m_ContentsCnt].m_ID = 0;
574 m_Contents[m_ContentsCnt].m_Page = new wxChar[deftopic.Length() + 1];
575 wxStrcpy(m_Contents[m_ContentsCnt].m_Page, deftopic.c_str());
576 m_Contents[m_ContentsCnt].m_Name = new wxChar [title.Length() + 1];
577 wxStrcpy(m_Contents[m_ContentsCnt].m_Name, title.c_str());
578 m_Contents[m_ContentsCnt].m_Book = bookr;
579
580 // store the contents index for later
581 int cont_start = m_ContentsCnt++;
582
583 // Try to find cached binary versions:
584 // 1. save file as book, but with .hhp.cached extension
585 // 2. same as 1. but in temp path
586 // 3. otherwise or if cache load failed, load it from MS.
587
588 fi = fsys.OpenFile(bookfile.GetLocation() + wxT(".cached"));
589
590 if (fi == NULL ||
591 fi->GetModificationTime() < bookfile.GetModificationTime() ||
592 !LoadCachedBook(bookr, fi->GetStream()))
593 {
594 if (fi != NULL) delete fi;
595 fi = fsys.OpenFile(m_TempPath + wxFileNameFromPath(bookfile.GetLocation()) + wxT(".cached"));
596 if (m_TempPath == wxEmptyString || fi == NULL ||
597 fi->GetModificationTime() < bookfile.GetModificationTime() ||
598 !LoadCachedBook(bookr, fi->GetStream()))
599 {
600 LoadMSProject(bookr, fsys, indexfile, contfile);
601 if (m_TempPath != wxEmptyString)
602 {
603 wxFileOutputStream *outs = new wxFileOutputStream(m_TempPath +
604 SafeFileName(wxFileNameFromPath(bookfile.GetLocation())) + wxT(".cached"));
605 SaveCachedBook(bookr, outs);
606 delete outs;
607 }
608 }
609 }
610
611 if (fi != NULL) delete fi;
612
613 // Now store the contents range
614 bookr->SetContentsRange(cont_start, m_ContentsCnt);
615
616 // Convert encoding, if neccessary:
617 if (encoding != wxFONTENCODING_SYSTEM)
618 {
619 wxFontEncodingArray a = wxEncodingConverter::GetPlatformEquivalents(encoding);
620 if (a.GetCount() != 0 && a[0] != encoding)
621 {
622 int i;
623 wxEncodingConverter conv;
624 conv.Init(encoding, a[0]);
625
626 for (i = IndexOld; i < m_IndexCnt; i++)
627 conv.Convert(m_Index[i].m_Name);
628 for (i = ContentsOld; i < m_ContentsCnt; i++)
629 conv.Convert(m_Contents[i].m_Name);
630 }
631 }
632
633 m_BookRecords.Add(bookr);
634 if (m_IndexCnt > 0)
635 qsort(m_Index, m_IndexCnt, sizeof(wxHtmlContentsItem), IndexCompareFunc);
636
637 return TRUE;
638 }
639
640
641 bool wxHtmlHelpData::AddBook(const wxString& book)
642 {
643 if (book.Right(4).Lower() == wxT(".zip") ||
644 book.Right(4).Lower() == wxT(".htb") /*html book*/)
645
646 {
647 wxFileSystem fsys;
648 wxString s;
649 bool rt = FALSE;
650
651 s = fsys.FindFirst(book + wxT("#zip:") + wxT("*.hhp"), wxFILE);
652 while (!s.IsEmpty())
653 {
654 if (AddBook(s)) rt = TRUE;
655 s = fsys.FindNext();
656 }
657
658 return rt;
659 }
660
661
662 else
663 {
664 wxFSFile *fi;
665 wxFileSystem fsys;
666 wxInputStream *s;
667 wxString bookFull;
668
669 int sz;
670 char *buff, *lineptr;
671 char linebuf[300];
672
673 wxString title = _("noname"),
674 safetitle,
675 start = wxEmptyString,
676 contents = wxEmptyString,
677 index = wxEmptyString,
678 charset = wxEmptyString;
679
680 if (wxIsAbsolutePath(book)) bookFull = book;
681 else bookFull = wxGetCwd() + "/" + book;
682
683 fi = fsys.OpenFile(bookFull);
684 if (fi == NULL)
685 {
686 wxLogError(_("Cannot open HTML help book: %s"), bookFull.c_str());
687 return FALSE;
688 }
689 fsys.ChangePathTo(bookFull);
690 s = fi->GetStream();
691 sz = s->GetSize();
692 buff = new char[sz + 1];
693 buff[sz] = 0;
694 s->Read(buff, sz);
695 lineptr = buff;
696
697 do {
698 lineptr = ReadLine(lineptr, linebuf);
699
700 if (strstr(linebuf, "Title=") == linebuf)
701 title = linebuf + strlen("Title=");
702 if (strstr(linebuf, "Default topic=") == linebuf)
703 start = linebuf + strlen("Default topic=");
704 if (strstr(linebuf, "Index file=") == linebuf)
705 index = linebuf + strlen("Index file=");
706 if (strstr(linebuf, "Contents file=") == linebuf)
707 contents = linebuf + strlen("Contents file=");
708 if (strstr(linebuf, "Charset=") == linebuf)
709 charset = linebuf + strlen("Charset=");
710 } while (lineptr != NULL);
711 delete[] buff;
712
713 wxFontEncoding enc;
714 if (charset == wxEmptyString) enc = wxFONTENCODING_SYSTEM;
715 else enc = wxTheFontMapper->CharsetToEncoding(charset);
716 bool rtval = AddBookParam(*fi, enc,
717 title, contents, index, start, fsys.GetPath());
718 delete fi;
719 return rtval;
720 }
721 }
722
723 wxString wxHtmlHelpData::FindPageByName(const wxString& x)
724 {
725 int cnt;
726 int i;
727 wxFileSystem fsys;
728 wxFSFile *f;
729 wxString url(wxEmptyString);
730
731 /* 1. try to open given file: */
732
733 cnt = m_BookRecords.GetCount();
734 for (i = 0; i < cnt; i++)
735 {
736 f = fsys.OpenFile(m_BookRecords[i].GetBasePath() + x);
737 if (f)
738 {
739 url = m_BookRecords[i].GetBasePath() + x;
740 delete f;
741 return url;
742 }
743 }
744
745
746 /* 2. try to find a book: */
747
748 for (i = 0; i < cnt; i++)
749 {
750 if (m_BookRecords[i].GetTitle() == x)
751 {
752 url = m_BookRecords[i].GetBasePath() + m_BookRecords[i].GetStart();
753 return url;
754 }
755 }
756
757 /* 3. try to find in contents: */
758
759 cnt = m_ContentsCnt;
760 for (i = 0; i < cnt; i++)
761 {
762 if (wxStrcmp(m_Contents[i].m_Name, x) == 0)
763 {
764 url = m_Contents[i].m_Book->GetBasePath() + m_Contents[i].m_Page;
765 return url;
766 }
767 }
768
769
770 /* 4. try to find in index: */
771
772 cnt = m_IndexCnt;
773 for (i = 0; i < cnt; i++)
774 {
775 if (wxStrcmp(m_Index[i].m_Name, x) == 0)
776 {
777 url = m_Index[i].m_Book->GetBasePath() + m_Index[i].m_Page;
778 return url;
779 }
780 }
781
782 return url;
783 }
784
785 wxString wxHtmlHelpData::FindPageById(int id)
786 {
787 int i;
788 wxString url(wxEmptyString);
789
790 for (i = 0; i < m_ContentsCnt; i++)
791 {
792 if (m_Contents[i].m_ID == id)
793 {
794 url = m_Contents[i].m_Book->GetBasePath() + m_Contents[i].m_Page;
795 return url;
796 }
797 }
798
799 return url;
800 }
801
802 //----------------------------------------------------------------------------------
803 // wxHtmlSearchStatus functions
804 //----------------------------------------------------------------------------------
805
806 wxHtmlSearchStatus::wxHtmlSearchStatus(wxHtmlHelpData* data, const wxString& keyword,
807 bool case_sensitive, bool whole_words_only,
808 const wxString& book)
809 {
810 m_Data = data;
811 m_Keyword = keyword;
812 wxHtmlBookRecord* bookr = NULL;
813 if (book != wxEmptyString)
814 {
815 // we have to search in a specific book. Find it first
816 int i, cnt = data->m_BookRecords.GetCount();
817 for (i = 0; i < cnt; i++)
818 if (data->m_BookRecords[i].GetTitle() == book)
819 {
820 bookr = &(data->m_BookRecords[i]);
821 m_CurIndex = bookr->GetContentsStart();
822 m_MaxIndex = bookr->GetContentsEnd();
823 break;
824 }
825 // check; we won't crash if the book doesn't exist, but it's Bad Anyway.
826 wxASSERT(bookr);
827 }
828 if (! bookr)
829 {
830 // no book specified; search all books
831 m_CurIndex = 0;
832 m_MaxIndex = m_Data->m_ContentsCnt;
833 }
834 m_Engine.LookFor(keyword, case_sensitive, whole_words_only);
835 m_Active = (m_CurIndex < m_MaxIndex);
836 m_LastPage = NULL;
837 }
838
839 bool wxHtmlSearchStatus::Search()
840 {
841 wxFSFile *file;
842 int i = m_CurIndex; // shortcut
843 bool found = FALSE;
844 wxChar *thepage;
845
846 if (!m_Active)
847 {
848 // sanity check. Illegal use, but we'll try to prevent a crash anyway
849 wxASSERT(m_Active);
850 return FALSE;
851 }
852
853 m_Name = wxEmptyString;
854 m_ContentsItem = NULL;
855 thepage = m_Data->m_Contents[i].m_Page;
856
857 m_Active = (++m_CurIndex < m_MaxIndex);
858 // check if it is same page with different anchor:
859 if (m_LastPage != NULL)
860 {
861 wxChar *p1, *p2;
862 for (p1 = thepage, p2 = m_LastPage;
863 *p1 != 0 && *p1 != _T('#') && *p1 == *p2; p1++, p2++) {}
864
865 m_LastPage = thepage;
866
867 if (*p1 == 0 || *p1 == _T('#'))
868 return FALSE;
869 }
870 else m_LastPage = thepage;
871
872 wxFileSystem fsys;
873 file = fsys.OpenFile(m_Data->m_Contents[i].m_Book->GetBasePath() + thepage);
874 if (file)
875 {
876 if (m_Engine.Scan(file->GetStream())) {
877 m_Name = m_Data->m_Contents[i].m_Name;
878 m_ContentsItem = m_Data->m_Contents + i;
879 found = TRUE;
880 }
881 delete file;
882 }
883 return found;
884 }
885
886
887
888
889
890
891
892
893 //--------------------------------------------------------------------------------
894 // wxSearchEngine
895 //--------------------------------------------------------------------------------
896
897 void wxSearchEngine::LookFor(const wxString& keyword, bool case_sensitive, bool whole_words_only)
898 {
899 m_CaseSensitive = case_sensitive;
900 m_WholeWords = whole_words_only;
901 if (m_Keyword) delete[] m_Keyword;
902 m_Keyword = new wxChar[keyword.Length() + 1];
903 wxStrcpy(m_Keyword, keyword.c_str());
904
905 if (!m_CaseSensitive)
906 {
907 for (int i = wxStrlen(m_Keyword) - 1; i >= 0; i--)
908 {
909 if ((m_Keyword[i] >= wxT('A')) && (m_Keyword[i] <= wxT('Z')))
910 m_Keyword[i] += wxT('a') - wxT('A');
911 }
912 }
913 }
914
915
916
917 #define WHITESPACE(c) (c == ' ' || c == '\n' || c == '\r' || c == '\t')
918
919 bool wxSearchEngine::Scan(wxInputStream *stream)
920 {
921 wxASSERT_MSG(m_Keyword != NULL, wxT("wxSearchEngine::LookFor must be called before scanning!"));
922
923 int i, j;
924 int lng = stream ->GetSize();
925 int wrd = wxStrlen(m_Keyword);
926 bool found = FALSE;
927 char *buf = new char[lng + 1];
928 stream->Read(buf, lng);
929 buf[lng] = 0;
930
931 if (!m_CaseSensitive)
932 for (i = 0; i < lng; i++)
933 if ((buf[i] >= 'A') && (buf[i] <= 'Z')) buf[i] += 'a' - 'A';
934
935 if (m_WholeWords)
936 {
937 for (i = 0; i < lng - wrd; i++)
938 {
939 if (WHITESPACE(buf[i])) continue;
940 j = 0;
941 while ((j < wrd) && (buf[i + j] == m_Keyword[j])) j++;
942 if (j == wrd && WHITESPACE(buf[i + j])) { found = TRUE; break; }
943 }
944 }
945
946 else
947 {
948 for (i = 0; i < lng - wrd; i++)
949 {
950 j = 0;
951 while ((j < wrd) && (buf[i + j] == m_Keyword[j])) j++;
952 if (j == wrd) { found = TRUE; break; }
953 }
954 }
955
956 delete[] buf;
957 return found;
958 }
959
960
961
962
963 #endif