1 /////////////////////////////////////////////////////////////////////////////
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
8 // Copyright: (c) Harm van der Heijden and Vaclav Slavik
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #if wxUSE_HTML && wxUSE_STREAMS
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"
38 #include "wx/html/htmlpars.h"
39 #include "wx/html/htmldefs.h"
41 #include "wx/arrimpl.cpp"
42 WX_DEFINE_OBJARRAY(wxHtmlBookRecArray
)
44 //-----------------------------------------------------------------------------
45 // static helper functions
46 //-----------------------------------------------------------------------------
48 // Reads one line, stores it into buf and returns pointer to new line or NULL.
49 static char* ReadLine(char *line
, char *buf
)
51 char *writeptr
= buf
, *readptr
= line
;
53 while (*readptr
!= 0 && *readptr
!= '\r' && *readptr
!= '\n') *(writeptr
++) = *(readptr
++);
55 while (*readptr
== '\r' || *readptr
== '\n') readptr
++;
56 if (*readptr
== 0) return NULL
;
62 static int LINKAGEMODE
IndexCompareFunc(const void *a
, const void *b
)
64 return wxStrcmp(((wxHtmlContentsItem
*)a
)->m_Name
, ((wxHtmlContentsItem
*)b
)->m_Name
);
68 //-----------------------------------------------------------------------------
70 //-----------------------------------------------------------------------------
72 class HP_Parser
: public wxHtmlParser
75 void AddText(const char* WXUNUSED(text
)) { }
76 wxObject
* GetProduct() { return NULL
; }
80 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
84 class HP_TagHandler
: public wxHtmlTagHandler
87 wxString m_Name
, m_Page
;
91 wxHtmlContentsItem
*m_Items
;
93 wxHtmlBookRecord
*m_Book
;
94 bool m_firstTime
; // For checking if we're adding sections at level zero, so we 'delete' the first one
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
);
107 bool HP_TagHandler::HandleTag(const wxHtmlTag
& tag
)
109 if (tag
.GetName() == wxT("UL"))
116 else if (tag
.GetName() == wxT("OBJECT"))
118 m_Name
= m_Page
= wxEmptyString
;
121 if (tag
.GetParam("TYPE") == "text/sitemap")
123 // if (!m_Page.IsEmpty())
124 /* Valid HHW's file may contain only two object tags:
126 <OBJECT type="text/site properties">
127 <param name="ImageType" value="Folder">
132 <OBJECT type="text/sitemap">
133 <param name="Name" value="main page">
134 <param name="Local" value="another.htm">
137 We're interested in the latter. !m_Page.IsEmpty() is valid
138 condition because text/site properties does not contain Local param
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
144 if (m_firstTime
&& (m_Level
== 0) && (m_ItemsCnt
> 0))
150 if (m_ItemsCnt
% wxHTML_REALLOC_STEP
== 0)
151 m_Items
= (wxHtmlContentsItem
*) realloc(m_Items
, (m_ItemsCnt
+ wxHTML_REALLOC_STEP
) * sizeof(wxHtmlContentsItem
));
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
;
169 if (m_Name
== wxEmptyString
&& tag
.GetParam(wxT("NAME")) == wxT("Name"))
171 m_Name
= tag
.GetParam(wxT("VALUE"));
172 if (m_Name
.Find(wxT('&')) != -1)
174 #define ESCSEQ(escape, subst) \
175 { _T("&") _T(escape) _T(";"), _T("&") _T(escape) _T(" "), _T("&") _T(escape), _T(subst) }
176 static wxChar
* substitutions
[][4] =
178 ESCSEQ("quot", "\""),
185 ESCSEQ("#94", "^"), /* ^ */
189 ESCSEQ("iexcl", "!"),
191 ESCSEQ("cent", "¢"/* ¢ */),
192 ESCSEQ("#162", "¢"/* ¢ */),
194 ESCSEQ("trade", "(TM)"),
195 ESCSEQ("#153", "(TM)"),
199 ESCSEQ("brkbar", "¦"),
206 ESCSEQ("copy", "©"), /* © */
210 ESCSEQ("laquo", "«"), /* « */
215 ESCSEQ("reg", "®"), /* ® */
218 ESCSEQ("deg", "°"), /* ° */
220 ESCSEQ("plusm", "±"), /* ± */
223 ESCSEQ("acute", "´"),
225 ESCSEQ("macron", "¯"),
227 ESCSEQ("micro", "µ"), /* µ */
229 ESCSEQ("para", "¶"), /* ¶ */
232 ESCSEQ("ordm", "º"), /* º */
234 ESCSEQ("raquo", "»"), /* » */
237 ESCSEQ("iquest", "¿"), /* ¿ */
239 ESCSEQ("Agrave", "\300"/* À */),
240 ESCSEQ("#193", "\300"/* À */),
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"/* Ì */),
254 ESCSEQ("Icirc", "\316"/* Î */),
255 ESCSEQ("Iuml", "\317"/* Ï */),
257 ESCSEQ("Ntilde", "\321"/* Ñ */),
258 ESCSEQ("Ograve", "\322"/* Ò */),
260 ESCSEQ("Ocirc", "\324"/* Ô */),
261 ESCSEQ("Otilde", "\325"/* Õ */),
262 ESCSEQ("Ouml", "\326"/* Ö */),
264 ESCSEQ("Oslash", " "),
265 ESCSEQ("Ugrave", "\331"/* Ù */),
267 ESCSEQ("Ucirc", " "),
268 ESCSEQ("Uuml", "\334"/* Ü */),
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"/* ï */),
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"/* ü */),
306 /* this one should ALWAYS stay the last one!!! */
313 for (int i
= 0; substitutions
[i
][0] != NULL
; i
++)
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
);
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
);
329 void HP_TagHandler::WriteOut(wxHtmlContentsItem
*& array
, int& size
)
337 void HP_TagHandler::ReadIn(wxHtmlContentsItem
* array
, int size
)
346 //-----------------------------------------------------------------------------
348 //-----------------------------------------------------------------------------
350 IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpData
, wxObject
)
352 wxHtmlHelpData::wxHtmlHelpData()
354 m_TempPath
= wxEmptyString
;
362 wxHtmlHelpData::~wxHtmlHelpData()
366 m_BookRecords
.Empty();
369 for (i
= 0; i
< m_ContentsCnt
; i
++)
371 delete[] m_Contents
[i
].m_Page
;
372 delete[] m_Contents
[i
].m_Name
;
378 for (i
= 0; i
< m_IndexCnt
; i
++)
380 delete[] m_Index
[i
].m_Page
;
381 delete[] m_Index
[i
].m_Name
;
387 bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord
*book
, wxFileSystem
& fsys
, const wxString
& indexfile
, const wxString
& contentsfile
)
395 HP_TagHandler
*handler
= new HP_TagHandler(book
);
396 parser
.AddTagHandler(handler
);
398 f
= ( contentsfile
.IsEmpty() ? (wxFSFile
*) NULL
: fsys
.OpenFile(contentsfile
) );
401 sz
= f
->GetStream()->GetSize();
402 buf
= new char[sz
+ 1];
404 f
->GetStream()->Read(buf
, sz
);
406 handler
->ReadIn(m_Contents
, m_ContentsCnt
);
408 handler
->WriteOut(m_Contents
, m_ContentsCnt
);
412 wxLogError(_("Cannot open contents file: %s"), contentsfile
.c_str());
414 f
= ( indexfile
.IsEmpty() ? (wxFSFile
*) NULL
: fsys
.OpenFile(indexfile
) );
417 sz
= f
->GetStream()->GetSize();
418 buf
= new char[sz
+ 1];
420 f
->GetStream()->Read(buf
, sz
);
422 handler
->ReadIn(m_Index
, m_IndexCnt
);
424 handler
->WriteOut(m_Index
, m_IndexCnt
);
427 else if (!indexfile
.IsEmpty())
428 wxLogError(_("Cannot open index file: %s"), indexfile
.c_str());
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);} }
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));
448 #define CURRENT_CACHED_BOOK_VERSION 1
450 bool wxHtmlHelpData::LoadCachedBook(wxHtmlBookRecord
*book
, wxInputStream
*f
)
456 /* load header - version info : */
458 f
->Read(&x
, sizeof(x
));
459 version
= wxINT32_SWAP_ON_BE(x
);
461 if (version
!= CURRENT_CACHED_BOOK_VERSION
)
463 wxLogError(_("Incorrect version of HTML help book"));
465 // NOTE: when adding new version, please ensure backward compatibility!
468 /* load contents : */
470 f
->Read(&x
, sizeof(x
));
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
++)
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
;
493 f
->Read(&x
, sizeof(x
));
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
++)
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
;
512 bool wxHtmlHelpData::SaveCachedBook(wxHtmlBookRecord
*book
, wxOutputStream
*f
)
517 /* save header - version info : */
519 x
= wxINT32_SWAP_ON_BE(CURRENT_CACHED_BOOK_VERSION
);
520 f
->Write(&x
, sizeof(x
));
522 /* save contents : */
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
++)
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
);
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
++)
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
);
563 void wxHtmlHelpData::SetTempDir(const wxString
& path
)
565 if (path
== wxEmptyString
) m_TempPath
= path
;
568 if (wxIsAbsolutePath(path
)) m_TempPath
= path
;
569 else m_TempPath
= wxGetCwd() + _T("/") + path
;
571 if (m_TempPath
[m_TempPath
.Length() - 1] != _T('/'))
572 m_TempPath
<< _T('/');
578 static wxString
SafeFileName(const wxString
& s
)
581 res
.Replace(wxT("#"), wxT("_"));
582 res
.Replace(wxT(":"), wxT("_"));
583 res
.Replace(wxT("\\"), wxT("_"));
584 res
.Replace(wxT("/"), wxT("_"));
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
)
596 wxHtmlBookRecord
*bookr
;
598 int IndexOld
= m_IndexCnt
,
599 ContentsOld
= m_ContentsCnt
;
601 if (! path
.IsEmpty())
602 fsys
.ChangePathTo(path
, TRUE
);
604 bookr
= new wxHtmlBookRecord(fsys
.GetPath(), title
, deftopic
);
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
;
616 // store the contents index for later
617 int cont_start
= m_ContentsCnt
++;
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.
624 fi
= fsys
.OpenFile(bookfile
.GetLocation() + wxT(".cached"));
627 fi
->GetModificationTime() < bookfile
.GetModificationTime() ||
628 !LoadCachedBook(bookr
, fi
->GetStream()))
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()))
636 LoadMSProject(bookr
, fsys
, indexfile
, contfile
);
637 if (m_TempPath
!= wxEmptyString
)
639 wxFileOutputStream
*outs
= new wxFileOutputStream(m_TempPath
+
640 SafeFileName(wxFileNameFromPath(bookfile
.GetLocation())) + wxT(".cached"));
641 SaveCachedBook(bookr
, outs
);
647 if (fi
!= NULL
) delete fi
;
649 // Now store the contents range
650 bookr
->SetContentsRange(cont_start
, m_ContentsCnt
);
652 // Convert encoding, if neccessary:
653 if (encoding
!= wxFONTENCODING_SYSTEM
)
655 wxFontEncodingArray a
= wxEncodingConverter::GetPlatformEquivalents(encoding
);
656 if (a
.GetCount() != 0 && a
[0] != encoding
)
659 wxEncodingConverter conv
;
660 conv
.Init(encoding
, a
[0]);
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
);
669 m_BookRecords
.Add(bookr
);
671 qsort(m_Index
, m_IndexCnt
, sizeof(wxHtmlContentsItem
), IndexCompareFunc
);
677 bool wxHtmlHelpData::AddBook(const wxString
& book
)
679 if (book
.Right(4).Lower() == wxT(".zip") ||
680 book
.Right(4).Lower() == wxT(".htb") /*html book*/)
687 s
= fsys
.FindFirst(book
+ wxT("#zip:") + wxT("*.hhp"), wxFILE
);
690 if (AddBook(s
)) rt
= TRUE
;
706 char *buff
, *lineptr
;
709 wxString title
= _("noname"),
711 start
= wxEmptyString
,
712 contents
= wxEmptyString
,
713 index
= wxEmptyString
,
714 charset
= wxEmptyString
;
716 if (wxIsAbsolutePath(book
)) bookFull
= book
;
717 else bookFull
= wxGetCwd() + "/" + book
;
719 fi
= fsys
.OpenFile(bookFull
);
722 wxLogError(_("Cannot open HTML help book: %s"), bookFull
.c_str());
725 fsys
.ChangePathTo(bookFull
);
728 buff
= new char[sz
+ 1];
734 lineptr
= ReadLine(lineptr
, linebuf
);
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
);
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());
759 wxString
wxHtmlHelpData::FindPageByName(const wxString
& x
)
765 wxString
url(wxEmptyString
);
767 /* 1. try to open given file: */
769 cnt
= m_BookRecords
.GetCount();
770 for (i
= 0; i
< cnt
; i
++)
772 f
= fsys
.OpenFile(m_BookRecords
[i
].GetBasePath() + x
);
775 url
= m_BookRecords
[i
].GetBasePath() + x
;
782 /* 2. try to find a book: */
784 for (i
= 0; i
< cnt
; i
++)
786 if (m_BookRecords
[i
].GetTitle() == x
)
788 url
= m_BookRecords
[i
].GetBasePath() + m_BookRecords
[i
].GetStart();
793 /* 3. try to find in contents: */
796 for (i
= 0; i
< cnt
; i
++)
798 if (wxStrcmp(m_Contents
[i
].m_Name
, x
) == 0)
800 url
= m_Contents
[i
].m_Book
->GetBasePath() + m_Contents
[i
].m_Page
;
806 /* 4. try to find in index: */
809 for (i
= 0; i
< cnt
; i
++)
811 if (wxStrcmp(m_Index
[i
].m_Name
, x
) == 0)
813 url
= m_Index
[i
].m_Book
->GetBasePath() + m_Index
[i
].m_Page
;
821 wxString
wxHtmlHelpData::FindPageById(int id
)
824 wxString
url(wxEmptyString
);
826 for (i
= 0; i
< m_ContentsCnt
; i
++)
828 if (m_Contents
[i
].m_ID
== id
)
830 url
= m_Contents
[i
].m_Book
->GetBasePath() + m_Contents
[i
].m_Page
;
838 //----------------------------------------------------------------------------------
839 // wxHtmlSearchStatus functions
840 //----------------------------------------------------------------------------------
842 wxHtmlSearchStatus::wxHtmlSearchStatus(wxHtmlHelpData
* data
, const wxString
& keyword
,
843 bool case_sensitive
, bool whole_words_only
,
844 const wxString
& book
)
848 wxHtmlBookRecord
* bookr
= NULL
;
849 if (book
!= wxEmptyString
)
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
)
856 bookr
= &(data
->m_BookRecords
[i
]);
857 m_CurIndex
= bookr
->GetContentsStart();
858 m_MaxIndex
= bookr
->GetContentsEnd();
861 // check; we won't crash if the book doesn't exist, but it's Bad Anyway.
866 // no book specified; search all books
868 m_MaxIndex
= m_Data
->m_ContentsCnt
;
870 m_Engine
.LookFor(keyword
, case_sensitive
, whole_words_only
);
871 m_Active
= (m_CurIndex
< m_MaxIndex
);
875 bool wxHtmlSearchStatus::Search()
878 int i
= m_CurIndex
; // shortcut
884 // sanity check. Illegal use, but we'll try to prevent a crash anyway
889 m_Name
= wxEmptyString
;
890 m_ContentsItem
= NULL
;
891 thepage
= m_Data
->m_Contents
[i
].m_Page
;
893 m_Active
= (++m_CurIndex
< m_MaxIndex
);
894 // check if it is same page with different anchor:
895 if (m_LastPage
!= NULL
)
898 for (p1
= thepage
, p2
= m_LastPage
;
899 *p1
!= 0 && *p1
!= _T('#') && *p1
== *p2
; p1
++, p2
++) {}
901 m_LastPage
= thepage
;
903 if (*p1
== 0 || *p1
== _T('#'))
906 else m_LastPage
= thepage
;
909 file
= fsys
.OpenFile(m_Data
->m_Contents
[i
].m_Book
->GetBasePath() + thepage
);
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
;
929 //--------------------------------------------------------------------------------
931 //--------------------------------------------------------------------------------
933 void wxSearchEngine::LookFor(const wxString
& keyword
, bool case_sensitive
, bool whole_words_only
)
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());
941 if (!m_CaseSensitive
)
943 for (int i
= wxStrlen(m_Keyword
) - 1; i
>= 0; i
--)
945 if ((m_Keyword
[i
] >= wxT('A')) && (m_Keyword
[i
] <= wxT('Z')))
946 m_Keyword
[i
] += wxT('a') - wxT('A');
953 #define WHITESPACE(c) (c == ' ' || c == '\n' || c == '\r' || c == '\t')
955 bool wxSearchEngine::Scan(wxInputStream
*stream
)
957 wxASSERT_MSG(m_Keyword
!= NULL
, wxT("wxSearchEngine::LookFor must be called before scanning!"));
960 int lng
= stream
->GetSize();
961 int wrd
= wxStrlen(m_Keyword
);
963 char *buf
= new char[lng
+ 1];
964 stream
->Read(buf
, lng
);
967 if (!m_CaseSensitive
)
968 for (i
= 0; i
< lng
; i
++)
969 if ((buf
[i
] >= 'A') && (buf
[i
] <= 'Z')) buf
[i
] += 'a' - 'A';
973 for (i
= 0; i
< lng
- wrd
; i
++)
975 if (WHITESPACE(buf
[i
])) continue;
977 while ((j
< wrd
) && (buf
[i
+ j
] == m_Keyword
[j
])) j
++;
978 if (j
== wrd
&& WHITESPACE(buf
[i
+ j
])) { found
= TRUE
; break; }
984 for (i
= 0; i
< lng
- wrd
; i
++)
987 while ((j
< wrd
) && (buf
[i
+ j
] == m_Keyword
[j
])) j
++;
988 if (j
== wrd
) { found
= TRUE
; break; }