Skip to content

Commit bce68d0

Browse files
committed
External file IO interface because currently there is no way to handle unicode file names platform independently. A windows and a wxWidgets implementation is also provided.
1 parent d15ab0c commit bce68d0

9 files changed

Lines changed: 145 additions & 34 deletions

File tree

Sources/NodeUIEngine/NUIE_NodeEditor.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ ExternalHeaderIO::~ExternalHeaderIO ()
7070

7171
}
7272

73+
ExternalFileIO::~ExternalFileIO ()
74+
{
75+
76+
}
77+
7378
NodeEditor::NodeEditor (NodeUIEnvironment& uiEnvironment) :
7479
uiManager (),
7580
interactionHandler (uiManager),
@@ -212,18 +217,13 @@ void NodeEditor::New ()
212217
Update ();
213218
}
214219

215-
bool NodeEditor::Open (const std::string& fileName, const ExternalHeaderIO* externalHeader)
220+
bool NodeEditor::Open (const std::wstring& fileName, const ExternalFileIO* externalFileIO, const ExternalHeaderIO* externalHeader)
216221
{
217-
std::ifstream file;
218-
file.open (fileName, std::ios::binary);
219-
if (DBGERROR (!file.is_open ())) {
222+
std::vector<char> buffer;
223+
if (DBGERROR (!externalFileIO->ReadBufferFromFile (fileName, buffer))) {
220224
return false;
221225
}
222226

223-
std::vector<char> buffer;
224-
buffer.assign (std::istreambuf_iterator<char> (file), std::istreambuf_iterator<char> ());
225-
file.close ();
226-
227227
NE::MemoryInputStream inputStream (buffer);
228228
return Open (inputStream, externalHeader);
229229
}
@@ -262,23 +262,18 @@ bool NodeEditor::Open (NE::InputStream& inputStream, const ExternalHeaderIO* ext
262262
return true;
263263
}
264264

265-
bool NodeEditor::Save (const std::string& fileName, const ExternalHeaderIO* externalHeader) const
265+
bool NodeEditor::Save (const std::wstring& fileName, const ExternalFileIO* externalFileIO, const ExternalHeaderIO* externalHeader) const
266266
{
267267
NE::MemoryOutputStream outputStream;
268268
if (DBGERROR (!Save (outputStream, externalHeader))) {
269269
return false;
270270
}
271271

272-
std::ofstream file;
273-
file.open (fileName, std::ios::binary);
274-
if (DBGERROR (!file.is_open ())) {
272+
const std::vector<char>& buffer = outputStream.GetBuffer ();
273+
if (DBGERROR (!externalFileIO->WriteBufferToFile (fileName, buffer))) {
275274
return false;
276275
}
277276

278-
const std::vector<char>& buffer = outputStream.GetBuffer ();
279-
file.write (buffer.data (), buffer.size ());
280-
file.close ();
281-
282277
return true;
283278
}
284279

Sources/NodeUIEngine/NUIE_NodeEditor.hpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ class ExternalHeaderIO
2020
virtual void Write (NE::OutputStream& outputStream) const = 0;
2121
};
2222

23+
class ExternalFileIO
24+
{
25+
public:
26+
virtual ~ExternalFileIO ();
27+
28+
virtual bool ReadBufferFromFile (const std::wstring& fileName, std::vector<char>& buffer) const = 0;
29+
virtual bool WriteBufferToFile (const std::wstring& fileName, const std::vector<char>& buffer) const = 0;
30+
};
31+
2332
class NodeEditor
2433
{
2534
public:
@@ -54,9 +63,9 @@ class NodeEditor
5463
void FitToWindow ();
5564

5665
void New ();
57-
bool Open (const std::string& fileName, const ExternalHeaderIO* externalHeader);
66+
bool Open (const std::wstring& fileName, const ExternalFileIO* externalFileIO, const ExternalHeaderIO* externalHeader);
5867
bool Open (NE::InputStream& inputStream, const ExternalHeaderIO* externalHeader);
59-
bool Save (const std::string& fileName, const ExternalHeaderIO* externalHeader) const;
68+
bool Save (const std::wstring& fileName, const ExternalFileIO* externalFileIO, const ExternalHeaderIO* externalHeader) const;
6069
bool Save (NE::OutputStream& outputStream, const ExternalHeaderIO* externalHeader) const;
6170
bool NeedToSave () const;
6271
void Undo ();
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "WAS_WindowsFileIO.hpp"
2+
#include "NE_Debug.hpp"
3+
4+
#include <iostream>
5+
#include <fstream>
6+
7+
namespace WAS
8+
{
9+
10+
bool WindowsFileIO::ReadBufferFromFile (const std::wstring& fileName, std::vector<char>& buffer) const
11+
{
12+
std::ifstream file;
13+
file.open (fileName, std::ios::binary);
14+
if (DBGERROR (!file.is_open ())) {
15+
return false;
16+
}
17+
18+
buffer.assign (std::istreambuf_iterator<char> (file), std::istreambuf_iterator<char> ());
19+
file.close ();
20+
21+
return true;
22+
}
23+
24+
bool WindowsFileIO::WriteBufferToFile (const std::wstring& fileName, const std::vector<char>& buffer) const
25+
{
26+
std::ofstream file;
27+
file.open (fileName, std::ios::binary);
28+
if (DBGERROR (!file.is_open ())) {
29+
return false;
30+
}
31+
32+
file.write (buffer.data (), buffer.size ());
33+
file.close ();
34+
35+
return true;
36+
}
37+
38+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef WAS_WINDOWSFILEIO_HPP
2+
#define WAS_WINDOWSFILEIO_HPP
3+
4+
#include "NUIE_NodeEditor.hpp"
5+
6+
namespace WAS
7+
{
8+
9+
class WindowsFileIO : public NUIE::ExternalFileIO
10+
{
11+
public:
12+
virtual bool ReadBufferFromFile (const std::wstring& fileName, std::vector<char>& buffer) const override;
13+
virtual bool WriteBufferToFile (const std::wstring& fileName, const std::vector<char>& buffer) const override;
14+
};
15+
16+
}
17+
18+
#endif

Sources/wxWidgetsAppSupport/WXAS_NodeEditorControl.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,14 +294,14 @@ void NodeEditorControl::New ()
294294
nodeEditor->New ();
295295
}
296296

297-
bool NodeEditorControl::Open (const std::string& fileName, const NUIE::ExternalHeaderIO* externalHeader)
297+
bool NodeEditorControl::Open (const std::wstring& fileName, const NUIE::ExternalFileIO* externalFileIO, const NUIE::ExternalHeaderIO* externalHeader)
298298
{
299-
return nodeEditor->Open (fileName, externalHeader);
299+
return nodeEditor->Open (fileName, externalFileIO, externalHeader);
300300
}
301301

302-
bool NodeEditorControl::Save (const std::string& fileName, const NUIE::ExternalHeaderIO* externalHeader)
302+
bool NodeEditorControl::Save (const std::wstring& fileName, const NUIE::ExternalFileIO* externalFileIO, const NUIE::ExternalHeaderIO* externalHeader)
303303
{
304-
return nodeEditor->Save (fileName, externalHeader);
304+
return nodeEditor->Save (fileName, externalFileIO, externalHeader);
305305
}
306306

307307
bool NodeEditorControl::NeedToSave () const

Sources/wxWidgetsAppSupport/WXAS_NodeEditorControl.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ class NodeEditorControl : public wxPanel
109109
void FitToWindow ();
110110

111111
void New ();
112-
bool Open (const std::string& fileName, const NUIE::ExternalHeaderIO* externalHeader);
113-
bool Save (const std::string& fileName, const NUIE::ExternalHeaderIO* externalHeader);
112+
bool Open (const std::wstring& fileName, const NUIE::ExternalFileIO* externalFileIO, const NUIE::ExternalHeaderIO* externalHeader);
113+
bool Save (const std::wstring& fileName, const NUIE::ExternalFileIO* externalFileIO, const NUIE::ExternalHeaderIO* externalHeader);
114114
bool NeedToSave () const;
115115
void Undo ();
116116
void Redo ();
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "WXAS_wxFileIO.hpp"
2+
#include "NE_Debug.hpp"
3+
4+
#include <wx/file.h>
5+
6+
namespace WXAS
7+
{
8+
9+
bool wxFileIO::ReadBufferFromFile (const std::wstring& fileName, std::vector<char>& buffer) const
10+
{
11+
wxFile file (wxString (fileName), wxFile::OpenMode::read);
12+
if (DBGERROR (!file.IsOpened ())) {
13+
return false;
14+
}
15+
static const size_t bufferSize = 1024;
16+
char actBuffer[1024];
17+
size_t readBytes = 0;
18+
while ((readBytes = file.Read (&actBuffer, bufferSize)) != 0) {
19+
buffer.insert (std::end (buffer), std::begin (actBuffer), std::begin (actBuffer) + bufferSize);
20+
}
21+
file.Close ();
22+
return true;
23+
}
24+
25+
bool wxFileIO::WriteBufferToFile (const std::wstring& fileName, const std::vector<char>& buffer) const
26+
{
27+
wxFile file (wxString (fileName), wxFile::OpenMode::write);
28+
if (DBGERROR (!file.IsOpened ())) {
29+
return false;
30+
}
31+
32+
file.Write (buffer.data (), buffer.size ());
33+
file.Close ();
34+
return true;
35+
}
36+
37+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef WAS_WXFILEIO_HPP
2+
#define WAS_WXFILEIO_HPP
3+
4+
#include "NUIE_NodeEditor.hpp"
5+
6+
namespace WXAS
7+
{
8+
9+
class wxFileIO : public NUIE::ExternalFileIO
10+
{
11+
public:
12+
virtual bool ReadBufferFromFile (const std::wstring& fileName, std::vector<char>& buffer) const override;
13+
virtual bool WriteBufferToFile (const std::wstring& fileName, const std::vector<char>& buffer) const override;
14+
};
15+
16+
}
17+
18+
#endif

Sources/wxWidgetsTestApp/main.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "BI_InputUINodes.hpp"
55
#include "BI_ArithmeticUINodes.hpp"
66
#include "BI_ViewerUINodes.hpp"
7+
#include "WXAS_wxFileIO.hpp"
78
#include "WXAS_ParameterDialog.hpp"
89
#include "WXAS_NodeEditorControl.hpp"
910

@@ -15,12 +16,6 @@
1516
#include <wx/wx.h>
1617
#include <wx/splitter.h>
1718

18-
static std::string WideStringToNormalString (const std::wstring& str)
19-
{
20-
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
21-
return converter.to_bytes (str);
22-
}
23-
2419
class ApplicationState
2520
{
2621
public:
@@ -364,6 +359,7 @@ class MainFrame : public wxFrame
364359

365360
void OnCommand (wxCommandEvent& evt)
366361
{
362+
WXAS::wxFileIO fileIO;
367363
MenuBar::CommandId commandId = (MenuBar::CommandId) evt.GetId ();
368364
switch (commandId) {
369365
case MenuBar::CommandId::File_New:
@@ -378,7 +374,7 @@ class MainFrame : public wxFrame
378374
std::wstring fileName = fileDialog.GetPath ().ToStdWstring ();
379375
drawingControl->ClearImage ();
380376
// TODO: handle when open fails
381-
if (nodeEditorControl->Open (WideStringToNormalString (fileName), &headerIO)) {
377+
if (nodeEditorControl->Open (fileName, &fileIO, &headerIO)) {
382378
applicationState.SetCurrentFileName (fileName);
383379
} else {
384380
Reset ();
@@ -390,10 +386,10 @@ class MainFrame : public wxFrame
390386
{
391387
wxFileDialog fileDialog (this, L"Save", L"", L"", L"Node Engine Files (*.ne)|*.ne", wxFD_SAVE);
392388
if (applicationState.HasCurrentFileName ()) {
393-
nodeEditorControl->Save (WideStringToNormalString (applicationState.GetCurrentFileName ()), &headerIO);
389+
nodeEditorControl->Save (applicationState.GetCurrentFileName (), &fileIO, &headerIO);
394390
} else if (fileDialog.ShowModal () == wxID_OK) {
395391
std::wstring fileName = fileDialog.GetPath ().ToStdWstring ();
396-
nodeEditorControl->Save (WideStringToNormalString (fileName), &headerIO);
392+
nodeEditorControl->Save (fileName, &fileIO, &headerIO);
397393
applicationState.SetCurrentFileName (fileName);
398394
}
399395
}
@@ -403,7 +399,7 @@ class MainFrame : public wxFrame
403399
wxFileDialog fileDialog (this, L"Save As", L"", L"", L"Node Engine Files (*.ne)|*.ne", wxFD_SAVE);
404400
if (fileDialog.ShowModal () == wxID_OK) {
405401
std::wstring fileName = fileDialog.GetPath ().ToStdWstring ();
406-
nodeEditorControl->Save (WideStringToNormalString (fileName), &headerIO);
402+
nodeEditorControl->Save (fileName, &fileIO, &headerIO);
407403
applicationState.SetCurrentFileName (fileName);
408404
}
409405
}

0 commit comments

Comments
 (0)