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
32 #include "wx/html/helpdata.h"
33 #include "wx/tokenzr.h"
34 #include "wx/wfstream.h"
35 #include "wx/busyinfo.h"
36 #include "wx/encconv.h"
37 #include "wx/fontmap.h"
39 #include "wx/html/htmlpars.h"
40 #include "wx/html/htmldefs.h"
41 #include "wx/filename.h"
43 #include "wx/arrimpl.cpp"
44 WX_DEFINE_OBJARRAY(wxHtmlBookRecArray
)
46 //-----------------------------------------------------------------------------
47 // static helper functions
48 //-----------------------------------------------------------------------------
50 // Reads one line, stores it into buf and returns pointer to new line or NULL.
51 static char* ReadLine(char *line
, char *buf
, size_t bufsize
)
54 char *endptr
= buf
+ bufsize
- 1;
57 while (*readptr
!= 0 && *readptr
!= '\r' && *readptr
!= '\n' &&
59 *(writeptr
++) = *(readptr
++);
61 while (*readptr
== '\r' || *readptr
== '\n')
71 extern "C" int LINKAGEMODE
72 wxHtmlHelpIndexCompareFunc(const void *a
, const void *b
)
74 return wxStricmp(((wxHtmlContentsItem
*)a
)->m_Name
, ((wxHtmlContentsItem
*)b
)->m_Name
);
78 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
82 class HP_Parser
: public wxHtmlParser
85 wxObject
* GetProduct() { return NULL
; }
87 virtual void AddText(const wxChar
* WXUNUSED(txt
)) {}
91 //-----------------------------------------------------------------------------
93 //-----------------------------------------------------------------------------
95 class HP_TagHandler
: public wxHtmlTagHandler
98 wxString m_Name
, m_Page
;
102 wxHtmlContentsItem
*m_Items
;
104 wxHtmlBookRecord
*m_Book
;
107 HP_TagHandler(wxHtmlBookRecord
*b
) : wxHtmlTagHandler()
108 { m_Book
= b
; m_Items
= NULL
; m_ItemsCnt
= 0; m_Name
= m_Page
= wxEmptyString
;
109 m_Level
= 0; m_ID
= -1; }
110 wxString
GetSupportedTags() { return wxT("UL,OBJECT,PARAM"); }
111 bool HandleTag(const wxHtmlTag
& tag
);
112 void WriteOut(wxHtmlContentsItem
*& array
, int& size
);
113 void ReadIn(wxHtmlContentsItem
* array
, int size
);
117 bool HP_TagHandler::HandleTag(const wxHtmlTag
& tag
)
119 if (tag
.GetName() == wxT("UL"))
126 else if (tag
.GetName() == wxT("OBJECT"))
128 m_Name
= m_Page
= wxEmptyString
;
132 if (!m_Page
.IsEmpty())
133 /* Valid HHW's file may contain only two object tags:
135 <OBJECT type="text/site properties">
136 <param name="ImageType" value="Folder">
141 <OBJECT type="text/sitemap">
142 <param name="Name" value="main page">
143 <param name="Local" value="another.htm">
146 We're interested in the latter. !m_Page.IsEmpty() is valid
147 condition because text/site properties does not contain Local param
150 if (tag
.GetParam(wxT("TYPE")) == wxT("text/sitemap"))
152 if (m_ItemsCnt
% wxHTML_REALLOC_STEP
== 0)
153 m_Items
= (wxHtmlContentsItem
*) realloc(m_Items
,
154 (m_ItemsCnt
+ wxHTML_REALLOC_STEP
) *
155 sizeof(wxHtmlContentsItem
));
157 m_Items
[m_ItemsCnt
].m_Level
= m_Level
;
158 m_Items
[m_ItemsCnt
].m_ID
= m_ID
;
159 m_Items
[m_ItemsCnt
].m_Page
= new wxChar
[m_Page
.Length() + 1];
160 wxStrcpy(m_Items
[m_ItemsCnt
].m_Page
, m_Page
.c_str());
161 m_Items
[m_ItemsCnt
].m_Name
= new wxChar
[m_Name
.Length() + 1];
162 wxStrcpy(m_Items
[m_ItemsCnt
].m_Name
, m_Name
.c_str());
163 m_Items
[m_ItemsCnt
].m_Book
= m_Book
;
171 if (m_Name
== wxEmptyString
&& tag
.GetParam(wxT("NAME")) == wxT("Name"))
172 m_Name
= tag
.GetParam(wxT("VALUE"));
173 if (tag
.GetParam(wxT("NAME")) == wxT("Local"))
174 m_Page
= tag
.GetParam(wxT("VALUE"));
175 if (tag
.GetParam(wxT("NAME")) == wxT("ID"))
176 tag
.GetParamAsInt(wxT("VALUE"), &m_ID
);
183 void HP_TagHandler::WriteOut(wxHtmlContentsItem
*& array
, int& size
)
191 void HP_TagHandler::ReadIn(wxHtmlContentsItem
* array
, int size
)
200 //-----------------------------------------------------------------------------
202 //-----------------------------------------------------------------------------
204 wxString
wxHtmlBookRecord::GetFullPath(const wxString
&page
) const
206 if (wxIsAbsolutePath(page
))
209 return m_BasePath
+ page
;
214 IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpData
, wxObject
)
216 wxHtmlHelpData::wxHtmlHelpData()
218 m_TempPath
= wxEmptyString
;
226 wxHtmlHelpData::~wxHtmlHelpData()
230 m_BookRecords
.Empty();
233 for (i
= 0; i
< m_ContentsCnt
; i
++)
235 delete[] m_Contents
[i
].m_Page
;
236 delete[] m_Contents
[i
].m_Name
;
242 for (i
= 0; i
< m_IndexCnt
; i
++)
244 delete[] m_Index
[i
].m_Page
;
245 delete[] m_Index
[i
].m_Name
;
251 bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord
*book
, wxFileSystem
& fsys
, const wxString
& indexfile
, const wxString
& contentsfile
)
259 HP_TagHandler
*handler
= new HP_TagHandler(book
);
260 parser
.AddTagHandler(handler
);
262 f
= ( contentsfile
.IsEmpty() ? (wxFSFile
*) NULL
: fsys
.OpenFile(contentsfile
) );
265 sz
= f
->GetStream()->GetSize();
266 buf
= new char[sz
+ 1];
268 f
->GetStream()->Read(buf
, sz
);
270 handler
->ReadIn(m_Contents
, m_ContentsCnt
);
272 handler
->WriteOut(m_Contents
, m_ContentsCnt
);
276 wxLogError(_("Cannot open contents file: %s"), contentsfile
.c_str());
278 f
= ( indexfile
.IsEmpty() ? (wxFSFile
*) NULL
: fsys
.OpenFile(indexfile
) );
281 sz
= f
->GetStream()->GetSize();
282 buf
= new char[sz
+ 1];
284 f
->GetStream()->Read(buf
, sz
);
286 handler
->ReadIn(m_Index
, m_IndexCnt
);
288 handler
->WriteOut(m_Index
, m_IndexCnt
);
291 else if (!indexfile
.IsEmpty())
292 wxLogError(_("Cannot open index file: %s"), indexfile
.c_str());
301 #define READ_STRING(f, s, lng) { char tmpc; for (int i = 0; i < lng; i++) { f->Read(&tmpc, 1); s[i] = (wxChar)tmpc;} }
302 #define WRITE_STRING(f, s, lng) { char tmpc; for (int i = 0; i < lng; i++) { tmpc = (char)s[i]; f->Write(&tmpc, 1);} }
306 #define READ_STRING(f, s, lng) f->Read(s, lng * sizeof(char));
307 #define WRITE_STRING(f, s, lng) f->Write(s, lng * sizeof(char));
312 #define CURRENT_CACHED_BOOK_VERSION 1
314 bool wxHtmlHelpData::LoadCachedBook(wxHtmlBookRecord
*book
, wxInputStream
*f
)
320 /* load header - version info : */
322 f
->Read(&x
, sizeof(x
));
323 version
= wxINT32_SWAP_ON_BE(x
);
325 if (version
!= CURRENT_CACHED_BOOK_VERSION
)
327 wxLogError(_("Incorrect version of HTML help book"));
329 // NOTE: when adding new version, please ensure backward compatibility!
332 /* load contents : */
334 f
->Read(&x
, sizeof(x
));
336 m_ContentsCnt
+= wxINT32_SWAP_ON_BE(x
);
337 m_Contents
= (wxHtmlContentsItem
*) realloc(m_Contents
,
338 (m_ContentsCnt
/ wxHTML_REALLOC_STEP
+ 1) *
339 wxHTML_REALLOC_STEP
* sizeof(wxHtmlContentsItem
));
340 for (i
= st
; i
< m_ContentsCnt
; i
++)
342 f
->Read(&x
, sizeof(x
));
343 m_Contents
[i
].m_Level
= wxINT32_SWAP_ON_BE(x
);
344 f
->Read(&x
, sizeof(x
));
345 m_Contents
[i
].m_ID
= wxINT32_SWAP_ON_BE(x
);
346 f
->Read(&x
, sizeof(x
)); x
= wxINT32_SWAP_ON_BE(x
);
347 m_Contents
[i
].m_Name
= new wxChar
[x
];
348 READ_STRING(f
, m_Contents
[i
].m_Name
, x
);
349 f
->Read(&x
, sizeof(x
)); x
= wxINT32_SWAP_ON_BE(x
);
350 m_Contents
[i
].m_Page
= new wxChar
[x
];
351 READ_STRING(f
, m_Contents
[i
].m_Page
, x
);
352 m_Contents
[i
].m_Book
= book
;
357 f
->Read(&x
, sizeof(x
));
359 m_IndexCnt
+= wxINT32_SWAP_ON_BE(x
);
360 m_Index
= (wxHtmlContentsItem
*) realloc(m_Index
, (m_IndexCnt
/ wxHTML_REALLOC_STEP
+ 1) *
361 wxHTML_REALLOC_STEP
* sizeof(wxHtmlContentsItem
));
362 for (i
= st
; i
< m_IndexCnt
; i
++)
364 f
->Read(&x
, sizeof(x
)); x
= wxINT32_SWAP_ON_BE(x
);
365 m_Index
[i
].m_Name
= new wxChar
[x
];
366 READ_STRING(f
, m_Index
[i
].m_Name
, x
);
367 f
->Read(&x
, sizeof(x
)); x
= wxINT32_SWAP_ON_BE(x
);
368 m_Index
[i
].m_Page
= new wxChar
[x
];
369 READ_STRING(f
, m_Index
[i
].m_Page
, x
);
370 m_Index
[i
].m_Book
= book
;
376 bool wxHtmlHelpData::SaveCachedBook(wxHtmlBookRecord
*book
, wxOutputStream
*f
)
381 /* save header - version info : */
383 x
= wxINT32_SWAP_ON_BE(CURRENT_CACHED_BOOK_VERSION
);
384 f
->Write(&x
, sizeof(x
));
386 /* save contents : */
389 for (i
= 0; i
< m_ContentsCnt
; i
++) if (m_Contents
[i
].m_Book
== book
&& m_Contents
[i
].m_Level
> 0) x
++;
390 x
= wxINT32_SWAP_ON_BE(x
);
391 f
->Write(&x
, sizeof(x
));
392 for (i
= 0; i
< m_ContentsCnt
; i
++)
394 if (m_Contents
[i
].m_Book
!= book
|| m_Contents
[i
].m_Level
== 0) continue;
395 x
= wxINT32_SWAP_ON_BE(m_Contents
[i
].m_Level
);
396 f
->Write(&x
, sizeof(x
));
397 x
= wxINT32_SWAP_ON_BE(m_Contents
[i
].m_ID
);
398 f
->Write(&x
, sizeof(x
));
399 x
= wxINT32_SWAP_ON_BE(wxStrlen(m_Contents
[i
].m_Name
) + 1);
400 f
->Write(&x
, sizeof(x
));
401 WRITE_STRING(f
, m_Contents
[i
].m_Name
, x
);
402 x
= wxINT32_SWAP_ON_BE(wxStrlen(m_Contents
[i
].m_Page
) + 1);
403 f
->Write(&x
, sizeof(x
));
404 WRITE_STRING(f
, m_Contents
[i
].m_Page
, x
);
410 for (i
= 0; i
< m_IndexCnt
; i
++) if (m_Index
[i
].m_Book
== book
&& m_Index
[i
].m_Level
> 0) x
++;
411 x
= wxINT32_SWAP_ON_BE(x
);
412 f
->Write(&x
, sizeof(x
));
413 for (i
= 0; i
< m_IndexCnt
; i
++)
415 if (m_Index
[i
].m_Book
!= book
|| m_Index
[i
].m_Level
== 0) continue;
416 x
= wxINT32_SWAP_ON_BE(wxStrlen(m_Index
[i
].m_Name
) + 1);
417 f
->Write(&x
, sizeof(x
));
418 WRITE_STRING(f
, m_Index
[i
].m_Name
, x
);
419 x
= wxINT32_SWAP_ON_BE(wxStrlen(m_Index
[i
].m_Page
) + 1);
420 f
->Write(&x
, sizeof(x
));
421 WRITE_STRING(f
, m_Index
[i
].m_Page
, x
);
427 void wxHtmlHelpData::SetTempDir(const wxString
& path
)
429 if (path
== wxEmptyString
) m_TempPath
= path
;
432 if (wxIsAbsolutePath(path
)) m_TempPath
= path
;
433 else m_TempPath
= wxGetCwd() + _T("/") + path
;
435 if (m_TempPath
[m_TempPath
.Length() - 1] != _T('/'))
436 m_TempPath
<< _T('/');
442 static wxString
SafeFileName(const wxString
& s
)
445 res
.Replace(wxT("#"), wxT("_"));
446 res
.Replace(wxT(":"), wxT("_"));
447 res
.Replace(wxT("\\"), wxT("_"));
448 res
.Replace(wxT("/"), wxT("_"));
452 bool wxHtmlHelpData::AddBookParam(const wxFSFile
& bookfile
,
453 wxFontEncoding encoding
,
454 const wxString
& title
, const wxString
& contfile
,
455 const wxString
& indexfile
, const wxString
& deftopic
,
456 const wxString
& path
)
460 wxHtmlBookRecord
*bookr
;
462 int IndexOld
= m_IndexCnt
,
463 ContentsOld
= m_ContentsCnt
;
465 if (! path
.IsEmpty())
466 fsys
.ChangePathTo(path
, TRUE
);
468 bookr
= new wxHtmlBookRecord(fsys
.GetPath(), title
, deftopic
);
470 if (m_ContentsCnt
% wxHTML_REALLOC_STEP
== 0)
471 m_Contents
= (wxHtmlContentsItem
*) realloc(m_Contents
, (m_ContentsCnt
+ wxHTML_REALLOC_STEP
) * sizeof(wxHtmlContentsItem
));
472 m_Contents
[m_ContentsCnt
].m_Level
= 0;
473 m_Contents
[m_ContentsCnt
].m_ID
= 0;
474 m_Contents
[m_ContentsCnt
].m_Page
= new wxChar
[deftopic
.Length() + 1];
475 wxStrcpy(m_Contents
[m_ContentsCnt
].m_Page
, deftopic
.c_str());
476 m_Contents
[m_ContentsCnt
].m_Name
= new wxChar
[title
.Length() + 1];
477 wxStrcpy(m_Contents
[m_ContentsCnt
].m_Name
, title
.c_str());
478 m_Contents
[m_ContentsCnt
].m_Book
= bookr
;
480 // store the contents index for later
481 int cont_start
= m_ContentsCnt
++;
483 // Try to find cached binary versions:
484 // 1. save file as book, but with .hhp.cached extension
485 // 2. same as 1. but in temp path
486 // 3. otherwise or if cache load failed, load it from MS.
488 fi
= fsys
.OpenFile(bookfile
.GetLocation() + wxT(".cached"));
491 fi
->GetModificationTime() < bookfile
.GetModificationTime() ||
492 !LoadCachedBook(bookr
, fi
->GetStream()))
494 if (fi
!= NULL
) delete fi
;
495 fi
= fsys
.OpenFile(m_TempPath
+ wxFileNameFromPath(bookfile
.GetLocation()) + wxT(".cached"));
496 if (m_TempPath
== wxEmptyString
|| fi
== NULL
||
497 fi
->GetModificationTime() < bookfile
.GetModificationTime() ||
498 !LoadCachedBook(bookr
, fi
->GetStream()))
500 LoadMSProject(bookr
, fsys
, indexfile
, contfile
);
501 if (m_TempPath
!= wxEmptyString
)
503 wxFileOutputStream
*outs
= new wxFileOutputStream(m_TempPath
+
504 SafeFileName(wxFileNameFromPath(bookfile
.GetLocation())) + wxT(".cached"));
505 SaveCachedBook(bookr
, outs
);
511 if (fi
!= NULL
) delete fi
;
513 // Now store the contents range
514 bookr
->SetContentsRange(cont_start
, m_ContentsCnt
);
516 // Convert encoding, if neccessary:
517 if (encoding
!= wxFONTENCODING_SYSTEM
)
519 wxFontEncodingArray a
= wxEncodingConverter::GetPlatformEquivalents(encoding
);
520 if (a
.GetCount() != 0 && a
[0] != encoding
)
523 wxEncodingConverter conv
;
524 conv
.Init(encoding
, a
[0]);
526 for (i
= IndexOld
; i
< m_IndexCnt
; i
++)
527 conv
.Convert(m_Index
[i
].m_Name
);
528 for (i
= ContentsOld
; i
< m_ContentsCnt
; i
++)
529 conv
.Convert(m_Contents
[i
].m_Name
);
533 m_BookRecords
.Add(bookr
);
535 qsort(m_Index
, m_IndexCnt
, sizeof(wxHtmlContentsItem
), wxHtmlHelpIndexCompareFunc
);
541 bool wxHtmlHelpData::AddBook(const wxString
& book
)
543 if (book
.Right(4).Lower() == wxT(".zip") ||
544 book
.Right(4).Lower() == wxT(".htb") /*html book*/)
551 s
= fsys
.FindFirst(book
+ wxT("#zip:") + wxT("*.hhp"), wxFILE
);
554 if (AddBook(s
)) rt
= TRUE
;
569 wxString title
= _("noname"),
571 start
= wxEmptyString
,
572 contents
= wxEmptyString
,
573 index
= wxEmptyString
,
574 charset
= wxEmptyString
;
576 #if defined(__WXMAC__) && !defined(__DARWIN__)
577 if (wxIsAbsolutePath(book
)) bookFull
= book
;
578 else bookFull
= wxGetCwd() + book
; // no slash or dot
579 wxFileName
fn( bookFull
);
580 bookFull
= fn
.GetFullPath( wxPATH_UNIX
);
582 if (wxIsAbsolutePath(book
)) bookFull
= book
;
583 else bookFull
= wxGetCwd() + "/" + book
;
586 fi
= fsys
.OpenFile(bookFull
);
589 wxLogError(_("Cannot open HTML help book: %s"), bookFull
.c_str());
592 fsys
.ChangePathTo(bookFull
);
596 char *buff
, *lineptr
;
600 buff
= new char[sz
+ 1];
607 lineptr
= ReadLine(lineptr
, linebuf
, 300);
609 for (char *ch
= linebuf
; *ch
!= '\0' && *ch
!= '='; ch
++)
612 if (strstr(linebuf
, "title=") == linebuf
)
613 title
= linebuf
+ strlen("title=");
614 if (strstr(linebuf
, "default topic=") == linebuf
)
615 start
= linebuf
+ strlen("default topic=");
616 if (strstr(linebuf
, "index file=") == linebuf
)
617 index
= linebuf
+ strlen("index file=");
618 if (strstr(linebuf
, "contents file=") == linebuf
)
619 contents
= linebuf
+ strlen("contents file=");
620 if (strstr(linebuf
, "charset=") == linebuf
)
621 charset
= linebuf
+ strlen("charset=");
622 } while (lineptr
!= NULL
);
626 if (charset
== wxEmptyString
) enc
= wxFONTENCODING_SYSTEM
;
627 else enc
= wxTheFontMapper
->CharsetToEncoding(charset
);
628 bool rtval
= AddBookParam(*fi
, enc
,
629 title
, contents
, index
, start
, fsys
.GetPath());
635 wxString
wxHtmlHelpData::FindPageByName(const wxString
& x
)
641 wxString
url(wxEmptyString
);
643 /* 1. try to open given file: */
645 cnt
= m_BookRecords
.GetCount();
646 for (i
= 0; i
< cnt
; i
++)
648 f
= fsys
.OpenFile(m_BookRecords
[i
].GetFullPath(x
));
651 url
= m_BookRecords
[i
].GetFullPath(x
);
658 /* 2. try to find a book: */
660 for (i
= 0; i
< cnt
; i
++)
662 if (m_BookRecords
[i
].GetTitle() == x
)
664 url
= m_BookRecords
[i
].GetFullPath(m_BookRecords
[i
].GetStart());
669 /* 3. try to find in contents: */
672 for (i
= 0; i
< cnt
; i
++)
674 if (wxStrcmp(m_Contents
[i
].m_Name
, x
) == 0)
676 url
= m_Contents
[i
].GetFullPath();
682 /* 4. try to find in index: */
685 for (i
= 0; i
< cnt
; i
++)
687 if (wxStrcmp(m_Index
[i
].m_Name
, x
) == 0)
689 url
= m_Index
[i
].GetFullPath();
697 wxString
wxHtmlHelpData::FindPageById(int id
)
700 wxString
url(wxEmptyString
);
702 for (i
= 0; i
< m_ContentsCnt
; i
++)
704 if (m_Contents
[i
].m_ID
== id
)
706 url
= m_Contents
[i
].GetFullPath();
714 //----------------------------------------------------------------------------------
715 // wxHtmlSearchStatus functions
716 //----------------------------------------------------------------------------------
718 wxHtmlSearchStatus::wxHtmlSearchStatus(wxHtmlHelpData
* data
, const wxString
& keyword
,
719 bool case_sensitive
, bool whole_words_only
,
720 const wxString
& book
)
724 wxHtmlBookRecord
* bookr
= NULL
;
725 if (book
!= wxEmptyString
)
727 // we have to search in a specific book. Find it first
728 int i
, cnt
= data
->m_BookRecords
.GetCount();
729 for (i
= 0; i
< cnt
; i
++)
730 if (data
->m_BookRecords
[i
].GetTitle() == book
)
732 bookr
= &(data
->m_BookRecords
[i
]);
733 m_CurIndex
= bookr
->GetContentsStart();
734 m_MaxIndex
= bookr
->GetContentsEnd();
737 // check; we won't crash if the book doesn't exist, but it's Bad Anyway.
742 // no book specified; search all books
744 m_MaxIndex
= m_Data
->m_ContentsCnt
;
746 m_Engine
.LookFor(keyword
, case_sensitive
, whole_words_only
);
747 m_Active
= (m_CurIndex
< m_MaxIndex
);
751 bool wxHtmlSearchStatus::Search()
754 int i
= m_CurIndex
; // shortcut
760 // sanity check. Illegal use, but we'll try to prevent a crash anyway
765 m_Name
= wxEmptyString
;
766 m_ContentsItem
= NULL
;
767 thepage
= m_Data
->m_Contents
[i
].m_Page
;
769 m_Active
= (++m_CurIndex
< m_MaxIndex
);
770 // check if it is same page with different anchor:
771 if (m_LastPage
!= NULL
)
774 for (p1
= thepage
, p2
= m_LastPage
;
775 *p1
!= 0 && *p1
!= _T('#') && *p1
== *p2
; p1
++, p2
++) {}
777 m_LastPage
= thepage
;
779 if (*p1
== 0 || *p1
== _T('#'))
782 else m_LastPage
= thepage
;
785 file
= fsys
.OpenFile(m_Data
->m_Contents
[i
].m_Book
->GetFullPath(thepage
));
788 if (m_Engine
.Scan(file
->GetStream()))
790 m_Name
= m_Data
->m_Contents
[i
].m_Name
;
791 m_ContentsItem
= m_Data
->m_Contents
+ i
;
806 //--------------------------------------------------------------------------------
808 //--------------------------------------------------------------------------------
810 void wxSearchEngine::LookFor(const wxString
& keyword
, bool case_sensitive
, bool whole_words_only
)
812 m_CaseSensitive
= case_sensitive
;
813 m_WholeWords
= whole_words_only
;
814 if (m_Keyword
) delete[] m_Keyword
;
815 m_Keyword
= new wxChar
[keyword
.Length() + 1];
816 wxStrcpy(m_Keyword
, keyword
.c_str());
818 if (!m_CaseSensitive
)
820 for (int i
= wxStrlen(m_Keyword
) - 1; i
>= 0; i
--)
822 if ((m_Keyword
[i
] >= wxT('A')) && (m_Keyword
[i
] <= wxT('Z')))
823 m_Keyword
[i
] += wxT('a') - wxT('A');
830 #define WHITESPACE(c) (c == ' ' || c == '\n' || c == '\r' || c == '\t')
832 bool wxSearchEngine::Scan(wxInputStream
*stream
)
834 wxASSERT_MSG(m_Keyword
!= NULL
, wxT("wxSearchEngine::LookFor must be called before scanning!"));
837 int lng
= stream
->GetSize();
838 int wrd
= wxStrlen(m_Keyword
);
840 char *buf
= new char[lng
+ 1];
841 stream
->Read(buf
, lng
);
844 if (!m_CaseSensitive
)
845 for (i
= 0; i
< lng
; i
++)
846 if ((buf
[i
] >= 'A') && (buf
[i
] <= 'Z')) buf
[i
] += 'a' - 'A';
850 for (i
= 0; i
< lng
- wrd
; i
++)
852 if (WHITESPACE(buf
[i
])) continue;
854 while ((j
< wrd
) && (buf
[i
+ j
] == m_Keyword
[j
])) j
++;
855 if (j
== wrd
&& WHITESPACE(buf
[i
+ j
])) { found
= TRUE
; break; }
861 for (i
= 0; i
< lng
- wrd
; i
++)
864 while ((j
< wrd
) && (buf
[i
+ j
] == m_Keyword
[j
])) j
++;
865 if (j
== wrd
) { found
= TRUE
; break; }