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(subst) }
176 static wxChar
* substitutions
[][3] =
178 ESCSEQ("quot", "\""),
183 ESCSEQ("iexcl", "!"),
184 ESCSEQ("cent", "\242"/* ¢ */),
187 ESCSEQ("brkbar", " "),
191 ESCSEQ("copy", "(c)"),
193 ESCSEQ("laquo", " "),
196 ESCSEQ("reg", "(r)"),
199 ESCSEQ("plusm", " "),
201 ESCSEQ("acute", " "),
202 ESCSEQ("micro", " "),
206 ESCSEQ("raquo", " "),
208 ESCSEQ("iquest", " "),
209 ESCSEQ("Agrave", "\300"/* À */),
211 ESCSEQ("Acirc", "\302"/* Â */),
212 ESCSEQ("Atilde", "\303"/* Ã */),
213 ESCSEQ("Auml", "\304"/* Ä */),
214 ESCSEQ("Aring", " "),
215 ESCSEQ("AElig", " "),
216 ESCSEQ("Ccedil", "\347"/* ç */),
217 ESCSEQ("Egrave", "\310"/* È */),
218 ESCSEQ("Eacute", "\311"/* É */),
219 ESCSEQ("Ecirc", "\312"/* Ê */),
220 ESCSEQ("Euml", "\313"/* Ë */),
221 ESCSEQ("Igrave", "\314"/* Ì */),
223 ESCSEQ("Icirc", "\316"/* Î */),
224 ESCSEQ("Iuml", "\317"/* Ï */),
226 ESCSEQ("Ntilde", "\321"/* Ñ */),
227 ESCSEQ("Ograve", "\322"/* Ò */),
229 ESCSEQ("Ocirc", "\324"/* Ô */),
230 ESCSEQ("Otilde", "\325"/* Õ */),
231 ESCSEQ("Ouml", "\326"/* Ö */),
233 ESCSEQ("Oslash", " "),
234 ESCSEQ("Ugrave", "\331"/* Ù */),
236 ESCSEQ("Ucirc", " "),
237 ESCSEQ("Uuml", "\334"/* Ü */),
239 ESCSEQ("szlig", "\247"/* § */),
240 ESCSEQ("agrave;","à"),
241 ESCSEQ("aacute", "\341"/* á */),
242 ESCSEQ("acirc", "\342"/* â */),
243 ESCSEQ("atilde", "\343"/* ã */),
244 ESCSEQ("auml", "\344"/* ä */),
245 ESCSEQ("aring", "a"),
246 ESCSEQ("aelig", "ae"),
247 ESCSEQ("ccedil", "\347"/* ç */),
248 ESCSEQ("egrave", "\350"/* è */),
249 ESCSEQ("eacute", "\351"/* é */),
250 ESCSEQ("ecirc", "\352"/* ê */),
251 ESCSEQ("euml", "\353"/* ë */),
252 ESCSEQ("igrave", "\354"/* ì */),
253 ESCSEQ("iacute", "\355"/* í */),
254 ESCSEQ("icirc", " "),
255 ESCSEQ("iuml", "\357"/* ï */),
257 ESCSEQ("ntilde", "\361"/* ñ */),
258 ESCSEQ("ograve", "\362"/* ò */),
259 ESCSEQ("oacute", "\363"/* ó */),
260 ESCSEQ("ocirc", "\364"/* ô */),
261 ESCSEQ("otilde", "\365"/* õ */),
262 ESCSEQ("ouml", "\366"/* ö */),
263 ESCSEQ("divide", " "),
264 ESCSEQ("oslash", " "),
265 ESCSEQ("ugrave", "\371"/* ù */),
266 ESCSEQ("uacute", "\372"/* ú */),
267 ESCSEQ("ucirc", "\373"/* û */),
268 ESCSEQ("uuml", "\374"/* ü */),
272 /* this one should ALWAYS stay the last one!!! */
278 for (int i
= 0; substitutions
[i
][0] != NULL
; i
++)
280 m_Name
.Replace(substitutions
[i
][0], substitutions
[i
][2], TRUE
);
281 m_Name
.Replace(substitutions
[i
][1], substitutions
[i
][2], TRUE
);
285 if (tag
.GetParam(wxT("NAME")) == wxT("Local")) m_Page
= tag
.GetParam(wxT("VALUE"));
286 if (tag
.GetParam(wxT("NAME")) == wxT("ID")) tag
.ScanParam(wxT("VALUE"), wxT("%i"), &m_ID
);
293 void HP_TagHandler::WriteOut(wxHtmlContentsItem
*& array
, int& size
)
301 void HP_TagHandler::ReadIn(wxHtmlContentsItem
* array
, int size
)
310 //-----------------------------------------------------------------------------
312 //-----------------------------------------------------------------------------
314 IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpData
, wxObject
)
316 wxHtmlHelpData::wxHtmlHelpData()
318 m_TempPath
= wxEmptyString
;
326 wxHtmlHelpData::~wxHtmlHelpData()
330 m_BookRecords
.Empty();
333 for (i
= 0; i
< m_ContentsCnt
; i
++)
335 delete[] m_Contents
[i
].m_Page
;
336 delete[] m_Contents
[i
].m_Name
;
342 for (i
= 0; i
< m_IndexCnt
; i
++)
344 delete[] m_Index
[i
].m_Page
;
345 delete[] m_Index
[i
].m_Name
;
351 bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord
*book
, wxFileSystem
& fsys
, const wxString
& indexfile
, const wxString
& contentsfile
)
359 HP_TagHandler
*handler
= new HP_TagHandler(book
);
360 parser
.AddTagHandler(handler
);
362 f
= ( contentsfile
.IsEmpty() ? (wxFSFile
*) NULL
: fsys
.OpenFile(contentsfile
) );
365 sz
= f
->GetStream()->GetSize();
366 buf
= new char[sz
+ 1];
368 f
->GetStream()->Read(buf
, sz
);
370 handler
->ReadIn(m_Contents
, m_ContentsCnt
);
372 handler
->WriteOut(m_Contents
, m_ContentsCnt
);
376 wxLogError(_("Cannot open contents file: %s"), contentsfile
.c_str());
378 f
= ( indexfile
.IsEmpty() ? (wxFSFile
*) NULL
: fsys
.OpenFile(indexfile
) );
381 sz
= f
->GetStream()->GetSize();
382 buf
= new char[sz
+ 1];
384 f
->GetStream()->Read(buf
, sz
);
386 handler
->ReadIn(m_Index
, m_IndexCnt
);
388 handler
->WriteOut(m_Index
, m_IndexCnt
);
391 else if (!indexfile
.IsEmpty())
392 wxLogError(_("Cannot open index file: %s"), indexfile
.c_str());
401 #define READ_STRING(f, s, lng) { char tmpc; for (int i = 0; i < lng; i++) { f->Read(&tmpc, 1); s[i] = (wxChar)tmpc;} }
402 #define WRITE_STRING(f, s, lng) { char tmpc; for (int i = 0; i < lng; i++) { tmpc = (char)s[i]; f->Write(&tmpc, 1);} }
406 #define READ_STRING(f, s, lng) f->Read(s, lng * sizeof(char));
407 #define WRITE_STRING(f, s, lng) f->Write(s, lng * sizeof(char));
412 #define CURRENT_CACHED_BOOK_VERSION 1
414 bool wxHtmlHelpData::LoadCachedBook(wxHtmlBookRecord
*book
, wxInputStream
*f
)
420 /* load header - version info : */
422 f
->Read(&x
, sizeof(x
));
423 version
= wxINT32_SWAP_ON_BE(x
);
425 if (version
!= CURRENT_CACHED_BOOK_VERSION
)
427 wxLogError(_("Incorrect version of HTML help book"));
429 // NOTE: when adding new version, please ensure backward compatibility!
432 /* load contents : */
434 f
->Read(&x
, sizeof(x
));
436 m_ContentsCnt
+= wxINT32_SWAP_ON_BE(x
);
437 m_Contents
= (wxHtmlContentsItem
*) realloc(m_Contents
,
438 (m_ContentsCnt
/ wxHTML_REALLOC_STEP
+ 1) *
439 wxHTML_REALLOC_STEP
* sizeof(wxHtmlContentsItem
));
440 for (i
= st
; i
< m_ContentsCnt
; i
++)
442 f
->Read(&x
, sizeof(x
));
443 m_Contents
[i
].m_Level
= wxINT32_SWAP_ON_BE(x
);
444 f
->Read(&x
, sizeof(x
));
445 m_Contents
[i
].m_ID
= wxINT32_SWAP_ON_BE(x
);
446 f
->Read(&x
, sizeof(x
)); x
= wxINT32_SWAP_ON_BE(x
);
447 m_Contents
[i
].m_Name
= new wxChar
[x
];
448 READ_STRING(f
, m_Contents
[i
].m_Name
, x
);
449 f
->Read(&x
, sizeof(x
)); x
= wxINT32_SWAP_ON_BE(x
);
450 m_Contents
[i
].m_Page
= new wxChar
[x
];
451 READ_STRING(f
, m_Contents
[i
].m_Page
, x
);
452 m_Contents
[i
].m_Book
= book
;
457 f
->Read(&x
, sizeof(x
));
459 m_IndexCnt
+= wxINT32_SWAP_ON_BE(x
);
460 m_Index
= (wxHtmlContentsItem
*) realloc(m_Index
, (m_IndexCnt
/ wxHTML_REALLOC_STEP
+ 1) *
461 wxHTML_REALLOC_STEP
* sizeof(wxHtmlContentsItem
));
462 for (i
= st
; i
< m_IndexCnt
; i
++)
464 f
->Read(&x
, sizeof(x
)); x
= wxINT32_SWAP_ON_BE(x
);
465 m_Index
[i
].m_Name
= new wxChar
[x
];
466 READ_STRING(f
, m_Index
[i
].m_Name
, x
);
467 f
->Read(&x
, sizeof(x
)); x
= wxINT32_SWAP_ON_BE(x
);
468 m_Index
[i
].m_Page
= new wxChar
[x
];
469 READ_STRING(f
, m_Index
[i
].m_Page
, x
);
470 m_Index
[i
].m_Book
= book
;
476 bool wxHtmlHelpData::SaveCachedBook(wxHtmlBookRecord
*book
, wxOutputStream
*f
)
481 /* save header - version info : */
483 x
= wxINT32_SWAP_ON_BE(CURRENT_CACHED_BOOK_VERSION
);
484 f
->Write(&x
, sizeof(x
));
486 /* save contents : */
489 for (i
= 0; i
< m_ContentsCnt
; i
++) if (m_Contents
[i
].m_Book
== book
&& m_Contents
[i
].m_Level
> 0) x
++;
490 x
= wxINT32_SWAP_ON_BE(x
);
491 f
->Write(&x
, sizeof(x
));
492 for (i
= 0; i
< m_ContentsCnt
; i
++)
494 if (m_Contents
[i
].m_Book
!= book
|| m_Contents
[i
].m_Level
== 0) continue;
495 x
= wxINT32_SWAP_ON_BE(m_Contents
[i
].m_Level
);
496 f
->Write(&x
, sizeof(x
));
497 x
= wxINT32_SWAP_ON_BE(m_Contents
[i
].m_ID
);
498 f
->Write(&x
, sizeof(x
));
499 x
= wxINT32_SWAP_ON_BE(wxStrlen(m_Contents
[i
].m_Name
) + 1);
500 f
->Write(&x
, sizeof(x
));
501 WRITE_STRING(f
, m_Contents
[i
].m_Name
, x
);
502 x
= wxINT32_SWAP_ON_BE(wxStrlen(m_Contents
[i
].m_Page
) + 1);
503 f
->Write(&x
, sizeof(x
));
504 WRITE_STRING(f
, m_Contents
[i
].m_Page
, x
);
510 for (i
= 0; i
< m_IndexCnt
; i
++) if (m_Index
[i
].m_Book
== book
&& m_Index
[i
].m_Level
> 0) x
++;
511 x
= wxINT32_SWAP_ON_BE(x
);
512 f
->Write(&x
, sizeof(x
));
513 for (i
= 0; i
< m_IndexCnt
; i
++)
515 if (m_Index
[i
].m_Book
!= book
|| m_Index
[i
].m_Level
== 0) continue;
516 x
= wxINT32_SWAP_ON_BE(wxStrlen(m_Index
[i
].m_Name
) + 1);
517 f
->Write(&x
, sizeof(x
));
518 WRITE_STRING(f
, m_Index
[i
].m_Name
, x
);
519 x
= wxINT32_SWAP_ON_BE(wxStrlen(m_Index
[i
].m_Page
) + 1);
520 f
->Write(&x
, sizeof(x
));
521 WRITE_STRING(f
, m_Index
[i
].m_Page
, x
);
527 void wxHtmlHelpData::SetTempDir(const wxString
& path
)
529 if (path
== wxEmptyString
) m_TempPath
= path
;
532 if (wxIsAbsolutePath(path
)) m_TempPath
= path
;
533 else m_TempPath
= wxGetCwd() + _T("/") + path
;
535 if (m_TempPath
[m_TempPath
.Length() - 1] != _T('/'))
536 m_TempPath
<< _T('/');
542 static wxString
SafeFileName(const wxString
& s
)
545 res
.Replace(wxT("#"), wxT("_"));
546 res
.Replace(wxT(":"), wxT("_"));
547 res
.Replace(wxT("\\"), wxT("_"));
548 res
.Replace(wxT("/"), wxT("_"));
552 bool wxHtmlHelpData::AddBookParam(const wxFSFile
& bookfile
,
553 wxFontEncoding encoding
,
554 const wxString
& title
, const wxString
& contfile
,
555 const wxString
& indexfile
, const wxString
& deftopic
,
556 const wxString
& path
)
560 wxHtmlBookRecord
*bookr
;
562 int IndexOld
= m_IndexCnt
,
563 ContentsOld
= m_ContentsCnt
;
565 if (! path
.IsEmpty())
566 fsys
.ChangePathTo(path
, TRUE
);
568 bookr
= new wxHtmlBookRecord(fsys
.GetPath(), title
, deftopic
);
570 if (m_ContentsCnt
% wxHTML_REALLOC_STEP
== 0)
571 m_Contents
= (wxHtmlContentsItem
*) realloc(m_Contents
, (m_ContentsCnt
+ wxHTML_REALLOC_STEP
) * sizeof(wxHtmlContentsItem
));
572 m_Contents
[m_ContentsCnt
].m_Level
= 0;
573 m_Contents
[m_ContentsCnt
].m_ID
= 0;
574 m_Contents
[m_ContentsCnt
].m_Page
= new wxChar
[deftopic
.Length() + 1];
575 wxStrcpy(m_Contents
[m_ContentsCnt
].m_Page
, deftopic
.c_str());
576 m_Contents
[m_ContentsCnt
].m_Name
= new wxChar
[title
.Length() + 1];
577 wxStrcpy(m_Contents
[m_ContentsCnt
].m_Name
, title
.c_str());
578 m_Contents
[m_ContentsCnt
].m_Book
= bookr
;
580 // store the contents index for later
581 int cont_start
= m_ContentsCnt
++;
583 // Try to find cached binary versions:
584 // 1. save file as book, but with .hhp.cached extension
585 // 2. same as 1. but in temp path
586 // 3. otherwise or if cache load failed, load it from MS.
588 fi
= fsys
.OpenFile(bookfile
.GetLocation() + wxT(".cached"));
591 fi
->GetModificationTime() < bookfile
.GetModificationTime() ||
592 !LoadCachedBook(bookr
, fi
->GetStream()))
594 if (fi
!= NULL
) delete fi
;
595 fi
= fsys
.OpenFile(m_TempPath
+ wxFileNameFromPath(bookfile
.GetLocation()) + wxT(".cached"));
596 if (m_TempPath
== wxEmptyString
|| fi
== NULL
||
597 fi
->GetModificationTime() < bookfile
.GetModificationTime() ||
598 !LoadCachedBook(bookr
, fi
->GetStream()))
600 LoadMSProject(bookr
, fsys
, indexfile
, contfile
);
601 if (m_TempPath
!= wxEmptyString
)
603 wxFileOutputStream
*outs
= new wxFileOutputStream(m_TempPath
+
604 SafeFileName(wxFileNameFromPath(bookfile
.GetLocation())) + wxT(".cached"));
605 SaveCachedBook(bookr
, outs
);
611 if (fi
!= NULL
) delete fi
;
613 // Now store the contents range
614 bookr
->SetContentsRange(cont_start
, m_ContentsCnt
);
616 // Convert encoding, if neccessary:
617 if (encoding
!= wxFONTENCODING_SYSTEM
)
619 wxFontEncodingArray a
= wxEncodingConverter::GetPlatformEquivalents(encoding
);
620 if (a
.GetCount() != 0 && a
[0] != encoding
)
623 wxEncodingConverter conv
;
624 conv
.Init(encoding
, a
[0]);
626 for (i
= IndexOld
; i
< m_IndexCnt
; i
++)
627 conv
.Convert(m_Index
[i
].m_Name
);
628 for (i
= ContentsOld
; i
< m_ContentsCnt
; i
++)
629 conv
.Convert(m_Contents
[i
].m_Name
);
633 m_BookRecords
.Add(bookr
);
635 qsort(m_Index
, m_IndexCnt
, sizeof(wxHtmlContentsItem
), IndexCompareFunc
);
641 bool wxHtmlHelpData::AddBook(const wxString
& book
)
643 if (book
.Right(4).Lower() == wxT(".zip") ||
644 book
.Right(4).Lower() == wxT(".htb") /*html book*/)
651 s
= fsys
.FindFirst(book
+ wxT("#zip:") + wxT("*.hhp"), wxFILE
);
654 if (AddBook(s
)) rt
= TRUE
;
670 char *buff
, *lineptr
;
673 wxString title
= _("noname"),
675 start
= wxEmptyString
,
676 contents
= wxEmptyString
,
677 index
= wxEmptyString
,
678 charset
= wxEmptyString
;
680 if (wxIsAbsolutePath(book
)) bookFull
= book
;
681 else bookFull
= wxGetCwd() + "/" + book
;
683 fi
= fsys
.OpenFile(bookFull
);
686 wxLogError(_("Cannot open HTML help book: %s"), bookFull
.c_str());
689 fsys
.ChangePathTo(bookFull
);
692 buff
= new char[sz
+ 1];
698 lineptr
= ReadLine(lineptr
, linebuf
);
700 if (strstr(linebuf
, "Title=") == linebuf
)
701 title
= linebuf
+ strlen("Title=");
702 if (strstr(linebuf
, "Default topic=") == linebuf
)
703 start
= linebuf
+ strlen("Default topic=");
704 if (strstr(linebuf
, "Index file=") == linebuf
)
705 index
= linebuf
+ strlen("Index file=");
706 if (strstr(linebuf
, "Contents file=") == linebuf
)
707 contents
= linebuf
+ strlen("Contents file=");
708 if (strstr(linebuf
, "Charset=") == linebuf
)
709 charset
= linebuf
+ strlen("Charset=");
710 } while (lineptr
!= NULL
);
714 if (charset
== wxEmptyString
) enc
= wxFONTENCODING_SYSTEM
;
715 else enc
= wxTheFontMapper
->CharsetToEncoding(charset
);
716 bool rtval
= AddBookParam(*fi
, enc
,
717 title
, contents
, index
, start
, fsys
.GetPath());
723 wxString
wxHtmlHelpData::FindPageByName(const wxString
& x
)
729 wxString
url(wxEmptyString
);
731 /* 1. try to open given file: */
733 cnt
= m_BookRecords
.GetCount();
734 for (i
= 0; i
< cnt
; i
++)
736 f
= fsys
.OpenFile(m_BookRecords
[i
].GetBasePath() + x
);
739 url
= m_BookRecords
[i
].GetBasePath() + x
;
746 /* 2. try to find a book: */
748 for (i
= 0; i
< cnt
; i
++)
750 if (m_BookRecords
[i
].GetTitle() == x
)
752 url
= m_BookRecords
[i
].GetBasePath() + m_BookRecords
[i
].GetStart();
757 /* 3. try to find in contents: */
760 for (i
= 0; i
< cnt
; i
++)
762 if (wxStrcmp(m_Contents
[i
].m_Name
, x
) == 0)
764 url
= m_Contents
[i
].m_Book
->GetBasePath() + m_Contents
[i
].m_Page
;
770 /* 4. try to find in index: */
773 for (i
= 0; i
< cnt
; i
++)
775 if (wxStrcmp(m_Index
[i
].m_Name
, x
) == 0)
777 url
= m_Index
[i
].m_Book
->GetBasePath() + m_Index
[i
].m_Page
;
785 wxString
wxHtmlHelpData::FindPageById(int id
)
788 wxString
url(wxEmptyString
);
790 for (i
= 0; i
< m_ContentsCnt
; i
++)
792 if (m_Contents
[i
].m_ID
== id
)
794 url
= m_Contents
[i
].m_Book
->GetBasePath() + m_Contents
[i
].m_Page
;
802 //----------------------------------------------------------------------------------
803 // wxHtmlSearchStatus functions
804 //----------------------------------------------------------------------------------
806 wxHtmlSearchStatus::wxHtmlSearchStatus(wxHtmlHelpData
* data
, const wxString
& keyword
,
807 bool case_sensitive
, bool whole_words_only
,
808 const wxString
& book
)
812 wxHtmlBookRecord
* bookr
= NULL
;
813 if (book
!= wxEmptyString
)
815 // we have to search in a specific book. Find it first
816 int i
, cnt
= data
->m_BookRecords
.GetCount();
817 for (i
= 0; i
< cnt
; i
++)
818 if (data
->m_BookRecords
[i
].GetTitle() == book
)
820 bookr
= &(data
->m_BookRecords
[i
]);
821 m_CurIndex
= bookr
->GetContentsStart();
822 m_MaxIndex
= bookr
->GetContentsEnd();
825 // check; we won't crash if the book doesn't exist, but it's Bad Anyway.
830 // no book specified; search all books
832 m_MaxIndex
= m_Data
->m_ContentsCnt
;
834 m_Engine
.LookFor(keyword
, case_sensitive
, whole_words_only
);
835 m_Active
= (m_CurIndex
< m_MaxIndex
);
839 bool wxHtmlSearchStatus::Search()
842 int i
= m_CurIndex
; // shortcut
848 // sanity check. Illegal use, but we'll try to prevent a crash anyway
853 m_Name
= wxEmptyString
;
854 m_ContentsItem
= NULL
;
855 thepage
= m_Data
->m_Contents
[i
].m_Page
;
857 m_Active
= (++m_CurIndex
< m_MaxIndex
);
858 // check if it is same page with different anchor:
859 if (m_LastPage
!= NULL
)
862 for (p1
= thepage
, p2
= m_LastPage
;
863 *p1
!= 0 && *p1
!= _T('#') && *p1
== *p2
; p1
++, p2
++) {}
865 m_LastPage
= thepage
;
867 if (*p1
== 0 || *p1
== _T('#'))
870 else m_LastPage
= thepage
;
873 file
= fsys
.OpenFile(m_Data
->m_Contents
[i
].m_Book
->GetBasePath() + thepage
);
876 if (m_Engine
.Scan(file
->GetStream())) {
877 m_Name
= m_Data
->m_Contents
[i
].m_Name
;
878 m_ContentsItem
= m_Data
->m_Contents
+ i
;
893 //--------------------------------------------------------------------------------
895 //--------------------------------------------------------------------------------
897 void wxSearchEngine::LookFor(const wxString
& keyword
, bool case_sensitive
, bool whole_words_only
)
899 m_CaseSensitive
= case_sensitive
;
900 m_WholeWords
= whole_words_only
;
901 if (m_Keyword
) delete[] m_Keyword
;
902 m_Keyword
= new wxChar
[keyword
.Length() + 1];
903 wxStrcpy(m_Keyword
, keyword
.c_str());
905 if (!m_CaseSensitive
)
907 for (int i
= wxStrlen(m_Keyword
) - 1; i
>= 0; i
--)
909 if ((m_Keyword
[i
] >= wxT('A')) && (m_Keyword
[i
] <= wxT('Z')))
910 m_Keyword
[i
] += wxT('a') - wxT('A');
917 #define WHITESPACE(c) (c == ' ' || c == '\n' || c == '\r' || c == '\t')
919 bool wxSearchEngine::Scan(wxInputStream
*stream
)
921 wxASSERT_MSG(m_Keyword
!= NULL
, wxT("wxSearchEngine::LookFor must be called before scanning!"));
924 int lng
= stream
->GetSize();
925 int wrd
= wxStrlen(m_Keyword
);
927 char *buf
= new char[lng
+ 1];
928 stream
->Read(buf
, lng
);
931 if (!m_CaseSensitive
)
932 for (i
= 0; i
< lng
; i
++)
933 if ((buf
[i
] >= 'A') && (buf
[i
] <= 'Z')) buf
[i
] += 'a' - 'A';
937 for (i
= 0; i
< lng
- wrd
; i
++)
939 if (WHITESPACE(buf
[i
])) continue;
941 while ((j
< wrd
) && (buf
[i
+ j
] == m_Keyword
[j
])) j
++;
942 if (j
== wrd
&& WHITESPACE(buf
[i
+ j
])) { found
= TRUE
; break; }
948 for (i
= 0; i
< lng
- wrd
; i
++)
951 while ((j
< wrd
) && (buf
[i
+ j
] == m_Keyword
[j
])) j
++;
952 if (j
== wrd
) { found
= TRUE
; break; }