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"/* ü */),
303 /* this one should ALWAYS stay the last one!!! */
310 for (int i
= 0; substitutions
[i
][0] != NULL
; i
++)
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
);
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
);
326 void HP_TagHandler::WriteOut(wxHtmlContentsItem
*& array
, int& size
)
334 void HP_TagHandler::ReadIn(wxHtmlContentsItem
* array
, int size
)
343 //-----------------------------------------------------------------------------
345 //-----------------------------------------------------------------------------
347 wxString
wxHtmlBookRecord::GetFullPath(const wxString
&page
) const
349 if (wxIsAbsolutePath(page
))
352 return m_BasePath
+ page
;
357 IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpData
, wxObject
)
359 wxHtmlHelpData::wxHtmlHelpData()
361 m_TempPath
= wxEmptyString
;
369 wxHtmlHelpData::~wxHtmlHelpData()
373 m_BookRecords
.Empty();
376 for (i
= 0; i
< m_ContentsCnt
; i
++)
378 delete[] m_Contents
[i
].m_Page
;
379 delete[] m_Contents
[i
].m_Name
;
385 for (i
= 0; i
< m_IndexCnt
; i
++)
387 delete[] m_Index
[i
].m_Page
;
388 delete[] m_Index
[i
].m_Name
;
394 bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord
*book
, wxFileSystem
& fsys
, const wxString
& indexfile
, const wxString
& contentsfile
)
402 HP_TagHandler
*handler
= new HP_TagHandler(book
);
403 parser
.AddTagHandler(handler
);
405 f
= ( contentsfile
.IsEmpty() ? (wxFSFile
*) NULL
: fsys
.OpenFile(contentsfile
) );
408 sz
= f
->GetStream()->GetSize();
409 buf
= new char[sz
+ 1];
411 f
->GetStream()->Read(buf
, sz
);
413 handler
->ReadIn(m_Contents
, m_ContentsCnt
);
415 handler
->WriteOut(m_Contents
, m_ContentsCnt
);
419 wxLogError(_("Cannot open contents file: %s"), contentsfile
.c_str());
421 f
= ( indexfile
.IsEmpty() ? (wxFSFile
*) NULL
: fsys
.OpenFile(indexfile
) );
424 sz
= f
->GetStream()->GetSize();
425 buf
= new char[sz
+ 1];
427 f
->GetStream()->Read(buf
, sz
);
429 handler
->ReadIn(m_Index
, m_IndexCnt
);
431 handler
->WriteOut(m_Index
, m_IndexCnt
);
434 else if (!indexfile
.IsEmpty())
435 wxLogError(_("Cannot open index file: %s"), indexfile
.c_str());
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);} }
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));
455 #define CURRENT_CACHED_BOOK_VERSION 1
457 bool wxHtmlHelpData::LoadCachedBook(wxHtmlBookRecord
*book
, wxInputStream
*f
)
463 /* load header - version info : */
465 f
->Read(&x
, sizeof(x
));
466 version
= wxINT32_SWAP_ON_BE(x
);
468 if (version
!= CURRENT_CACHED_BOOK_VERSION
)
470 wxLogError(_("Incorrect version of HTML help book"));
472 // NOTE: when adding new version, please ensure backward compatibility!
475 /* load contents : */
477 f
->Read(&x
, sizeof(x
));
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
++)
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
;
500 f
->Read(&x
, sizeof(x
));
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
++)
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
;
519 bool wxHtmlHelpData::SaveCachedBook(wxHtmlBookRecord
*book
, wxOutputStream
*f
)
524 /* save header - version info : */
526 x
= wxINT32_SWAP_ON_BE(CURRENT_CACHED_BOOK_VERSION
);
527 f
->Write(&x
, sizeof(x
));
529 /* save contents : */
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
++)
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
);
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
++)
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
);
570 void wxHtmlHelpData::SetTempDir(const wxString
& path
)
572 if (path
== wxEmptyString
) m_TempPath
= path
;
575 if (wxIsAbsolutePath(path
)) m_TempPath
= path
;
576 else m_TempPath
= wxGetCwd() + _T("/") + path
;
578 if (m_TempPath
[m_TempPath
.Length() - 1] != _T('/'))
579 m_TempPath
<< _T('/');
585 static wxString
SafeFileName(const wxString
& s
)
588 res
.Replace(wxT("#"), wxT("_"));
589 res
.Replace(wxT(":"), wxT("_"));
590 res
.Replace(wxT("\\"), wxT("_"));
591 res
.Replace(wxT("/"), wxT("_"));
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
)
603 wxHtmlBookRecord
*bookr
;
605 int IndexOld
= m_IndexCnt
,
606 ContentsOld
= m_ContentsCnt
;
608 if (! path
.IsEmpty())
609 fsys
.ChangePathTo(path
, TRUE
);
611 bookr
= new wxHtmlBookRecord(fsys
.GetPath(), title
, deftopic
);
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
;
623 // store the contents index for later
624 int cont_start
= m_ContentsCnt
++;
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.
631 fi
= fsys
.OpenFile(bookfile
.GetLocation() + wxT(".cached"));
634 fi
->GetModificationTime() < bookfile
.GetModificationTime() ||
635 !LoadCachedBook(bookr
, fi
->GetStream()))
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()))
643 LoadMSProject(bookr
, fsys
, indexfile
, contfile
);
644 if (m_TempPath
!= wxEmptyString
)
646 wxFileOutputStream
*outs
= new wxFileOutputStream(m_TempPath
+
647 SafeFileName(wxFileNameFromPath(bookfile
.GetLocation())) + wxT(".cached"));
648 SaveCachedBook(bookr
, outs
);
654 if (fi
!= NULL
) delete fi
;
656 // Now store the contents range
657 bookr
->SetContentsRange(cont_start
, m_ContentsCnt
);
659 // Convert encoding, if neccessary:
660 if (encoding
!= wxFONTENCODING_SYSTEM
)
662 wxFontEncodingArray a
= wxEncodingConverter::GetPlatformEquivalents(encoding
);
663 if (a
.GetCount() != 0 && a
[0] != encoding
)
666 wxEncodingConverter conv
;
667 conv
.Init(encoding
, a
[0]);
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
);
676 m_BookRecords
.Add(bookr
);
678 qsort(m_Index
, m_IndexCnt
, sizeof(wxHtmlContentsItem
), IndexCompareFunc
);
684 bool wxHtmlHelpData::AddBook(const wxString
& book
)
686 if (book
.Right(4).Lower() == wxT(".zip") ||
687 book
.Right(4).Lower() == wxT(".htb") /*html book*/)
694 s
= fsys
.FindFirst(book
+ wxT("#zip:") + wxT("*.hhp"), wxFILE
);
697 if (AddBook(s
)) rt
= TRUE
;
713 char *buff
, *lineptr
;
716 wxString title
= _("noname"),
718 start
= wxEmptyString
,
719 contents
= wxEmptyString
,
720 index
= wxEmptyString
,
721 charset
= wxEmptyString
;
723 if (wxIsAbsolutePath(book
)) bookFull
= book
;
724 else bookFull
= wxGetCwd() + "/" + book
;
726 fi
= fsys
.OpenFile(bookFull
);
729 wxLogError(_("Cannot open HTML help book: %s"), bookFull
.c_str());
732 fsys
.ChangePathTo(bookFull
);
735 buff
= new char[sz
+ 1];
741 lineptr
= ReadLine(lineptr
, linebuf
);
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
);
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());
766 wxString
wxHtmlHelpData::FindPageByName(const wxString
& x
)
772 wxString
url(wxEmptyString
);
774 /* 1. try to open given file: */
776 cnt
= m_BookRecords
.GetCount();
777 for (i
= 0; i
< cnt
; i
++)
779 f
= fsys
.OpenFile(m_BookRecords
[i
].GetFullPath(x
));
782 url
= m_BookRecords
[i
].GetFullPath(x
);
789 /* 2. try to find a book: */
791 for (i
= 0; i
< cnt
; i
++)
793 if (m_BookRecords
[i
].GetTitle() == x
)
795 url
= m_BookRecords
[i
].GetFullPath(m_BookRecords
[i
].GetStart());
800 /* 3. try to find in contents: */
803 for (i
= 0; i
< cnt
; i
++)
805 if (wxStrcmp(m_Contents
[i
].m_Name
, x
) == 0)
807 url
= m_Contents
[i
].GetFullPath();
813 /* 4. try to find in index: */
816 for (i
= 0; i
< cnt
; i
++)
818 if (wxStrcmp(m_Index
[i
].m_Name
, x
) == 0)
820 url
= m_Index
[i
].GetFullPath();
828 wxString
wxHtmlHelpData::FindPageById(int id
)
831 wxString
url(wxEmptyString
);
833 for (i
= 0; i
< m_ContentsCnt
; i
++)
835 if (m_Contents
[i
].m_ID
== id
)
837 url
= m_Contents
[i
].GetFullPath();
845 //----------------------------------------------------------------------------------
846 // wxHtmlSearchStatus functions
847 //----------------------------------------------------------------------------------
849 wxHtmlSearchStatus::wxHtmlSearchStatus(wxHtmlHelpData
* data
, const wxString
& keyword
,
850 bool case_sensitive
, bool whole_words_only
,
851 const wxString
& book
)
855 wxHtmlBookRecord
* bookr
= NULL
;
856 if (book
!= wxEmptyString
)
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
)
863 bookr
= &(data
->m_BookRecords
[i
]);
864 m_CurIndex
= bookr
->GetContentsStart();
865 m_MaxIndex
= bookr
->GetContentsEnd();
868 // check; we won't crash if the book doesn't exist, but it's Bad Anyway.
873 // no book specified; search all books
875 m_MaxIndex
= m_Data
->m_ContentsCnt
;
877 m_Engine
.LookFor(keyword
, case_sensitive
, whole_words_only
);
878 m_Active
= (m_CurIndex
< m_MaxIndex
);
882 bool wxHtmlSearchStatus::Search()
885 int i
= m_CurIndex
; // shortcut
891 // sanity check. Illegal use, but we'll try to prevent a crash anyway
896 m_Name
= wxEmptyString
;
897 m_ContentsItem
= NULL
;
898 thepage
= m_Data
->m_Contents
[i
].m_Page
;
900 m_Active
= (++m_CurIndex
< m_MaxIndex
);
901 // check if it is same page with different anchor:
902 if (m_LastPage
!= NULL
)
905 for (p1
= thepage
, p2
= m_LastPage
;
906 *p1
!= 0 && *p1
!= _T('#') && *p1
== *p2
; p1
++, p2
++) {}
908 m_LastPage
= thepage
;
910 if (*p1
== 0 || *p1
== _T('#'))
913 else m_LastPage
= thepage
;
916 file
= fsys
.OpenFile(m_Data
->m_Contents
[i
].m_Book
->GetFullPath(thepage
));
919 if (m_Engine
.Scan(file
->GetStream()))
921 m_Name
= m_Data
->m_Contents
[i
].m_Name
;
922 m_ContentsItem
= m_Data
->m_Contents
+ i
;
937 //--------------------------------------------------------------------------------
939 //--------------------------------------------------------------------------------
941 void wxSearchEngine::LookFor(const wxString
& keyword
, bool case_sensitive
, bool whole_words_only
)
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());
949 if (!m_CaseSensitive
)
951 for (int i
= wxStrlen(m_Keyword
) - 1; i
>= 0; i
--)
953 if ((m_Keyword
[i
] >= wxT('A')) && (m_Keyword
[i
] <= wxT('Z')))
954 m_Keyword
[i
] += wxT('a') - wxT('A');
961 #define WHITESPACE(c) (c == ' ' || c == '\n' || c == '\r' || c == '\t')
963 bool wxSearchEngine::Scan(wxInputStream
*stream
)
965 wxASSERT_MSG(m_Keyword
!= NULL
, wxT("wxSearchEngine::LookFor must be called before scanning!"));
968 int lng
= stream
->GetSize();
969 int wrd
= wxStrlen(m_Keyword
);
971 char *buf
= new char[lng
+ 1];
972 stream
->Read(buf
, lng
);
975 if (!m_CaseSensitive
)
976 for (i
= 0; i
< lng
; i
++)
977 if ((buf
[i
] >= 'A') && (buf
[i
] <= 'Z')) buf
[i
] += 'a' - 'A';
981 for (i
= 0; i
< lng
- wrd
; i
++)
983 if (WHITESPACE(buf
[i
])) continue;
985 while ((j
< wrd
) && (buf
[i
+ j
] == m_Keyword
[j
])) j
++;
986 if (j
== wrd
&& WHITESPACE(buf
[i
+ j
])) { found
= TRUE
; break; }
992 for (i
= 0; i
< lng
- wrd
; i
++)
995 while ((j
< wrd
) && (buf
[i
+ j
] == m_Keyword
[j
])) j
++;
996 if (j
== wrd
) { found
= TRUE
; break; }