-`/////////////////////////////////////////////////////////////////////////////
+/////////////////////////////////////////////////////////////////////////////
// Name: src/common/docview.cpp
// Purpose: Document/view classes
// Author: Julian Smart
// Modified by:
// Created: 01/02/97
// RCS-ID: $Id$
-// Copyright: (c) Julian Smart and Markus Holzem
+// Copyright: (c) Julian Smart
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#include "wx/log.h"
#endif
+#include "wx/ffile.h"
+
#ifdef __WXMAC__
#include "wx/filename.h"
#endif
msgTitle = wxString(_("File error"));
#if wxUSE_STD_IOSTREAM
- wxSTD ofstream store(wxString(file.fn_str()).mb_str()); // ?????
+ wxSTD ofstream store(file.mb_str());
if (store.fail() || store.bad())
#else
- wxFileOutputStream store( file );
+ wxFileOutputStream store(file);
if (store.GetLastError() != wxSTREAM_NO_ERROR)
#endif
{
msgTitle = wxString(_("File error"));
#if wxUSE_STD_IOSTREAM
- wxSTD ifstream store(wxString(file.fn_str()).mb_str()); // ????
+ wxSTD ifstream store(file.mb_str());
if (store.fail() || store.bad())
#else
- wxFileInputStream store( file );
+ wxFileInputStream store(file);
if (store.GetLastError() != wxSTREAM_NO_ERROR)
#endif
{
m_fileHistory->AddFileToHistory(file);
}
-void wxDocManager::RemoveFileFromHistory(int i)
+void wxDocManager::RemoveFileFromHistory(size_t i)
{
if (m_fileHistory)
m_fileHistory->RemoveFileFromHistory(i);
}
-wxString wxDocManager::GetHistoryFile(int i) const
+wxString wxDocManager::GetHistoryFile(size_t i) const
{
wxString histFile;
m_fileHistory->AddFilesToMenu();
}
-int wxDocManager::GetNoHistoryFiles() const
+size_t wxDocManager::GetNoHistoryFiles() const
{
if (m_fileHistory)
return m_fileHistory->GetNoHistoryFiles();
// File history processor
// ----------------------------------------------------------------------------
-wxFileHistory::wxFileHistory(int maxFiles)
+wxFileHistory::wxFileHistory(size_t maxFiles, wxWindowID idBase)
{
m_fileMaxFiles = maxFiles;
+ m_idBase = idBase;
m_fileHistoryN = 0;
m_fileHistory = new wxChar *[m_fileMaxFiles];
}
wxFileHistory::~wxFileHistory()
{
- int i;
+ size_t i;
for (i = 0; i < m_fileHistoryN; i++)
delete[] m_fileHistory[i];
delete[] m_fileHistory;
// File history management
void wxFileHistory::AddFileToHistory(const wxString& file)
{
- int i;
+ size_t i;
// Check we don't already have this file
for (i = 0; i < m_fileHistoryN; i++)
{
menu->AppendSeparator();
}
- menu->Append(wxID_FILE1+m_fileHistoryN, _("[EMPTY]"));
+ menu->Append(m_idBase+m_fileHistoryN, _("[EMPTY]"));
node = node->GetNext();
}
m_fileHistoryN ++;
while (node)
{
wxMenu* menu = (wxMenu*) node->GetData();
- menu->SetLabel(wxID_FILE1 + i, buf);
+ menu->SetLabel(m_idBase + i, buf);
node = node->GetNext();
}
}
}
}
-void wxFileHistory::RemoveFileFromHistory(int i)
+void wxFileHistory::RemoveFileFromHistory(size_t i)
{
wxCHECK_RET( i < m_fileHistoryN,
wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
- // delete the element from the array (could use memmove() too...)
- delete [] m_fileHistory[i];
+ // delete the element from the array (could use memmove() too...)
+ delete [] m_fileHistory[i];
- int j;
- for ( j = i; j < m_fileHistoryN - 1; j++ )
- {
- m_fileHistory[j] = m_fileHistory[j + 1];
- }
+ size_t j;
+ for ( j = i; j < m_fileHistoryN - 1; j++ )
+ {
+ m_fileHistory[j] = m_fileHistory[j + 1];
+ }
wxNode* node = m_fileMenus.GetFirst();
while ( node )
{
- wxMenu* menu = (wxMenu*) node->GetData();
+ wxMenu* menu = (wxMenu*) node->GetData();
- // shuffle filenames up
- wxString buf;
- for ( j = i; j < m_fileHistoryN - 1; j++ )
- {
- buf.Printf(s_MRUEntryFormat, j + 1, m_fileHistory[j]);
- menu->SetLabel(wxID_FILE1 + j, buf);
- }
+ // shuffle filenames up
+ wxString buf;
+ for ( j = i; j < m_fileHistoryN - 1; j++ )
+ {
+ buf.Printf(s_MRUEntryFormat, j + 1, m_fileHistory[j]);
+ menu->SetLabel(m_idBase + j, buf);
+ }
- node = node->GetNext();
+ node = node->GetNext();
// delete the last menu item which is unused now
- if (menu->FindItem(wxID_FILE1 + m_fileHistoryN - 1))
- menu->Delete(wxID_FILE1 + m_fileHistoryN - 1);
+ wxWindowID lastItemId = m_idBase + m_fileHistoryN - 1;
+ if (menu->FindItem(lastItemId))
+ {
+ menu->Delete(lastItemId);
+ }
// delete the last separator too if no more files are left
if ( m_fileHistoryN == 1 )
m_fileHistoryN--;
}
-wxString wxFileHistory::GetHistoryFile(int i) const
+wxString wxFileHistory::GetHistoryFile(size_t i) const
{
wxString s;
if ( i < m_fileHistoryN )
{
m_fileHistoryN = 0;
wxString buf;
- buf.Printf(wxT("file%d"), m_fileHistoryN+1);
+ buf.Printf(wxT("file%d"), (int)m_fileHistoryN+1);
wxString historyFile;
while ((m_fileHistoryN < m_fileMaxFiles) && config.Read(buf, &historyFile) && (historyFile != wxT("")))
{
m_fileHistory[m_fileHistoryN] = copystring((const wxChar*) historyFile);
m_fileHistoryN ++;
- buf.Printf(wxT("file%d"), m_fileHistoryN+1);
+ buf.Printf(wxT("file%d"), (int)m_fileHistoryN+1);
historyFile = wxT("");
}
AddFilesToMenu();
void wxFileHistory::Save(wxConfigBase& config)
{
- int i;
+ size_t i;
for (i = 0; i < m_fileHistoryN; i++)
{
wxString buf;
- buf.Printf(wxT("file%d"), i+1);
+ buf.Printf(wxT("file%d"), (int)i+1);
config.Write(buf, wxString(m_fileHistory[i]));
}
}
menu->AppendSeparator();
}
- int i;
+ size_t i;
for (i = 0; i < m_fileHistoryN; i++)
{
if (m_fileHistory[i])
{
wxString buf;
buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]);
- menu->Append(wxID_FILE1+i, buf);
+ menu->Append(m_idBase+i, buf);
}
}
node = node->GetNext();
menu->AppendSeparator();
}
- int i;
+ size_t i;
for (i = 0; i < m_fileHistoryN; i++)
{
if (m_fileHistory[i])
{
wxString buf;
buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]);
- menu->Append(wxID_FILE1+i, buf);
+ menu->Append(m_idBase+i, buf);
}
}
}
// ----------------------------------------------------------------------------
#if wxUSE_STD_IOSTREAM
+
bool wxTransferFileToStream(const wxString& filename, wxSTD ostream& stream)
{
- FILE *fd1;
- int ch;
-
- if ((fd1 = wxFopen (filename.fn_str(), _T("rb"))) == NULL)
+ wxFFile file(filename, _T("rb"));
+ if ( !file.IsOpened() )
return FALSE;
- while ((ch = getc (fd1)) != EOF)
- stream << (unsigned char)ch;
+ char buf[4096];
+
+ size_t nRead;
+ do
+ {
+ nRead = file.Read(buf, WXSIZEOF(buf));
+ if ( file.Error() )
+ return FALSE;
+
+ stream.write(buf, nRead);
+ if ( !stream )
+ return FALSE;
+ }
+ while ( !file.Eof() );
- fclose (fd1);
return TRUE;
}
bool wxTransferStreamToFile(wxSTD istream& stream, const wxString& filename)
{
- FILE *fd1;
- int ch;
-
- if ((fd1 = wxFopen (filename.fn_str(), _T("wb"))) == NULL)
- {
+ wxFFile file(filename, _T("wb"));
+ if ( !file.IsOpened() )
return FALSE;
- }
- while (!stream.eof())
+ char buf[4096];
+ do
{
- ch = stream.get();
- if (!stream.eof())
- putc (ch, fd1);
+ stream.read(buf, WXSIZEOF(buf));
+ if ( !stream.bad() ) // fail may be set on EOF, don't use operator!()
+ {
+ if ( !file.Write(buf, stream.gcount()) )
+ return FALSE;
+ }
}
- fclose (fd1);
+ while ( !stream.eof() );
+
return TRUE;
}
-#else
+
+#else // !wxUSE_STD_IOSTREAM
+
bool wxTransferFileToStream(const wxString& filename, wxOutputStream& stream)
{
- FILE *fd1;
- int ch;
-
- if ((fd1 = wxFopen (filename, wxT("rb"))) == NULL)
+ wxFFile file(filename, _T("rb"));
+ if ( !file.IsOpened() )
return FALSE;
- while ((ch = getc (fd1)) != EOF)
- stream.PutC((char) ch);
+ char buf[4096];
+
+ size_t nRead;
+ do
+ {
+ nRead = file.Read(buf, WXSIZEOF(buf));
+ if ( file.Error() )
+ return FALSE;
+
+ stream.Write(buf, nRead);
+ if ( !stream )
+ return FALSE;
+ }
+ while ( !file.Eof() );
- fclose (fd1);
return TRUE;
}
bool wxTransferStreamToFile(wxInputStream& stream, const wxString& filename)
{
- FILE *fd1;
- char ch;
-
- if ((fd1 = wxFopen (filename, wxT("wb"))) == NULL)
- {
+ wxFFile file(filename, _T("wb"));
+ if ( !file.IsOpened() )
return FALSE;
- }
- int len = stream.GetSize();
- // TODO: is this the correct test for EOF?
- while (stream.TellI() < (len - 1))
+ char buf[4096];
+ do
{
- ch = stream.GetC();
- putc (ch, fd1);
+ stream.Read(buf, WXSIZEOF(buf));
+
+ const size_t nRead = stream.LastRead();
+ if ( !nRead || !file.Write(buf, nRead) )
+ return FALSE;
}
- fclose (fd1);
+ while ( !stream.Eof() );
+
return TRUE;
}
-#endif
+
+#endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
#endif // wxUSE_DOC_VIEW_ARCHITECTURE