Added support for more special characters
[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 ESCSEQ("#60", "<"),
304 ESCSEQ("#62", ">"),
305
306 /* this one should ALWAYS stay the last one!!! */
307 ESCSEQ("amp", "&"),
308 ESCSEQ("#38", "&"),
309
310 { NULL, NULL, NULL }
311 };
312
313 for (int i = 0; substitutions[i][0] != NULL; i++)
314 {
315 m_Name.Replace(substitutions[i][0], substitutions[i][3], TRUE);
316 m_Name.Replace(substitutions[i][1], substitutions[i][3], TRUE);
317 m_Name.Replace(substitutions[i][2], substitutions[i][3], TRUE);
318 }
319 }
320 }
321 if (tag.GetParam(wxT("NAME")) == wxT("Local")) m_Page = tag.GetParam(wxT("VALUE"));
322 if (tag.GetParam(wxT("NAME")) == wxT("ID")) tag.ScanParam(wxT("VALUE"), wxT("%i"), &m_ID);
323 return FALSE;
324 }
325 }
326
327
328
329 void HP_TagHandler::WriteOut(wxHtmlContentsItem*& array, int& size)
330 {
331 array = m_Items;
332 size = m_ItemsCnt;
333 m_Items = NULL;
334 m_ItemsCnt = 0;
335 }
336
337 void HP_TagHandler::ReadIn(wxHtmlContentsItem* array, int size)
338 {
339 m_Items = array;
340 m_ItemsCnt = size;
341 }
342
343
344
345
346 //-----------------------------------------------------------------------------
347 // wxHtmlHelpData
348 //-----------------------------------------------------------------------------
349
350 IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpData, wxObject)
351
352 wxHtmlHelpData::wxHtmlHelpData()
353 {
354 m_TempPath = wxEmptyString;
355
356 m_Contents = NULL;
357 m_ContentsCnt = 0;
358 m_Index = NULL;
359 m_IndexCnt = 0;
360 }
361
362 wxHtmlHelpData::~wxHtmlHelpData()
363 {
364 int i;
365
366 m_BookRecords.Empty();
367 if (m_Contents)
368 {
369 for (i = 0; i < m_ContentsCnt; i++)
370 {
371 delete[] m_Contents[i].m_Page;
372 delete[] m_Contents[i].m_Name;
373 }
374 free(m_Contents);
375 }
376 if (m_Index)
377 {
378 for (i = 0; i < m_IndexCnt; i++)
379 {
380 delete[] m_Index[i].m_Page;
381 delete[] m_Index[i].m_Name;
382 }
383 free(m_Index);
384 }
385 }
386
387 bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord *book, wxFileSystem& fsys, const wxString& indexfile, const wxString& contentsfile)
388 {
389 wxFSFile *f;
390 char *buf;
391 int sz;
392 wxString string;
393
394 HP_Parser parser;
395 HP_TagHandler *handler = new HP_TagHandler(book);
396 parser.AddTagHandler(handler);
397
398 f = ( contentsfile.IsEmpty() ? (wxFSFile*) NULL : fsys.OpenFile(contentsfile) );
399 if (f)
400 {
401 sz = f->GetStream()->GetSize();
402 buf = new char[sz + 1];
403 buf[sz] = 0;
404 f->GetStream()->Read(buf, sz);
405 delete f;
406 handler->ReadIn(m_Contents, m_ContentsCnt);
407 parser.Parse(buf);
408 handler->WriteOut(m_Contents, m_ContentsCnt);
409 delete[] buf;
410 }
411 else
412 wxLogError(_("Cannot open contents file: %s"), contentsfile.c_str());
413
414 f = ( indexfile.IsEmpty() ? (wxFSFile*) NULL : fsys.OpenFile(indexfile) );
415 if (f)
416 {
417 sz = f->GetStream()->GetSize();
418 buf = new char[sz + 1];
419 buf[sz] = 0;
420 f->GetStream()->Read(buf, sz);
421 delete f;
422 handler->ReadIn(m_Index, m_IndexCnt);
423 parser.Parse(buf);
424 handler->WriteOut(m_Index, m_IndexCnt);
425 delete[] buf;
426 }
427 else if (!indexfile.IsEmpty())
428 wxLogError(_("Cannot open index file: %s"), indexfile.c_str());
429 return TRUE;
430 }
431
432
433
434
435 #if wxUSE_UNICODE
436
437 #define READ_STRING(f, s, lng) { char tmpc; for (int i = 0; i < lng; i++) { f->Read(&tmpc, 1); s[i] = (wxChar)tmpc;} }
438 #define WRITE_STRING(f, s, lng) { char tmpc; for (int i = 0; i < lng; i++) { tmpc = (char)s[i]; f->Write(&tmpc, 1);} }
439
440 #else
441
442 #define READ_STRING(f, s, lng) f->Read(s, lng * sizeof(char));
443 #define WRITE_STRING(f, s, lng) f->Write(s, lng * sizeof(char));
444
445 #endif
446
447
448 #define CURRENT_CACHED_BOOK_VERSION 1
449
450 bool wxHtmlHelpData::LoadCachedBook(wxHtmlBookRecord *book, wxInputStream *f)
451 {
452 int i, st;
453 wxInt32 x;
454 wxInt32 version;
455
456 /* load header - version info : */
457
458 f->Read(&x, sizeof(x));
459 version = wxINT32_SWAP_ON_BE(x);
460
461 if (version != CURRENT_CACHED_BOOK_VERSION)
462 {
463 wxLogError(_("Incorrect version of HTML help book"));
464 return FALSE;
465 // NOTE: when adding new version, please ensure backward compatibility!
466 }
467
468 /* load contents : */
469
470 f->Read(&x, sizeof(x));
471 st = m_ContentsCnt;
472 m_ContentsCnt += wxINT32_SWAP_ON_BE(x);
473 m_Contents = (wxHtmlContentsItem*) realloc(m_Contents,
474 (m_ContentsCnt / wxHTML_REALLOC_STEP + 1) *
475 wxHTML_REALLOC_STEP * sizeof(wxHtmlContentsItem));
476 for (i = st; i < m_ContentsCnt; i++)
477 {
478 f->Read(&x, sizeof(x));
479 m_Contents[i].m_Level = wxINT32_SWAP_ON_BE(x);
480 f->Read(&x, sizeof(x));
481 m_Contents[i].m_ID = wxINT32_SWAP_ON_BE(x);
482 f->Read(&x, sizeof(x)); x = wxINT32_SWAP_ON_BE(x);
483 m_Contents[i].m_Name = new wxChar[x];
484 READ_STRING(f, m_Contents[i].m_Name, x);
485 f->Read(&x, sizeof(x)); x = wxINT32_SWAP_ON_BE(x);
486 m_Contents[i].m_Page = new wxChar[x];
487 READ_STRING(f, m_Contents[i].m_Page, x);
488 m_Contents[i].m_Book = book;
489 }
490
491 /* load index : */
492
493 f->Read(&x, sizeof(x));
494 st = m_IndexCnt;
495 m_IndexCnt += wxINT32_SWAP_ON_BE(x);
496 m_Index = (wxHtmlContentsItem*) realloc(m_Index, (m_IndexCnt / wxHTML_REALLOC_STEP + 1) *
497 wxHTML_REALLOC_STEP * sizeof(wxHtmlContentsItem));
498 for (i = st; i < m_IndexCnt; i++)
499 {
500 f->Read(&x, sizeof(x)); x = wxINT32_SWAP_ON_BE(x);
501 m_Index[i].m_Name = new wxChar[x];
502 READ_STRING(f, m_Index[i].m_Name, x);
503 f->Read(&x, sizeof(x)); x = wxINT32_SWAP_ON_BE(x);
504 m_Index[i].m_Page = new wxChar[x];
505 READ_STRING(f, m_Index[i].m_Page, x);
506 m_Index[i].m_Book = book;
507 }
508 return TRUE;
509 }
510
511
512 bool wxHtmlHelpData::SaveCachedBook(wxHtmlBookRecord *book, wxOutputStream *f)
513 {
514 int i;
515 wxInt32 x;
516
517 /* save header - version info : */
518
519 x = wxINT32_SWAP_ON_BE(CURRENT_CACHED_BOOK_VERSION);
520 f->Write(&x, sizeof(x));
521
522 /* save contents : */
523
524 x = 0;
525 for (i = 0; i < m_ContentsCnt; i++) if (m_Contents[i].m_Book == book && m_Contents[i].m_Level > 0) x++;
526 x = wxINT32_SWAP_ON_BE(x);
527 f->Write(&x, sizeof(x));
528 for (i = 0; i < m_ContentsCnt; i++)
529 {
530 if (m_Contents[i].m_Book != book || m_Contents[i].m_Level == 0) continue;
531 x = wxINT32_SWAP_ON_BE(m_Contents[i].m_Level);
532 f->Write(&x, sizeof(x));
533 x = wxINT32_SWAP_ON_BE(m_Contents[i].m_ID);
534 f->Write(&x, sizeof(x));
535 x = wxINT32_SWAP_ON_BE(wxStrlen(m_Contents[i].m_Name) + 1);
536 f->Write(&x, sizeof(x));
537 WRITE_STRING(f, m_Contents[i].m_Name, x);
538 x = wxINT32_SWAP_ON_BE(wxStrlen(m_Contents[i].m_Page) + 1);
539 f->Write(&x, sizeof(x));
540 WRITE_STRING(f, m_Contents[i].m_Page, x);
541 }
542
543 /* save index : */
544
545 x = 0;
546 for (i = 0; i < m_IndexCnt; i++) if (m_Index[i].m_Book == book && m_Index[i].m_Level > 0) x++;
547 x = wxINT32_SWAP_ON_BE(x);
548 f->Write(&x, sizeof(x));
549 for (i = 0; i < m_IndexCnt; i++)
550 {
551 if (m_Index[i].m_Book != book || m_Index[i].m_Level == 0) continue;
552 x = wxINT32_SWAP_ON_BE(wxStrlen(m_Index[i].m_Name) + 1);
553 f->Write(&x, sizeof(x));
554 WRITE_STRING(f, m_Index[i].m_Name, x);
555 x = wxINT32_SWAP_ON_BE(wxStrlen(m_Index[i].m_Page) + 1);
556 f->Write(&x, sizeof(x));
557 WRITE_STRING(f, m_Index[i].m_Page, x);
558 }
559 return TRUE;
560 }
561
562
563 void wxHtmlHelpData::SetTempDir(const wxString& path)
564 {
565 if (path == wxEmptyString) m_TempPath = path;
566 else
567 {
568 if (wxIsAbsolutePath(path)) m_TempPath = path;
569 else m_TempPath = wxGetCwd() + _T("/") + path;
570
571 if (m_TempPath[m_TempPath.Length() - 1] != _T('/'))
572 m_TempPath << _T('/');
573 }
574 }
575
576
577
578 static wxString SafeFileName(const wxString& s)
579 {
580 wxString res(s);
581 res.Replace(wxT("#"), wxT("_"));
582 res.Replace(wxT(":"), wxT("_"));
583 res.Replace(wxT("\\"), wxT("_"));
584 res.Replace(wxT("/"), wxT("_"));
585 return res;
586 }
587
588 bool wxHtmlHelpData::AddBookParam(const wxFSFile& bookfile,
589 wxFontEncoding encoding,
590 const wxString& title, const wxString& contfile,
591 const wxString& indexfile, const wxString& deftopic,
592 const wxString& path)
593 {
594 wxFileSystem fsys;
595 wxFSFile *fi;
596 wxHtmlBookRecord *bookr;
597
598 int IndexOld = m_IndexCnt,
599 ContentsOld = m_ContentsCnt;
600
601 if (! path.IsEmpty())
602 fsys.ChangePathTo(path, TRUE);
603
604 bookr = new wxHtmlBookRecord(fsys.GetPath(), title, deftopic);
605
606 if (m_ContentsCnt % wxHTML_REALLOC_STEP == 0)
607 m_Contents = (wxHtmlContentsItem*) realloc(m_Contents, (m_ContentsCnt + wxHTML_REALLOC_STEP) * sizeof(wxHtmlContentsItem));
608 m_Contents[m_ContentsCnt].m_Level = 0;
609 m_Contents[m_ContentsCnt].m_ID = 0;
610 m_Contents[m_ContentsCnt].m_Page = new wxChar[deftopic.Length() + 1];
611 wxStrcpy(m_Contents[m_ContentsCnt].m_Page, deftopic.c_str());
612 m_Contents[m_ContentsCnt].m_Name = new wxChar [title.Length() + 1];
613 wxStrcpy(m_Contents[m_ContentsCnt].m_Name, title.c_str());
614 m_Contents[m_ContentsCnt].m_Book = bookr;
615
616 // store the contents index for later
617 int cont_start = m_ContentsCnt++;
618
619 // Try to find cached binary versions:
620 // 1. save file as book, but with .hhp.cached extension
621 // 2. same as 1. but in temp path
622 // 3. otherwise or if cache load failed, load it from MS.
623
624 fi = fsys.OpenFile(bookfile.GetLocation() + wxT(".cached"));
625
626 if (fi == NULL ||
627 fi->GetModificationTime() < bookfile.GetModificationTime() ||
628 !LoadCachedBook(bookr, fi->GetStream()))
629 {
630 if (fi != NULL) delete fi;
631 fi = fsys.OpenFile(m_TempPath + wxFileNameFromPath(bookfile.GetLocation()) + wxT(".cached"));
632 if (m_TempPath == wxEmptyString || fi == NULL ||
633 fi->GetModificationTime() < bookfile.GetModificationTime() ||
634 !LoadCachedBook(bookr, fi->GetStream()))
635 {
636 LoadMSProject(bookr, fsys, indexfile, contfile);
637 if (m_TempPath != wxEmptyString)
638 {
639 wxFileOutputStream *outs = new wxFileOutputStream(m_TempPath +
640 SafeFileName(wxFileNameFromPath(bookfile.GetLocation())) + wxT(".cached"));
641 SaveCachedBook(bookr, outs);
642 delete outs;
643 }
644 }
645 }
646
647 if (fi != NULL) delete fi;
648
649 // Now store the contents range
650 bookr->SetContentsRange(cont_start, m_ContentsCnt);
651
652 // Convert encoding, if neccessary:
653 if (encoding != wxFONTENCODING_SYSTEM)
654 {
655 wxFontEncodingArray a = wxEncodingConverter::GetPlatformEquivalents(encoding);
656 if (a.GetCount() != 0 && a[0] != encoding)
657 {
658 int i;
659 wxEncodingConverter conv;
660 conv.Init(encoding, a[0]);
661
662 for (i = IndexOld; i < m_IndexCnt; i++)
663 conv.Convert(m_Index[i].m_Name);
664 for (i = ContentsOld; i < m_ContentsCnt; i++)
665 conv.Convert(m_Contents[i].m_Name);
666 }
667 }
668
669 m_BookRecords.Add(bookr);
670 if (m_IndexCnt > 0)
671 qsort(m_Index, m_IndexCnt, sizeof(wxHtmlContentsItem), IndexCompareFunc);
672
673 return TRUE;
674 }
675
676
677 bool wxHtmlHelpData::AddBook(const wxString& book)
678 {
679 if (book.Right(4).Lower() == wxT(".zip") ||
680 book.Right(4).Lower() == wxT(".htb") /*html book*/)
681
682 {
683 wxFileSystem fsys;
684 wxString s;
685 bool rt = FALSE;
686
687 s = fsys.FindFirst(book + wxT("#zip:") + wxT("*.hhp"), wxFILE);
688 while (!s.IsEmpty())
689 {
690 if (AddBook(s)) rt = TRUE;
691 s = fsys.FindNext();
692 }
693
694 return rt;
695 }
696
697
698 else
699 {
700 wxFSFile *fi;
701 wxFileSystem fsys;
702 wxInputStream *s;
703 wxString bookFull;
704
705 int sz;
706 char *buff, *lineptr;
707 char linebuf[300];
708
709 wxString title = _("noname"),
710 safetitle,
711 start = wxEmptyString,
712 contents = wxEmptyString,
713 index = wxEmptyString,
714 charset = wxEmptyString;
715
716 if (wxIsAbsolutePath(book)) bookFull = book;
717 else bookFull = wxGetCwd() + "/" + book;
718
719 fi = fsys.OpenFile(bookFull);
720 if (fi == NULL)
721 {
722 wxLogError(_("Cannot open HTML help book: %s"), bookFull.c_str());
723 return FALSE;
724 }
725 fsys.ChangePathTo(bookFull);
726 s = fi->GetStream();
727 sz = s->GetSize();
728 buff = new char[sz + 1];
729 buff[sz] = 0;
730 s->Read(buff, sz);
731 lineptr = buff;
732
733 do {
734 lineptr = ReadLine(lineptr, linebuf);
735
736 if (strstr(linebuf, "Title=") == linebuf)
737 title = linebuf + strlen("Title=");
738 if (strstr(linebuf, "Default topic=") == linebuf)
739 start = linebuf + strlen("Default topic=");
740 if (strstr(linebuf, "Index file=") == linebuf)
741 index = linebuf + strlen("Index file=");
742 if (strstr(linebuf, "Contents file=") == linebuf)
743 contents = linebuf + strlen("Contents file=");
744 if (strstr(linebuf, "Charset=") == linebuf)
745 charset = linebuf + strlen("Charset=");
746 } while (lineptr != NULL);
747 delete[] buff;
748
749 wxFontEncoding enc;
750 if (charset == wxEmptyString) enc = wxFONTENCODING_SYSTEM;
751 else enc = wxTheFontMapper->CharsetToEncoding(charset);
752 bool rtval = AddBookParam(*fi, enc,
753 title, contents, index, start, fsys.GetPath());
754 delete fi;
755 return rtval;
756 }
757 }
758
759 wxString wxHtmlHelpData::FindPageByName(const wxString& x)
760 {
761 int cnt;
762 int i;
763 wxFileSystem fsys;
764 wxFSFile *f;
765 wxString url(wxEmptyString);
766
767 /* 1. try to open given file: */
768
769 cnt = m_BookRecords.GetCount();
770 for (i = 0; i < cnt; i++)
771 {
772 f = fsys.OpenFile(m_BookRecords[i].GetBasePath() + x);
773 if (f)
774 {
775 url = m_BookRecords[i].GetBasePath() + x;
776 delete f;
777 return url;
778 }
779 }
780
781
782 /* 2. try to find a book: */
783
784 for (i = 0; i < cnt; i++)
785 {
786 if (m_BookRecords[i].GetTitle() == x)
787 {
788 url = m_BookRecords[i].GetBasePath() + m_BookRecords[i].GetStart();
789 return url;
790 }
791 }
792
793 /* 3. try to find in contents: */
794
795 cnt = m_ContentsCnt;
796 for (i = 0; i < cnt; i++)
797 {
798 if (wxStrcmp(m_Contents[i].m_Name, x) == 0)
799 {
800 url = m_Contents[i].m_Book->GetBasePath() + m_Contents[i].m_Page;
801 return url;
802 }
803 }
804
805
806 /* 4. try to find in index: */
807
808 cnt = m_IndexCnt;
809 for (i = 0; i < cnt; i++)
810 {
811 if (wxStrcmp(m_Index[i].m_Name, x) == 0)
812 {
813 url = m_Index[i].m_Book->GetBasePath() + m_Index[i].m_Page;
814 return url;
815 }
816 }
817
818 return url;
819 }
820
821 wxString wxHtmlHelpData::FindPageById(int id)
822 {
823 int i;
824 wxString url(wxEmptyString);
825
826 for (i = 0; i < m_ContentsCnt; i++)
827 {
828 if (m_Contents[i].m_ID == id)
829 {
830 url = m_Contents[i].m_Book->GetBasePath() + m_Contents[i].m_Page;
831 return url;
832 }
833 }
834
835 return url;
836 }
837
838 //----------------------------------------------------------------------------------
839 // wxHtmlSearchStatus functions
840 //----------------------------------------------------------------------------------
841
842 wxHtmlSearchStatus::wxHtmlSearchStatus(wxHtmlHelpData* data, const wxString& keyword,
843 bool case_sensitive, bool whole_words_only,
844 const wxString& book)
845 {
846 m_Data = data;
847 m_Keyword = keyword;
848 wxHtmlBookRecord* bookr = NULL;
849 if (book != wxEmptyString)
850 {
851 // we have to search in a specific book. Find it first
852 int i, cnt = data->m_BookRecords.GetCount();
853 for (i = 0; i < cnt; i++)
854 if (data->m_BookRecords[i].GetTitle() == book)
855 {
856 bookr = &(data->m_BookRecords[i]);
857 m_CurIndex = bookr->GetContentsStart();
858 m_MaxIndex = bookr->GetContentsEnd();
859 break;
860 }
861 // check; we won't crash if the book doesn't exist, but it's Bad Anyway.
862 wxASSERT(bookr);
863 }
864 if (! bookr)
865 {
866 // no book specified; search all books
867 m_CurIndex = 0;
868 m_MaxIndex = m_Data->m_ContentsCnt;
869 }
870 m_Engine.LookFor(keyword, case_sensitive, whole_words_only);
871 m_Active = (m_CurIndex < m_MaxIndex);
872 m_LastPage = NULL;
873 }
874
875 bool wxHtmlSearchStatus::Search()
876 {
877 wxFSFile *file;
878 int i = m_CurIndex; // shortcut
879 bool found = FALSE;
880 wxChar *thepage;
881
882 if (!m_Active)
883 {
884 // sanity check. Illegal use, but we'll try to prevent a crash anyway
885 wxASSERT(m_Active);
886 return FALSE;
887 }
888
889 m_Name = wxEmptyString;
890 m_ContentsItem = NULL;
891 thepage = m_Data->m_Contents[i].m_Page;
892
893 m_Active = (++m_CurIndex < m_MaxIndex);
894 // check if it is same page with different anchor:
895 if (m_LastPage != NULL)
896 {
897 wxChar *p1, *p2;
898 for (p1 = thepage, p2 = m_LastPage;
899 *p1 != 0 && *p1 != _T('#') && *p1 == *p2; p1++, p2++) {}
900
901 m_LastPage = thepage;
902
903 if (*p1 == 0 || *p1 == _T('#'))
904 return FALSE;
905 }
906 else m_LastPage = thepage;
907
908 wxFileSystem fsys;
909 file = fsys.OpenFile(m_Data->m_Contents[i].m_Book->GetBasePath() + thepage);
910 if (file)
911 {
912 if (m_Engine.Scan(file->GetStream())) {
913 m_Name = m_Data->m_Contents[i].m_Name;
914 m_ContentsItem = m_Data->m_Contents + i;
915 found = TRUE;
916 }
917 delete file;
918 }
919 return found;
920 }
921
922
923
924
925
926
927
928
929 //--------------------------------------------------------------------------------
930 // wxSearchEngine
931 //--------------------------------------------------------------------------------
932
933 void wxSearchEngine::LookFor(const wxString& keyword, bool case_sensitive, bool whole_words_only)
934 {
935 m_CaseSensitive = case_sensitive;
936 m_WholeWords = whole_words_only;
937 if (m_Keyword) delete[] m_Keyword;
938 m_Keyword = new wxChar[keyword.Length() + 1];
939 wxStrcpy(m_Keyword, keyword.c_str());
940
941 if (!m_CaseSensitive)
942 {
943 for (int i = wxStrlen(m_Keyword) - 1; i >= 0; i--)
944 {
945 if ((m_Keyword[i] >= wxT('A')) && (m_Keyword[i] <= wxT('Z')))
946 m_Keyword[i] += wxT('a') - wxT('A');
947 }
948 }
949 }
950
951
952
953 #define WHITESPACE(c) (c == ' ' || c == '\n' || c == '\r' || c == '\t')
954
955 bool wxSearchEngine::Scan(wxInputStream *stream)
956 {
957 wxASSERT_MSG(m_Keyword != NULL, wxT("wxSearchEngine::LookFor must be called before scanning!"));
958
959 int i, j;
960 int lng = stream ->GetSize();
961 int wrd = wxStrlen(m_Keyword);
962 bool found = FALSE;
963 char *buf = new char[lng + 1];
964 stream->Read(buf, lng);
965 buf[lng] = 0;
966
967 if (!m_CaseSensitive)
968 for (i = 0; i < lng; i++)
969 if ((buf[i] >= 'A') && (buf[i] <= 'Z')) buf[i] += 'a' - 'A';
970
971 if (m_WholeWords)
972 {
973 for (i = 0; i < lng - wrd; i++)
974 {
975 if (WHITESPACE(buf[i])) continue;
976 j = 0;
977 while ((j < wrd) && (buf[i + j] == m_Keyword[j])) j++;
978 if (j == wrd && WHITESPACE(buf[i + j])) { found = TRUE; break; }
979 }
980 }
981
982 else
983 {
984 for (i = 0; i < lng - wrd; i++)
985 {
986 j = 0;
987 while ((j < wrd) && (buf[i + j] == m_Keyword[j])) j++;
988 if (j == wrd) { found = TRUE; break; }
989 }
990 }
991
992 delete[] buf;
993 return found;
994 }
995
996
997
998
999 #endif