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
)
53 char *writeptr
= buf
, *readptr
= line
;
55 while (*readptr
!= 0 && *readptr
!= '\r' && *readptr
!= '\n') *(writeptr
++) = *(readptr
++);
57 while (*readptr
== '\r' || *readptr
== '\n') readptr
++;
58 if (*readptr
== 0) return NULL
;
64 extern "C" int LINKAGEMODE
65 wxHtmlHelpIndexCompareFunc(const void *a
, const void *b
)
67 return wxStricmp(((wxHtmlContentsItem
*)a
)->m_Name
, ((wxHtmlContentsItem
*)b
)->m_Name
);
71 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
75 class HP_Parser
: public wxHtmlParser
78 wxObject
* GetProduct() { return NULL
; }
80 virtual void AddText(const wxChar
* WXUNUSED(txt
)) {}
84 //-----------------------------------------------------------------------------
86 //-----------------------------------------------------------------------------
88 class HP_TagHandler
: public wxHtmlTagHandler
91 wxString m_Name
, m_Page
;
95 wxHtmlContentsItem
*m_Items
;
97 wxHtmlBookRecord
*m_Book
;
100 HP_TagHandler(wxHtmlBookRecord
*b
) : wxHtmlTagHandler()
101 { m_Book
= b
; m_Items
= NULL
; m_ItemsCnt
= 0; m_Name
= m_Page
= wxEmptyString
;
102 m_Level
= 0; m_ID
= -1; }
103 wxString
GetSupportedTags() { return wxT("UL,OBJECT,PARAM"); }
104 bool HandleTag(const wxHtmlTag
& tag
);
105 void WriteOut(wxHtmlContentsItem
*& array
, int& size
);
106 void ReadIn(wxHtmlContentsItem
* array
, int size
);
110 bool HP_TagHandler::HandleTag(const wxHtmlTag
& tag
)
112 if (tag
.GetName() == wxT("UL"))
119 else if (tag
.GetName() == wxT("OBJECT"))
121 m_Name
= m_Page
= wxEmptyString
;
125 if (!m_Page
.IsEmpty())
126 /* Valid HHW's file may contain only two object tags:
128 <OBJECT type="text/site properties">
129 <param name="ImageType" value="Folder">
134 <OBJECT type="text/sitemap">
135 <param name="Name" value="main page">
136 <param name="Local" value="another.htm">
139 We're interested in the latter. !m_Page.IsEmpty() is valid
140 condition because text/site properties does not contain Local param
143 if (tag
.GetParam(wxT("TYPE")) == wxT("text/sitemap"))
145 if (m_ItemsCnt
% wxHTML_REALLOC_STEP
== 0)
146 m_Items
= (wxHtmlContentsItem
*) realloc(m_Items
,
147 (m_ItemsCnt
+ wxHTML_REALLOC_STEP
) *
148 sizeof(wxHtmlContentsItem
));
150 m_Items
[m_ItemsCnt
].m_Level
= m_Level
;
151 m_Items
[m_ItemsCnt
].m_ID
= m_ID
;
152 m_Items
[m_ItemsCnt
].m_Page
= new wxChar
[m_Page
.Length() + 1];
153 wxStrcpy(m_Items
[m_ItemsCnt
].m_Page
, m_Page
.c_str());
154 m_Items
[m_ItemsCnt
].m_Name
= new wxChar
[m_Name
.Length() + 1];
155 wxStrcpy(m_Items
[m_ItemsCnt
].m_Name
, m_Name
.c_str());
156 m_Items
[m_ItemsCnt
].m_Book
= m_Book
;
164 if (m_Name
== wxEmptyString
&& tag
.GetParam(wxT("NAME")) == wxT("Name"))
165 m_Name
= tag
.GetParam(wxT("VALUE"));
166 if (tag
.GetParam(wxT("NAME")) == wxT("Local"))
167 m_Page
= tag
.GetParam(wxT("VALUE"));
168 if (tag
.GetParam(wxT("NAME")) == wxT("ID"))
169 tag
.GetParamAsInt(wxT("VALUE"), &m_ID
);
176 void HP_TagHandler::WriteOut(wxHtmlContentsItem
*& array
, int& size
)
184 void HP_TagHandler::ReadIn(wxHtmlContentsItem
* array
, int size
)
193 //-----------------------------------------------------------------------------
195 //-----------------------------------------------------------------------------
197 wxString
wxHtmlBookRecord::GetFullPath(const wxString
&page
) const
199 if (wxIsAbsolutePath(page
))
202 return m_BasePath
+ page
;
207 IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpData
, wxObject
)
209 wxHtmlHelpData::wxHtmlHelpData()
211 m_TempPath
= wxEmptyString
;
219 wxHtmlHelpData::~wxHtmlHelpData()
223 m_BookRecords
.Empty();
226 for (i
= 0; i
< m_ContentsCnt
; i
++)
228 delete[] m_Contents
[i
].m_Page
;
229 delete[] m_Contents
[i
].m_Name
;
235 for (i
= 0; i
< m_IndexCnt
; i
++)
237 delete[] m_Index
[i
].m_Page
;
238 delete[] m_Index
[i
].m_Name
;
244 bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord
*book
, wxFileSystem
& fsys
, const wxString
& indexfile
, const wxString
& contentsfile
)
252 HP_TagHandler
*handler
= new HP_TagHandler(book
);
253 parser
.AddTagHandler(handler
);
255 f
= ( contentsfile
.IsEmpty() ? (wxFSFile
*) NULL
: fsys
.OpenFile(contentsfile
) );
258 sz
= f
->GetStream()->GetSize();
259 buf
= new char[sz
+ 1];
261 f
->GetStream()->Read(buf
, sz
);
263 handler
->ReadIn(m_Contents
, m_ContentsCnt
);
265 handler
->WriteOut(m_Contents
, m_ContentsCnt
);
269 wxLogError(_("Cannot open contents file: %s"), contentsfile
.c_str());
271 f
= ( indexfile
.IsEmpty() ? (wxFSFile
*) NULL
: fsys
.OpenFile(indexfile
) );
274 sz
= f
->GetStream()->GetSize();
275 buf
= new char[sz
+ 1];
277 f
->GetStream()->Read(buf
, sz
);
279 handler
->ReadIn(m_Index
, m_IndexCnt
);
281 handler
->WriteOut(m_Index
, m_IndexCnt
);
284 else if (!indexfile
.IsEmpty())
285 wxLogError(_("Cannot open index file: %s"), indexfile
.c_str());
294 #define READ_STRING(f, s, lng) { char tmpc; for (int i = 0; i < lng; i++) { f->Read(&tmpc, 1); s[i] = (wxChar)tmpc;} }
295 #define WRITE_STRING(f, s, lng) { char tmpc; for (int i = 0; i < lng; i++) { tmpc = (char)s[i]; f->Write(&tmpc, 1);} }
299 #define READ_STRING(f, s, lng) f->Read(s, lng * sizeof(char));
300 #define WRITE_STRING(f, s, lng) f->Write(s, lng * sizeof(char));
305 #define CURRENT_CACHED_BOOK_VERSION 1
307 bool wxHtmlHelpData::LoadCachedBook(wxHtmlBookRecord
*book
, wxInputStream
*f
)
313 /* load header - version info : */
315 f
->Read(&x
, sizeof(x
));
316 version
= wxINT32_SWAP_ON_BE(x
);
318 if (version
!= CURRENT_CACHED_BOOK_VERSION
)
320 wxLogError(_("Incorrect version of HTML help book"));
322 // NOTE: when adding new version, please ensure backward compatibility!
325 /* load contents : */
327 f
->Read(&x
, sizeof(x
));
329 m_ContentsCnt
+= wxINT32_SWAP_ON_BE(x
);
330 m_Contents
= (wxHtmlContentsItem
*) realloc(m_Contents
,
331 (m_ContentsCnt
/ wxHTML_REALLOC_STEP
+ 1) *
332 wxHTML_REALLOC_STEP
* sizeof(wxHtmlContentsItem
));
333 for (i
= st
; i
< m_ContentsCnt
; i
++)
335 f
->Read(&x
, sizeof(x
));
336 m_Contents
[i
].m_Level
= wxINT32_SWAP_ON_BE(x
);
337 f
->Read(&x
, sizeof(x
));
338 m_Contents
[i
].m_ID
= wxINT32_SWAP_ON_BE(x
);
339 f
->Read(&x
, sizeof(x
)); x
= wxINT32_SWAP_ON_BE(x
);
340 m_Contents
[i
].m_Name
= new wxChar
[x
];
341 READ_STRING(f
, m_Contents
[i
].m_Name
, x
);
342 f
->Read(&x
, sizeof(x
)); x
= wxINT32_SWAP_ON_BE(x
);
343 m_Contents
[i
].m_Page
= new wxChar
[x
];
344 READ_STRING(f
, m_Contents
[i
].m_Page
, x
);
345 m_Contents
[i
].m_Book
= book
;
350 f
->Read(&x
, sizeof(x
));
352 m_IndexCnt
+= wxINT32_SWAP_ON_BE(x
);
353 m_Index
= (wxHtmlContentsItem
*) realloc(m_Index
, (m_IndexCnt
/ wxHTML_REALLOC_STEP
+ 1) *
354 wxHTML_REALLOC_STEP
* sizeof(wxHtmlContentsItem
));
355 for (i
= st
; i
< m_IndexCnt
; i
++)
357 f
->Read(&x
, sizeof(x
)); x
= wxINT32_SWAP_ON_BE(x
);
358 m_Index
[i
].m_Name
= new wxChar
[x
];
359 READ_STRING(f
, m_Index
[i
].m_Name
, x
);
360 f
->Read(&x
, sizeof(x
)); x
= wxINT32_SWAP_ON_BE(x
);
361 m_Index
[i
].m_Page
= new wxChar
[x
];
362 READ_STRING(f
, m_Index
[i
].m_Page
, x
);
363 m_Index
[i
].m_Book
= book
;
369 bool wxHtmlHelpData::SaveCachedBook(wxHtmlBookRecord
*book
, wxOutputStream
*f
)
374 /* save header - version info : */
376 x
= wxINT32_SWAP_ON_BE(CURRENT_CACHED_BOOK_VERSION
);
377 f
->Write(&x
, sizeof(x
));
379 /* save contents : */
382 for (i
= 0; i
< m_ContentsCnt
; i
++) if (m_Contents
[i
].m_Book
== book
&& m_Contents
[i
].m_Level
> 0) x
++;
383 x
= wxINT32_SWAP_ON_BE(x
);
384 f
->Write(&x
, sizeof(x
));
385 for (i
= 0; i
< m_ContentsCnt
; i
++)
387 if (m_Contents
[i
].m_Book
!= book
|| m_Contents
[i
].m_Level
== 0) continue;
388 x
= wxINT32_SWAP_ON_BE(m_Contents
[i
].m_Level
);
389 f
->Write(&x
, sizeof(x
));
390 x
= wxINT32_SWAP_ON_BE(m_Contents
[i
].m_ID
);
391 f
->Write(&x
, sizeof(x
));
392 x
= wxINT32_SWAP_ON_BE(wxStrlen(m_Contents
[i
].m_Name
) + 1);
393 f
->Write(&x
, sizeof(x
));
394 WRITE_STRING(f
, m_Contents
[i
].m_Name
, x
);
395 x
= wxINT32_SWAP_ON_BE(wxStrlen(m_Contents
[i
].m_Page
) + 1);
396 f
->Write(&x
, sizeof(x
));
397 WRITE_STRING(f
, m_Contents
[i
].m_Page
, x
);
403 for (i
= 0; i
< m_IndexCnt
; i
++) if (m_Index
[i
].m_Book
== book
&& m_Index
[i
].m_Level
> 0) x
++;
404 x
= wxINT32_SWAP_ON_BE(x
);
405 f
->Write(&x
, sizeof(x
));
406 for (i
= 0; i
< m_IndexCnt
; i
++)
408 if (m_Index
[i
].m_Book
!= book
|| m_Index
[i
].m_Level
== 0) continue;
409 x
= wxINT32_SWAP_ON_BE(wxStrlen(m_Index
[i
].m_Name
) + 1);
410 f
->Write(&x
, sizeof(x
));
411 WRITE_STRING(f
, m_Index
[i
].m_Name
, x
);
412 x
= wxINT32_SWAP_ON_BE(wxStrlen(m_Index
[i
].m_Page
) + 1);
413 f
->Write(&x
, sizeof(x
));
414 WRITE_STRING(f
, m_Index
[i
].m_Page
, x
);
420 void wxHtmlHelpData::SetTempDir(const wxString
& path
)
422 if (path
== wxEmptyString
) m_TempPath
= path
;
425 if (wxIsAbsolutePath(path
)) m_TempPath
= path
;
426 else m_TempPath
= wxGetCwd() + _T("/") + path
;
428 if (m_TempPath
[m_TempPath
.Length() - 1] != _T('/'))
429 m_TempPath
<< _T('/');
435 static wxString
SafeFileName(const wxString
& s
)
438 res
.Replace(wxT("#"), wxT("_"));
439 res
.Replace(wxT(":"), wxT("_"));
440 res
.Replace(wxT("\\"), wxT("_"));
441 res
.Replace(wxT("/"), wxT("_"));
445 bool wxHtmlHelpData::AddBookParam(const wxFSFile
& bookfile
,
446 wxFontEncoding encoding
,
447 const wxString
& title
, const wxString
& contfile
,
448 const wxString
& indexfile
, const wxString
& deftopic
,
449 const wxString
& path
)
453 wxHtmlBookRecord
*bookr
;
455 int IndexOld
= m_IndexCnt
,
456 ContentsOld
= m_ContentsCnt
;
458 if (! path
.IsEmpty())
459 fsys
.ChangePathTo(path
, TRUE
);
461 bookr
= new wxHtmlBookRecord(fsys
.GetPath(), title
, deftopic
);
463 if (m_ContentsCnt
% wxHTML_REALLOC_STEP
== 0)
464 m_Contents
= (wxHtmlContentsItem
*) realloc(m_Contents
, (m_ContentsCnt
+ wxHTML_REALLOC_STEP
) * sizeof(wxHtmlContentsItem
));
465 m_Contents
[m_ContentsCnt
].m_Level
= 0;
466 m_Contents
[m_ContentsCnt
].m_ID
= 0;
467 m_Contents
[m_ContentsCnt
].m_Page
= new wxChar
[deftopic
.Length() + 1];
468 wxStrcpy(m_Contents
[m_ContentsCnt
].m_Page
, deftopic
.c_str());
469 m_Contents
[m_ContentsCnt
].m_Name
= new wxChar
[title
.Length() + 1];
470 wxStrcpy(m_Contents
[m_ContentsCnt
].m_Name
, title
.c_str());
471 m_Contents
[m_ContentsCnt
].m_Book
= bookr
;
473 // store the contents index for later
474 int cont_start
= m_ContentsCnt
++;
476 // Try to find cached binary versions:
477 // 1. save file as book, but with .hhp.cached extension
478 // 2. same as 1. but in temp path
479 // 3. otherwise or if cache load failed, load it from MS.
481 fi
= fsys
.OpenFile(bookfile
.GetLocation() + wxT(".cached"));
484 fi
->GetModificationTime() < bookfile
.GetModificationTime() ||
485 !LoadCachedBook(bookr
, fi
->GetStream()))
487 if (fi
!= NULL
) delete fi
;
488 fi
= fsys
.OpenFile(m_TempPath
+ wxFileNameFromPath(bookfile
.GetLocation()) + wxT(".cached"));
489 if (m_TempPath
== wxEmptyString
|| fi
== NULL
||
490 fi
->GetModificationTime() < bookfile
.GetModificationTime() ||
491 !LoadCachedBook(bookr
, fi
->GetStream()))
493 LoadMSProject(bookr
, fsys
, indexfile
, contfile
);
494 if (m_TempPath
!= wxEmptyString
)
496 wxFileOutputStream
*outs
= new wxFileOutputStream(m_TempPath
+
497 SafeFileName(wxFileNameFromPath(bookfile
.GetLocation())) + wxT(".cached"));
498 SaveCachedBook(bookr
, outs
);
504 if (fi
!= NULL
) delete fi
;
506 // Now store the contents range
507 bookr
->SetContentsRange(cont_start
, m_ContentsCnt
);
509 // Convert encoding, if neccessary:
510 if (encoding
!= wxFONTENCODING_SYSTEM
)
512 wxFontEncodingArray a
= wxEncodingConverter::GetPlatformEquivalents(encoding
);
513 if (a
.GetCount() != 0 && a
[0] != encoding
)
516 wxEncodingConverter conv
;
517 conv
.Init(encoding
, a
[0]);
519 for (i
= IndexOld
; i
< m_IndexCnt
; i
++)
520 conv
.Convert(m_Index
[i
].m_Name
);
521 for (i
= ContentsOld
; i
< m_ContentsCnt
; i
++)
522 conv
.Convert(m_Contents
[i
].m_Name
);
526 m_BookRecords
.Add(bookr
);
528 qsort(m_Index
, m_IndexCnt
, sizeof(wxHtmlContentsItem
), wxHtmlHelpIndexCompareFunc
);
534 bool wxHtmlHelpData::AddBook(const wxString
& book
)
536 if (book
.Right(4).Lower() == wxT(".zip") ||
537 book
.Right(4).Lower() == wxT(".htb") /*html book*/)
544 s
= fsys
.FindFirst(book
+ wxT("#zip:") + wxT("*.hhp"), wxFILE
);
547 if (AddBook(s
)) rt
= TRUE
;
563 char *buff
, *lineptr
;
566 wxString title
= _("noname"),
568 start
= wxEmptyString
,
569 contents
= wxEmptyString
,
570 index
= wxEmptyString
,
571 charset
= wxEmptyString
;
573 #if defined(__WXMAC__) && !defined(__DARWIN__)
574 if (wxIsAbsolutePath(book
)) bookFull
= book
;
575 else bookFull
= wxGetCwd() + book
; // no slash or dot
576 wxFileName
fn( bookFull
);
577 bookFull
= fn
.GetFullPath( wxPATH_UNIX
);
579 if (wxIsAbsolutePath(book
)) bookFull
= book
;
580 else bookFull
= wxGetCwd() + "/" + book
;
583 fi
= fsys
.OpenFile(bookFull
);
586 wxLogError(_("Cannot open HTML help book: %s"), bookFull
.c_str());
589 fsys
.ChangePathTo(bookFull
);
592 buff
= new char[sz
+ 1];
598 lineptr
= ReadLine(lineptr
, linebuf
);
600 if (strstr(linebuf
, "Title=") == linebuf
)
601 title
= linebuf
+ strlen("Title=");
602 if (strstr(linebuf
, "Default topic=") == linebuf
)
603 start
= linebuf
+ strlen("Default topic=");
604 if (strstr(linebuf
, "Index file=") == linebuf
)
605 index
= linebuf
+ strlen("Index file=");
606 if (strstr(linebuf
, "Contents file=") == linebuf
)
607 contents
= linebuf
+ strlen("Contents file=");
608 if (strstr(linebuf
, "Charset=") == linebuf
)
609 charset
= linebuf
+ strlen("Charset=");
610 } while (lineptr
!= NULL
);
614 if (charset
== wxEmptyString
) enc
= wxFONTENCODING_SYSTEM
;
615 else enc
= wxTheFontMapper
->CharsetToEncoding(charset
);
616 bool rtval
= AddBookParam(*fi
, enc
,
617 title
, contents
, index
, start
, fsys
.GetPath());
623 wxString
wxHtmlHelpData::FindPageByName(const wxString
& x
)
629 wxString
url(wxEmptyString
);
631 /* 1. try to open given file: */
633 cnt
= m_BookRecords
.GetCount();
634 for (i
= 0; i
< cnt
; i
++)
636 f
= fsys
.OpenFile(m_BookRecords
[i
].GetFullPath(x
));
639 url
= m_BookRecords
[i
].GetFullPath(x
);
646 /* 2. try to find a book: */
648 for (i
= 0; i
< cnt
; i
++)
650 if (m_BookRecords
[i
].GetTitle() == x
)
652 url
= m_BookRecords
[i
].GetFullPath(m_BookRecords
[i
].GetStart());
657 /* 3. try to find in contents: */
660 for (i
= 0; i
< cnt
; i
++)
662 if (wxStrcmp(m_Contents
[i
].m_Name
, x
) == 0)
664 url
= m_Contents
[i
].GetFullPath();
670 /* 4. try to find in index: */
673 for (i
= 0; i
< cnt
; i
++)
675 if (wxStrcmp(m_Index
[i
].m_Name
, x
) == 0)
677 url
= m_Index
[i
].GetFullPath();
685 wxString
wxHtmlHelpData::FindPageById(int id
)
688 wxString
url(wxEmptyString
);
690 for (i
= 0; i
< m_ContentsCnt
; i
++)
692 if (m_Contents
[i
].m_ID
== id
)
694 url
= m_Contents
[i
].GetFullPath();
702 //----------------------------------------------------------------------------------
703 // wxHtmlSearchStatus functions
704 //----------------------------------------------------------------------------------
706 wxHtmlSearchStatus::wxHtmlSearchStatus(wxHtmlHelpData
* data
, const wxString
& keyword
,
707 bool case_sensitive
, bool whole_words_only
,
708 const wxString
& book
)
712 wxHtmlBookRecord
* bookr
= NULL
;
713 if (book
!= wxEmptyString
)
715 // we have to search in a specific book. Find it first
716 int i
, cnt
= data
->m_BookRecords
.GetCount();
717 for (i
= 0; i
< cnt
; i
++)
718 if (data
->m_BookRecords
[i
].GetTitle() == book
)
720 bookr
= &(data
->m_BookRecords
[i
]);
721 m_CurIndex
= bookr
->GetContentsStart();
722 m_MaxIndex
= bookr
->GetContentsEnd();
725 // check; we won't crash if the book doesn't exist, but it's Bad Anyway.
730 // no book specified; search all books
732 m_MaxIndex
= m_Data
->m_ContentsCnt
;
734 m_Engine
.LookFor(keyword
, case_sensitive
, whole_words_only
);
735 m_Active
= (m_CurIndex
< m_MaxIndex
);
739 bool wxHtmlSearchStatus::Search()
742 int i
= m_CurIndex
; // shortcut
748 // sanity check. Illegal use, but we'll try to prevent a crash anyway
753 m_Name
= wxEmptyString
;
754 m_ContentsItem
= NULL
;
755 thepage
= m_Data
->m_Contents
[i
].m_Page
;
757 m_Active
= (++m_CurIndex
< m_MaxIndex
);
758 // check if it is same page with different anchor:
759 if (m_LastPage
!= NULL
)
762 for (p1
= thepage
, p2
= m_LastPage
;
763 *p1
!= 0 && *p1
!= _T('#') && *p1
== *p2
; p1
++, p2
++) {}
765 m_LastPage
= thepage
;
767 if (*p1
== 0 || *p1
== _T('#'))
770 else m_LastPage
= thepage
;
773 file
= fsys
.OpenFile(m_Data
->m_Contents
[i
].m_Book
->GetFullPath(thepage
));
776 if (m_Engine
.Scan(file
->GetStream()))
778 m_Name
= m_Data
->m_Contents
[i
].m_Name
;
779 m_ContentsItem
= m_Data
->m_Contents
+ i
;
794 //--------------------------------------------------------------------------------
796 //--------------------------------------------------------------------------------
798 void wxSearchEngine::LookFor(const wxString
& keyword
, bool case_sensitive
, bool whole_words_only
)
800 m_CaseSensitive
= case_sensitive
;
801 m_WholeWords
= whole_words_only
;
802 if (m_Keyword
) delete[] m_Keyword
;
803 m_Keyword
= new wxChar
[keyword
.Length() + 1];
804 wxStrcpy(m_Keyword
, keyword
.c_str());
806 if (!m_CaseSensitive
)
808 for (int i
= wxStrlen(m_Keyword
) - 1; i
>= 0; i
--)
810 if ((m_Keyword
[i
] >= wxT('A')) && (m_Keyword
[i
] <= wxT('Z')))
811 m_Keyword
[i
] += wxT('a') - wxT('A');
818 #define WHITESPACE(c) (c == ' ' || c == '\n' || c == '\r' || c == '\t')
820 bool wxSearchEngine::Scan(wxInputStream
*stream
)
822 wxASSERT_MSG(m_Keyword
!= NULL
, wxT("wxSearchEngine::LookFor must be called before scanning!"));
825 int lng
= stream
->GetSize();
826 int wrd
= wxStrlen(m_Keyword
);
828 char *buf
= new char[lng
+ 1];
829 stream
->Read(buf
, lng
);
832 if (!m_CaseSensitive
)
833 for (i
= 0; i
< lng
; i
++)
834 if ((buf
[i
] >= 'A') && (buf
[i
] <= 'Z')) buf
[i
] += 'a' - 'A';
838 for (i
= 0; i
< lng
- wrd
; i
++)
840 if (WHITESPACE(buf
[i
])) continue;
842 while ((j
< wrd
) && (buf
[i
+ j
] == m_Keyword
[j
])) j
++;
843 if (j
== wrd
&& WHITESPACE(buf
[i
+ j
])) { found
= TRUE
; break; }
849 for (i
= 0; i
< lng
- wrd
; i
++)
852 while ((j
< wrd
) && (buf
[i
+ j
] == m_Keyword
[j
])) j
++;
853 if (j
== wrd
) { found
= TRUE
; break; }