X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/3444e4a8f9c29ab501a65d2a0ffcd0c04de52371..22a35096a0a9c3fe4d3d32b8d68a361126ef9677:/src/html/helpdata.cpp diff --git a/src/html/helpdata.cpp b/src/html/helpdata.cpp index 5f9d39ea19..65d3da7f4c 100644 --- a/src/html/helpdata.cpp +++ b/src/html/helpdata.cpp @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: helpdata.cpp +// Name: src/html/helpdata.cpp // Purpose: wxHtmlHelpData // Notes: Based on htmlhelp.cpp, implementing a monolithic // HTML Help controller class, by Vaclav Slavik @@ -9,19 +9,13 @@ // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) -#pragma implementation "helpdata.h" -#endif - // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" #ifdef __BORLANDC__ -#pragma hdrstop + #pragma hdrstop #endif -#include "wx/defs.h" - #if wxUSE_HTML && wxUSE_STREAMS #ifndef WXPRECOMP @@ -37,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" @@ -78,6 +71,11 @@ wxHtmlHelpIndexCompareFunc(wxHtmlHelpDataItem **a, wxHtmlHelpDataItem **b) wxHtmlHelpDataItem *ia = *a; wxHtmlHelpDataItem *ib = *b; + if (ia == NULL) + return -1; + if (ib == NULL) + return 1; + if (ia->parent == ib->parent) { return ia->name.CmpNoCase(ib->name); @@ -293,7 +291,7 @@ bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord *book, wxFileSystem& fsys, HP_TagHandler *handler = new HP_TagHandler(book); parser.AddTagHandler(handler); - f = ( contentsfile.IsEmpty() ? (wxFSFile*) NULL : fsys.OpenFile(contentsfile) ); + f = ( contentsfile.empty() ? (wxFSFile*) NULL : fsys.OpenFile(contentsfile) ); if (f) { buf.clear(); @@ -307,7 +305,7 @@ bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord *book, wxFileSystem& fsys, wxLogError(_("Cannot open contents file: %s"), contentsfile.c_str()); } - f = ( indexfile.IsEmpty() ? (wxFSFile*) NULL : fsys.OpenFile(indexfile) ); + f = ( indexfile.empty() ? (wxFSFile*) NULL : fsys.OpenFile(indexfile) ); if (f) { buf.clear(); @@ -316,7 +314,7 @@ bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord *book, wxFileSystem& fsys, handler->Reset(m_index); parser.Parse(buf); } - else if (!indexfile.IsEmpty()) + else if (!indexfile.empty()) { wxLogError(_("Cannot open index file: %s"), indexfile.c_str()); } @@ -461,17 +459,17 @@ bool wxHtmlHelpData::SaveCachedBook(wxHtmlBookRecord *book, wxOutputStream *f) } else { - int cnt = 0; + int cnt2 = 0; wxHtmlHelpDataItem *parent = m_index[i].parent; for (int j = i-1; j >= 0; j--) { if (m_index[j].book == book && m_index[j].level > 0) - cnt++; + cnt2++; if (&m_index[j] == parent) break; } - wxASSERT(cnt > 0); - CacheWriteInt32(f, cnt); + wxASSERT(cnt2 > 0); + CacheWriteInt32(f, cnt2); } } return true; @@ -487,7 +485,7 @@ void wxHtmlHelpData::SetTempDir(const wxString& path) if (wxIsAbsolutePath(path)) m_tempPath = path; else m_tempPath = wxGetCwd() + _T("/") + path; - if (m_tempPath[m_tempPath.Length() - 1] != _T('/')) + if (m_tempPath[m_tempPath.length() - 1] != _T('/')) m_tempPath << _T('/'); } } @@ -517,7 +515,7 @@ bool wxHtmlHelpData::AddBookParam(const wxFSFile& bookfile, int IndexOld = m_index.size(), ContentsOld = m_contents.size(); - if (!path.IsEmpty()) + if (!path.empty()) fsys.ChangePathTo(path, true); size_t booksCnt = m_bookRecords.GetCount(); @@ -642,7 +640,7 @@ bool wxHtmlHelpData::AddBook(const wxString& book) #endif s = fsys.FindFirst(book + wxT("#zip:*.hhp"), wxFILE); - while (!s.IsEmpty()) + while (!s.empty()) { if (AddBook(s)) rt = true; s = fsys.FindNext(); @@ -681,7 +679,7 @@ bool wxHtmlHelpData::AddBook(const wxString& book) lineptr = ReadLine(lineptr, linebuf, 300); for (wxChar *ch = linebuf; *ch != wxT('\0') && *ch != wxT('='); ch++) - *ch = tolower(*ch); + *ch = (wxChar)wxTolower(*ch); if (wxStrstr(linebuf, _T("title=")) == linebuf) title = linebuf + wxStrlen(_T("title=")); @@ -695,9 +693,11 @@ bool wxHtmlHelpData::AddBook(const wxString& book) charset = linebuf + wxStrlen(_T("charset=")); } while (lineptr != NULL); - wxFontEncoding enc; - if (charset == wxEmptyString) enc = wxFONTENCODING_SYSTEM; - else enc = wxFontMapper::Get()->CharsetToEncoding(charset); + wxFontEncoding enc = wxFONTENCODING_SYSTEM; +#if wxUSE_FONTMAP + if (charset != wxEmptyString) + enc = wxFontMapper::Get()->CharsetToEncoding(charset); +#endif bool rtval = AddBookParam(*fi, enc, title, contents, index, start, fsys.GetPath()); @@ -981,47 +981,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