diff --git a/include/TextStream.hpp b/include/TextStream.hpp index bfacd06..ed1c554 100644 --- a/include/TextStream.hpp +++ b/include/TextStream.hpp @@ -85,6 +85,13 @@ public: */ std::string readLineAt(Uint32 line); + /*! \brief Writes a line at the given address. + * + * \param line The address to write to. + * \param str The string to write. + */ + void writeLineAt(Uint32 line, const std::string& str); + /*! \brief Sets the current line in the Stream. * * \param line The line to seek to. diff --git a/src/TextStream.cpp b/src/TextStream.cpp index c14d2b7..ac25de2 100644 --- a/src/TextStream.cpp +++ b/src/TextStream.cpp @@ -52,6 +52,8 @@ TextStream::TextStream(const std::string& filename, TextMode fileMode, AccessMod void TextStream::save(const std::string& filename) { + if (m_accessmode != WriteOnly || m_accessmode != ReadWrite) + throw InvalidOperationException("Stream not open for writing"); if (filename != std::string()) m_filename = filename; @@ -95,7 +97,7 @@ std::string TextStream::readLine() void TextStream::writeLine(const std::string& str) { if (m_accessmode != WriteOnly || m_accessmode != ReadWrite) - throw InvalidOperationException("Stream not open for reading"); + throw InvalidOperationException("Stream not open for writing"); else if (m_currentLine > m_lines.size()) { m_lines.push_back(str); @@ -109,7 +111,7 @@ void TextStream::writeLine(const std::string& str) void TextStream::writeLines(std::vector strings) { if (m_accessmode != WriteOnly || m_accessmode != ReadWrite) - throw InvalidOperationException("Stream not open for reading"); + throw InvalidOperationException("Stream not open for writing"); for (std::string s: strings) writeLine(s); @@ -144,6 +146,17 @@ std::string TextStream::readLineAt(Uint32 line) return m_lines[line - 1]; } +void TextStream::writeLineAt(Uint32 line, const std::string& line) +{ + if (m_accessmode != WriteOnly || m_accessmode != ReadWrite) + throw InvalidOperationException("Stream not open for reading"); + if (line <= 0) + throw InvalidOperationException("A line cannot be zero indexed"); + + m_currentLine = line; + writeLine(line); +} + std::vector TextStream::readAllLines() { if (m_accessmode != ReadOnly || m_accessmode != ReadWrite)