2010-01-23 19:44:49 +00:00
|
|
|
/**********************************************************************
|
|
|
|
|
|
|
|
Audacity: A Digital Audio Editor
|
2014-06-03 20:30:19 +00:00
|
|
|
|
2010-01-23 19:44:49 +00:00
|
|
|
FileIO.cpp
|
2014-06-03 20:30:19 +00:00
|
|
|
|
2010-01-23 19:44:49 +00:00
|
|
|
Leland Lucius
|
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
2021-05-09 15:16:56 +00:00
|
|
|
|
2018-11-10 19:47:12 +00:00
|
|
|
#include "FileIO.h"
|
2010-01-23 19:44:49 +00:00
|
|
|
|
|
|
|
#include <wx/defs.h>
|
2015-04-11 23:56:34 +00:00
|
|
|
#include <wx/crt.h>
|
2010-01-23 19:44:49 +00:00
|
|
|
#include <wx/filename.h>
|
|
|
|
#include <wx/wfstream.h>
|
2019-12-13 18:18:18 +00:00
|
|
|
#include "wxFileNameWrapper.h"
|
2010-01-23 19:44:49 +00:00
|
|
|
|
2019-12-13 18:18:18 +00:00
|
|
|
FileIO::FileIO(const wxFileNameWrapper & name, FileIOMode mode)
|
|
|
|
: mMode(mode),
|
2010-01-23 19:44:49 +00:00
|
|
|
mOpen(false)
|
|
|
|
{
|
|
|
|
wxString scheme;
|
|
|
|
|
2019-12-13 18:18:18 +00:00
|
|
|
auto path = name.GetFullPath();
|
2010-01-23 19:44:49 +00:00
|
|
|
if (mMode == FileIO::Input) {
|
2019-12-13 18:18:18 +00:00
|
|
|
mInputStream = std::make_unique<wxFFileInputStream>(path);
|
2014-12-06 12:06:48 +00:00
|
|
|
if (mInputStream == NULL || !mInputStream->IsOk()) {
|
2019-12-13 18:18:18 +00:00
|
|
|
wxPrintf(wxT("Couldn't get input stream: %s\n"), path);
|
2010-01-23 19:44:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2019-12-13 18:18:18 +00:00
|
|
|
mOutputStream = std::make_unique<wxFFileOutputStream>(path);
|
2014-12-06 12:06:48 +00:00
|
|
|
if (mOutputStream == NULL || !mOutputStream->IsOk()) {
|
2019-12-13 18:18:18 +00:00
|
|
|
wxPrintf(wxT("Couldn't get output stream: %s\n"), path);
|
2010-01-23 19:44:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mOpen = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileIO::~FileIO()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileIO::IsOpened()
|
|
|
|
{
|
|
|
|
return mOpen;
|
|
|
|
}
|
|
|
|
|
2018-01-22 19:52:27 +00:00
|
|
|
bool FileIO::Close()
|
2010-01-23 19:44:49 +00:00
|
|
|
{
|
2018-01-22 19:52:27 +00:00
|
|
|
bool success = true;
|
|
|
|
if (mOutputStream) {
|
|
|
|
// mOutputStream->Sync() returns void! Rrr!
|
|
|
|
success = mOutputStream->GetFile()->Flush() &&
|
|
|
|
mOutputStream->Close();
|
|
|
|
mOutputStream.reset();
|
|
|
|
}
|
2016-04-08 08:32:11 +00:00
|
|
|
mInputStream.reset();
|
2010-01-23 19:44:49 +00:00
|
|
|
mOpen = false;
|
2018-01-22 19:52:27 +00:00
|
|
|
return success;
|
2010-01-23 19:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wxInputStream & FileIO::Read(void *buf, size_t size)
|
|
|
|
{
|
|
|
|
if (mInputStream == NULL) {
|
|
|
|
return *mInputStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mInputStream->Read(buf, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
wxOutputStream & FileIO::Write(const void *buf, size_t size)
|
|
|
|
{
|
|
|
|
if (mOutputStream == NULL) {
|
|
|
|
return *mOutputStream;
|
|
|
|
}
|
2014-06-03 20:30:19 +00:00
|
|
|
|
2010-01-23 19:44:49 +00:00
|
|
|
return mOutputStream->Write(buf, size);
|
|
|
|
}
|