Skip to content

Commit 7656ea9

Browse files
committed
Added read/write with arbitrary streams
1 parent 2f29d79 commit 7656ea9

File tree

4 files changed

+95
-15
lines changed

4 files changed

+95
-15
lines changed

System/Reader.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@ namespace RTE {
2626
m_NonModulePath = false;
2727
}
2828

29+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
30+
31+
Reader::Reader(const std::string &fileName, bool overwrites, const ProgressCallback &progressCallback, bool failOK, bool nonModulePath) {
32+
Clear();
33+
m_NonModulePath = nonModulePath;
34+
Create(fileName, overwrites, progressCallback, failOK);
35+
}
36+
37+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
38+
39+
Reader::Reader(std::unique_ptr<std::istream> &&stream, bool overwrites, const ProgressCallback &progressCallback, bool failOK) {
40+
Clear();
41+
Create(std::move(stream), overwrites, progressCallback, failOK);
42+
}
43+
2944
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
3045

3146
int Reader::Create(const std::string &fileName, bool overwrites, const ProgressCallback &progressCallback, bool failOK) {
@@ -47,16 +62,27 @@ namespace RTE {
4762
m_DataModuleID = g_PresetMan.GetModuleID(m_DataModuleName);
4863
}
4964

65+
return Create(std::make_unique<std::ifstream>(m_FilePath), overwrites, progressCallback, failOK);
66+
}
67+
68+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
69+
70+
int Reader::Create(std::unique_ptr<std::istream> &&stream, bool overwrites, const ProgressCallback &progressCallback, bool failOK) {
5071
m_CanFail = failOK;
5172

52-
m_Stream = std::make_unique<std::ifstream>(m_FilePath);
53-
if (!m_CanFail) { RTEAssert(System::PathExistsCaseSensitive(m_FilePath) && m_Stream->good(), "Failed to open data file \"" + m_FilePath + "\"!"); }
73+
m_Stream = std::move(stream);
74+
75+
if (!m_CanFail) {
76+
RTEAssert(System::PathExistsCaseSensitive(m_FilePath) && m_Stream->good(), "Failed to open data file \"" + m_FilePath + "\"!");
77+
}
5478

5579
m_OverwriteExisting = overwrites;
5680

5781
// Report that we're starting a new file
5882
m_ReportProgress = progressCallback;
59-
if (m_ReportProgress && m_Stream->good()) { m_ReportProgress("\t" + m_FileName + " on line " + std::to_string(m_CurrentLine), true); }
83+
if (m_ReportProgress && m_Stream->good()) {
84+
m_ReportProgress("\t" + m_FileName + " on line " + std::to_string(m_CurrentLine), true);
85+
}
6086

6187
return m_Stream->good() ? 0 : -1;
6288
}
@@ -311,7 +337,7 @@ namespace RTE {
311337

312338
if (m_Stream->fail() || !System::PathExistsCaseSensitive(includeFilePath)) {
313339
// Backpedal and set up to read the next property in the old stream
314-
m_Stream.reset(m_StreamStack.top().Stream); // Destructs the current m_Stream and takes back ownership and management of the raw StreamInfo std::ifstream pointer.
340+
m_Stream.reset(m_StreamStack.top().Stream); // Destructs the current m_Stream and takes back ownership and management of the raw StreamInfo std::istream pointer.
315341
m_FilePath = m_StreamStack.top().FilePath;
316342
m_CurrentLine = m_StreamStack.top().CurrentLine;
317343
m_PreviousIndent = m_StreamStack.top().PreviousIndent;

System/Reader.h

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,16 @@ namespace RTE {
2626
/// <param name="progressCallback">A function pointer to a function that will be called and sent a string with information about the progress of this Reader's reading.</param>
2727
/// <param name="failOK">Whether it's ok for the file to not be there, ie we're only trying to open, and if it's not there, then fail silently.</param>
2828
/// <param name="nonModulePath">Whether this Reader is reading from path that is not a DataModule and should just read it as provided.</param>
29-
Reader(const std::string &fileName, bool overwrites = false, const ProgressCallback &progressCallback = nullptr, bool failOK = false, bool nonModulePath = false) { Clear(); m_NonModulePath = nonModulePath; Create(fileName, overwrites, progressCallback, failOK); }
29+
Reader(const std::string &fileName, bool overwrites = false, const ProgressCallback &progressCallback = nullptr, bool failOK = false, bool nonModulePath = false);
30+
31+
/// <summary>
32+
/// Constructor method used to instantiate a Reader object in system memory and make it ready for reading from the passed in file path.
33+
/// </summary>
34+
/// <param name="stream">Stream to read from.</param>
35+
/// <param name="overwrites">Whether object definitions read here overwrite existing ones with the same names.</param>
36+
/// <param name="progressCallback">A function pointer to a function that will be called and sent a string with information about the progress of this Reader's reading.</param>
37+
/// <param name="failOK">Whether it's ok for the file to not be there, ie we're only trying to open, and if it's not there, then fail silently.</param>
38+
Reader(std::unique_ptr<std::istream> &&stream, bool overwrites = false, const ProgressCallback &progressCallback = nullptr, bool failOK = false);
3039

3140
/// <summary>
3241
/// Makes the Reader object ready for use.
@@ -37,6 +46,16 @@ namespace RTE {
3746
/// <param name="failOK">Whether it's ok for the file to not be there, ie we're only trying to open, and if it's not there, then fail silently.</param>
3847
/// <returns>An error return value signaling success or any particular failure. Anything below 0 is an error signal.</returns>
3948
int Create(const std::string &fileName, bool overwrites = false, const ProgressCallback &progressCallback = nullptr, bool failOK = false);
49+
50+
/// <summary>
51+
/// Makes the Reader object ready for use.
52+
/// </summary>
53+
/// <param name="stream">Stream to read from.</param>
54+
/// <param name="overwrites"> Whether object definitions read here overwrite existing ones with the same names.</param>
55+
/// <param name="progressCallback">A function pointer to a function that will be called and sent a string with information about the progress of this Reader's reading.</param>
56+
/// <param name="failOK">Whether it's ok for the file to not be there, ie we're only trying to open, and if it's not there, then fail silently.</param>
57+
/// <returns>An error return value signaling success or any particular failure. Anything below 0 is an error signal.</returns>
58+
int Create(std::unique_ptr<std::istream> &&stream, bool overwrites = false, const ProgressCallback &progressCallback = nullptr, bool failOK = false);
4059
#pragma endregion
4160

4261
#pragma region Getters and Setters
@@ -152,7 +171,7 @@ namespace RTE {
152171
/// Shows whether this is still OK to read from. If file isn't present, etc, this will return false.
153172
/// </summary>
154173
/// <returns>Whether this Reader's stream is OK or not.</returns>
155-
bool ReaderOK() const { return m_Stream.get() && !m_Stream->fail() && m_Stream->is_open(); }
174+
bool ReaderOK() const { return m_Stream.get() && m_Stream->good(); }
156175

157176
/// <summary>
158177
/// Makes an error message box pop up for the user that tells them something went wrong with the reading, and where.
@@ -192,16 +211,16 @@ namespace RTE {
192211
/// <summary>
193212
/// Constructor method used to instantiate a StreamInfo object in system memory.
194213
/// </summary>
195-
StreamInfo(std::ifstream *stream, const std::string &filePath, int currentLine, int prevIndent) : Stream(stream), FilePath(filePath), CurrentLine(currentLine), PreviousIndent(prevIndent) {}
214+
StreamInfo(std::istream *stream, const std::string &filePath, int currentLine, int prevIndent) : Stream(stream), FilePath(filePath), CurrentLine(currentLine), PreviousIndent(prevIndent) {}
196215

197216
// NOTE: These members are owned by the reader that owns this struct, so are not deleted when this is destroyed.
198-
std::ifstream *Stream; //!< Currently used stream, is not on the StreamStack until a new stream is opened.
217+
std::istream *Stream; //!< Currently used stream, is not on the StreamStack until a new stream is opened.
199218
std::string FilePath; //!< Currently used stream's filepath.
200219
int CurrentLine; //!< The line number the stream is on.
201220
int PreviousIndent; //!< Count of tabs encountered on the last line DiscardEmptySpace() discarded.
202221
};
203222

204-
std::unique_ptr<std::ifstream> m_Stream; //!< Currently used stream, is not on the StreamStack until a new stream is opened.
223+
std::unique_ptr<std::istream> m_Stream; //!< Currently used stream, is not on the StreamStack until a new stream is opened.
205224
std::stack<StreamInfo> m_StreamStack; //!< Stack of open streams in this Reader, each one representing a file opened to read from within another.
206225
bool m_EndOfStreams; //!< All streams have been depleted.
207226

System/Writer.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ namespace RTE {
1313
m_IndentCount = 0;
1414
}
1515

16+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
17+
18+
Writer::Writer(const std::string &fileName, bool append, bool createDir) {
19+
Clear();
20+
Create(fileName, append, createDir);
21+
}
22+
23+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
24+
25+
Writer::Writer(std::unique_ptr<std::ostream> &&stream) {
26+
Clear();
27+
Create(std::move(stream));
28+
}
29+
1630
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1731

1832
int Writer::Create(const std::string &fileName, bool append, bool createDir) {
@@ -23,13 +37,21 @@ namespace RTE {
2337
m_FileName = m_FilePath.substr(slashPos + 1);
2438
m_FolderPath = m_FilePath.substr(0, slashPos + 1);
2539

26-
if (createDir && !std::filesystem::exists(System::GetWorkingDirectory() + m_FolderPath)) { System::MakeDirectory(System::GetWorkingDirectory() + m_FolderPath); }
40+
if (createDir && !std::filesystem::exists(System::GetWorkingDirectory() + m_FolderPath)) {
41+
System::MakeDirectory(System::GetWorkingDirectory() + m_FolderPath);
42+
}
43+
44+
return Create(std::make_unique<std::ofstream>(fileName, append ? (std::ios::out | std::ios::app | std::ios::ate) : (std::ios::out | std::ios::trunc)));
45+
}
2746

28-
m_Stream = std::make_unique<std::ofstream>(fileName, append ? (std::ios::out | std::ios::app | std::ios::ate) : (std::ios::out | std::ios::trunc));
47+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2948

49+
int Writer::Create(std::unique_ptr<std::ostream> &&stream) {
50+
m_Stream = std::move(stream);
3051
if (!m_Stream->good()) {
3152
return -1;
3253
}
54+
3355
return 0;
3456
}
3557

System/Writer.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ namespace RTE {
2222
/// <param name="filename">Path to the file to open for writing. If the directory doesn't exist the stream will fail to open.</param>
2323
/// <param name="append">Whether to append to the file if it exists, or to overwrite it.</param>
2424
/// <param name="createDir">Whether to create the directory path to the file name before attempting to open the stream, in case it doesn't exist.</param>
25-
Writer(const std::string &fileName, bool append = false, bool createDir = false) { Clear(); Create(fileName, append, createDir); }
25+
Writer(const std::string &fileName, bool append = false, bool createDir = false);
26+
27+
/// <summary>
28+
/// Constructor method used to instantiate a Writer object in system memory and make it ready for writing to the passed in file path.
29+
/// </summary>
30+
/// <param name="stream">Stream to write to.</param>
31+
Writer(std::unique_ptr<std::ostream> &&stream);
2632

2733
/// <summary>
2834
/// Makes the Writer object ready for use.
@@ -32,6 +38,13 @@ namespace RTE {
3238
/// <param name="createDir">Whether to create the directory path to the file name before attempting to open the stream, in case it doesn't exist.</param>
3339
/// <returns>An error return value signaling success or any particular failure. Anything below 0 is an error signal.</returns>
3440
int Create(const std::string &fileName, bool append = false, bool createDir = false);
41+
42+
/// <summary>
43+
/// Makes the Writer object ready for use.
44+
/// </summary>
45+
/// <param name="stream">Stream to write to.</param>
46+
/// <returns>An error return value signaling success or any particular failure. Anything below 0 is an error signal.</returns>
47+
int Create(std::unique_ptr<std::ostream> &&stream);
3548
#pragma endregion
3649

3750
#pragma region Getters
@@ -111,12 +124,12 @@ namespace RTE {
111124
/// Shows whether the writer is ready to start accepting data streamed to it.
112125
/// </summary>
113126
/// <returns>Whether the writer is ready to start accepting data streamed to it or not.</returns>
114-
bool WriterOK() const { return m_Stream.get() && !m_Stream->fail() && m_Stream->is_open(); }
127+
bool WriterOK() const { return m_Stream.get() && m_Stream->good(); }
115128

116129
/// <summary>
117130
/// Flushes and closes the output stream of this Writer. This happens automatically at destruction but needs to be called manually if a written file must be read from in the same scope.
118131
/// </summary>
119-
void EndWrite() const { m_Stream->flush(); m_Stream->close(); }
132+
void EndWrite() { m_Stream->flush(); m_Stream.reset(); }
120133
#pragma endregion
121134

122135
#pragma region Operator Overloads
@@ -144,7 +157,7 @@ namespace RTE {
144157

145158
protected:
146159

147-
std::unique_ptr<std::ofstream> m_Stream; //!< Stream used for writing to files.
160+
std::unique_ptr<std::ostream> m_Stream; //!< Stream used for writing.
148161
std::string m_FilePath; //!< Currently used stream's filepath.
149162
std::string m_FolderPath; //!< Only the path to the folder that we are writing a file in, excluding the filename.
150163
std::string m_FileName; //!< Only the name of the currently read file, excluding the path.

0 commit comments

Comments
 (0)