From 2e53be3baa37068b5b089c793fa0996746ef2052 Mon Sep 17 00:00:00 2001 From: Antidote Date: Sun, 27 Jan 2013 12:13:09 -0800 Subject: [PATCH] * Finished adding features to TextStream * Finished Documentation of TextStream --- include/BinaryReader.hpp | 1 + include/BinaryWriter.hpp | 1 + include/TextStream.hpp | 128 ++++++++++++++++++++++++++++++++++----- src/TextStream.cpp | 49 +++++++++++++-- 4 files changed, 160 insertions(+), 19 deletions(-) diff --git a/include/BinaryReader.hpp b/include/BinaryReader.hpp index 3c7f2ff..460d3d9 100644 --- a/include/BinaryReader.hpp +++ b/include/BinaryReader.hpp @@ -19,6 +19,7 @@ #include /*! \class BinaryReader + * \brief A Stream class for reading binary data * * A Class for reading binary data from a file or memory stream, * all work is done using a memory buffer, and not read directly from the disk diff --git a/include/BinaryWriter.hpp b/include/BinaryWriter.hpp index 06b7913..8e37ab5 100644 --- a/include/BinaryWriter.hpp +++ b/include/BinaryWriter.hpp @@ -19,6 +19,7 @@ #include /*! \class BinaryWriter + * \brief A Stream class for writing binary data * * A Class for writing binary data to a file or memory stream, * all work is done using a memory buffer, and not written directly to the disk diff --git a/include/TextStream.hpp b/include/TextStream.hpp index 374bb6f..bfacd06 100644 --- a/include/TextStream.hpp +++ b/include/TextStream.hpp @@ -1,34 +1,132 @@ -#ifndef __FILESTREAM_HPP__ -#define __FILESTREAM_HPP__ +#ifndef __TEXTSTREAM_HPP__ +#define __TEXTSTREAM_HPP__ #include "Stream.hpp" #include #include - + + +// TODO (Phil#1#): Need to actually use AccessMode +/*! \class TextStream + * \brief A Class for reading or writing Text data. + * + * TextStream is a class for reading/writing TextData.
+ * It is currently incomplete, but usable, so use with caution.
+ * Since it's incomplete it may not behave exactly as expected. + */ class TextStream : public Stream { -public: - enum FileMode { Open, Create, OpenOrCreate = Open|Create, Truncate, Append }; - enum AccessMode { ReadOnly, WriteOnly, ReadWrite }; - TextStream(const std::string& filename, FileMode fileMode = Open, AccessMode accessMode = ReadWrite); +public: + /*! \enum TextMode + * \brief Specifies how the file is opened. + */ + enum TextMode + { + Open, //!< The file is opened if it exists. + Create, //!< Create the file if it does not exist. + OpenOrCreate = Open|Create, //!< If the file does not exist when opening the file it is created + Truncate, //!< All the data currently that is in the file is erased. + Append //!< After opening the file the current line is set to the end of the buffer + }; - void save(const std::string& filename = ""); - std::string readLine(); - void writeLine(const std::string& str); + /*! \enum AccessMode + * \brief Specifies how the Stream can be interacted with. + */ + enum AccessMode + { + ReadOnly, //!< The Stream can only be read from. + WriteOnly,//!< The Stream can only be written to. + ReadWrite //!< The Stream can be read from or written to. + }; + /*! \brief This constructor opens the file and loads all the lines. */ + TextStream(const std::string& filename, TextMode fileMode = Open, AccessMode accessMode = ReadWrite); + + /*! \brief Creates a new buffer and saves all lines to the specified file. + * \param filename The file, including path to save to. + */ + void save(const std::string& filename = ""); + + /*! \brief Reads the line at the current address. + * + * \return std::string The line read. + */ + std::string readLine(); + + /*! \brief Writes a line to the buffer + * + * \param str The string to write. + */ + void writeLine(const std::string& str); + + /*! \brief Reads a given amount of lines relative to the current address + * + * \param numLines The amount of lines to read. + * \return std::vector The lines read. + */ std::vector readLines(Uint32 numLines); - void writeLines(std::vector lines); + /*! \brief Reads a given list of lines relative to the current address + * + * \param lines The lines to write. + */ + void writeLines(std::vector lines); + + /*! \brief Reads all the lines in the current buffer. + * + * \return The lines read. + */ std::vector readAllLines(); - std::string readLineAt(Uint32 line); + /*! \brief Reads a line at the given address. + * + * \param line The line to read. + * \return std::string The lines read. + */ + std::string readLineAt(Uint32 line); - void setCurrentLine(Uint32 line); - Uint32 currentLine() const; + /*! \brief Sets the current line in the Stream. + * + * \param line The line to seek to. + */ + void setCurrentLine(Uint32 line); + + /*! \brief Returns the current line in the stream. + * + * \return Uint32 The current line in the stream. + */ + Uint32 currentLine() const; + + /*! \brief Sets the AccessMode of the Stream. + * + * \param mode The mode to set. + */ + void setAccessMode(AccessMode mode); + + /*! \brief Returns the AccessMode of the Stream. + * + * \return AccessModeThe mode to set. + */ + AccessMode accessMode() const; + + /*! \brief Sets the Textmode of the Stream. + * + * \param mode The mode to set. + */ + void setTextMode(TextMode mode); + + /*! \brief Returns the TextMode of the Stream. + * + * \return TextMode The mode to set. + */ + TextMode textMode() const; + + bool isOpenForReading() const; + bool isOpenForWriting() const; private: void loadLines(); std::string m_filename; - FileMode m_filemode; + TextMode m_textmode; AccessMode m_accessmode; std::vector m_lines; diff --git a/src/TextStream.cpp b/src/TextStream.cpp index cb6afc5..c14d2b7 100644 --- a/src/TextStream.cpp +++ b/src/TextStream.cpp @@ -4,9 +4,9 @@ #include "InvalidOperationException.hpp" #include "IOException.hpp" -TextStream::TextStream(const std::string& filename, FileMode fileMode, AccessMode accessMode) : +TextStream::TextStream(const std::string& filename, TextMode fileMode, AccessMode accessMode) : m_filename(filename), - m_filemode(fileMode), + m_textmode(fileMode), m_accessmode(accessMode), m_currentLine(0) { @@ -82,16 +82,20 @@ void TextStream::save(const std::string& filename) std::string TextStream::readLine() { + if (m_accessmode != ReadOnly || m_accessmode != ReadWrite) + throw InvalidOperationException("Stream not open for reading"); + if (m_currentLine > m_lines.size()) throw IOException("Position past stream bounds"); + return m_lines[m_currentLine++]; } void TextStream::writeLine(const std::string& str) { - if (m_currentLine > m_lines.size() && !m_autoResize) - throw IOException("Position past stream bounds"); + if (m_accessmode != WriteOnly || m_accessmode != ReadWrite) + throw InvalidOperationException("Stream not open for reading"); else if (m_currentLine > m_lines.size()) { m_lines.push_back(str); @@ -104,12 +108,21 @@ 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"); + for (std::string s: strings) writeLine(s); } std::vector TextStream::readLines(Uint32 numLines) { + if (m_accessmode != ReadOnly || m_accessmode != ReadWrite) + throw InvalidOperationException("Stream not open for reading"); + + if (numLines > m_lines.size()) + throw InvalidOperationException("numLines exceeds the number of stored strings."); + Uint32 currentLine = m_currentLine; std::vector ret; @@ -121,14 +134,21 @@ std::vector TextStream::readLines(Uint32 numLines) std::string TextStream::readLineAt(Uint32 line) { + if (m_accessmode != ReadOnly || m_accessmode != ReadWrite) + throw InvalidOperationException("Stream not open for reading"); if (line <= 0) throw InvalidOperationException("A line cannot be zero indexed"); + if ((line - 1) >= m_lines.size()) + throw IOException("Line index out of range"); return m_lines[line - 1]; } std::vector TextStream::readAllLines() { + if (m_accessmode != ReadOnly || m_accessmode != ReadWrite) + throw InvalidOperationException("Stream not open for reading"); + return m_lines; } @@ -144,6 +164,27 @@ Uint32 TextStream::currentLine() const return m_currentLine + 1; } +void TextStream::setAccessMode(AccessMode mode) +{ + m_accessmode = mode; +} + +TextStream::AccessMode TextStream::accessMode() const +{ + return m_accessmode; +} + +void TextStream::setTextMode(TextMode mode) +{ + m_textmode = mode; +} + +TextStream::TextMode TextStream::textMode() const +{ + return m_textmode; +} + +// PRIVATE FUNCTIONS void TextStream::loadLines() { while (!atEnd())