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 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "helpdata.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #if wxUSE_HTML && wxUSE_STREAMS
34 #include "wx/html/helpdata.h"
35 #include "wx/tokenzr.h"
36 #include "wx/wfstream.h"
37 #include "wx/busyinfo.h"
38 #include "wx/encconv.h"
39 #include "wx/fontmap.h"
41 #include "wx/html/htmlpars.h"
42 #include "wx/html/htmldefs.h"
43 #include "wx/html/htmlfilt.h"
44 #include "wx/filename.h"
46 #include "wx/arrimpl.cpp"
47 WX_DEFINE_OBJARRAY(wxHtmlBookRecArray
)
48 WX_DEFINE_OBJARRAY(wxHtmlHelpDataItems
)
50 //-----------------------------------------------------------------------------
51 // static helper functions
52 //-----------------------------------------------------------------------------
54 // Reads one line, stores it into buf and returns pointer to new line or NULL.
55 static const wxChar
* ReadLine(const wxChar
*line
, wxChar
*buf
, size_t bufsize
)
57 wxChar
*writeptr
= buf
;
58 wxChar
*endptr
= buf
+ bufsize
- 1;
59 const wxChar
*readptr
= line
;
61 while (*readptr
!= 0 && *readptr
!= _T('\r') && *readptr
!= _T('\n') &&
63 *(writeptr
++) = *(readptr
++);
65 while (*readptr
== _T('\r') || *readptr
== _T('\n'))
76 wxHtmlHelpIndexCompareFunc(wxHtmlHelpDataItem
**a
, wxHtmlHelpDataItem
**b
)
78 wxHtmlHelpDataItem
*ia
= *a
;
79 wxHtmlHelpDataItem
*ib
= *b
;
81 if (ia
->parent
== ib
->parent
)
83 return ia
->name
.CmpNoCase(ib
->name
);
85 else if (ia
->level
== ib
->level
)
87 return wxHtmlHelpIndexCompareFunc(&ia
->parent
, &ib
->parent
);
91 wxHtmlHelpDataItem
*ia2
= ia
;
92 wxHtmlHelpDataItem
*ib2
= ib
;
94 while (ia2
->level
> ib2
->level
)
98 while (ib2
->level
> ia2
->level
)
105 int res
= wxHtmlHelpIndexCompareFunc(&ia2
, &ib2
);
108 else if (ia
->level
> ib
->level
)
115 //-----------------------------------------------------------------------------
117 //-----------------------------------------------------------------------------
119 class HP_Parser
: public wxHtmlParser
124 GetEntitiesParser()->SetEncoding(wxFONTENCODING_ISO8859_1
);
127 wxObject
* GetProduct() { return NULL
; }
130 virtual void AddText(const wxChar
* WXUNUSED(txt
)) {}
132 DECLARE_NO_COPY_CLASS(HP_Parser
)
136 //-----------------------------------------------------------------------------
138 //-----------------------------------------------------------------------------
140 class HP_TagHandler
: public wxHtmlTagHandler
143 wxString m_name
, m_page
;
148 wxHtmlHelpDataItem
*m_parentItem
;
149 wxHtmlBookRecord
*m_book
;
151 wxHtmlHelpDataItems
*m_data
;
154 HP_TagHandler(wxHtmlBookRecord
*b
) : wxHtmlTagHandler()
158 m_name
= m_page
= wxEmptyString
;
164 wxString
GetSupportedTags() { return wxT("UL,OBJECT,PARAM"); }
165 bool HandleTag(const wxHtmlTag
& tag
);
167 void Reset(wxHtmlHelpDataItems
& data
)
175 DECLARE_NO_COPY_CLASS(HP_TagHandler
)
179 bool HP_TagHandler::HandleTag(const wxHtmlTag
& tag
)
181 if (tag
.GetName() == wxT("UL"))
183 wxHtmlHelpDataItem
*oldparent
= m_parentItem
;
185 m_parentItem
= (m_count
> 0) ? &(*m_data
)[m_data
->size()-1] : NULL
;
188 m_parentItem
= oldparent
;
191 else if (tag
.GetName() == wxT("OBJECT"))
193 m_name
= m_page
= wxEmptyString
;
198 /* Valid HHW's file may contain only two object tags:
200 <OBJECT type="text/site properties">
201 <param name="ImageType" value="Folder">
206 <OBJECT type="text/sitemap">
207 <param name="Name" value="main page">
208 <param name="Local" value="another.htm">
211 We're interested in the latter. !page.IsEmpty() is valid
212 condition because text/site properties does not contain Local param
215 if (tag
.GetParam(wxT("TYPE")) == wxT("text/sitemap"))
217 wxHtmlHelpDataItem
*item
= new wxHtmlHelpDataItem();
218 item
->parent
= m_parentItem
;
219 item
->level
= m_level
;
233 if (m_name
.empty() && tag
.GetParam(wxT("NAME")) == wxT("Name"))
234 m_name
= tag
.GetParam(wxT("VALUE"));
235 if (tag
.GetParam(wxT("NAME")) == wxT("Local"))
236 m_page
= tag
.GetParam(wxT("VALUE"));
237 if (tag
.GetParam(wxT("NAME")) == wxT("ID"))
238 tag
.GetParamAsInt(wxT("VALUE"), &m_id
);
244 //-----------------------------------------------------------------------------
246 //-----------------------------------------------------------------------------
248 wxString
wxHtmlBookRecord::GetFullPath(const wxString
&page
) const
250 if (wxIsAbsolutePath(page
))
253 return m_BasePath
+ page
;
256 wxString
wxHtmlHelpDataItem::GetIndentedName() const
259 for (int i
= 1; i
< level
; i
++)
266 IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpData
, wxObject
)
268 wxHtmlHelpData::wxHtmlHelpData()
270 #if WXWIN_COMPATIBILITY_2_4
271 m_cacheContents
= NULL
;
276 wxHtmlHelpData::~wxHtmlHelpData()
278 #if WXWIN_COMPATIBILITY_2_4
279 CleanCompatibilityData();
283 bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord
*book
, wxFileSystem
& fsys
,
284 const wxString
& indexfile
,
285 const wxString
& contentsfile
)
288 wxHtmlFilterHTML filter
;
293 HP_TagHandler
*handler
= new HP_TagHandler(book
);
294 parser
.AddTagHandler(handler
);
296 f
= ( contentsfile
.IsEmpty() ? (wxFSFile
*) NULL
: fsys
.OpenFile(contentsfile
) );
300 buf
= filter
.ReadFile(*f
);
302 handler
->Reset(m_contents
);
307 wxLogError(_("Cannot open contents file: %s"), contentsfile
.c_str());
310 f
= ( indexfile
.IsEmpty() ? (wxFSFile
*) NULL
: fsys
.OpenFile(indexfile
) );
314 buf
= filter
.ReadFile(*f
);
316 handler
->Reset(m_index
);
319 else if (!indexfile
.IsEmpty())
321 wxLogError(_("Cannot open index file: %s"), indexfile
.c_str());
326 inline static void CacheWriteInt32(wxOutputStream
*f
, wxInt32 value
)
328 wxInt32 x
= wxINT32_SWAP_ON_BE(value
);
329 f
->Write(&x
, sizeof(x
));
332 inline static wxInt32
CacheReadInt32(wxInputStream
*f
)
335 f
->Read(&x
, sizeof(x
));
336 return wxINT32_SWAP_ON_BE(x
);
339 inline static void CacheWriteString(wxOutputStream
*f
, const wxString
& str
)
341 const wxWX2MBbuf mbstr
= str
.mb_str(wxConvUTF8
);
342 size_t len
= strlen((const char*)mbstr
)+1;
343 CacheWriteInt32(f
, len
);
344 f
->Write((const char*)mbstr
, len
);
347 inline static wxString
CacheReadString(wxInputStream
*f
)
349 size_t len
= (size_t)CacheReadInt32(f
);
350 wxCharBuffer
str(len
-1);
351 f
->Read(str
.data(), len
);
352 return wxString(str
, wxConvUTF8
);
355 #define CURRENT_CACHED_BOOK_VERSION 5
357 // Additional flags to detect incompatibilities of the runtime environment:
358 #define CACHED_BOOK_FORMAT_FLAGS \
362 bool wxHtmlHelpData::LoadCachedBook(wxHtmlBookRecord
*book
, wxInputStream
*f
)
367 /* load header - version info : */
368 version
= CacheReadInt32(f
);
370 if (version
!= CURRENT_CACHED_BOOK_VERSION
)
372 // NB: We can just silently return FALSE here and don't worry about
373 // it anymore, because AddBookParam will load the MS project in
374 // absence of (properly versioned) .cached file and automatically
375 // create new .cached file immediately afterward.
379 if (CacheReadInt32(f
) != CACHED_BOOK_FORMAT_FLAGS
)
382 /* load contents : */
383 st
= m_contents
.size();
384 newsize
= st
+ CacheReadInt32(f
);
385 m_contents
.Alloc(newsize
);
386 for (i
= st
; i
< newsize
; i
++)
388 wxHtmlHelpDataItem
*item
= new wxHtmlHelpDataItem
;
389 item
->level
= CacheReadInt32(f
);
390 item
->id
= CacheReadInt32(f
);
391 item
->name
= CacheReadString(f
);
392 item
->page
= CacheReadString(f
);
394 m_contents
.Add(item
);
399 newsize
= st
+ CacheReadInt32(f
);
400 m_index
.Alloc(newsize
);
401 for (i
= st
; i
< newsize
; i
++)
403 wxHtmlHelpDataItem
*item
= new wxHtmlHelpDataItem
;
404 item
->name
= CacheReadString(f
);
405 item
->page
= CacheReadString(f
);
406 item
->level
= CacheReadInt32(f
);
408 int parentShift
= CacheReadInt32(f
);
409 if (parentShift
!= 0)
410 item
->parent
= &m_index
[m_index
.size() - parentShift
];
417 bool wxHtmlHelpData::SaveCachedBook(wxHtmlBookRecord
*book
, wxOutputStream
*f
)
422 /* save header - version info : */
423 CacheWriteInt32(f
, CURRENT_CACHED_BOOK_VERSION
);
424 CacheWriteInt32(f
, CACHED_BOOK_FORMAT_FLAGS
);
426 /* save contents : */
427 int len
= m_contents
.size();
428 for (cnt
= 0, i
= 0; i
< len
; i
++)
429 if (m_contents
[i
].book
== book
&& m_contents
[i
].level
> 0)
431 CacheWriteInt32(f
, cnt
);
433 for (i
= 0; i
< len
; i
++)
435 if (m_contents
[i
].book
!= book
|| m_contents
[i
].level
== 0)
437 CacheWriteInt32(f
, m_contents
[i
].level
);
438 CacheWriteInt32(f
, m_contents
[i
].id
);
439 CacheWriteString(f
, m_contents
[i
].name
);
440 CacheWriteString(f
, m_contents
[i
].page
);
444 len
= m_index
.size();
445 for (cnt
= 0, i
= 0; i
< len
; i
++)
446 if (m_index
[i
].book
== book
&& m_index
[i
].level
> 0)
448 CacheWriteInt32(f
, cnt
);
450 for (i
= 0; i
< len
; i
++)
452 if (m_index
[i
].book
!= book
|| m_index
[i
].level
== 0)
454 CacheWriteString(f
, m_index
[i
].name
);
455 CacheWriteString(f
, m_index
[i
].page
);
456 CacheWriteInt32(f
, m_index
[i
].level
);
457 // save distance to parent item, if any:
458 if (m_index
[i
].parent
== NULL
)
460 CacheWriteInt32(f
, 0);
465 wxHtmlHelpDataItem
*parent
= m_index
[i
].parent
;
466 for (int j
= i
-1; j
>= 0; j
--)
468 if (m_index
[j
].book
== book
&& m_index
[j
].level
> 0)
470 if (&m_index
[j
] == parent
)
474 CacheWriteInt32(f
, cnt
);
481 void wxHtmlHelpData::SetTempDir(const wxString
& path
)
487 if (wxIsAbsolutePath(path
)) m_tempPath
= path
;
488 else m_tempPath
= wxGetCwd() + _T("/") + path
;
490 if (m_tempPath
[m_tempPath
.Length() - 1] != _T('/'))
491 m_tempPath
<< _T('/');
497 static wxString
SafeFileName(const wxString
& s
)
500 res
.Replace(wxT("#"), wxT("_"));
501 res
.Replace(wxT(":"), wxT("_"));
502 res
.Replace(wxT("\\"), wxT("_"));
503 res
.Replace(wxT("/"), wxT("_"));
507 bool wxHtmlHelpData::AddBookParam(const wxFSFile
& bookfile
,
508 wxFontEncoding encoding
,
509 const wxString
& title
, const wxString
& contfile
,
510 const wxString
& indexfile
, const wxString
& deftopic
,
511 const wxString
& path
)
515 wxHtmlBookRecord
*bookr
;
517 int IndexOld
= m_index
.size(),
518 ContentsOld
= m_contents
.size();
521 fsys
.ChangePathTo(path
, TRUE
);
523 size_t booksCnt
= m_bookRecords
.GetCount();
524 for (size_t i
= 0; i
< booksCnt
; i
++)
526 if ( m_bookRecords
[i
].GetBookFile() == bookfile
.GetLocation() )
527 return true; // book is (was) loaded
530 bookr
= new wxHtmlBookRecord(bookfile
.GetLocation(), fsys
.GetPath(), title
, deftopic
);
532 wxHtmlHelpDataItem
*bookitem
= new wxHtmlHelpDataItem
;
535 bookitem
->page
= deftopic
;
536 bookitem
->name
= title
;
537 bookitem
->book
= bookr
;
539 // store the contents index for later
540 int cont_start
= m_contents
.size();
542 m_contents
.Add(bookitem
);
544 // Try to find cached binary versions:
545 // 1. save file as book, but with .hhp.cached extension
546 // 2. same as 1. but in temp path
547 // 3. otherwise or if cache load failed, load it from MS.
549 fi
= fsys
.OpenFile(bookfile
.GetLocation() + wxT(".cached"));
553 fi
->GetModificationTime() < bookfile
.GetModificationTime() ||
554 #endif // wxUSE_DATETIME
555 !LoadCachedBook(bookr
, fi
->GetStream()))
557 if (fi
!= NULL
) delete fi
;
558 fi
= fsys
.OpenFile(m_tempPath
+ wxFileNameFromPath(bookfile
.GetLocation()) + wxT(".cached"));
559 if (m_tempPath
.empty() || fi
== NULL
||
561 fi
->GetModificationTime() < bookfile
.GetModificationTime() ||
562 #endif // wxUSE_DATETIME
563 !LoadCachedBook(bookr
, fi
->GetStream()))
565 LoadMSProject(bookr
, fsys
, indexfile
, contfile
);
566 if (!m_tempPath
.empty())
568 wxFileOutputStream
*outs
= new wxFileOutputStream(m_tempPath
+
569 SafeFileName(wxFileNameFromPath(bookfile
.GetLocation())) + wxT(".cached"));
570 SaveCachedBook(bookr
, outs
);
576 if (fi
!= NULL
) delete fi
;
578 // Now store the contents range
579 bookr
->SetContentsRange(cont_start
, m_contents
.size());
582 // MS HTML Help files [written by MS HTML Help Workshop] are broken
583 // in that the data are iso-8859-1 (including HTML entities), but must
584 // be interpreted as being in language's windows charset. Correct the
585 // differences here and also convert to wxConvLocal in ANSI build
586 if (encoding
!= wxFONTENCODING_SYSTEM
)
589 #define CORRECT_STR(str, conv) \
590 str = wxString((str).mb_str(wxConvISO8859_1), conv)
592 #define CORRECT_STR(str, conv) \
593 str = wxString((str).wc_str(conv), wxConvLocal)
595 wxCSConv
conv(encoding
);
596 size_t IndexCnt
= m_index
.size();
597 size_t ContentsCnt
= m_contents
.size();
599 for (i
= IndexOld
; i
< IndexCnt
; i
++)
601 CORRECT_STR(m_index
[i
].name
, conv
);
603 for (i
= ContentsOld
; i
< ContentsCnt
; i
++)
605 CORRECT_STR(m_contents
[i
].name
, conv
);
610 wxUnusedVar(IndexOld
);
611 wxUnusedVar(ContentsOld
);
612 wxASSERT_MSG(encoding
== wxFONTENCODING_SYSTEM
, wxT("Help files need charset conversion, but wxUSE_WCHAR_T is 0"));
613 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
615 m_bookRecords
.Add(bookr
);
616 if (!m_index
.empty())
618 m_index
.Sort(wxHtmlHelpIndexCompareFunc
);
625 bool wxHtmlHelpData::AddBook(const wxString
& book
)
627 wxString
extension(book
.Right(4).Lower());
628 if (extension
== wxT(".zip") ||
630 extension
== wxT(".chm") /*compressed html help book*/ ||
632 extension
== wxT(".htb") /*html book*/)
639 if (extension
== wxT(".chm"))
640 s
= fsys
.FindFirst(book
+ wxT("#chm:*.hhp"), wxFILE
);
643 s
= fsys
.FindFirst(book
+ wxT("#zip:*.hhp"), wxFILE
);
647 if (AddBook(s
)) rt
= TRUE
;
657 wxString title
= _("noname"),
659 start
= wxEmptyString
,
660 contents
= wxEmptyString
,
661 index
= wxEmptyString
,
662 charset
= wxEmptyString
;
664 fi
= fsys
.OpenFile(book
);
667 wxLogError(_("Cannot open HTML help book: %s"), book
.c_str());
670 fsys
.ChangePathTo(book
);
672 const wxChar
*lineptr
;
675 wxHtmlFilterPlainText filter
;
676 tmp
= filter
.ReadFile(*fi
);
677 lineptr
= tmp
.c_str();
681 lineptr
= ReadLine(lineptr
, linebuf
, 300);
683 for (wxChar
*ch
= linebuf
; *ch
!= wxT('\0') && *ch
!= wxT('='); ch
++)
686 if (wxStrstr(linebuf
, _T("title=")) == linebuf
)
687 title
= linebuf
+ wxStrlen(_T("title="));
688 if (wxStrstr(linebuf
, _T("default topic=")) == linebuf
)
689 start
= linebuf
+ wxStrlen(_T("default topic="));
690 if (wxStrstr(linebuf
, _T("index file=")) == linebuf
)
691 index
= linebuf
+ wxStrlen(_T("index file="));
692 if (wxStrstr(linebuf
, _T("contents file=")) == linebuf
)
693 contents
= linebuf
+ wxStrlen(_T("contents file="));
694 if (wxStrstr(linebuf
, _T("charset=")) == linebuf
)
695 charset
= linebuf
+ wxStrlen(_T("charset="));
696 } while (lineptr
!= NULL
);
699 if (charset
== wxEmptyString
) enc
= wxFONTENCODING_SYSTEM
;
700 else enc
= wxFontMapper::Get()->CharsetToEncoding(charset
);
702 bool rtval
= AddBookParam(*fi
, enc
,
703 title
, contents
, index
, start
, fsys
.GetPath());
706 #if WXWIN_COMPATIBILITY_2_4
707 CleanCompatibilityData();
713 wxString
wxHtmlHelpData::FindPageByName(const wxString
& x
)
720 /* 1. try to open given file: */
722 cnt
= m_bookRecords
.GetCount();
723 for (i
= 0; i
< cnt
; i
++)
725 f
= fsys
.OpenFile(m_bookRecords
[i
].GetFullPath(x
));
728 wxString url
= m_bookRecords
[i
].GetFullPath(x
);
735 /* 2. try to find a book: */
737 for (i
= 0; i
< cnt
; i
++)
739 if (m_bookRecords
[i
].GetTitle() == x
)
740 return m_bookRecords
[i
].GetFullPath(m_bookRecords
[i
].GetStart());
743 /* 3. try to find in contents: */
745 cnt
= m_contents
.size();
746 for (i
= 0; i
< cnt
; i
++)
748 if (m_contents
[i
].name
== x
)
749 return m_contents
[i
].GetFullPath();
753 /* 4. try to find in index: */
755 cnt
= m_index
.size();
756 for (i
= 0; i
< cnt
; i
++)
758 if (m_index
[i
].name
== x
)
759 return m_index
[i
].GetFullPath();
762 return wxEmptyString
;
765 wxString
wxHtmlHelpData::FindPageById(int id
)
767 size_t cnt
= m_contents
.size();
768 for (size_t i
= 0; i
< cnt
; i
++)
770 if (m_contents
[i
].id
== id
)
772 return m_contents
[i
].GetFullPath();
776 return wxEmptyString
;
779 #if WXWIN_COMPATIBILITY_2_4
780 wxHtmlContentsItem::wxHtmlContentsItem()
781 : m_Level(0), m_ID(-1), m_Name(NULL
), m_Page(NULL
), m_Book(NULL
),
786 wxHtmlContentsItem::wxHtmlContentsItem(const wxHtmlHelpDataItem
& d
)
791 m_Name
= wxStrdup(d
.name
.c_str());
792 m_Page
= wxStrdup(d
.page
.c_str());
796 wxHtmlContentsItem
& wxHtmlContentsItem::operator=(const wxHtmlContentsItem
& d
)
806 m_Name
= d
.m_Name
? wxStrdup(d
.m_Name
) : NULL
;
807 m_Page
= d
.m_Page
? wxStrdup(d
.m_Page
) : NULL
;
812 wxHtmlContentsItem::~wxHtmlContentsItem()
821 wxHtmlContentsItem
* wxHtmlHelpData::GetContents()
823 if (!m_cacheContents
&& !m_contents
.empty())
825 size_t len
= m_contents
.size();
826 m_cacheContents
= new wxHtmlContentsItem
[len
];
827 for (size_t i
= 0; i
< len
; i
++)
828 m_cacheContents
[i
] = m_contents
[i
];
830 return m_cacheContents
;
833 int wxHtmlHelpData::GetContentsCnt()
835 return m_contents
.size();
838 wxHtmlContentsItem
* wxHtmlHelpData::GetIndex()
840 if (!m_cacheContents
&& !m_index
.empty())
842 size_t len
= m_index
.size();
843 m_cacheContents
= new wxHtmlContentsItem
[len
];
844 for (size_t i
= 0; i
< len
; i
++)
845 m_cacheContents
[i
] = m_index
[i
];
847 return m_cacheContents
;
850 int wxHtmlHelpData::GetIndexCnt()
852 return m_index
.size();
855 void wxHtmlHelpData::CleanCompatibilityData()
857 delete[] m_cacheContents
;
858 m_cacheContents
= NULL
;
859 delete[] m_cacheIndex
;
862 #endif // WXWIN_COMPATIBILITY_2_4
864 //----------------------------------------------------------------------------------
865 // wxHtmlSearchStatus functions
866 //----------------------------------------------------------------------------------
868 wxHtmlSearchStatus::wxHtmlSearchStatus(wxHtmlHelpData
* data
, const wxString
& keyword
,
869 bool case_sensitive
, bool whole_words_only
,
870 const wxString
& book
)
874 wxHtmlBookRecord
* bookr
= NULL
;
875 if (book
!= wxEmptyString
)
877 // we have to search in a specific book. Find it first
878 int i
, cnt
= data
->m_bookRecords
.GetCount();
879 for (i
= 0; i
< cnt
; i
++)
880 if (data
->m_bookRecords
[i
].GetTitle() == book
)
882 bookr
= &(data
->m_bookRecords
[i
]);
883 m_CurIndex
= bookr
->GetContentsStart();
884 m_MaxIndex
= bookr
->GetContentsEnd();
887 // check; we won't crash if the book doesn't exist, but it's Bad Anyway.
892 // no book specified; search all books
894 m_MaxIndex
= m_Data
->m_contents
.size();
896 m_Engine
.LookFor(keyword
, case_sensitive
, whole_words_only
);
897 m_Active
= (m_CurIndex
< m_MaxIndex
);
900 #if WXWIN_COMPATIBILITY_2_4
901 wxHtmlContentsItem
* wxHtmlSearchStatus::GetContentsItem()
903 static wxHtmlContentsItem it
;
904 it
= wxHtmlContentsItem(*m_CurItem
);
909 bool wxHtmlSearchStatus::Search()
912 int i
= m_CurIndex
; // shortcut
918 // sanity check. Illegal use, but we'll try to prevent a crash anyway
923 m_Name
= wxEmptyString
;
925 thepage
= m_Data
->m_contents
[i
].page
;
927 m_Active
= (++m_CurIndex
< m_MaxIndex
);
928 // check if it is same page with different anchor:
929 if (!m_LastPage
.empty())
931 const wxChar
*p1
, *p2
;
932 for (p1
= thepage
.c_str(), p2
= m_LastPage
.c_str();
933 *p1
!= 0 && *p1
!= _T('#') && *p1
== *p2
; p1
++, p2
++) {}
935 m_LastPage
= thepage
;
937 if (*p1
== 0 || *p1
== _T('#'))
940 else m_LastPage
= thepage
;
943 file
= fsys
.OpenFile(m_Data
->m_contents
[i
].book
->GetFullPath(thepage
));
946 if (m_Engine
.Scan(*file
))
948 m_Name
= m_Data
->m_contents
[i
].name
;
949 m_CurItem
= &m_Data
->m_contents
[i
];
964 //--------------------------------------------------------------------------------
965 // wxHtmlSearchEngine
966 //--------------------------------------------------------------------------------
968 void wxHtmlSearchEngine::LookFor(const wxString
& keyword
, bool case_sensitive
, bool whole_words_only
)
970 m_CaseSensitive
= case_sensitive
;
971 m_WholeWords
= whole_words_only
;
975 m_Keyword
.LowerCase();
979 static inline bool WHITESPACE(wxChar c
)
981 return c
== _T(' ') || c
== _T('\n') || c
== _T('\r') || c
== _T('\t');
984 bool wxHtmlSearchEngine::Scan(const wxFSFile
& file
)
986 wxASSERT_MSG(!m_Keyword
.empty(), wxT("wxHtmlSearchEngine::LookFor must be called before scanning!"));
989 int wrd
= m_Keyword
.Length();
991 wxHtmlFilterHTML filter
;
992 wxString tmp
= filter
.ReadFile(file
);
993 int lng
= tmp
.length();
994 const wxChar
*buf
= tmp
.c_str();
996 if (!m_CaseSensitive
)
999 const wxChar
*kwd
= m_Keyword
.c_str();
1003 for (i
= 0; i
< lng
- wrd
; i
++)
1005 if (WHITESPACE(buf
[i
])) continue;
1007 while ((j
< wrd
) && (buf
[i
+ j
] == kwd
[j
])) j
++;
1008 if (j
== wrd
&& WHITESPACE(buf
[i
+ j
])) { found
= true; break; }
1014 for (i
= 0; i
< lng
- wrd
; i
++)
1017 while ((j
< wrd
) && (buf
[i
+ j
] == kwd
[j
])) j
++;
1018 if (j
== wrd
) { found
= true; break; }