mirror of https://github.com/libAthena/athena.git
* Changed how TextStream works; It's now less derpish and doesn't
produce a corrupted buffer everytime you edit a line.
This commit is contained in:
parent
3c8b493014
commit
f644c7d1a3
|
@ -21,9 +21,7 @@
|
||||||
/*! \class FileNotFoundException
|
/*! \class FileNotFoundException
|
||||||
* \brief An excpeption thrown when a file could not be found at the given path.
|
* \brief An excpeption thrown when a file could not be found at the given path.
|
||||||
*
|
*
|
||||||
* This should only be thrown when the library tries to write to a buffer
|
* This should only be thrown when the Stream is unable to open a file.<br />
|
||||||
* e.g when the position is greater than the position and the stream
|
|
||||||
* is not set to autoresize.<br />
|
|
||||||
* <br />
|
* <br />
|
||||||
* It is <b>NOT</b> appropriate to use <b>throw new</b> so avoid doing so,
|
* It is <b>NOT</b> appropriate to use <b>throw new</b> so avoid doing so,
|
||||||
* keeping things on the stack as much as possible is very important for speed.
|
* keeping things on the stack as much as possible is very important for speed.
|
||||||
|
|
|
@ -1,21 +0,0 @@
|
||||||
#ifndef __FILESTREAM_HPP__
|
|
||||||
#define __FILESTREAM_HPP__
|
|
||||||
|
|
||||||
#include "Stream.hpp"
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
class FileStream : public Stream
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
enum FileMode { Open, Create, OpenOrCreate = Open|Create, Truncate, Append };
|
|
||||||
enum AccessMode { ReadOnly, WriteOnly, ReadWrite };
|
|
||||||
FileStream(const std::string& filename, FileMode fileMode, AccessMode accessMode);
|
|
||||||
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::string m_filename;
|
|
||||||
FileMode m_filemode;
|
|
||||||
AccessMode m_accessmode;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -25,10 +25,13 @@ public:
|
||||||
|
|
||||||
void setCurrentLine(Uint32 line);
|
void setCurrentLine(Uint32 line);
|
||||||
Uint32 currentLine() const;
|
Uint32 currentLine() const;
|
||||||
private:
|
private:
|
||||||
|
void loadLines();
|
||||||
std::string m_filename;
|
std::string m_filename;
|
||||||
FileMode m_filemode;
|
FileMode m_filemode;
|
||||||
AccessMode m_accessmode;
|
AccessMode m_accessmode;
|
||||||
|
|
||||||
|
std::vector<std::string> m_lines;
|
||||||
Uint32 m_currentLine;
|
Uint32 m_currentLine;
|
||||||
Uint32 m_startLength;
|
Uint32 m_startLength;
|
||||||
};
|
};
|
||||||
|
|
|
@ -75,6 +75,7 @@
|
||||||
<Unit filename="include/IOException.hpp" />
|
<Unit filename="include/IOException.hpp" />
|
||||||
<Unit filename="include/InvalidOperationException.hpp" />
|
<Unit filename="include/InvalidOperationException.hpp" />
|
||||||
<Unit filename="include/Stream.hpp" />
|
<Unit filename="include/Stream.hpp" />
|
||||||
|
<Unit filename="include/TextStream.hpp" />
|
||||||
<Unit filename="include/Types.hpp" />
|
<Unit filename="include/Types.hpp" />
|
||||||
<Unit filename="include/utf8.h" />
|
<Unit filename="include/utf8.h" />
|
||||||
<Unit filename="include/utf8/checked.h" />
|
<Unit filename="include/utf8/checked.h" />
|
||||||
|
@ -84,6 +85,7 @@
|
||||||
<Unit filename="src/BinaryReader.cpp" />
|
<Unit filename="src/BinaryReader.cpp" />
|
||||||
<Unit filename="src/BinaryWriter.cpp" />
|
<Unit filename="src/BinaryWriter.cpp" />
|
||||||
<Unit filename="src/Stream.cpp" />
|
<Unit filename="src/Stream.cpp" />
|
||||||
|
<Unit filename="src/TextStream.cpp" />
|
||||||
<Unit filename="src/utility.cpp" />
|
<Unit filename="src/utility.cpp" />
|
||||||
<Extensions>
|
<Extensions>
|
||||||
<code_completion />
|
<code_completion />
|
||||||
|
|
|
@ -1,33 +0,0 @@
|
||||||
#include "FileStream.hpp"
|
|
||||||
#include <stdio.h>
|
|
||||||
#include "FileNotFoundException.hpp"
|
|
||||||
#include "IOException.hpp"
|
|
||||||
|
|
||||||
FileStream::FileStream(const std::string& filename, FileMode fileMode, AccessMode accessMode) :
|
|
||||||
m_filename(filename),
|
|
||||||
m_filemode(fileMode),
|
|
||||||
m_accessmode(accessMode)
|
|
||||||
{
|
|
||||||
FILE* in;
|
|
||||||
int length;
|
|
||||||
in = fopen(filename.c_str(), "rb");
|
|
||||||
|
|
||||||
if (!in)
|
|
||||||
{
|
|
||||||
if((fileMode & Create) != Create)
|
|
||||||
throw FileNotFoundException(filename.c_str());
|
|
||||||
|
|
||||||
in = fopen(filename.c_str(), "w+b");
|
|
||||||
}
|
|
||||||
|
|
||||||
fseek(in, 0, SEEK_END);
|
|
||||||
length = ftell(in);
|
|
||||||
fseek(in, 0, SEEK_SET);
|
|
||||||
m_data = new Uint8[length];
|
|
||||||
|
|
||||||
fread(m_data, 1, length, in);
|
|
||||||
fclose(in);
|
|
||||||
printf("%i\n", length);
|
|
||||||
m_length = length;
|
|
||||||
m_position = 0;
|
|
||||||
}
|
|
|
@ -161,7 +161,7 @@ Int8 Stream::readByte()
|
||||||
m_position += sizeof(Uint8);
|
m_position += sizeof(Uint8);
|
||||||
}
|
}
|
||||||
if (m_position + 1 > m_length)
|
if (m_position + 1 > m_length)
|
||||||
throw IOException("Position passed stream bounds: " + m_position);
|
throw IOException("Position passed stream bounds");
|
||||||
|
|
||||||
return *(Int8*)(m_data + m_position++);
|
return *(Int8*)(m_data + m_position++);
|
||||||
}
|
}
|
||||||
|
@ -228,7 +228,7 @@ void Stream::seek(Int64 position, SeekOrigin origin)
|
||||||
void Stream::resize(Uint64 newSize)
|
void Stream::resize(Uint64 newSize)
|
||||||
{
|
{
|
||||||
if (newSize < m_length)
|
if (newSize < m_length)
|
||||||
throw InvalidOperationException("Stream:Resize() -> New size cannot be less to the old size.");
|
throw InvalidOperationException("Stream::Resize() -> New size cannot be less to the old size.");
|
||||||
|
|
||||||
// Allocate and copy new buffer
|
// Allocate and copy new buffer
|
||||||
Uint8* newArray = new Uint8[newSize];
|
Uint8* newArray = new Uint8[newSize];
|
||||||
|
@ -274,7 +274,7 @@ Int64 Stream::position()
|
||||||
|
|
||||||
bool Stream::atEnd()
|
bool Stream::atEnd()
|
||||||
{
|
{
|
||||||
return m_position == m_length;
|
return m_position >= m_length;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Stream::setAutoResizing(bool val)
|
void Stream::setAutoResizing(bool val)
|
||||||
|
|
|
@ -45,7 +45,9 @@ TextStream::TextStream(const std::string& filename, FileMode fileMode, AccessMod
|
||||||
m_position = 0;
|
m_position = 0;
|
||||||
|
|
||||||
m_length = length;
|
m_length = length;
|
||||||
m_startLength = length;
|
m_startLength = length;
|
||||||
|
|
||||||
|
loadLines();
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextStream::save(const std::string& filename)
|
void TextStream::save(const std::string& filename)
|
||||||
|
@ -53,6 +55,20 @@ void TextStream::save(const std::string& filename)
|
||||||
if (filename != std::string())
|
if (filename != std::string())
|
||||||
m_filename = filename;
|
m_filename = filename;
|
||||||
|
|
||||||
|
// We need a new buffer to write the new lines
|
||||||
|
if (m_data)
|
||||||
|
delete[] m_data;
|
||||||
|
|
||||||
|
m_position = 0;
|
||||||
|
m_length = 1;
|
||||||
|
|
||||||
|
// Set the new buffer
|
||||||
|
m_data = new Uint8[m_length];
|
||||||
|
|
||||||
|
// Now write all the strings to the new buffer
|
||||||
|
for (std::string s : m_lines)
|
||||||
|
writeBytes((Int8*)s.c_str(), s.size());
|
||||||
|
|
||||||
FILE* out = fopen(m_filename.c_str(), "wb");
|
FILE* out = fopen(m_filename.c_str(), "wb");
|
||||||
|
|
||||||
if(out)
|
if(out)
|
||||||
|
@ -63,43 +79,27 @@ void TextStream::save(const std::string& filename)
|
||||||
else
|
else
|
||||||
throw FileNotFoundException(m_filename);
|
throw FileNotFoundException(m_filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string TextStream::readLine()
|
std::string TextStream::readLine()
|
||||||
{
|
{
|
||||||
std::string ret;
|
if (m_currentLine > m_lines.size())
|
||||||
Uint8 c;
|
throw IOException("Position past stream bounds");
|
||||||
for (;;)
|
return m_lines[m_currentLine++];
|
||||||
{
|
|
||||||
c = readByte();
|
|
||||||
|
|
||||||
if (c == '\r' || c == '\n')
|
|
||||||
{
|
|
||||||
m_currentLine++;
|
|
||||||
ret.push_back(c);
|
|
||||||
if (*(Uint8*)(m_data + m_position + 1) == '\n')
|
|
||||||
{
|
|
||||||
ret.push_back('\n');
|
|
||||||
m_currentLine++;
|
|
||||||
m_position++; // advance position past the new line character
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c == '\0')
|
|
||||||
{
|
|
||||||
ret.push_back('\n');
|
|
||||||
m_currentLine++;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
ret.push_back(c);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void TextStream::writeLine(const std::string& str)
|
void TextStream::writeLine(const std::string& str)
|
||||||
{
|
{
|
||||||
this->writeBytes((Int8*)str.c_str(), str.size());
|
if (m_currentLine > m_lines.size() && !m_autoResize)
|
||||||
m_currentLine++;
|
throw IOException("Position past stream bounds");
|
||||||
|
else if (m_currentLine > m_lines.size())
|
||||||
|
{
|
||||||
|
m_lines.push_back(str);
|
||||||
|
m_currentLine++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_lines[m_currentLine++] = str;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextStream::writeLines(std::vector<std::string> strings)
|
void TextStream::writeLines(std::vector<std::string> strings)
|
||||||
|
@ -110,14 +110,11 @@ void TextStream::writeLines(std::vector<std::string> strings)
|
||||||
|
|
||||||
std::vector<std::string> TextStream::readLines(Uint32 numLines)
|
std::vector<std::string> TextStream::readLines(Uint32 numLines)
|
||||||
{
|
{
|
||||||
m_position = 0;
|
Uint32 currentLine = m_currentLine;
|
||||||
m_currentLine = 0;
|
|
||||||
std::vector<std::string> ret;
|
std::vector<std::string> ret;
|
||||||
|
|
||||||
while ((numLines--) > 0)
|
while ((m_currentLine++) <= currentLine + numLines)
|
||||||
{
|
ret.push_back(m_lines[m_currentLine]);
|
||||||
ret.push_back(readLine());
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -127,44 +124,56 @@ std::string TextStream::readLineAt(Uint32 line)
|
||||||
if (line <= 0)
|
if (line <= 0)
|
||||||
throw InvalidOperationException("A line cannot be zero indexed");
|
throw InvalidOperationException("A line cannot be zero indexed");
|
||||||
|
|
||||||
m_position = 0;
|
return m_lines[line - 1];
|
||||||
m_currentLine = 0;
|
|
||||||
while (m_currentLine < line - 1)
|
|
||||||
{
|
|
||||||
readLine();
|
|
||||||
}
|
|
||||||
|
|
||||||
return readLine();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> TextStream::readAllLines()
|
std::vector<std::string> TextStream::readAllLines()
|
||||||
{
|
{
|
||||||
m_position = 0;
|
return m_lines;
|
||||||
m_currentLine = 0;
|
|
||||||
std::vector<std::string> ret;
|
|
||||||
|
|
||||||
while (!atEnd())
|
|
||||||
{
|
|
||||||
ret.push_back(readLine());
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextStream::setCurrentLine(Uint32 line)
|
void TextStream::setCurrentLine(Uint32 line)
|
||||||
{
|
{
|
||||||
if (line <= 0)
|
if (line <= 0)
|
||||||
throw InvalidOperationException("A line cannot be zero indexed");
|
throw InvalidOperationException("A line cannot be zero indexed");
|
||||||
m_currentLine = 0;
|
m_currentLine = line - 1;
|
||||||
m_position = 0;
|
|
||||||
|
|
||||||
while(m_currentLine != line - 1)
|
|
||||||
{
|
|
||||||
readLine();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Uint32 TextStream::currentLine() const
|
Uint32 TextStream::currentLine() const
|
||||||
{
|
{
|
||||||
return m_currentLine;
|
return m_currentLine + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextStream::loadLines()
|
||||||
|
{
|
||||||
|
while (!atEnd())
|
||||||
|
{
|
||||||
|
std::string line;
|
||||||
|
Uint8 c;
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
c = readByte();
|
||||||
|
|
||||||
|
if (c == '\r' || c == '\n')
|
||||||
|
{
|
||||||
|
m_currentLine++;
|
||||||
|
line.push_back(c);
|
||||||
|
if (*(Uint8*)(m_data + m_position + 1) == '\n')
|
||||||
|
{
|
||||||
|
line.push_back('\n');
|
||||||
|
m_position++; // advance position past the new line character
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c == '\0')
|
||||||
|
{
|
||||||
|
line.push_back('\n');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
line.push_back(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
m_lines.push_back(line);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue