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