Audacity  2.2.2
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 "Audacity.h"
12 
13 #include <wx/defs.h>
14 #include <wx/crt.h>
15 #include <wx/filename.h>
16 #include <wx/wfstream.h>
17 
18 #include "FileIO.h"
19 
20 FileIO::FileIO(const wxString & name, FileIOMode mode)
21 : mName(name),
22  mMode(mode),
23  mOpen(false)
24 {
25  wxString scheme;
26 
27  if (mMode == FileIO::Input) {
28  mInputStream = std::make_unique<wxFFileInputStream>(mName);
29  if (mInputStream == NULL || !mInputStream->IsOk()) {
30  wxPrintf(wxT("Couldn't get input stream: %s\n"), name);
31  return;
32  }
33  }
34  else {
35  mOutputStream = std::make_unique<wxFFileOutputStream>(mName);
36  if (mOutputStream == NULL || !mOutputStream->IsOk()) {
37  wxPrintf(wxT("Couldn't get output stream: %s\n"), name);
38  return;
39  }
40  }
41 
42  mOpen = true;
43 }
44 
46 {
47  Close();
48 }
49 
51 {
52  return mOpen;
53 }
54 
56 {
57  bool success = true;
58  if (mOutputStream) {
59  // mOutputStream->Sync() returns void! Rrr!
60  success = mOutputStream->GetFile()->Flush() &&
61  mOutputStream->Close();
62  mOutputStream.reset();
63  }
64  mInputStream.reset();
65  mOpen = false;
66  return success;
67 }
68 
69 wxInputStream & FileIO::Read(void *buf, size_t size)
70 {
71  if (mInputStream == NULL) {
72  return *mInputStream;
73  }
74 
75  return mInputStream->Read(buf, size);
76 }
77 
78 wxOutputStream & FileIO::Write(const void *buf, size_t size)
79 {
80  if (mOutputStream == NULL) {
81  return *mOutputStream;
82  }
83 
84  return mOutputStream->Write(buf, size);
85 }
wxString mName
Definition: FileIO.h:41
FileIOMode
Definition: FileIO.h:21
bool Close()
Definition: FileIO.cpp:55
std::unique_ptr< wxFFileOutputStream > mOutputStream
Definition: FileIO.h:44
bool IsOpened()
Definition: FileIO.cpp:50
FileIO(const wxString &name, FileIOMode mode)
Definition: FileIO.cpp:20
wxOutputStream & Write(const void *buffer, size_t size)
Definition: FileIO.cpp:78
~FileIO()
Definition: FileIO.cpp:45
std::unique_ptr< wxInputStream > mInputStream
Definition: FileIO.h:43
const wxChar * name
Definition: Distortion.cpp:94
FileIOMode mMode
Definition: FileIO.h:42
bool mOpen
Definition: FileIO.h:45
wxInputStream & Read(void *buffer, size_t size)
Definition: FileIO.cpp:69