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"))
142 // We're reading in items at level zero, which must mean we want to specify
143 // our own 'books', so chuck out the first (empty) one that AddBook already
145 if (m_firstTime && (m_Level == 0) && (m_ItemsCnt > 0))
147 delete[] m_Items[0].m_Name;
148 delete[] m_Items[0].m_Page;
153 if (m_ItemsCnt % wxHTML_REALLOC_STEP == 0)
154 m_Items = (wxHtmlContentsItem*) realloc(m_Items, (m_ItemsCnt + wxHTML_REALLOC_STEP) * sizeof(wxHtmlContentsItem));
157 if (m_ItemsCnt
% wxHTML_REALLOC_STEP
== 0)
158 m_Items
= (wxHtmlContentsItem
*) realloc(m_Items
,
159 (m_ItemsCnt
+ wxHTML_REALLOC_STEP
) *
160 sizeof(wxHtmlContentsItem
));
162 m_Items
[m_ItemsCnt
].m_Level
= m_Level
;
163 m_Items
[m_ItemsCnt
].m_ID
= m_ID
;
164 m_Items
[m_ItemsCnt
].m_Page
= new wxChar
[m_Page
.Length() + 1];
165 wxStrcpy(m_Items
[m_ItemsCnt
].m_Page
, m_Page
.c_str());
166 m_Items
[m_ItemsCnt
].m_Name
= new wxChar
[m_Name
.Length() + 1];
167 wxStrcpy(m_Items
[m_ItemsCnt
].m_Name
, m_Name
.c_str());
168 m_Items
[m_ItemsCnt
].m_Book
= m_Book
;
176 if (m_Name
== wxEmptyString
&& tag
.GetParam(wxT("NAME")) == wxT("Name"))
178 m_Name
= tag
.GetParam(wxT("VALUE"));
179 if (m_Name
.Find(wxT('&')) != -1)
181 #define ESCSEQ(escape, subst) \
182 { _T("&") _T(escape) _T(";"), _T("&") _T(escape) _T(" "), _T("&") _T(escape), _T(subst) }
183 static wxChar
* substitutions
[][4] =
185 ESCSEQ("quot", "\""),
192 ESCSEQ("#94", "^"), /* ^ */
196 ESCSEQ("iexcl", "!"),
198 ESCSEQ("cent", "¢"/* ¢ */),
199 ESCSEQ("#162", "¢"/* ¢ */),
201 ESCSEQ("trade", "(TM)"),
202 ESCSEQ("#153", "(TM)"),
206 ESCSEQ("brkbar", "¦"),
213 ESCSEQ("copy", "©"), /* © */
217 ESCSEQ("laquo", "«"), /* « */
222 ESCSEQ("reg", "®"), /* ® */
225 ESCSEQ("deg", "°"), /* ° */
227 ESCSEQ("plusm", "±"), /* ± */
230 ESCSEQ("acute", "´"),
232 ESCSEQ("macron", "¯"),
234 ESCSEQ("micro", "µ"), /* µ */
236 ESCSEQ("para", "¶"), /* ¶ */
239 ESCSEQ("ordm", "º"), /* º */
241 ESCSEQ("raquo", "»"), /* » */
244 ESCSEQ("iquest", "¿"), /* ¿ */
246 ESCSEQ("Agrave", "\300"/* À */),
247 ESCSEQ("#193", "\300"/* À */),
249 ESCSEQ("Acirc", "\302"/* Â */),
250 ESCSEQ("Atilde", "\303"/* Ã */),
251 ESCSEQ("Auml", "\304"/* Ä */),
252 ESCSEQ("Aring", " "),
253 ESCSEQ("AElig", " "),
254 ESCSEQ("Ccedil", "\347"/* ç */),
255 ESCSEQ("Egrave", "\310"/* È */),
256 ESCSEQ("Eacute", "\311"/* É */),
257 ESCSEQ("Ecirc", "\312"/* Ê */),
258 ESCSEQ("Euml", "\313"/* Ë */),
259 ESCSEQ("Igrave", "\314"/* Ì */),
261 ESCSEQ("Icirc", "\316"/* Î */),
262 ESCSEQ("Iuml", "\317"/* Ï */),
264 ESCSEQ("Ntilde", "\321"/* Ñ */),
265 ESCSEQ("Ograve", "\322"/* Ò */),
267 ESCSEQ("Ocirc", "\324"/* Ô */),
268 ESCSEQ("Otilde", "\325"/* Õ */),
269 ESCSEQ("Ouml", "\326"/* Ö */),
271 ESCSEQ("Oslash", " "),
272 ESCSEQ("Ugrave", "\331"/* Ù */),
274 ESCSEQ("Ucirc", " "),
275 ESCSEQ("Uuml", "\334"/* Ü */),
277 ESCSEQ("szlig", "\247"/* § */),
278 ESCSEQ("agrave;","à"),
279 ESCSEQ("aacute", "\341"/* á */),
280 ESCSEQ("acirc", "\342"/* â */),
281 ESCSEQ("atilde", "\343"/* ã */),
282 ESCSEQ("auml", "\344"/* ä */),
283 ESCSEQ("aring", "a"),
284 ESCSEQ("aelig", "ae"),
285 ESCSEQ("ccedil", "\347"/* ç */),
286 ESCSEQ("egrave", "\350"/* è */),
287 ESCSEQ("eacute", "\351"/* é */),
288 ESCSEQ("ecirc", "\352"/* ê */),
289 ESCSEQ("euml", "\353"/* ë */),
290 ESCSEQ("igrave", "\354"/* ì */),
291 ESCSEQ("iacute", "\355"/* í */),
292 ESCSEQ("icirc", " "),
293 ESCSEQ("iuml", "\357"/* ï */),
295 ESCSEQ("ntilde", "\361"/* ñ */),
296 ESCSEQ("ograve", "\362"/* ò */),
297 ESCSEQ("oacute", "\363"/* ó */),
298 ESCSEQ("ocirc", "\364"/* ô */),
299 ESCSEQ("otilde", "\365"/* õ */),
300 ESCSEQ("ouml", "\366"/* ö */),
301 ESCSEQ("divide", " "),
302 ESCSEQ("oslash", " "),
303 ESCSEQ("ugrave", "\371"/* ù */),
304 ESCSEQ("uacute", "\372"/* ú */),
305 ESCSEQ("ucirc", "\373"/* û */),
306 ESCSEQ("uuml", "\374"/* ü */),
310 /* this one should ALWAYS stay the last one!!! */
317 for (int i
= 0; substitutions
[i
][0] != NULL
; i
++)
319 m_Name
.Replace(substitutions
[i
][0], substitutions
[i
][3], TRUE
);
320 m_Name
.Replace(substitutions
[i
][1], substitutions
[i
][3], TRUE
);
321 m_Name
.Replace(substitutions
[i
][2], substitutions
[i
][3], TRUE
);
325 if (tag
.GetParam(wxT("NAME")) == wxT("Local")) m_Page
= tag
.GetParam(wxT("VALUE"));
326 if (tag
.GetParam(wxT("NAME")) == wxT("ID")) tag
.ScanParam(wxT("VALUE"), wxT("%i"), &m_ID
);
333 void HP_TagHandler::WriteOut(wxHtmlContentsItem
*& array
, int& size
)
341 void HP_TagHandler::ReadIn(wxHtmlContentsItem
* array
, int size
)
350 //-----------------------------------------------------------------------------
352 //-----------------------------------------------------------------------------
354 wxString
wxHtmlBookRecord::GetFullPath(const wxString
&page
) const
356 if (wxIsAbsolutePath(page
))
359 return m_BasePath
+ page
;
364 IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpData
, wxObject
)
366 wxHtmlHelpData::wxHtmlHelpData()
368 m_TempPath
= wxEmptyString
;
376 wxHtmlHelpData::~wxHtmlHelpData()
380 m_BookRecords
.Empty();
383 for (i
= 0; i
< m_ContentsCnt
; i
++)
385 delete[] m_Contents
[i
].m_Page
;
386 delete[] m_Contents
[i
].m_Name
;
392 for (i
= 0; i
< m_IndexCnt
; i
++)
394 delete[] m_Index
[i
].m_Page
;
395 delete[] m_Index
[i
].m_Name
;
401 bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord
*book
, wxFileSystem
& fsys
, const wxString
& indexfile
, const wxString
& contentsfile
)
409 HP_TagHandler
*handler
= new HP_TagHandler(book
);
410 parser
.AddTagHandler(handler
);
412 f
= ( contentsfile
.IsEmpty() ? (wxFSFile
*) NULL
: fsys
.OpenFile(contentsfile
) );
415 sz
= f
->GetStream()->GetSize();
416 buf
= new char[sz
+ 1];
418 f
->GetStream()->Read(buf
, sz
);
420 handler
->ReadIn(m_Contents
, m_ContentsCnt
);
422 handler
->WriteOut(m_Contents
, m_ContentsCnt
);
426 wxLogError(_("Cannot open contents file: %s"), contentsfile
.c_str());
428 f
= ( indexfile
.IsEmpty() ? (wxFSFile
*) NULL
: fsys
.OpenFile(indexfile
) );
431 sz
= f
->GetStream()->GetSize();
432 buf
= new char[sz
+ 1];
434 f
->GetStream()->Read(buf
, sz
);
436 handler
->ReadIn(m_Index
, m_IndexCnt
);
438 handler
->WriteOut(m_Index
, m_IndexCnt
);
441 else if (!indexfile
.IsEmpty())
442 wxLogError(_("Cannot open index file: %s"), indexfile
.c_str());
451 #define READ_STRING(f, s, lng) { char tmpc; for (int i = 0; i < lng; i++) { f->Read(&tmpc, 1); s[i] = (wxChar)tmpc;} }
452 #define WRITE_STRING(f, s, lng) { char tmpc; for (int i = 0; i < lng; i++) { tmpc = (char)s[i]; f->Write(&tmpc, 1);} }
456 #define READ_STRING(f, s, lng) f->Read(s, lng * sizeof(char));
457 #define WRITE_STRING(f, s, lng) f->Write(s, lng * sizeof(char));
462 #define CURRENT_CACHED_BOOK_VERSION 1
464 bool wxHtmlHelpData::LoadCachedBook(wxHtmlBookRecord
*book
, wxInputStream
*f
)
470 /* load header - version info : */
472 f
->Read(&x
, sizeof(x
));
473 version
= wxINT32_SWAP_ON_BE(x
);
475 if (version
!= CURRENT_CACHED_BOOK_VERSION
)
477 wxLogError(_("Incorrect version of HTML help book"));
479 // NOTE: when adding new version, please ensure backward compatibility!
482 /* load contents : */
484 f
->Read(&x
, sizeof(x
));
486 m_ContentsCnt
+= wxINT32_SWAP_ON_BE(x
);
487 m_Contents
= (wxHtmlContentsItem
*) realloc(m_Contents
,
488 (m_ContentsCnt
/ wxHTML_REALLOC_STEP
+ 1) *
489 wxHTML_REALLOC_STEP
* sizeof(wxHtmlContentsItem
));
490 for (i
= st
; i
< m_ContentsCnt
; i
++)
492 f
->Read(&x
, sizeof(x
));
493 m_Contents
[i
].m_Level
= wxINT32_SWAP_ON_BE(x
);
494 f
->Read(&x
, sizeof(x
));
495 m_Contents
[i
].m_ID
= wxINT32_SWAP_ON_BE(x
);
496 f
->Read(&x
, sizeof(x
)); x
= wxINT32_SWAP_ON_BE(x
);
497 m_Contents
[i
].m_Name
= new wxChar
[x
];
498 READ_STRING(f
, m_Contents
[i
].m_Name
, x
);
499 f
->Read(&x
, sizeof(x
)); x
= wxINT32_SWAP_ON_BE(x
);
500 m_Contents
[i
].m_Page
= new wxChar
[x
];
501 READ_STRING(f
, m_Contents
[i
].m_Page
, x
);
502 m_Contents
[i
].m_Book
= book
;
507 f
->Read(&x
, sizeof(x
));
509 m_IndexCnt
+= wxINT32_SWAP_ON_BE(x
);
510 m_Index
= (wxHtmlContentsItem
*) realloc(m_Index
, (m_IndexCnt
/ wxHTML_REALLOC_STEP
+ 1) *
511 wxHTML_REALLOC_STEP
* sizeof(wxHtmlContentsItem
));
512 for (i
= st
; i
< m_IndexCnt
; i
++)
514 f
->Read(&x
, sizeof(x
)); x
= wxINT32_SWAP_ON_BE(x
);
515 m_Index
[i
].m_Name
= new wxChar
[x
];
516 READ_STRING(f
, m_Index
[i
].m_Name
, x
);
517 f
->Read(&x
, sizeof(x
)); x
= wxINT32_SWAP_ON_BE(x
);
518 m_Index
[i
].m_Page
= new wxChar
[x
];
519 READ_STRING(f
, m_Index
[i
].m_Page
, x
);
520 m_Index
[i
].m_Book
= book
;
526 bool wxHtmlHelpData::SaveCachedBook(wxHtmlBookRecord
*book
, wxOutputStream
*f
)
531 /* save header - version info : */
533 x
= wxINT32_SWAP_ON_BE(CURRENT_CACHED_BOOK_VERSION
);
534 f
->Write(&x
, sizeof(x
));
536 /* save contents : */
539 for (i
= 0; i
< m_ContentsCnt
; i
++) if (m_Contents
[i
].m_Book
== book
&& m_Contents
[i
].m_Level
> 0) x
++;
540 x
= wxINT32_SWAP_ON_BE(x
);
541 f
->Write(&x
, sizeof(x
));
542 for (i
= 0; i
< m_ContentsCnt
; i
++)
544 if (m_Contents
[i
].m_Book
!= book
|| m_Contents
[i
].m_Level
== 0) continue;
545 x
= wxINT32_SWAP_ON_BE(m_Contents
[i
].m_Level
);
546 f
->Write(&x
, sizeof(x
));
547 x
= wxINT32_SWAP_ON_BE(m_Contents
[i
].m_ID
);
548 f
->Write(&x
, sizeof(x
));
549 x
= wxINT32_SWAP_ON_BE(wxStrlen(m_Contents
[i
].m_Name
) + 1);
550 f
->Write(&x
, sizeof(x
));
551 WRITE_STRING(f
, m_Contents
[i
].m_Name
, x
);
552 x
= wxINT32_SWAP_ON_BE(wxStrlen(m_Contents
[i
].m_Page
) + 1);
553 f
->Write(&x
, sizeof(x
));
554 WRITE_STRING(f
, m_Contents
[i
].m_Page
, x
);
560 for (i
= 0; i
< m_IndexCnt
; i
++) if (m_Index
[i
].m_Book
== book
&& m_Index
[i
].m_Level
> 0) x
++;
561 x
= wxINT32_SWAP_ON_BE(x
);
562 f
->Write(&x
, sizeof(x
));
563 for (i
= 0; i
< m_IndexCnt
; i
++)
565 if (m_Index
[i
].m_Book
!= book
|| m_Index
[i
].m_Level
== 0) continue;
566 x
= wxINT32_SWAP_ON_BE(wxStrlen(m_Index
[i
].m_Name
) + 1);
567 f
->Write(&x
, sizeof(x
));
568 WRITE_STRING(f
, m_Index
[i
].m_Name
, x
);
569 x
= wxINT32_SWAP_ON_BE(wxStrlen(m_Index
[i
].m_Page
) + 1);
570 f
->Write(&x
, sizeof(x
));
571 WRITE_STRING(f
, m_Index
[i
].m_Page
, x
);
577 void wxHtmlHelpData::SetTempDir(const wxString
& path
)
579 if (path
== wxEmptyString
) m_TempPath
= path
;
582 if (wxIsAbsolutePath(path
)) m_TempPath
= path
;
583 else m_TempPath
= wxGetCwd() + _T("/") + path
;
585 if (m_TempPath
[m_TempPath
.Length() - 1] != _T('/'))
586 m_TempPath
<< _T('/');
592 static wxString
SafeFileName(const wxString
& s
)
595 res
.Replace(wxT("#"), wxT("_"));
596 res
.Replace(wxT(":"), wxT("_"));
597 res
.Replace(wxT("\\"), wxT("_"));
598 res
.Replace(wxT("/"), wxT("_"));
602 bool wxHtmlHelpData::AddBookParam(const wxFSFile
& bookfile
,
603 wxFontEncoding encoding
,
604 const wxString
& title
, const wxString
& contfile
,
605 const wxString
& indexfile
, const wxString
& deftopic
,
606 const wxString
& path
)
610 wxHtmlBookRecord
*bookr
;
612 int IndexOld
= m_IndexCnt
,
613 ContentsOld
= m_ContentsCnt
;
615 if (! path
.IsEmpty())
616 fsys
.ChangePathTo(path
, TRUE
);
618 bookr
= new wxHtmlBookRecord(fsys
.GetPath(), title
, deftopic
);
620 if (m_ContentsCnt
% wxHTML_REALLOC_STEP
== 0)
621 m_Contents
= (wxHtmlContentsItem
*) realloc(m_Contents
, (m_ContentsCnt
+ wxHTML_REALLOC_STEP
) * sizeof(wxHtmlContentsItem
));
622 m_Contents
[m_ContentsCnt
].m_Level
= 0;
623 m_Contents
[m_ContentsCnt
].m_ID
= 0;
624 m_Contents
[m_ContentsCnt
].m_Page
= new wxChar
[deftopic
.Length() + 1];
625 wxStrcpy(m_Contents
[m_ContentsCnt
].m_Page
, deftopic
.c_str());
626 m_Contents
[m_ContentsCnt
].m_Name
= new wxChar
[title
.Length() + 1];
627 wxStrcpy(m_Contents
[m_ContentsCnt
].m_Name
, title
.c_str());
628 m_Contents
[m_ContentsCnt
].m_Book
= bookr
;
630 // store the contents index for later
631 int cont_start
= m_ContentsCnt
++;
633 // Try to find cached binary versions:
634 // 1. save file as book, but with .hhp.cached extension
635 // 2. same as 1. but in temp path
636 // 3. otherwise or if cache load failed, load it from MS.
638 fi
= fsys
.OpenFile(bookfile
.GetLocation() + wxT(".cached"));
641 fi
->GetModificationTime() < bookfile
.GetModificationTime() ||
642 !LoadCachedBook(bookr
, fi
->GetStream()))
644 if (fi
!= NULL
) delete fi
;
645 fi
= fsys
.OpenFile(m_TempPath
+ wxFileNameFromPath(bookfile
.GetLocation()) + wxT(".cached"));
646 if (m_TempPath
== wxEmptyString
|| fi
== NULL
||
647 fi
->GetModificationTime() < bookfile
.GetModificationTime() ||
648 !LoadCachedBook(bookr
, fi
->GetStream()))
650 LoadMSProject(bookr
, fsys
, indexfile
, contfile
);
651 if (m_TempPath
!= wxEmptyString
)
653 wxFileOutputStream
*outs
= new wxFileOutputStream(m_TempPath
+
654 SafeFileName(wxFileNameFromPath(bookfile
.GetLocation())) + wxT(".cached"));
655 SaveCachedBook(bookr
, outs
);
661 if (fi
!= NULL
) delete fi
;
663 // Now store the contents range
664 bookr
->SetContentsRange(cont_start
, m_ContentsCnt
);
666 // Convert encoding, if neccessary:
667 if (encoding
!= wxFONTENCODING_SYSTEM
)
669 wxFontEncodingArray a
= wxEncodingConverter::GetPlatformEquivalents(encoding
);
670 if (a
.GetCount() != 0 && a
[0] != encoding
)
673 wxEncodingConverter conv
;
674 conv
.Init(encoding
, a
[0]);
676 for (i
= IndexOld
; i
< m_IndexCnt
; i
++)
677 conv
.Convert(m_Index
[i
].m_Name
);
678 for (i
= ContentsOld
; i
< m_ContentsCnt
; i
++)
679 conv
.Convert(m_Contents
[i
].m_Name
);
683 m_BookRecords
.Add(bookr
);
685 qsort(m_Index
, m_IndexCnt
, sizeof(wxHtmlContentsItem
), IndexCompareFunc
);
691 bool wxHtmlHelpData::AddBook(const wxString
& book
)
693 if (book
.Right(4).Lower() == wxT(".zip") ||
694 book
.Right(4).Lower() == wxT(".htb") /*html book*/)
701 s
= fsys
.FindFirst(book
+ wxT("#zip:") + wxT("*.hhp"), wxFILE
);
704 if (AddBook(s
)) rt
= TRUE
;
720 char *buff
, *lineptr
;
723 wxString title
= _("noname"),
725 start
= wxEmptyString
,
726 contents
= wxEmptyString
,
727 index
= wxEmptyString
,
728 charset
= wxEmptyString
;
730 if (wxIsAbsolutePath(book
)) bookFull
= book
;
731 else bookFull
= wxGetCwd() + "/" + book
;
733 fi
= fsys
.OpenFile(bookFull
);
736 wxLogError(_("Cannot open HTML help book: %s"), bookFull
.c_str());
739 fsys
.ChangePathTo(bookFull
);
742 buff
= new char[sz
+ 1];
748 lineptr
= ReadLine(lineptr
, linebuf
);
750 if (strstr(linebuf
, "Title=") == linebuf
)
751 title
= linebuf
+ strlen("Title=");
752 if (strstr(linebuf
, "Default topic=") == linebuf
)
753 start
= linebuf
+ strlen("Default topic=");
754 if (strstr(linebuf
, "Index file=") == linebuf
)
755 index
= linebuf
+ strlen("Index file=");
756 if (strstr(linebuf
, "Contents file=") == linebuf
)
757 contents
= linebuf
+ strlen("Contents file=");
758 if (strstr(linebuf
, "Charset=") == linebuf
)
759 charset
= linebuf
+ strlen("Charset=");
760 } while (lineptr
!= NULL
);
764 if (charset
== wxEmptyString
) enc
= wxFONTENCODING_SYSTEM
;
765 else enc
= wxTheFontMapper
->CharsetToEncoding(charset
);
766 bool rtval
= AddBookParam(*fi
, enc
,
767 title
, contents
, index
, start
, fsys
.GetPath());
773 wxString
wxHtmlHelpData::FindPageByName(const wxString
& x
)
779 wxString
url(wxEmptyString
);
781 /* 1. try to open given file: */
783 cnt
= m_BookRecords
.GetCount();
784 for (i
= 0; i
< cnt
; i
++)
786 f
= fsys
.OpenFile(m_BookRecords
[i
].GetFullPath(x
));
789 url
= m_BookRecords
[i
].GetFullPath(x
);
796 /* 2. try to find a book: */
798 for (i
= 0; i
< cnt
; i
++)
800 if (m_BookRecords
[i
].GetTitle() == x
)
802 url
= m_BookRecords
[i
].GetFullPath(m_BookRecords
[i
].GetStart());
807 /* 3. try to find in contents: */
810 for (i
= 0; i
< cnt
; i
++)
812 if (wxStrcmp(m_Contents
[i
].m_Name
, x
) == 0)
814 url
= m_Contents
[i
].GetFullPath();
820 /* 4. try to find in index: */
823 for (i
= 0; i
< cnt
; i
++)
825 if (wxStrcmp(m_Index
[i
].m_Name
, x
) == 0)
827 url
= m_Index
[i
].GetFullPath();
835 wxString
wxHtmlHelpData::FindPageById(int id
)
838 wxString
url(wxEmptyString
);
840 for (i
= 0; i
< m_ContentsCnt
; i
++)
842 if (m_Contents
[i
].m_ID
== id
)
844 url
= m_Contents
[i
].GetFullPath();
852 //----------------------------------------------------------------------------------
853 // wxHtmlSearchStatus functions
854 //----------------------------------------------------------------------------------
856 wxHtmlSearchStatus::wxHtmlSearchStatus(wxHtmlHelpData
* data
, const wxString
& keyword
,
857 bool case_sensitive
, bool whole_words_only
,
858 const wxString
& book
)
862 wxHtmlBookRecord
* bookr
= NULL
;
863 if (book
!= wxEmptyString
)
865 // we have to search in a specific book. Find it first
866 int i
, cnt
= data
->m_BookRecords
.GetCount();
867 for (i
= 0; i
< cnt
; i
++)
868 if (data
->m_BookRecords
[i
].GetTitle() == book
)
870 bookr
= &(data
->m_BookRecords
[i
]);
871 m_CurIndex
= bookr
->GetContentsStart();
872 m_MaxIndex
= bookr
->GetContentsEnd();
875 // check; we won't crash if the book doesn't exist, but it's Bad Anyway.
880 // no book specified; search all books
882 m_MaxIndex
= m_Data
->m_ContentsCnt
;
884 m_Engine
.LookFor(keyword
, case_sensitive
, whole_words_only
);
885 m_Active
= (m_CurIndex
< m_MaxIndex
);
889 bool wxHtmlSearchStatus::Search()
892 int i
= m_CurIndex
; // shortcut
898 // sanity check. Illegal use, but we'll try to prevent a crash anyway
903 m_Name
= wxEmptyString
;
904 m_ContentsItem
= NULL
;
905 thepage
= m_Data
->m_Contents
[i
].m_Page
;
907 m_Active
= (++m_CurIndex
< m_MaxIndex
);
908 // check if it is same page with different anchor:
909 if (m_LastPage
!= NULL
)
912 for (p1
= thepage
, p2
= m_LastPage
;
913 *p1
!= 0 && *p1
!= _T('#') && *p1
== *p2
; p1
++, p2
++) {}
915 m_LastPage
= thepage
;
917 if (*p1
== 0 || *p1
== _T('#'))
920 else m_LastPage
= thepage
;
923 file
= fsys
.OpenFile(m_Data
->m_Contents
[i
].m_Book
->GetFullPath(thepage
));
926 if (m_Engine
.Scan(file
->GetStream()))
928 m_Name
= m_Data
->m_Contents
[i
].m_Name
;
929 m_ContentsItem
= m_Data
->m_Contents
+ i
;
944 //--------------------------------------------------------------------------------
946 //--------------------------------------------------------------------------------
948 void wxSearchEngine::LookFor(const wxString
& keyword
, bool case_sensitive
, bool whole_words_only
)
950 m_CaseSensitive
= case_sensitive
;
951 m_WholeWords
= whole_words_only
;
952 if (m_Keyword
) delete[] m_Keyword
;
953 m_Keyword
= new wxChar
[keyword
.Length() + 1];
954 wxStrcpy(m_Keyword
, keyword
.c_str());
956 if (!m_CaseSensitive
)
958 for (int i
= wxStrlen(m_Keyword
) - 1; i
>= 0; i
--)
960 if ((m_Keyword
[i
] >= wxT('A')) && (m_Keyword
[i
] <= wxT('Z')))
961 m_Keyword
[i
] += wxT('a') - wxT('A');
968 #define WHITESPACE(c) (c == ' ' || c == '\n' || c == '\r' || c == '\t')
970 bool wxSearchEngine::Scan(wxInputStream
*stream
)
972 wxASSERT_MSG(m_Keyword
!= NULL
, wxT("wxSearchEngine::LookFor must be called before scanning!"));
975 int lng
= stream
->GetSize();
976 int wrd
= wxStrlen(m_Keyword
);
978 char *buf
= new char[lng
+ 1];
979 stream
->Read(buf
, lng
);
982 if (!m_CaseSensitive
)
983 for (i
= 0; i
< lng
; i
++)
984 if ((buf
[i
] >= 'A') && (buf
[i
] <= 'Z')) buf
[i
] += 'a' - 'A';
988 for (i
= 0; i
< lng
- wrd
; i
++)
990 if (WHITESPACE(buf
[i
])) continue;
992 while ((j
< wrd
) && (buf
[i
+ j
] == m_Keyword
[j
])) j
++;
993 if (j
== wrd
&& WHITESPACE(buf
[i
+ j
])) { found
= TRUE
; break; }
999 for (i
= 0; i
< lng
- wrd
; i
++)
1002 while ((j
< wrd
) && (buf
[i
+ j
] == m_Keyword
[j
])) j
++;
1003 if (j
== wrd
) { found
= TRUE
; break; }