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