X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/93763ad5ba900aecb9220a36ebe7447313ea3e31..defde6bc668aab38d574451a63fb3dbd99c42c61:/src/html/helpdata.cpp diff --git a/src/html/helpdata.cpp b/src/html/helpdata.cpp index 5acac76914..a21a2ab70b 100644 --- a/src/html/helpdata.cpp +++ b/src/html/helpdata.cpp @@ -31,7 +31,6 @@ #include "wx/busyinfo.h" #include "wx/encconv.h" #include "wx/fontmap.h" -#include "wx/log.h" #include "wx/html/htmlpars.h" #include "wx/html/htmldefs.h" #include "wx/html/htmlfilt.h" @@ -266,17 +265,10 @@ IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpData, wxObject) wxHtmlHelpData::wxHtmlHelpData() { -#if WXWIN_COMPATIBILITY_2_4 - m_cacheContents = NULL; - m_cacheIndex = NULL; -#endif } wxHtmlHelpData::~wxHtmlHelpData() { -#if WXWIN_COMPATIBILITY_2_4 - CleanCompatibilityData(); -#endif } bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord *book, wxFileSystem& fsys, @@ -704,10 +696,6 @@ bool wxHtmlHelpData::AddBook(const wxString& book) title, contents, index, start, fsys.GetPath()); delete fi; -#if WXWIN_COMPATIBILITY_2_4 - CleanCompatibilityData(); -#endif - return rtval; } @@ -718,8 +706,7 @@ wxString wxHtmlHelpData::FindPageByName(const wxString& x) wxFileSystem fsys; wxFSFile *f; - /* 1. try to open given file: */ - + // 1. try to open given file: cnt = m_bookRecords.GetCount(); for (i = 0; i < cnt; i++) { @@ -733,16 +720,14 @@ wxString wxHtmlHelpData::FindPageByName(const wxString& x) } - /* 2. try to find a book: */ - + // 2. try to find a book: for (i = 0; i < cnt; i++) { if (m_bookRecords[i].GetTitle() == x) return m_bookRecords[i].GetFullPath(m_bookRecords[i].GetStart()); } - /* 3. try to find in contents: */ - + // 3. try to find in contents: cnt = m_contents.size(); for (i = 0; i < cnt; i++) { @@ -751,8 +736,7 @@ wxString wxHtmlHelpData::FindPageByName(const wxString& x) } - /* 4. try to find in index: */ - + // 4. try to find in index: cnt = m_index.size(); for (i = 0; i < cnt; i++) { @@ -760,6 +744,13 @@ wxString wxHtmlHelpData::FindPageByName(const wxString& x) return m_index[i].GetFullPath(); } + // 4b. if still not found, try case-insensitive comparison + for (i = 0; i < cnt; i++) + { + if (m_index[i].name.CmpNoCase(x) == 0) + return m_index[i].GetFullPath(); + } + return wxEmptyString; } @@ -777,90 +768,6 @@ wxString wxHtmlHelpData::FindPageById(int id) return wxEmptyString; } -#if WXWIN_COMPATIBILITY_2_4 -wxHtmlContentsItem::wxHtmlContentsItem() - : m_Level(0), m_ID(wxID_ANY), m_Name(NULL), m_Page(NULL), m_Book(NULL), - m_autofree(false) -{ -} - -wxHtmlContentsItem::wxHtmlContentsItem(const wxHtmlHelpDataItem& d) -{ - m_autofree = true; - m_Level = d.level; - m_ID = d.id; - m_Name = wxStrdup(d.name.c_str()); - m_Page = wxStrdup(d.page.c_str()); - m_Book = d.book; -} - -wxHtmlContentsItem& wxHtmlContentsItem::operator=(const wxHtmlContentsItem& d) -{ - if (m_autofree) - { - free(m_Name); - free(m_Page); - } - m_autofree = true; - m_Level = d.m_Level; - m_ID = d.m_ID; - m_Name = d.m_Name ? wxStrdup(d.m_Name) : NULL; - m_Page = d.m_Page ? wxStrdup(d.m_Page) : NULL; - m_Book = d.m_Book; - return *this; -} - -wxHtmlContentsItem::~wxHtmlContentsItem() -{ - if (m_autofree) - { - free(m_Name); - free(m_Page); - } -} - -wxHtmlContentsItem* wxHtmlHelpData::GetContents() -{ - if (!m_cacheContents && !m_contents.empty()) - { - size_t len = m_contents.size(); - m_cacheContents = new wxHtmlContentsItem[len]; - for (size_t i = 0; i < len; i++) - m_cacheContents[i] = m_contents[i]; - } - return m_cacheContents; -} - -int wxHtmlHelpData::GetContentsCnt() -{ - return m_contents.size(); -} - -wxHtmlContentsItem* wxHtmlHelpData::GetIndex() -{ - if (!m_cacheContents && !m_index.empty()) - { - size_t len = m_index.size(); - m_cacheContents = new wxHtmlContentsItem[len]; - for (size_t i = 0; i < len; i++) - m_cacheContents[i] = m_index[i]; - } - return m_cacheContents; -} - -int wxHtmlHelpData::GetIndexCnt() -{ - return m_index.size(); -} - -void wxHtmlHelpData::CleanCompatibilityData() -{ - delete[] m_cacheContents; - m_cacheContents = NULL; - delete[] m_cacheIndex; - m_cacheIndex = NULL; -} -#endif // WXWIN_COMPATIBILITY_2_4 //---------------------------------------------------------------------------------- // wxHtmlSearchStatus functions @@ -898,15 +805,6 @@ wxHtmlSearchStatus::wxHtmlSearchStatus(wxHtmlHelpData* data, const wxString& key m_Active = (m_CurIndex < m_MaxIndex); } -#if WXWIN_COMPATIBILITY_2_4 -wxHtmlContentsItem* wxHtmlSearchStatus::GetContentsItem() -{ - static wxHtmlContentsItem it; - it = wxHtmlContentsItem(*m_CurItem); - return ⁢ -} -#endif - bool wxHtmlSearchStatus::Search() { wxFSFile *file; @@ -982,47 +880,94 @@ static inline bool WHITESPACE(wxChar c) return c == _T(' ') || c == _T('\n') || c == _T('\r') || c == _T('\t'); } +// replace continuous spaces by one single space +static inline wxString CompressSpaces(const wxString & str) +{ + wxString buf; + buf.reserve( str.size() ); + + bool space_counted = false; + for( const wxChar * pstr = str.c_str(); *pstr; ++pstr ) + { + wxChar ch = *pstr; + if( WHITESPACE( ch ) ) + { + if( space_counted ) + { + continue; + } + ch = _T(' '); + space_counted = true; + } + else + { + space_counted = false; + } + buf += ch; + } + + return buf; +} + bool wxHtmlSearchEngine::Scan(const wxFSFile& file) { wxASSERT_MSG(!m_Keyword.empty(), wxT("wxHtmlSearchEngine::LookFor must be called before scanning!")); - int i, j; - int wrd = m_Keyword.length(); - bool found = false; wxHtmlFilterHTML filter; - wxString tmp = filter.ReadFile(file); - int lng = tmp.length(); - const wxChar *buf = tmp.c_str(); + wxString bufStr = filter.ReadFile(file); if (!m_CaseSensitive) - tmp.LowerCase(); - - const wxChar *kwd = m_Keyword.c_str(); + bufStr.LowerCase(); - if (m_WholeWords) - { - for (i = 0; i < lng - wrd; i++) + { // remove html tags + wxString bufStrCopy; + bufStrCopy.reserve( bufStr.size() ); + bool insideTag = false; + for (const wxChar * pBufStr = bufStr.c_str(); *pBufStr; ++pBufStr) { - if (WHITESPACE(buf[i])) continue; - j = 0; - while ((j < wrd) && (buf[i + j] == kwd[j])) j++; - if (j == wrd && WHITESPACE(buf[i + j])) { found = true; break; } + wxChar c = *pBufStr; + if (insideTag) + { + if (c == _T('>')) + { + insideTag = false; + // replace the tag by an empty space + c = _T(' '); + } + else + continue; + } + else if (c == _T('<')) + { + wxChar nextCh = *(pBufStr + 1); + if (nextCh == _T('/') || !WHITESPACE(nextCh)) + { + insideTag = true; + continue; + } + } + bufStrCopy += c; } + bufStr.swap( bufStrCopy ); } - else + wxString keyword = m_Keyword; + + if (m_WholeWords) { - for (i = 0; i < lng - wrd; i++) - { - j = 0; - while ((j < wrd) && (buf[i + j] == kwd[j])) j++; - if (j == wrd) { found = true; break; } - } + // insert ' ' at the beginning and at the end + keyword.insert( 0, _T(" ") ); + keyword.append( _T(" ") ); + bufStr.insert( 0, _T(" ") ); + bufStr.append( _T(" ") ); } - return found; -} - + // remove continuous spaces + keyword = CompressSpaces( keyword ); + bufStr = CompressSpaces( bufStr ); + // finally do the search + return bufStr.find( keyword ) != wxString::npos; +} #endif