Removed the need for wxStream::GetSize in wxHTML.

Ascape '&' in text/plain.


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@15229 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Mattia Barbon
2002-04-21 17:36:16 +00:00
parent f90566f5c3
commit eb37e1d2f3
3 changed files with 55 additions and 46 deletions

View File

@@ -48,11 +48,11 @@ WX_DEFINE_OBJARRAY(wxHtmlBookRecArray)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Reads one line, stores it into buf and returns pointer to new line or NULL. // Reads one line, stores it into buf and returns pointer to new line or NULL.
static char* ReadLine(char *line, char *buf, size_t bufsize) static const char* ReadLine(const char *line, char *buf, size_t bufsize)
{ {
char *writeptr = buf; char *writeptr = buf;
char *endptr = buf + bufsize - 1; char *endptr = buf + bufsize - 1;
char *readptr = line; const char *readptr = line;
while (*readptr != 0 && *readptr != '\r' && *readptr != '\n' && while (*readptr != 0 && *readptr != '\r' && *readptr != '\n' &&
writeptr != endptr) writeptr != endptr)
@@ -248,11 +248,13 @@ wxHtmlHelpData::~wxHtmlHelpData()
} }
} }
// defined in htmlfilt.cpp
void wxPrivate_ReadString(wxString& str, wxInputStream* s);
bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord *book, wxFileSystem& fsys, const wxString& indexfile, const wxString& contentsfile) bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord *book, wxFileSystem& fsys, const wxString& indexfile, const wxString& contentsfile)
{ {
wxFSFile *f; wxFSFile *f;
char *buf; wxString buf;
int sz;
wxString string; wxString string;
HP_Parser parser; HP_Parser parser;
@@ -262,15 +264,12 @@ bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord *book, wxFileSystem& fsys, c
f = ( contentsfile.IsEmpty() ? (wxFSFile*) NULL : fsys.OpenFile(contentsfile) ); f = ( contentsfile.IsEmpty() ? (wxFSFile*) NULL : fsys.OpenFile(contentsfile) );
if (f) if (f)
{ {
sz = f->GetStream()->GetSize(); buf.clear();
buf = new char[sz + 1]; wxPrivate_ReadString(buf, f->GetStream());
buf[sz] = 0;
f->GetStream()->Read(buf, sz);
delete f; delete f;
handler->ReadIn(m_Contents, m_ContentsCnt); handler->ReadIn(m_Contents, m_ContentsCnt);
parser.Parse(buf); parser.Parse(buf);
handler->WriteOut(m_Contents, m_ContentsCnt); handler->WriteOut(m_Contents, m_ContentsCnt);
delete[] buf;
} }
else else
wxLogError(_("Cannot open contents file: %s"), contentsfile.c_str()); wxLogError(_("Cannot open contents file: %s"), contentsfile.c_str());
@@ -278,15 +277,12 @@ bool wxHtmlHelpData::LoadMSProject(wxHtmlBookRecord *book, wxFileSystem& fsys, c
f = ( indexfile.IsEmpty() ? (wxFSFile*) NULL : fsys.OpenFile(indexfile) ); f = ( indexfile.IsEmpty() ? (wxFSFile*) NULL : fsys.OpenFile(indexfile) );
if (f) if (f)
{ {
sz = f->GetStream()->GetSize(); buf.clear();
buf = new char[sz + 1]; wxPrivate_ReadString(buf, f->GetStream());
buf[sz] = 0;
f->GetStream()->Read(buf, sz);
delete f; delete f;
handler->ReadIn(m_Index, m_IndexCnt); handler->ReadIn(m_Index, m_IndexCnt);
parser.Parse(buf); parser.Parse(buf);
handler->WriteOut(m_Index, m_IndexCnt); handler->WriteOut(m_Index, m_IndexCnt);
delete[] buf;
} }
else if (!indexfile.IsEmpty()) else if (!indexfile.IsEmpty())
wxLogError(_("Cannot open index file: %s"), indexfile.c_str()); wxLogError(_("Cannot open index file: %s"), indexfile.c_str());
@@ -586,15 +582,12 @@ bool wxHtmlHelpData::AddBook(const wxString& book)
fsys.ChangePathTo(bookFull); fsys.ChangePathTo(bookFull);
s = fi->GetStream(); s = fi->GetStream();
int sz; const char *lineptr;
char *buff, *lineptr;
char linebuf[300]; char linebuf[300];
wxString tmp;
sz = s->GetSize(); wxPrivate_ReadString(tmp, s);
buff = new char[sz + 1]; lineptr = tmp.c_str();
buff[sz] = 0;
s->Read(buff, sz);
lineptr = buff;
do do
{ {
@@ -614,7 +607,6 @@ bool wxHtmlHelpData::AddBook(const wxString& book)
if (strstr(linebuf, "charset=") == linebuf) if (strstr(linebuf, "charset=") == linebuf)
charset = linebuf + strlen("charset="); charset = linebuf + strlen("charset=");
} while (lineptr != NULL); } while (lineptr != NULL);
delete[] buff;
wxFontEncoding enc; wxFontEncoding enc;
if (charset == wxEmptyString) enc = wxFONTENCODING_SYSTEM; if (charset == wxEmptyString) enc = wxFONTENCODING_SYSTEM;
@@ -828,16 +820,16 @@ bool wxSearchEngine::Scan(wxInputStream *stream)
wxASSERT_MSG(m_Keyword != NULL, wxT("wxSearchEngine::LookFor must be called before scanning!")); wxASSERT_MSG(m_Keyword != NULL, wxT("wxSearchEngine::LookFor must be called before scanning!"));
int i, j; int i, j;
int lng = stream ->GetSize();
int wrd = wxStrlen(m_Keyword); int wrd = wxStrlen(m_Keyword);
bool found = FALSE; bool found = FALSE;
char *buf = new char[lng + 1]; wxString tmp;
stream->Read(buf, lng); wxPrivate_ReadString(tmp, stream);
buf[lng] = 0; int lng = tmp.length();
const char *buf = tmp.c_str();
if (!m_CaseSensitive) if (!m_CaseSensitive)
for (i = 0; i < lng; i++) for (i = 0; i < lng; i++)
if ((buf[i] >= 'A') && (buf[i] <= 'Z')) buf[i] += 'a' - 'A'; tmp[size_t(i)] = (char)tolower(tmp[i]);
if (m_WholeWords) if (m_WholeWords)
{ {
@@ -860,7 +852,6 @@ bool wxSearchEngine::Scan(wxInputStream *stream)
} }
} }
delete[] buf;
return found; return found;
} }

View File

@@ -29,6 +29,35 @@
#include "wx/html/htmlfilt.h" #include "wx/html/htmlfilt.h"
#include "wx/html/htmlwin.h" #include "wx/html/htmlwin.h"
// utility function: read a wxString from a wxInputStream
void wxPrivate_ReadString(wxString& str, wxInputStream* s)
{
size_t streamSize = s->GetSize();
if(streamSize == ~(size_t)0)
{
const size_t bufSize = 4095;
char buffer[bufSize+1];
size_t lastRead;
do
{
s->Read(buffer, bufSize);
lastRead = s->LastRead();
buffer[lastRead] = 0;
str.Append(buffer);
}
while(lastRead == bufSize);
}
else
{
char* src = new char[streamSize+1];
s->Read(src, streamSize);
src[streamSize] = 0;
str = src;
delete [] src;
}
}
/* /*
@@ -55,16 +84,12 @@ bool wxHtmlFilterPlainText::CanRead(const wxFSFile& WXUNUSED(file)) const
wxString wxHtmlFilterPlainText::ReadFile(const wxFSFile& file) const wxString wxHtmlFilterPlainText::ReadFile(const wxFSFile& file) const
{ {
wxInputStream *s = file.GetStream(); wxInputStream *s = file.GetStream();
char *src;
wxString doc, doc2; wxString doc, doc2;
if (s == NULL) return wxEmptyString; if (s == NULL) return wxEmptyString;
src = new char[s->GetSize()+1]; wxPrivate_ReadString(doc, s);
src[s->GetSize()] = 0;
s->Read(src, s->GetSize());
doc = src;
delete [] src;
doc.Replace(wxT("&"), wxT("&amp;"), TRUE);
doc.Replace(wxT("<"), wxT("&lt;"), TRUE); doc.Replace(wxT("<"), wxT("&lt;"), TRUE);
doc.Replace(wxT(">"), wxT("&gt;"), TRUE); doc.Replace(wxT(">"), wxT("&gt;"), TRUE);
doc2 = "<HTML><BODY><PRE>\n" + doc + "\n</PRE></BODY></HTML>"; doc2 = "<HTML><BODY><PRE>\n" + doc + "\n</PRE></BODY></HTML>";
@@ -139,7 +164,6 @@ bool wxHtmlFilterHTML::CanRead(const wxFSFile& file) const
wxString wxHtmlFilterHTML::ReadFile(const wxFSFile& file) const wxString wxHtmlFilterHTML::ReadFile(const wxFSFile& file) const
{ {
wxInputStream *s = file.GetStream(); wxInputStream *s = file.GetStream();
char *src;
wxString doc; wxString doc;
if (s == NULL) if (s == NULL)
@@ -147,11 +171,7 @@ wxString wxHtmlFilterHTML::ReadFile(const wxFSFile& file) const
wxLogError(_("Cannot open HTML document: %s"), file.GetLocation().c_str()); wxLogError(_("Cannot open HTML document: %s"), file.GetLocation().c_str());
return wxEmptyString; return wxEmptyString;
} }
src = new char[s->GetSize() + 1]; wxPrivate_ReadString(doc, s);
src[s->GetSize()] = 0;
s->Read(src, s->GetSize());
doc = src;
delete[] src;
// add meta tag if we obtained this through http: // add meta tag if we obtained this through http:
if (file.GetMimeType().Find(_T("; charset=")) == 0) if (file.GetMimeType().Find(_T("; charset=")) == 0)

View File

@@ -269,7 +269,8 @@ void wxHtmlPrintout::SetHtmlText(const wxString& html, const wxString &basepath,
m_BasePathIsDir = isdir; m_BasePathIsDir = isdir;
} }
// defined in htmlfilt.cpp
void wxPrivate_ReadString(wxString& str, wxInputStream* s);
void wxHtmlPrintout::SetHtmlFile(const wxString& htmlfile) void wxHtmlPrintout::SetHtmlFile(const wxString& htmlfile)
{ {
@@ -283,12 +284,9 @@ void wxHtmlPrintout::SetHtmlFile(const wxString& htmlfile)
} }
wxInputStream *st = ff->GetStream(); wxInputStream *st = ff->GetStream();
char *t = new char[st->GetSize() + 1]; wxString doc;
st->Read(t, st->GetSize()); wxPrivate_ReadString(doc, st);
t[st->GetSize()] = 0;
wxString doc = wxString(t);
delete t;
delete ff; delete ff;
SetHtmlText(doc, htmlfile, FALSE); SetHtmlText(doc, htmlfile, FALSE);