2013-02-16 18:28:30 +00:00
|
|
|
// This file is part of libZelda.
|
|
|
|
//
|
|
|
|
// libZelda is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// libZelda is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with libZelda. If not, see <http://www.gnu.org/licenses/>
|
2013-01-27 20:13:09 +00:00
|
|
|
#ifndef __TEXTSTREAM_HPP__
|
|
|
|
#define __TEXTSTREAM_HPP__
|
2013-01-27 05:57:53 +00:00
|
|
|
|
|
|
|
#include "Stream.hpp"
|
|
|
|
#include <string>
|
2013-02-16 18:28:30 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
2013-07-21 03:57:20 +00:00
|
|
|
namespace zelda
|
|
|
|
{
|
|
|
|
namespace io
|
|
|
|
{
|
2013-02-16 18:28:30 +00:00
|
|
|
// 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.
|
|
|
|
*/
|
2013-01-27 05:57:53 +00:00
|
|
|
class TextStream : public Stream
|
|
|
|
{
|
2013-02-16 18:28:30 +00:00
|
|
|
public:
|
|
|
|
/*! \enum TextMode
|
|
|
|
* \brief Specifies how the file is opened.
|
|
|
|
*/
|
|
|
|
enum TextMode
|
|
|
|
{
|
2013-09-09 03:36:54 +00:00
|
|
|
Open = 0x01, //!< The file is opened if it exists.
|
|
|
|
Create = 0x02, //!< Create the file if it does not exist.
|
2013-02-16 18:28:30 +00:00
|
|
|
OpenOrCreate = Open|Create, //!< If the file does not exist when opening the file it is created
|
2013-09-09 03:36:54 +00:00
|
|
|
Truncate = 0x04, //!< All the data currently that is in the file is erased.
|
|
|
|
Append = 0x08 //!< After opening the file the current line is set to the end of the buffer
|
2013-02-16 18:28:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*! \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.
|
|
|
|
};
|
|
|
|
|
2013-09-09 03:36:54 +00:00
|
|
|
TextStream();
|
2013-02-16 18:28:30 +00:00
|
|
|
/*! \brief This constructor opens the file and loads all the lines. */
|
2013-09-09 03:36:54 +00:00
|
|
|
TextStream(const std::string& filename, Uint32 fileMode = Open, AccessMode accessMode = ReadWrite);
|
2013-02-16 18:28:30 +00:00
|
|
|
|
|
|
|
/*! \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);
|
|
|
|
|
|
|
|
/*! \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();
|
|
|
|
|
|
|
|
/*! \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);
|
|
|
|
|
|
|
|
/*! \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.
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*/
|
2013-07-21 03:57:20 +00:00
|
|
|
void setAccessMode(AccessMode mode);
|
2013-02-16 18:28:30 +00:00
|
|
|
|
|
|
|
/*! \brief Returns the AccessMode of the Stream.
|
|
|
|
*
|
|
|
|
* \return AccessModeThe mode to set.
|
|
|
|
*/
|
2013-07-21 03:57:20 +00:00
|
|
|
AccessMode accessMode() const;
|
2013-02-16 18:28:30 +00:00
|
|
|
|
2013-07-21 03:57:20 +00:00
|
|
|
/*! \brief Sets the Textmode of the Stream.
|
2013-02-16 18:28:30 +00:00
|
|
|
*
|
|
|
|
* \param mode The mode to set.
|
|
|
|
*/
|
2013-07-21 03:57:20 +00:00
|
|
|
void setTextMode(TextMode mode);
|
2013-02-16 18:28:30 +00:00
|
|
|
|
|
|
|
/*! \brief Returns the TextMode of the Stream.
|
|
|
|
*
|
|
|
|
* \return TextMode The mode to set.
|
|
|
|
*/
|
2013-09-09 03:36:54 +00:00
|
|
|
Uint32 textMode() const;
|
|
|
|
|
|
|
|
|
|
|
|
/*! \brief Empties the stream.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void truncate();
|
2013-02-16 18:28:30 +00:00
|
|
|
|
2013-07-21 03:57:20 +00:00
|
|
|
bool isOpenForReading() const;
|
|
|
|
bool isOpenForWriting() const;
|
2013-02-16 18:28:30 +00:00
|
|
|
private:
|
2013-01-27 16:49:48 +00:00
|
|
|
void loadLines();
|
2013-01-27 05:57:53 +00:00
|
|
|
std::string m_filename;
|
2013-09-09 03:36:54 +00:00
|
|
|
Uint32 m_textmode;
|
2013-02-16 18:28:30 +00:00
|
|
|
AccessMode m_accessmode;
|
|
|
|
|
|
|
|
std::vector<std::string> m_lines;
|
|
|
|
Uint32 m_currentLine;
|
2013-01-27 05:57:53 +00:00
|
|
|
Uint32 m_startLength;
|
|
|
|
};
|
2013-07-21 03:57:20 +00:00
|
|
|
} // io
|
|
|
|
} // zelda
|
2013-01-27 05:57:53 +00:00
|
|
|
#endif
|