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
;
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
);
106 bool HP_TagHandler::HandleTag(const wxHtmlTag
& tag
)
108 if (tag
.GetName() == wxT("UL"))
115 else if (tag
.GetName() == wxT("OBJECT"))
117 m_Name
= m_Page
= wxEmptyString
;
121 if (!m_Page
.IsEmpty())
122 /* Valid HHW's file may contain only two object tags:
124 <OBJECT type="text/site properties">
125 <param name="ImageType" value="Folder">
130 <OBJECT type="text/sitemap">
131 <param name="Name" value="main page">
132 <param name="Local" value="another.htm">
135 We're interested in the latter. !m_Page.IsEmpty() is valid
136 condition because text/site properties does not contain Local param
139 if (tag
.GetParam(wxT("TYPE")) == wxT("text/sitemap"))
141 if (m_ItemsCnt
% wxHTML_REALLOC_STEP
== 0)
142 m_Items
= (wxHtmlContentsItem
*) realloc(m_Items
,
143 (m_ItemsCnt
+ wxHTML_REALLOC_STEP
) *
144 sizeof(wxHtmlContentsItem
));
146 m_Items
[m_ItemsCnt
].m_Level
= m_Level
;
147 m_Items
[m_ItemsCnt
].m_ID
= m_ID
;
148 m_Items
[m_ItemsCnt
].m_Page
= new wxChar
[m_Page
.Length() + 1];
149 wxStrcpy(m_Items
[m_ItemsCnt
].m_Page
, m_Page
.c_str());
150 m_Items
[m_ItemsCnt
].m_Name
= new wxChar
[m_Name
.Length() + 1];
151 wxStrcpy(m_Items
[m_ItemsCnt
].m_Name
, m_Name
.c_str());
152 m_Items
[m_ItemsCnt
].m_Book
= m_Book
;
160 if (m_Name
== wxEmptyString
&& tag
.GetParam(wxT("NAME")) == wxT("Name"))
162 m_Name
= tag
.GetParam(wxT("VALUE"));
163 if (m_Name
.Find(wxT('&')) != -1)
165 #define ESCSEQ(escape, subst) \
166 { _T("&") _T(escape) _T(";"), _T("&") _T(escape) _T(" "), _T("&") _T(escape), _T(subst) }
167 static wxChar
* substitutions
[][4] =
169 ESCSEQ("quot", "\""),
171 ESCSEQ("#8220", "\""),
172 ESCSEQ("#8221", "\""),
178 ESCSEQ("#94", "^"), /* ^ */
182 ESCSEQ("iexcl", "!"),
184 ESCSEQ("cent", "¢"/* ¢ */),
185 ESCSEQ("#162", "¢"/* ¢ */),
187 ESCSEQ("trade", "(TM)"),
188 ESCSEQ("#153", "(TM)"),
189 ESCSEQ("#8482", "(TM)"),
193 ESCSEQ("brkbar", "¦"),
200 ESCSEQ("copy", "©"), /* © */
204 ESCSEQ("laquo", "«"), /* « */
209 ESCSEQ("reg", "®"), /* ® */
212 ESCSEQ("deg", "°"), /* ° */
214 ESCSEQ("plusm", "±"), /* ± */
217 ESCSEQ("acute", "´"),
219 ESCSEQ("macron", "¯"),
221 ESCSEQ("micro", "µ"), /* µ */
223 ESCSEQ("para", "¶"), /* ¶ */
226 ESCSEQ("ordm", "º"), /* º */
228 ESCSEQ("raquo", "»"), /* » */
231 ESCSEQ("iquest", "¿"), /* ¿ */
233 ESCSEQ("Agrave", "\300"/* À */),
234 ESCSEQ("#193", "\300"/* À */),
236 ESCSEQ("Acirc", "\302"/* Â */),
237 ESCSEQ("Atilde", "\303"/* Ã */),
238 ESCSEQ("Auml", "\304"/* Ä */),
239 ESCSEQ("Aring", " "),
240 ESCSEQ("AElig", " "),
241 ESCSEQ("Ccedil", "\347"/* ç */),
242 ESCSEQ("Egrave", "\310"/* È */),
243 ESCSEQ("Eacute", "\311"/* É */),
244 ESCSEQ("Ecirc", "\312"/* Ê */),
245 ESCSEQ("Euml", "\313"/* Ë */),
246 ESCSEQ("Igrave", "\314"/* Ì */),
248 ESCSEQ("Icirc", "\316"/* Î */),
249 ESCSEQ("Iuml", "\317"/* Ï */),
251 ESCSEQ("Ntilde", "\321"/* Ñ */),
252 ESCSEQ("Ograve", "\322"/* Ò */),
254 ESCSEQ("Ocirc", "\324"/* Ô */),
255 ESCSEQ("Otilde", "\325"/* Õ */),
256 ESCSEQ("Ouml", "\326"/* Ö */),
258 ESCSEQ("Oslash", " "),
259 ESCSEQ("Ugrave", "\331"/* Ù */),
261 ESCSEQ("Ucirc", " "),
262 ESCSEQ("Uuml", "\334"/* Ü */),
264 ESCSEQ("szlig", "\247"/* § */),
265 ESCSEQ("agrave","à"),
266 ESCSEQ("aacute", "\341"/* á */),
267 ESCSEQ("acirc", "\342"/* â */),
268 ESCSEQ("atilde", "\343"/* ã */),
269 ESCSEQ("auml", "\344"/* ä */),
270 ESCSEQ("aring", "a"),
271 ESCSEQ("aelig", "ae"),
272 ESCSEQ("ccedil", "\347"/* ç */),
273 ESCSEQ("egrave", "\350"/* è */),
274 ESCSEQ("eacute", "\351"/* é */),
275 ESCSEQ("ecirc", "\352"/* ê */),
276 ESCSEQ("euml", "\353"/* ë */),
277 ESCSEQ("igrave", "\354"/* ì */),
278 ESCSEQ("iacute", "\355"/* í */),
279 ESCSEQ("icirc", " "),
280 ESCSEQ("iuml", "\357"/* ï */),
282 ESCSEQ("ntilde", "\361"/* ñ */),
283 ESCSEQ("ograve", "\362"/* ò */),
284 ESCSEQ("oacute", "\363"/* ó */),
285 ESCSEQ("ocirc", "\364"/* ô */),
286 ESCSEQ("otilde", "\365"/* õ */),
287 ESCSEQ("ouml", "\366"/* ö */),
288 ESCSEQ("divide", " "),
289 ESCSEQ("oslash", " "),
290 ESCSEQ("ugrave", "\371"/* ù */),
291 ESCSEQ("uacute", "\372"/* ú */),
292 ESCSEQ("ucirc", "\373"/* û */),
293 ESCSEQ("uuml", "\374"/* ü */),
297 /* this one should ALWAYS stay the last one!!! */
304 for (int i
= 0; substitutions
[i
][0] != NULL
; i
++)
306 m_Name
.Replace(substitutions
[i
][0], substitutions
[i
][3], TRUE
);
307 m_Name
.Replace(substitutions
[i
][1], substitutions
[i
][3], TRUE
);
308 m_Name
.Replace(substitutions
[i
][2], substitutions
[i
][3], TRUE
);
312 if (tag
.GetParam(wxT("NAME")) == wxT("Local")) m_Page
= tag
.GetParam(wxT("VALUE"));
313 if (tag
.GetParam(wxT("NAME")) == wxT("ID")) tag
.ScanParam(wxT("VALUE"), wxT("%i"), &m_ID
);
320 void HP_TagHandler::WriteOut(wxHtmlContentsItem
*& array
, int& size
)
328 void HP_TagHandler::ReadIn(wxHtmlContentsItem
* array
, int size
)
337 //-----------------------------------------------------------------------------
339 //-----------------------------------------------------------------------------
341 wxString
wxHtmlBookRecord::GetFullPath(const wxString
&page
) const
343 if (wxIsAbsolutePath(page
))
346 return m_BasePath
+ page
;
351 IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpData
, wxObject
)
353 wxHtmlHelpData::wxHtmlHelpData()
355 m_TempPath
= wxEmptyString
;
363 wxHtmlHelpData::~wxHtmlHelpData()
367 m_BookRecords
.Empty();
370 for (i
= 0; i
< m_ContentsCnt
; i
++)
372 delete[] m_Contents
[i
].m_Page
;
373 delete[] m_Contents
[i
].m_Name
;
379 for (i
= 0; i
< m_IndexCnt
; i
++)
381 delete[] m_Index
[i
].m_Page
;
382 delete[] m_Index
[i
].m_Name
;
388 bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord
*book
, wxFileSystem
& fsys
, const wxString
& indexfile
, const wxString
& contentsfile
)
396 HP_TagHandler
*handler
= new HP_TagHandler(book
);
397 parser
.AddTagHandler(handler
);
399 f
= ( contentsfile
.IsEmpty() ? (wxFSFile
*) NULL
: fsys
.OpenFile(contentsfile
) );
402 sz
= f
->GetStream()->GetSize();
403 buf
= new char[sz
+ 1];
405 f
->GetStream()->Read(buf
, sz
);
407 handler
->ReadIn(m_Contents
, m_ContentsCnt
);
409 handler
->WriteOut(m_Contents
, m_ContentsCnt
);
413 wxLogError(_("Cannot open contents file: %s"), contentsfile
.c_str());
415 f
= ( indexfile
.IsEmpty() ? (wxFSFile
*) NULL
: fsys
.OpenFile(indexfile
) );
418 sz
= f
->GetStream()->GetSize();
419 buf
= new char[sz
+ 1];
421 f
->GetStream()->Read(buf
, sz
);
423 handler
->ReadIn(m_Index
, m_IndexCnt
);
425 handler
->WriteOut(m_Index
, m_IndexCnt
);
428 else if (!indexfile
.IsEmpty())
429 wxLogError(_("Cannot open index file: %s"), indexfile
.c_str());
438 #define READ_STRING(f, s, lng) { char tmpc; for (int i = 0; i < lng; i++) { f->Read(&tmpc, 1); s[i] = (wxChar)tmpc;} }
439 #define WRITE_STRING(f, s, lng) { char tmpc; for (int i = 0; i < lng; i++) { tmpc = (char)s[i]; f->Write(&tmpc, 1);} }
443 #define READ_STRING(f, s, lng) f->Read(s, lng * sizeof(char));
444 #define WRITE_STRING(f, s, lng) f->Write(s, lng * sizeof(char));
449 #define CURRENT_CACHED_BOOK_VERSION 1
451 bool wxHtmlHelpData::LoadCachedBook(wxHtmlBookRecord
*book
, wxInputStream
*f
)
457 /* load header - version info : */
459 f
->Read(&x
, sizeof(x
));
460 version
= wxINT32_SWAP_ON_BE(x
);
462 if (version
!= CURRENT_CACHED_BOOK_VERSION
)
464 wxLogError(_("Incorrect version of HTML help book"));
466 // NOTE: when adding new version, please ensure backward compatibility!
469 /* load contents : */
471 f
->Read(&x
, sizeof(x
));
473 m_ContentsCnt
+= wxINT32_SWAP_ON_BE(x
);
474 m_Contents
= (wxHtmlContentsItem
*) realloc(m_Contents
,
475 (m_ContentsCnt
/ wxHTML_REALLOC_STEP
+ 1) *
476 wxHTML_REALLOC_STEP
* sizeof(wxHtmlContentsItem
));
477 for (i
= st
; i
< m_ContentsCnt
; i
++)
479 f
->Read(&x
, sizeof(x
));
480 m_Contents
[i
].m_Level
= wxINT32_SWAP_ON_BE(x
);
481 f
->Read(&x
, sizeof(x
));
482 m_Contents
[i
].m_ID
= wxINT32_SWAP_ON_BE(x
);
483 f
->Read(&x
, sizeof(x
)); x
= wxINT32_SWAP_ON_BE(x
);
484 m_Contents
[i
].m_Name
= new wxChar
[x
];
485 READ_STRING(f
, m_Contents
[i
].m_Name
, x
);
486 f
->Read(&x
, sizeof(x
)); x
= wxINT32_SWAP_ON_BE(x
);
487 m_Contents
[i
].m_Page
= new wxChar
[x
];
488 READ_STRING(f
, m_Contents
[i
].m_Page
, x
);
489 m_Contents
[i
].m_Book
= book
;
494 f
->Read(&x
, sizeof(x
));
496 m_IndexCnt
+= wxINT32_SWAP_ON_BE(x
);
497 m_Index
= (wxHtmlContentsItem
*) realloc(m_Index
, (m_IndexCnt
/ wxHTML_REALLOC_STEP
+ 1) *
498 wxHTML_REALLOC_STEP
* sizeof(wxHtmlContentsItem
));
499 for (i
= st
; i
< m_IndexCnt
; i
++)
501 f
->Read(&x
, sizeof(x
)); x
= wxINT32_SWAP_ON_BE(x
);
502 m_Index
[i
].m_Name
= new wxChar
[x
];
503 READ_STRING(f
, m_Index
[i
].m_Name
, x
);
504 f
->Read(&x
, sizeof(x
)); x
= wxINT32_SWAP_ON_BE(x
);
505 m_Index
[i
].m_Page
= new wxChar
[x
];
506 READ_STRING(f
, m_Index
[i
].m_Page
, x
);
507 m_Index
[i
].m_Book
= book
;
513 bool wxHtmlHelpData::SaveCachedBook(wxHtmlBookRecord
*book
, wxOutputStream
*f
)
518 /* save header - version info : */
520 x
= wxINT32_SWAP_ON_BE(CURRENT_CACHED_BOOK_VERSION
);
521 f
->Write(&x
, sizeof(x
));
523 /* save contents : */
526 for (i
= 0; i
< m_ContentsCnt
; i
++) if (m_Contents
[i
].m_Book
== book
&& m_Contents
[i
].m_Level
> 0) x
++;
527 x
= wxINT32_SWAP_ON_BE(x
);
528 f
->Write(&x
, sizeof(x
));
529 for (i
= 0; i
< m_ContentsCnt
; i
++)
531 if (m_Contents
[i
].m_Book
!= book
|| m_Contents
[i
].m_Level
== 0) continue;
532 x
= wxINT32_SWAP_ON_BE(m_Contents
[i
].m_Level
);
533 f
->Write(&x
, sizeof(x
));
534 x
= wxINT32_SWAP_ON_BE(m_Contents
[i
].m_ID
);
535 f
->Write(&x
, sizeof(x
));
536 x
= wxINT32_SWAP_ON_BE(wxStrlen(m_Contents
[i
].m_Name
) + 1);
537 f
->Write(&x
, sizeof(x
));
538 WRITE_STRING(f
, m_Contents
[i
].m_Name
, x
);
539 x
= wxINT32_SWAP_ON_BE(wxStrlen(m_Contents
[i
].m_Page
) + 1);
540 f
->Write(&x
, sizeof(x
));
541 WRITE_STRING(f
, m_Contents
[i
].m_Page
, x
);
547 for (i
= 0; i
< m_IndexCnt
; i
++) if (m_Index
[i
].m_Book
== book
&& m_Index
[i
].m_Level
> 0) x
++;
548 x
= wxINT32_SWAP_ON_BE(x
);
549 f
->Write(&x
, sizeof(x
));
550 for (i
= 0; i
< m_IndexCnt
; i
++)
552 if (m_Index
[i
].m_Book
!= book
|| m_Index
[i
].m_Level
== 0) continue;
553 x
= wxINT32_SWAP_ON_BE(wxStrlen(m_Index
[i
].m_Name
) + 1);
554 f
->Write(&x
, sizeof(x
));
555 WRITE_STRING(f
, m_Index
[i
].m_Name
, x
);
556 x
= wxINT32_SWAP_ON_BE(wxStrlen(m_Index
[i
].m_Page
) + 1);
557 f
->Write(&x
, sizeof(x
));
558 WRITE_STRING(f
, m_Index
[i
].m_Page
, x
);
564 void wxHtmlHelpData::SetTempDir(const wxString
& path
)
566 if (path
== wxEmptyString
) m_TempPath
= path
;
569 if (wxIsAbsolutePath(path
)) m_TempPath
= path
;
570 else m_TempPath
= wxGetCwd() + _T("/") + path
;
572 if (m_TempPath
[m_TempPath
.Length() - 1] != _T('/'))
573 m_TempPath
<< _T('/');
579 static wxString
SafeFileName(const wxString
& s
)
582 res
.Replace(wxT("#"), wxT("_"));
583 res
.Replace(wxT(":"), wxT("_"));
584 res
.Replace(wxT("\\"), wxT("_"));
585 res
.Replace(wxT("/"), wxT("_"));
589 bool wxHtmlHelpData::AddBookParam(const wxFSFile
& bookfile
,
590 wxFontEncoding encoding
,
591 const wxString
& title
, const wxString
& contfile
,
592 const wxString
& indexfile
, const wxString
& deftopic
,
593 const wxString
& path
)
597 wxHtmlBookRecord
*bookr
;
599 int IndexOld
= m_IndexCnt
,
600 ContentsOld
= m_ContentsCnt
;
602 if (! path
.IsEmpty())
603 fsys
.ChangePathTo(path
, TRUE
);
605 bookr
= new wxHtmlBookRecord(fsys
.GetPath(), title
, deftopic
);
607 if (m_ContentsCnt
% wxHTML_REALLOC_STEP
== 0)
608 m_Contents
= (wxHtmlContentsItem
*) realloc(m_Contents
, (m_ContentsCnt
+ wxHTML_REALLOC_STEP
) * sizeof(wxHtmlContentsItem
));
609 m_Contents
[m_ContentsCnt
].m_Level
= 0;
610 m_Contents
[m_ContentsCnt
].m_ID
= 0;
611 m_Contents
[m_ContentsCnt
].m_Page
= new wxChar
[deftopic
.Length() + 1];
612 wxStrcpy(m_Contents
[m_ContentsCnt
].m_Page
, deftopic
.c_str());
613 m_Contents
[m_ContentsCnt
].m_Name
= new wxChar
[title
.Length() + 1];
614 wxStrcpy(m_Contents
[m_ContentsCnt
].m_Name
, title
.c_str());
615 m_Contents
[m_ContentsCnt
].m_Book
= bookr
;
617 // store the contents index for later
618 int cont_start
= m_ContentsCnt
++;
620 // Try to find cached binary versions:
621 // 1. save file as book, but with .hhp.cached extension
622 // 2. same as 1. but in temp path
623 // 3. otherwise or if cache load failed, load it from MS.
625 fi
= fsys
.OpenFile(bookfile
.GetLocation() + wxT(".cached"));
628 fi
->GetModificationTime() < bookfile
.GetModificationTime() ||
629 !LoadCachedBook(bookr
, fi
->GetStream()))
631 if (fi
!= NULL
) delete fi
;
632 fi
= fsys
.OpenFile(m_TempPath
+ wxFileNameFromPath(bookfile
.GetLocation()) + wxT(".cached"));
633 if (m_TempPath
== wxEmptyString
|| fi
== NULL
||
634 fi
->GetModificationTime() < bookfile
.GetModificationTime() ||
635 !LoadCachedBook(bookr
, fi
->GetStream()))
637 LoadMSProject(bookr
, fsys
, indexfile
, contfile
);
638 if (m_TempPath
!= wxEmptyString
)
640 wxFileOutputStream
*outs
= new wxFileOutputStream(m_TempPath
+
641 SafeFileName(wxFileNameFromPath(bookfile
.GetLocation())) + wxT(".cached"));
642 SaveCachedBook(bookr
, outs
);
648 if (fi
!= NULL
) delete fi
;
650 // Now store the contents range
651 bookr
->SetContentsRange(cont_start
, m_ContentsCnt
);
653 // Convert encoding, if neccessary:
654 if (encoding
!= wxFONTENCODING_SYSTEM
)
656 wxFontEncodingArray a
= wxEncodingConverter::GetPlatformEquivalents(encoding
);
657 if (a
.GetCount() != 0 && a
[0] != encoding
)
660 wxEncodingConverter conv
;
661 conv
.Init(encoding
, a
[0]);
663 for (i
= IndexOld
; i
< m_IndexCnt
; i
++)
664 conv
.Convert(m_Index
[i
].m_Name
);
665 for (i
= ContentsOld
; i
< m_ContentsCnt
; i
++)
666 conv
.Convert(m_Contents
[i
].m_Name
);
670 m_BookRecords
.Add(bookr
);
672 qsort(m_Index
, m_IndexCnt
, sizeof(wxHtmlContentsItem
), IndexCompareFunc
);
678 bool wxHtmlHelpData::AddBook(const wxString
& book
)
680 if (book
.Right(4).Lower() == wxT(".zip") ||
681 book
.Right(4).Lower() == wxT(".htb") /*html book*/)
688 s
= fsys
.FindFirst(book
+ wxT("#zip:") + wxT("*.hhp"), wxFILE
);
691 if (AddBook(s
)) rt
= TRUE
;
707 char *buff
, *lineptr
;
710 wxString title
= _("noname"),
712 start
= wxEmptyString
,
713 contents
= wxEmptyString
,
714 index
= wxEmptyString
,
715 charset
= wxEmptyString
;
717 if (wxIsAbsolutePath(book
)) bookFull
= book
;
718 else bookFull
= wxGetCwd() + "/" + book
;
720 fi
= fsys
.OpenFile(bookFull
);
723 wxLogError(_("Cannot open HTML help book: %s"), bookFull
.c_str());
726 fsys
.ChangePathTo(bookFull
);
729 buff
= new char[sz
+ 1];
735 lineptr
= ReadLine(lineptr
, linebuf
);
737 if (strstr(linebuf
, "Title=") == linebuf
)
738 title
= linebuf
+ strlen("Title=");
739 if (strstr(linebuf
, "Default topic=") == linebuf
)
740 start
= linebuf
+ strlen("Default topic=");
741 if (strstr(linebuf
, "Index file=") == linebuf
)
742 index
= linebuf
+ strlen("Index file=");
743 if (strstr(linebuf
, "Contents file=") == linebuf
)
744 contents
= linebuf
+ strlen("Contents file=");
745 if (strstr(linebuf
, "Charset=") == linebuf
)
746 charset
= linebuf
+ strlen("Charset=");
747 } while (lineptr
!= NULL
);
751 if (charset
== wxEmptyString
) enc
= wxFONTENCODING_SYSTEM
;
752 else enc
= wxTheFontMapper
->CharsetToEncoding(charset
);
753 bool rtval
= AddBookParam(*fi
, enc
,
754 title
, contents
, index
, start
, fsys
.GetPath());
760 wxString
wxHtmlHelpData::FindPageByName(const wxString
& x
)
766 wxString
url(wxEmptyString
);
768 /* 1. try to open given file: */
770 cnt
= m_BookRecords
.GetCount();
771 for (i
= 0; i
< cnt
; i
++)
773 f
= fsys
.OpenFile(m_BookRecords
[i
].GetFullPath(x
));
776 url
= m_BookRecords
[i
].GetFullPath(x
);
783 /* 2. try to find a book: */
785 for (i
= 0; i
< cnt
; i
++)
787 if (m_BookRecords
[i
].GetTitle() == x
)
789 url
= m_BookRecords
[i
].GetFullPath(m_BookRecords
[i
].GetStart());
794 /* 3. try to find in contents: */
797 for (i
= 0; i
< cnt
; i
++)
799 if (wxStrcmp(m_Contents
[i
].m_Name
, x
) == 0)
801 url
= m_Contents
[i
].GetFullPath();
807 /* 4. try to find in index: */
810 for (i
= 0; i
< cnt
; i
++)
812 if (wxStrcmp(m_Index
[i
].m_Name
, x
) == 0)
814 url
= m_Index
[i
].GetFullPath();
822 wxString
wxHtmlHelpData::FindPageById(int id
)
825 wxString
url(wxEmptyString
);
827 for (i
= 0; i
< m_ContentsCnt
; i
++)
829 if (m_Contents
[i
].m_ID
== id
)
831 url
= m_Contents
[i
].GetFullPath();
839 //----------------------------------------------------------------------------------
840 // wxHtmlSearchStatus functions
841 //----------------------------------------------------------------------------------
843 wxHtmlSearchStatus::wxHtmlSearchStatus(wxHtmlHelpData
* data
, const wxString
& keyword
,
844 bool case_sensitive
, bool whole_words_only
,
845 const wxString
& book
)
849 wxHtmlBookRecord
* bookr
= NULL
;
850 if (book
!= wxEmptyString
)
852 // we have to search in a specific book. Find it first
853 int i
, cnt
= data
->m_BookRecords
.GetCount();
854 for (i
= 0; i
< cnt
; i
++)
855 if (data
->m_BookRecords
[i
].GetTitle() == book
)
857 bookr
= &(data
->m_BookRecords
[i
]);
858 m_CurIndex
= bookr
->GetContentsStart();
859 m_MaxIndex
= bookr
->GetContentsEnd();
862 // check; we won't crash if the book doesn't exist, but it's Bad Anyway.
867 // no book specified; search all books
869 m_MaxIndex
= m_Data
->m_ContentsCnt
;
871 m_Engine
.LookFor(keyword
, case_sensitive
, whole_words_only
);
872 m_Active
= (m_CurIndex
< m_MaxIndex
);
876 bool wxHtmlSearchStatus::Search()
879 int i
= m_CurIndex
; // shortcut
885 // sanity check. Illegal use, but we'll try to prevent a crash anyway
890 m_Name
= wxEmptyString
;
891 m_ContentsItem
= NULL
;
892 thepage
= m_Data
->m_Contents
[i
].m_Page
;
894 m_Active
= (++m_CurIndex
< m_MaxIndex
);
895 // check if it is same page with different anchor:
896 if (m_LastPage
!= NULL
)
899 for (p1
= thepage
, p2
= m_LastPage
;
900 *p1
!= 0 && *p1
!= _T('#') && *p1
== *p2
; p1
++, p2
++) {}
902 m_LastPage
= thepage
;
904 if (*p1
== 0 || *p1
== _T('#'))
907 else m_LastPage
= thepage
;
910 file
= fsys
.OpenFile(m_Data
->m_Contents
[i
].m_Book
->GetFullPath(thepage
));
913 if (m_Engine
.Scan(file
->GetStream()))
915 m_Name
= m_Data
->m_Contents
[i
].m_Name
;
916 m_ContentsItem
= m_Data
->m_Contents
+ i
;
931 //--------------------------------------------------------------------------------
933 //--------------------------------------------------------------------------------
935 void wxSearchEngine::LookFor(const wxString
& keyword
, bool case_sensitive
, bool whole_words_only
)
937 m_CaseSensitive
= case_sensitive
;
938 m_WholeWords
= whole_words_only
;
939 if (m_Keyword
) delete[] m_Keyword
;
940 m_Keyword
= new wxChar
[keyword
.Length() + 1];
941 wxStrcpy(m_Keyword
, keyword
.c_str());
943 if (!m_CaseSensitive
)
945 for (int i
= wxStrlen(m_Keyword
) - 1; i
>= 0; i
--)
947 if ((m_Keyword
[i
] >= wxT('A')) && (m_Keyword
[i
] <= wxT('Z')))
948 m_Keyword
[i
] += wxT('a') - wxT('A');
955 #define WHITESPACE(c) (c == ' ' || c == '\n' || c == '\r' || c == '\t')
957 bool wxSearchEngine::Scan(wxInputStream
*stream
)
959 wxASSERT_MSG(m_Keyword
!= NULL
, wxT("wxSearchEngine::LookFor must be called before scanning!"));
962 int lng
= stream
->GetSize();
963 int wrd
= wxStrlen(m_Keyword
);
965 char *buf
= new char[lng
+ 1];
966 stream
->Read(buf
, lng
);
969 if (!m_CaseSensitive
)
970 for (i
= 0; i
< lng
; i
++)
971 if ((buf
[i
] >= 'A') && (buf
[i
] <= 'Z')) buf
[i
] += 'a' - 'A';
975 for (i
= 0; i
< lng
- wrd
; i
++)
977 if (WHITESPACE(buf
[i
])) continue;
979 while ((j
< wrd
) && (buf
[i
+ j
] == m_Keyword
[j
])) j
++;
980 if (j
== wrd
&& WHITESPACE(buf
[i
+ j
])) { found
= TRUE
; break; }
986 for (i
= 0; i
< lng
- wrd
; i
++)
989 while ((j
< wrd
) && (buf
[i
+ j
] == m_Keyword
[j
])) j
++;
990 if (j
== wrd
) { found
= TRUE
; break; }