mirror of https://github.com/libAthena/athena.git
* Finished adding features to TextStream
* Finished Documentation of TextStream
This commit is contained in:
parent
f644c7d1a3
commit
2e53be3baa
|
@ -19,6 +19,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
/*! \class BinaryReader
|
/*! \class BinaryReader
|
||||||
|
* \brief A Stream class for reading binary data
|
||||||
*
|
*
|
||||||
* A Class for reading binary data from a file or memory stream,
|
* 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
|
* all work is done using a memory buffer, and not read directly from the disk
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
/*! \class BinaryWriter
|
/*! \class BinaryWriter
|
||||||
|
* \brief A Stream class for writing binary data
|
||||||
*
|
*
|
||||||
* A Class for writing binary data to a file or memory stream,
|
* 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
|
* all work is done using a memory buffer, and not written directly to the disk
|
||||||
|
|
|
@ -1,34 +1,132 @@
|
||||||
#ifndef __FILESTREAM_HPP__
|
#ifndef __TEXTSTREAM_HPP__
|
||||||
#define __FILESTREAM_HPP__
|
#define __TEXTSTREAM_HPP__
|
||||||
|
|
||||||
#include "Stream.hpp"
|
#include "Stream.hpp"
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
|
// 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.<br />
|
||||||
|
* It is currently incomplete, but usable, so use with caution.<br/>
|
||||||
|
* Since it's incomplete it may not behave exactly as expected.
|
||||||
|
*/
|
||||||
class TextStream : public Stream
|
class TextStream : public Stream
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
enum FileMode { Open, Create, OpenOrCreate = Open|Create, Truncate, Append };
|
/*! \enum TextMode
|
||||||
enum AccessMode { ReadOnly, WriteOnly, ReadWrite };
|
* \brief Specifies how the file is opened.
|
||||||
TextStream(const std::string& filename, FileMode fileMode = Open, AccessMode accessMode = ReadWrite);
|
*/
|
||||||
|
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 = "");
|
/*! \enum AccessMode
|
||||||
std::string readLine();
|
* \brief Specifies how the Stream can be interacted with.
|
||||||
void writeLine(const std::string& str);
|
*/
|
||||||
|
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<std::string> The lines read.
|
||||||
|
*/
|
||||||
std::vector<std::string> readLines(Uint32 numLines);
|
std::vector<std::string> readLines(Uint32 numLines);
|
||||||
void writeLines(std::vector<std::string> lines);
|
|
||||||
|
|
||||||
|
/*! \brief Reads a given list of lines relative to the current address
|
||||||
|
*
|
||||||
|
* \param lines The lines to write.
|
||||||
|
*/
|
||||||
|
void writeLines(std::vector<std::string> lines);
|
||||||
|
|
||||||
|
/*! \brief Reads all the lines in the current buffer.
|
||||||
|
*
|
||||||
|
* \return The lines read.
|
||||||
|
*/
|
||||||
std::vector<std::string> readAllLines();
|
std::vector<std::string> 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);
|
/*! \brief Sets the current line in the Stream.
|
||||||
Uint32 currentLine() const;
|
*
|
||||||
|
* \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:
|
private:
|
||||||
void loadLines();
|
void loadLines();
|
||||||
std::string m_filename;
|
std::string m_filename;
|
||||||
FileMode m_filemode;
|
TextMode m_textmode;
|
||||||
AccessMode m_accessmode;
|
AccessMode m_accessmode;
|
||||||
|
|
||||||
std::vector<std::string> m_lines;
|
std::vector<std::string> m_lines;
|
||||||
|
|
|
@ -4,9 +4,9 @@
|
||||||
#include "InvalidOperationException.hpp"
|
#include "InvalidOperationException.hpp"
|
||||||
#include "IOException.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_filename(filename),
|
||||||
m_filemode(fileMode),
|
m_textmode(fileMode),
|
||||||
m_accessmode(accessMode),
|
m_accessmode(accessMode),
|
||||||
m_currentLine(0)
|
m_currentLine(0)
|
||||||
{
|
{
|
||||||
|
@ -82,16 +82,20 @@ void TextStream::save(const std::string& filename)
|
||||||
|
|
||||||
std::string TextStream::readLine()
|
std::string TextStream::readLine()
|
||||||
{
|
{
|
||||||
|
if (m_accessmode != ReadOnly || m_accessmode != ReadWrite)
|
||||||
|
throw InvalidOperationException("Stream not open for reading");
|
||||||
|
|
||||||
if (m_currentLine > m_lines.size())
|
if (m_currentLine > m_lines.size())
|
||||||
throw IOException("Position past stream bounds");
|
throw IOException("Position past stream bounds");
|
||||||
|
|
||||||
return m_lines[m_currentLine++];
|
return m_lines[m_currentLine++];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void TextStream::writeLine(const std::string& str)
|
void TextStream::writeLine(const std::string& str)
|
||||||
{
|
{
|
||||||
if (m_currentLine > m_lines.size() && !m_autoResize)
|
if (m_accessmode != WriteOnly || m_accessmode != ReadWrite)
|
||||||
throw IOException("Position past stream bounds");
|
throw InvalidOperationException("Stream not open for reading");
|
||||||
else if (m_currentLine > m_lines.size())
|
else if (m_currentLine > m_lines.size())
|
||||||
{
|
{
|
||||||
m_lines.push_back(str);
|
m_lines.push_back(str);
|
||||||
|
@ -104,12 +108,21 @@ void TextStream::writeLine(const std::string& str)
|
||||||
|
|
||||||
void TextStream::writeLines(std::vector<std::string> strings)
|
void TextStream::writeLines(std::vector<std::string> strings)
|
||||||
{
|
{
|
||||||
|
if (m_accessmode != WriteOnly || m_accessmode != ReadWrite)
|
||||||
|
throw InvalidOperationException("Stream not open for reading");
|
||||||
|
|
||||||
for (std::string s: strings)
|
for (std::string s: strings)
|
||||||
writeLine(s);
|
writeLine(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> TextStream::readLines(Uint32 numLines)
|
std::vector<std::string> 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;
|
Uint32 currentLine = m_currentLine;
|
||||||
std::vector<std::string> ret;
|
std::vector<std::string> ret;
|
||||||
|
|
||||||
|
@ -121,14 +134,21 @@ std::vector<std::string> TextStream::readLines(Uint32 numLines)
|
||||||
|
|
||||||
std::string TextStream::readLineAt(Uint32 line)
|
std::string TextStream::readLineAt(Uint32 line)
|
||||||
{
|
{
|
||||||
|
if (m_accessmode != ReadOnly || m_accessmode != ReadWrite)
|
||||||
|
throw InvalidOperationException("Stream not open for reading");
|
||||||
if (line <= 0)
|
if (line <= 0)
|
||||||
throw InvalidOperationException("A line cannot be zero indexed");
|
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];
|
return m_lines[line - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> TextStream::readAllLines()
|
std::vector<std::string> TextStream::readAllLines()
|
||||||
{
|
{
|
||||||
|
if (m_accessmode != ReadOnly || m_accessmode != ReadWrite)
|
||||||
|
throw InvalidOperationException("Stream not open for reading");
|
||||||
|
|
||||||
return m_lines;
|
return m_lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,6 +164,27 @@ Uint32 TextStream::currentLine() const
|
||||||
return m_currentLine + 1;
|
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()
|
void TextStream::loadLines()
|
||||||
{
|
{
|
||||||
while (!atEnd())
|
while (!atEnd())
|
||||||
|
|
Loading…
Reference in New Issue