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