Audacity 3.2.0
FileIO.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 FileIO.cpp
6
7 Leland Lucius
8
9**********************************************************************/
10
11#include "FileIO.h"
12
13#include <wx/defs.h>
14#include <wx/crt.h>
15#include <wx/filename.h>
16#include <wx/wfstream.h>
17#include "wxFileNameWrapper.h"
18
20: mMode(mode),
21 mOpen(false)
22{
23 wxString scheme;
24
25 auto path = name.GetFullPath();
26 if (mMode == FileIO::Input) {
27 mInputStream = std::make_unique<wxFFileInputStream>(path);
28 if (mInputStream == NULL || !mInputStream->IsOk()) {
29 wxPrintf(wxT("Couldn't get input stream: %s\n"), path);
30 return;
31 }
32 }
33 else {
34 mOutputStream = std::make_unique<wxFFileOutputStream>(path);
35 if (mOutputStream == NULL || !mOutputStream->IsOk()) {
36 wxPrintf(wxT("Couldn't get output stream: %s\n"), path);
37 return;
38 }
39 }
40
41 mOpen = true;
42}
43
45{
46 Close();
47}
48
50{
51 return mOpen;
52}
53
55{
56 bool success = true;
57 if (mOutputStream) {
58 // mOutputStream->Sync() returns void! Rrr!
59 success = mOutputStream->GetFile()->Flush() &&
60 mOutputStream->Close();
61 mOutputStream.reset();
62 }
63 mInputStream.reset();
64 mOpen = false;
65 return success;
66}
67
68wxInputStream & FileIO::Read(void *buf, size_t size)
69{
70 if (mInputStream == NULL) {
71 return *mInputStream;
72 }
73
74 return mInputStream->Read(buf, size);
75}
76
77wxOutputStream & FileIO::Write(const void *buf, size_t size)
78{
79 if (mOutputStream == NULL) {
80 return *mOutputStream;
81 }
82
83 return mOutputStream->Write(buf, size);
84}
wxT("CloseDown"))
wxString name
Definition: TagsEditor.cpp:166
FileIOMode mMode
Definition: FileIO.h:44
FileIOMode
Definition: FileIO.h:25
@ Input
Definition: FileIO.h:26
FileIO(const wxFileNameWrapper &name, FileIOMode mode)
Definition: FileIO.cpp:19
bool Close()
Definition: FileIO.cpp:54
std::unique_ptr< wxInputStream > mInputStream
Definition: FileIO.h:45
bool mOpen
Definition: FileIO.h:47
wxInputStream & Read(void *buffer, size_t size)
Definition: FileIO.cpp:68
wxOutputStream & Write(const void *buffer, size_t size)
Definition: FileIO.cpp:77
std::unique_ptr< wxFFileOutputStream > mOutputStream
Definition: FileIO.h:46
~FileIO()
Definition: FileIO.cpp:44
bool IsOpened()
Definition: FileIO.cpp:49