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", "\""),
176 ESCSEQ("#94", "^"), /* ^ */
180 ESCSEQ("iexcl", "!"),
182 ESCSEQ("cent", "¢"/* ¢ */),
183 ESCSEQ("#162", "¢"/* ¢ */),
185 ESCSEQ("trade", "(TM)"),
186 ESCSEQ("#153", "(TM)"),
190 ESCSEQ("brkbar", "¦"),
197 ESCSEQ("copy", "©"), /* © */
201 ESCSEQ("laquo", "«"), /* « */
206 ESCSEQ("reg", "®"), /* ® */
209 ESCSEQ("deg", "°"), /* ° */
211 ESCSEQ("plusm", "±"), /* ± */
214 ESCSEQ("acute", "´"),
216 ESCSEQ("macron", "¯"),
218 ESCSEQ("micro", "µ"), /* µ */
220 ESCSEQ("para", "¶"), /* ¶ */
223 ESCSEQ("ordm", "º"), /* º */
225 ESCSEQ("raquo", "»"), /* » */
228 ESCSEQ("iquest", "¿"), /* ¿ */
230 ESCSEQ("Agrave", "\300"/* À */),
231 ESCSEQ("#193", "\300"/* À */),
233 ESCSEQ("Acirc", "\302"/* Â */),
234 ESCSEQ("Atilde", "\303"/* Ã */),
235 ESCSEQ("Auml", "\304"/* Ä */),
236 ESCSEQ("Aring", " "),
237 ESCSEQ("AElig", " "),
238 ESCSEQ("Ccedil", "\347"/* ç */),
239 ESCSEQ("Egrave", "\310"/* È */),
240 ESCSEQ("Eacute", "\311"/* É */),
241 ESCSEQ("Ecirc", "\312"/* Ê */),
242 ESCSEQ("Euml", "\313"/* Ë */),
243 ESCSEQ("Igrave", "\314"/* Ì */),
245 ESCSEQ("Icirc", "\316"/* Î */),
246 ESCSEQ("Iuml", "\317"/* Ï */),
248 ESCSEQ("Ntilde", "\321"/* Ñ */),
249 ESCSEQ("Ograve", "\322"/* Ò */),
251 ESCSEQ("Ocirc", "\324"/* Ô */),
252 ESCSEQ("Otilde", "\325"/* Õ */),
253 ESCSEQ("Ouml", "\326"/* Ö */),
255 ESCSEQ("Oslash", " "),
256 ESCSEQ("Ugrave", "\331"/* Ù */),
258 ESCSEQ("Ucirc", " "),
259 ESCSEQ("Uuml", "\334"/* Ü */),
261 ESCSEQ("szlig", "\247"/* § */),
262 ESCSEQ("agrave;","à"),
263 ESCSEQ("aacute", "\341"/* á */),
264 ESCSEQ("acirc", "\342"/* â */),
265 ESCSEQ("atilde", "\343"/* ã */),
266 ESCSEQ("auml", "\344"/* ä */),
267 ESCSEQ("aring", "a"),
268 ESCSEQ("aelig", "ae"),
269 ESCSEQ("ccedil", "\347"/* ç */),
270 ESCSEQ("egrave", "\350"/* è */),
271 ESCSEQ("eacute", "\351"/* é */),
272 ESCSEQ("ecirc", "\352"/* ê */),
273 ESCSEQ("euml", "\353"/* ë */),
274 ESCSEQ("igrave", "\354"/* ì */),
275 ESCSEQ("iacute", "\355"/* í */),
276 ESCSEQ("icirc", " "),
277 ESCSEQ("iuml", "\357"/* ï */),
279 ESCSEQ("ntilde", "\361"/* ñ */),
280 ESCSEQ("ograve", "\362"/* ò */),
281 ESCSEQ("oacute", "\363"/* ó */),
282 ESCSEQ("ocirc", "\364"/* ô */),
283 ESCSEQ("otilde", "\365"/* õ */),
284 ESCSEQ("ouml", "\366"/* ö */),
285 ESCSEQ("divide", " "),
286 ESCSEQ("oslash", " "),
287 ESCSEQ("ugrave", "\371"/* ù */),
288 ESCSEQ("uacute", "\372"/* ú */),
289 ESCSEQ("ucirc", "\373"/* û */),
290 ESCSEQ("uuml", "\374"/* ü */),
294 /* this one should ALWAYS stay the last one!!! */
301 for (int i
= 0; substitutions
[i
][0] != NULL
; i
++)
303 m_Name
.Replace(substitutions
[i
][0], substitutions
[i
][3], TRUE
);
304 m_Name
.Replace(substitutions
[i
][1], substitutions
[i
][3], TRUE
);
305 m_Name
.Replace(substitutions
[i
][2], substitutions
[i
][3], TRUE
);
309 if (tag
.GetParam(wxT("NAME")) == wxT("Local")) m_Page
= tag
.GetParam(wxT("VALUE"));
310 if (tag
.GetParam(wxT("NAME")) == wxT("ID")) tag
.ScanParam(wxT("VALUE"), wxT("%i"), &m_ID
);
317 void HP_TagHandler::WriteOut(wxHtmlContentsItem
*& array
, int& size
)
325 void HP_TagHandler::ReadIn(wxHtmlContentsItem
* array
, int size
)
334 //-----------------------------------------------------------------------------
336 //-----------------------------------------------------------------------------
338 wxString
wxHtmlBookRecord::GetFullPath(const wxString
&page
) const
340 if (wxIsAbsolutePath(page
))
343 return m_BasePath
+ page
;
348 IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpData
, wxObject
)
350 wxHtmlHelpData::wxHtmlHelpData()
352 m_TempPath
= wxEmptyString
;
360 wxHtmlHelpData::~wxHtmlHelpData()
364 m_BookRecords
.Empty();
367 for (i
= 0; i
< m_ContentsCnt
; i
++)
369 delete[] m_Contents
[i
].m_Page
;
370 delete[] m_Contents
[i
].m_Name
;
376 for (i
= 0; i
< m_IndexCnt
; i
++)
378 delete[] m_Index
[i
].m_Page
;
379 delete[] m_Index
[i
].m_Name
;
385 bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord
*book
, wxFileSystem
& fsys
, const wxString
& indexfile
, const wxString
& contentsfile
)
393 HP_TagHandler
*handler
= new HP_TagHandler(book
);
394 parser
.AddTagHandler(handler
);
396 f
= ( contentsfile
.IsEmpty() ? (wxFSFile
*) NULL
: fsys
.OpenFile(contentsfile
) );
399 sz
= f
->GetStream()->GetSize();
400 buf
= new char[sz
+ 1];
402 f
->GetStream()->Read(buf
, sz
);
404 handler
->ReadIn(m_Contents
, m_ContentsCnt
);
406 handler
->WriteOut(m_Contents
, m_ContentsCnt
);
410 wxLogError(_("Cannot open contents file: %s"), contentsfile
.c_str());
412 f
= ( indexfile
.IsEmpty() ? (wxFSFile
*) NULL
: fsys
.OpenFile(indexfile
) );
415 sz
= f
->GetStream()->GetSize();
416 buf
= new char[sz
+ 1];
418 f
->GetStream()->Read(buf
, sz
);
420 handler
->ReadIn(m_Index
, m_IndexCnt
);
422 handler
->WriteOut(m_Index
, m_IndexCnt
);
425 else if (!indexfile
.IsEmpty())
426 wxLogError(_("Cannot open index file: %s"), indexfile
.c_str());
435 #define READ_STRING(f, s, lng) { char tmpc; for (int i = 0; i < lng; i++) { f->Read(&tmpc, 1); s[i] = (wxChar)tmpc;} }
436 #define WRITE_STRING(f, s, lng) { char tmpc; for (int i = 0; i < lng; i++) { tmpc = (char)s[i]; f->Write(&tmpc, 1);} }
440 #define READ_STRING(f, s, lng) f->Read(s, lng * sizeof(char));
441 #define WRITE_STRING(f, s, lng) f->Write(s, lng * sizeof(char));
446 #define CURRENT_CACHED_BOOK_VERSION 1
448 bool wxHtmlHelpData::LoadCachedBook(wxHtmlBookRecord
*book
, wxInputStream
*f
)
454 /* load header - version info : */
456 f
->Read(&x
, sizeof(x
));
457 version
= wxINT32_SWAP_ON_BE(x
);
459 if (version
!= CURRENT_CACHED_BOOK_VERSION
)
461 wxLogError(_("Incorrect version of HTML help book"));
463 // NOTE: when adding new version, please ensure backward compatibility!
466 /* load contents : */
468 f
->Read(&x
, sizeof(x
));
470 m_ContentsCnt
+= wxINT32_SWAP_ON_BE(x
);
471 m_Contents
= (wxHtmlContentsItem
*) realloc(m_Contents
,
472 (m_ContentsCnt
/ wxHTML_REALLOC_STEP
+ 1) *
473 wxHTML_REALLOC_STEP
* sizeof(wxHtmlContentsItem
));
474 for (i
= st
; i
< m_ContentsCnt
; i
++)
476 f
->Read(&x
, sizeof(x
));
477 m_Contents
[i
].m_Level
= wxINT32_SWAP_ON_BE(x
);
478 f
->Read(&x
, sizeof(x
));
479 m_Contents
[i
].m_ID
= wxINT32_SWAP_ON_BE(x
);
480 f
->Read(&x
, sizeof(x
)); x
= wxINT32_SWAP_ON_BE(x
);
481 m_Contents
[i
].m_Name
= new wxChar
[x
];
482 READ_STRING(f
, m_Contents
[i
].m_Name
, x
);
483 f
->Read(&x
, sizeof(x
)); x
= wxINT32_SWAP_ON_BE(x
);
484 m_Contents
[i
].m_Page
= new wxChar
[x
];
485 READ_STRING(f
, m_Contents
[i
].m_Page
, x
);
486 m_Contents
[i
].m_Book
= book
;
491 f
->Read(&x
, sizeof(x
));
493 m_IndexCnt
+= wxINT32_SWAP_ON_BE(x
);
494 m_Index
= (wxHtmlContentsItem
*) realloc(m_Index
, (m_IndexCnt
/ wxHTML_REALLOC_STEP
+ 1) *
495 wxHTML_REALLOC_STEP
* sizeof(wxHtmlContentsItem
));
496 for (i
= st
; i
< m_IndexCnt
; i
++)
498 f
->Read(&x
, sizeof(x
)); x
= wxINT32_SWAP_ON_BE(x
);
499 m_Index
[i
].m_Name
= new wxChar
[x
];
500 READ_STRING(f
, m_Index
[i
].m_Name
, x
);
501 f
->Read(&x
, sizeof(x
)); x
= wxINT32_SWAP_ON_BE(x
);
502 m_Index
[i
].m_Page
= new wxChar
[x
];
503 READ_STRING(f
, m_Index
[i
].m_Page
, x
);
504 m_Index
[i
].m_Book
= book
;
510 bool wxHtmlHelpData::SaveCachedBook(wxHtmlBookRecord
*book
, wxOutputStream
*f
)
515 /* save header - version info : */
517 x
= wxINT32_SWAP_ON_BE(CURRENT_CACHED_BOOK_VERSION
);
518 f
->Write(&x
, sizeof(x
));
520 /* save contents : */
523 for (i
= 0; i
< m_ContentsCnt
; i
++) if (m_Contents
[i
].m_Book
== book
&& m_Contents
[i
].m_Level
> 0) x
++;
524 x
= wxINT32_SWAP_ON_BE(x
);
525 f
->Write(&x
, sizeof(x
));
526 for (i
= 0; i
< m_ContentsCnt
; i
++)
528 if (m_Contents
[i
].m_Book
!= book
|| m_Contents
[i
].m_Level
== 0) continue;
529 x
= wxINT32_SWAP_ON_BE(m_Contents
[i
].m_Level
);
530 f
->Write(&x
, sizeof(x
));
531 x
= wxINT32_SWAP_ON_BE(m_Contents
[i
].m_ID
);
532 f
->Write(&x
, sizeof(x
));
533 x
= wxINT32_SWAP_ON_BE(wxStrlen(m_Contents
[i
].m_Name
) + 1);
534 f
->Write(&x
, sizeof(x
));
535 WRITE_STRING(f
, m_Contents
[i
].m_Name
, x
);
536 x
= wxINT32_SWAP_ON_BE(wxStrlen(m_Contents
[i
].m_Page
) + 1);
537 f
->Write(&x
, sizeof(x
));
538 WRITE_STRING(f
, m_Contents
[i
].m_Page
, x
);
544 for (i
= 0; i
< m_IndexCnt
; i
++) if (m_Index
[i
].m_Book
== book
&& m_Index
[i
].m_Level
> 0) x
++;
545 x
= wxINT32_SWAP_ON_BE(x
);
546 f
->Write(&x
, sizeof(x
));
547 for (i
= 0; i
< m_IndexCnt
; i
++)
549 if (m_Index
[i
].m_Book
!= book
|| m_Index
[i
].m_Level
== 0) continue;
550 x
= wxINT32_SWAP_ON_BE(wxStrlen(m_Index
[i
].m_Name
) + 1);
551 f
->Write(&x
, sizeof(x
));
552 WRITE_STRING(f
, m_Index
[i
].m_Name
, x
);
553 x
= wxINT32_SWAP_ON_BE(wxStrlen(m_Index
[i
].m_Page
) + 1);
554 f
->Write(&x
, sizeof(x
));
555 WRITE_STRING(f
, m_Index
[i
].m_Page
, x
);
561 void wxHtmlHelpData::SetTempDir(const wxString
& path
)
563 if (path
== wxEmptyString
) m_TempPath
= path
;
566 if (wxIsAbsolutePath(path
)) m_TempPath
= path
;
567 else m_TempPath
= wxGetCwd() + _T("/") + path
;
569 if (m_TempPath
[m_TempPath
.Length() - 1] != _T('/'))
570 m_TempPath
<< _T('/');
576 static wxString
SafeFileName(const wxString
& s
)
579 res
.Replace(wxT("#"), wxT("_"));
580 res
.Replace(wxT(":"), wxT("_"));
581 res
.Replace(wxT("\\"), wxT("_"));
582 res
.Replace(wxT("/"), wxT("_"));
586 bool wxHtmlHelpData::AddBookParam(const wxFSFile
& bookfile
,
587 wxFontEncoding encoding
,
588 const wxString
& title
, const wxString
& contfile
,
589 const wxString
& indexfile
, const wxString
& deftopic
,
590 const wxString
& path
)
594 wxHtmlBookRecord
*bookr
;
596 int IndexOld
= m_IndexCnt
,
597 ContentsOld
= m_ContentsCnt
;
599 if (! path
.IsEmpty())
600 fsys
.ChangePathTo(path
, TRUE
);
602 bookr
= new wxHtmlBookRecord(fsys
.GetPath(), title
, deftopic
);
604 if (m_ContentsCnt
% wxHTML_REALLOC_STEP
== 0)
605 m_Contents
= (wxHtmlContentsItem
*) realloc(m_Contents
, (m_ContentsCnt
+ wxHTML_REALLOC_STEP
) * sizeof(wxHtmlContentsItem
));
606 m_Contents
[m_ContentsCnt
].m_Level
= 0;
607 m_Contents
[m_ContentsCnt
].m_ID
= 0;
608 m_Contents
[m_ContentsCnt
].m_Page
= new wxChar
[deftopic
.Length() + 1];
609 wxStrcpy(m_Contents
[m_ContentsCnt
].m_Page
, deftopic
.c_str());
610 m_Contents
[m_ContentsCnt
].m_Name
= new wxChar
[title
.Length() + 1];
611 wxStrcpy(m_Contents
[m_ContentsCnt
].m_Name
, title
.c_str());
612 m_Contents
[m_ContentsCnt
].m_Book
= bookr
;
614 // store the contents index for later
615 int cont_start
= m_ContentsCnt
++;
617 // Try to find cached binary versions:
618 // 1. save file as book, but with .hhp.cached extension
619 // 2. same as 1. but in temp path
620 // 3. otherwise or if cache load failed, load it from MS.
622 fi
= fsys
.OpenFile(bookfile
.GetLocation() + wxT(".cached"));
625 fi
->GetModificationTime() < bookfile
.GetModificationTime() ||
626 !LoadCachedBook(bookr
, fi
->GetStream()))
628 if (fi
!= NULL
) delete fi
;
629 fi
= fsys
.OpenFile(m_TempPath
+ wxFileNameFromPath(bookfile
.GetLocation()) + wxT(".cached"));
630 if (m_TempPath
== wxEmptyString
|| fi
== NULL
||
631 fi
->GetModificationTime() < bookfile
.GetModificationTime() ||
632 !LoadCachedBook(bookr
, fi
->GetStream()))
634 LoadMSProject(bookr
, fsys
, indexfile
, contfile
);
635 if (m_TempPath
!= wxEmptyString
)
637 wxFileOutputStream
*outs
= new wxFileOutputStream(m_TempPath
+
638 SafeFileName(wxFileNameFromPath(bookfile
.GetLocation())) + wxT(".cached"));
639 SaveCachedBook(bookr
, outs
);
645 if (fi
!= NULL
) delete fi
;
647 // Now store the contents range
648 bookr
->SetContentsRange(cont_start
, m_ContentsCnt
);
650 // Convert encoding, if neccessary:
651 if (encoding
!= wxFONTENCODING_SYSTEM
)
653 wxFontEncodingArray a
= wxEncodingConverter::GetPlatformEquivalents(encoding
);
654 if (a
.GetCount() != 0 && a
[0] != encoding
)
657 wxEncodingConverter conv
;
658 conv
.Init(encoding
, a
[0]);
660 for (i
= IndexOld
; i
< m_IndexCnt
; i
++)
661 conv
.Convert(m_Index
[i
].m_Name
);
662 for (i
= ContentsOld
; i
< m_ContentsCnt
; i
++)
663 conv
.Convert(m_Contents
[i
].m_Name
);
667 m_BookRecords
.Add(bookr
);
669 qsort(m_Index
, m_IndexCnt
, sizeof(wxHtmlContentsItem
), IndexCompareFunc
);
675 bool wxHtmlHelpData::AddBook(const wxString
& book
)
677 if (book
.Right(4).Lower() == wxT(".zip") ||
678 book
.Right(4).Lower() == wxT(".htb") /*html book*/)
685 s
= fsys
.FindFirst(book
+ wxT("#zip:") + wxT("*.hhp"), wxFILE
);
688 if (AddBook(s
)) rt
= TRUE
;
704 char *buff
, *lineptr
;
707 wxString title
= _("noname"),
709 start
= wxEmptyString
,
710 contents
= wxEmptyString
,
711 index
= wxEmptyString
,
712 charset
= wxEmptyString
;
714 if (wxIsAbsolutePath(book
)) bookFull
= book
;
715 else bookFull
= wxGetCwd() + "/" + book
;
717 fi
= fsys
.OpenFile(bookFull
);
720 wxLogError(_("Cannot open HTML help book: %s"), bookFull
.c_str());
723 fsys
.ChangePathTo(bookFull
);
726 buff
= new char[sz
+ 1];
732 lineptr
= ReadLine(lineptr
, linebuf
);
734 if (strstr(linebuf
, "Title=") == linebuf
)
735 title
= linebuf
+ strlen("Title=");
736 if (strstr(linebuf
, "Default topic=") == linebuf
)
737 start
= linebuf
+ strlen("Default topic=");
738 if (strstr(linebuf
, "Index file=") == linebuf
)
739 index
= linebuf
+ strlen("Index file=");
740 if (strstr(linebuf
, "Contents file=") == linebuf
)
741 contents
= linebuf
+ strlen("Contents file=");
742 if (strstr(linebuf
, "Charset=") == linebuf
)
743 charset
= linebuf
+ strlen("Charset=");
744 } while (lineptr
!= NULL
);
748 if (charset
== wxEmptyString
) enc
= wxFONTENCODING_SYSTEM
;
749 else enc
= wxTheFontMapper
->CharsetToEncoding(charset
);
750 bool rtval
= AddBookParam(*fi
, enc
,
751 title
, contents
, index
, start
, fsys
.GetPath());
757 wxString
wxHtmlHelpData::FindPageByName(const wxString
& x
)
763 wxString
url(wxEmptyString
);
765 /* 1. try to open given file: */
767 cnt
= m_BookRecords
.GetCount();
768 for (i
= 0; i
< cnt
; i
++)
770 f
= fsys
.OpenFile(m_BookRecords
[i
].GetFullPath(x
));
773 url
= m_BookRecords
[i
].GetFullPath(x
);
780 /* 2. try to find a book: */
782 for (i
= 0; i
< cnt
; i
++)
784 if (m_BookRecords
[i
].GetTitle() == x
)
786 url
= m_BookRecords
[i
].GetFullPath(m_BookRecords
[i
].GetStart());
791 /* 3. try to find in contents: */
794 for (i
= 0; i
< cnt
; i
++)
796 if (wxStrcmp(m_Contents
[i
].m_Name
, x
) == 0)
798 url
= m_Contents
[i
].GetFullPath();
804 /* 4. try to find in index: */
807 for (i
= 0; i
< cnt
; i
++)
809 if (wxStrcmp(m_Index
[i
].m_Name
, x
) == 0)
811 url
= m_Index
[i
].GetFullPath();
819 wxString
wxHtmlHelpData::FindPageById(int id
)
822 wxString
url(wxEmptyString
);
824 for (i
= 0; i
< m_ContentsCnt
; i
++)
826 if (m_Contents
[i
].m_ID
== id
)
828 url
= m_Contents
[i
].GetFullPath();
836 //----------------------------------------------------------------------------------
837 // wxHtmlSearchStatus functions
838 //----------------------------------------------------------------------------------
840 wxHtmlSearchStatus::wxHtmlSearchStatus(wxHtmlHelpData
* data
, const wxString
& keyword
,
841 bool case_sensitive
, bool whole_words_only
,
842 const wxString
& book
)
846 wxHtmlBookRecord
* bookr
= NULL
;
847 if (book
!= wxEmptyString
)
849 // we have to search in a specific book. Find it first
850 int i
, cnt
= data
->m_BookRecords
.GetCount();
851 for (i
= 0; i
< cnt
; i
++)
852 if (data
->m_BookRecords
[i
].GetTitle() == book
)
854 bookr
= &(data
->m_BookRecords
[i
]);
855 m_CurIndex
= bookr
->GetContentsStart();
856 m_MaxIndex
= bookr
->GetContentsEnd();
859 // check; we won't crash if the book doesn't exist, but it's Bad Anyway.
864 // no book specified; search all books
866 m_MaxIndex
= m_Data
->m_ContentsCnt
;
868 m_Engine
.LookFor(keyword
, case_sensitive
, whole_words_only
);
869 m_Active
= (m_CurIndex
< m_MaxIndex
);
873 bool wxHtmlSearchStatus::Search()
876 int i
= m_CurIndex
; // shortcut
882 // sanity check. Illegal use, but we'll try to prevent a crash anyway
887 m_Name
= wxEmptyString
;
888 m_ContentsItem
= NULL
;
889 thepage
= m_Data
->m_Contents
[i
].m_Page
;
891 m_Active
= (++m_CurIndex
< m_MaxIndex
);
892 // check if it is same page with different anchor:
893 if (m_LastPage
!= NULL
)
896 for (p1
= thepage
, p2
= m_LastPage
;
897 *p1
!= 0 && *p1
!= _T('#') && *p1
== *p2
; p1
++, p2
++) {}
899 m_LastPage
= thepage
;
901 if (*p1
== 0 || *p1
== _T('#'))
904 else m_LastPage
= thepage
;
907 file
= fsys
.OpenFile(m_Data
->m_Contents
[i
].m_Book
->GetFullPath(thepage
));
910 if (m_Engine
.Scan(file
->GetStream()))
912 m_Name
= m_Data
->m_Contents
[i
].m_Name
;
913 m_ContentsItem
= m_Data
->m_Contents
+ i
;
928 //--------------------------------------------------------------------------------
930 //--------------------------------------------------------------------------------
932 void wxSearchEngine::LookFor(const wxString
& keyword
, bool case_sensitive
, bool whole_words_only
)
934 m_CaseSensitive
= case_sensitive
;
935 m_WholeWords
= whole_words_only
;
936 if (m_Keyword
) delete[] m_Keyword
;
937 m_Keyword
= new wxChar
[keyword
.Length() + 1];
938 wxStrcpy(m_Keyword
, keyword
.c_str());
940 if (!m_CaseSensitive
)
942 for (int i
= wxStrlen(m_Keyword
) - 1; i
>= 0; i
--)
944 if ((m_Keyword
[i
] >= wxT('A')) && (m_Keyword
[i
] <= wxT('Z')))
945 m_Keyword
[i
] += wxT('a') - wxT('A');
952 #define WHITESPACE(c) (c == ' ' || c == '\n' || c == '\r' || c == '\t')
954 bool wxSearchEngine::Scan(wxInputStream
*stream
)
956 wxASSERT_MSG(m_Keyword
!= NULL
, wxT("wxSearchEngine::LookFor must be called before scanning!"));
959 int lng
= stream
->GetSize();
960 int wrd
= wxStrlen(m_Keyword
);
962 char *buf
= new char[lng
+ 1];
963 stream
->Read(buf
, lng
);
966 if (!m_CaseSensitive
)
967 for (i
= 0; i
< lng
; i
++)
968 if ((buf
[i
] >= 'A') && (buf
[i
] <= 'Z')) buf
[i
] += 'a' - 'A';
972 for (i
= 0; i
< lng
- wrd
; i
++)
974 if (WHITESPACE(buf
[i
])) continue;
976 while ((j
< wrd
) && (buf
[i
+ j
] == m_Keyword
[j
])) j
++;
977 if (j
== wrd
&& WHITESPACE(buf
[i
+ j
])) { found
= TRUE
; break; }
983 for (i
= 0; i
< lng
- wrd
; i
++)
986 while ((j
< wrd
) && (buf
[i
+ j
] == m_Keyword
[j
])) j
++;
987 if (j
== wrd
) { found
= TRUE
; break; }