From 39f7ed55fc6cb2bcf17639b196de1510a71513fa Mon Sep 17 00:00:00 2001 From: Phillip Stephens Date: Wed, 28 Jan 2015 22:20:57 -0800 Subject: [PATCH] * Add maxlen to readString --- include/Athena/BinaryReader.hpp | 4 ++-- include/Athena/FileReader.hpp | 10 +++++----- include/Athena/Stream.hpp | 4 ++-- src/Athena/BinaryReader.cpp | 15 +++++++++++++-- src/Athena/FileReader.cpp | 14 ++++++++++++-- 5 files changed, 34 insertions(+), 13 deletions(-) diff --git a/include/Athena/BinaryReader.hpp b/include/Athena/BinaryReader.hpp index b1f8255..e12c6f6 100644 --- a/include/Athena/BinaryReader.hpp +++ b/include/Athena/BinaryReader.hpp @@ -267,14 +267,14 @@ public: * \return std::string The value at the current address * \throw IOException when address is out of range */ - std::string readUnicode(); + std::string readUnicode(atInt32 maxlen = -1); /*! \brief Reads a string and advances the position in the file * * \return std::string The value at the current address * \throw IOException when address is out of range */ - std::string readString(); + std::string readString(atInt32 maxlen = -1); void setProgressCallback(std::function cb); protected: diff --git a/include/Athena/FileReader.hpp b/include/Athena/FileReader.hpp index 2b5d09e..d99111a 100644 --- a/include/Athena/FileReader.hpp +++ b/include/Athena/FileReader.hpp @@ -60,9 +60,9 @@ public: double readDouble(); float readFloat(); bool readBool(); - std::string readString(); - std::string readUnicode(); -private: + std::string readString(atInt32 maxlen = -1); + std::string readUnicode(atInt32 maxlen = -1); +protected: std::string m_filename; FILE* m_fileHandle; Endian m_endian; @@ -74,9 +74,9 @@ private: } // Athena #ifndef FILEREADER_BASE -#define FILEREADER_BASE \ +#define FILEREADER_BASE() \ private: \ - typedef Athena::io::FileReader base; + typedef Athena::io::FileReader base #endif // FILEREADER_BASE diff --git a/include/Athena/Stream.hpp b/include/Athena/Stream.hpp index 3c3a9c4..a7cc069 100644 --- a/include/Athena/Stream.hpp +++ b/include/Athena/Stream.hpp @@ -56,8 +56,8 @@ protected: virtual double readDouble() {THROW_NOT_IMPLEMENTED_EXCEPTION();} virtual float readFloat() {THROW_NOT_IMPLEMENTED_EXCEPTION();} virtual bool readBool() {THROW_NOT_IMPLEMENTED_EXCEPTION();} - virtual std::string readUnicode(){THROW_NOT_IMPLEMENTED_EXCEPTION();} - virtual std::string readString() {THROW_NOT_IMPLEMENTED_EXCEPTION();} + virtual std::string readUnicode(atInt32){THROW_NOT_IMPLEMENTED_EXCEPTION();} + virtual std::string readString(atInt32) {THROW_NOT_IMPLEMENTED_EXCEPTION();} // Writing virtual void writeBit (bool){THROW_NOT_IMPLEMENTED_EXCEPTION();} virtual void writeUByte (atUint8){THROW_NOT_IMPLEMENTED_EXCEPTION();} diff --git a/src/Athena/BinaryReader.cpp b/src/Athena/BinaryReader.cpp index 84bf950..76f155c 100644 --- a/src/Athena/BinaryReader.cpp +++ b/src/Athena/BinaryReader.cpp @@ -403,34 +403,45 @@ bool BinaryReader::readBool() return ret; } -std::string BinaryReader::readUnicode() +std::string BinaryReader::readUnicode(atInt32 maxlen) { if (!m_data) loadData(); std::string ret; std::vector tmp; atUint16 chr = readUint16(); + + atInt32 i = 0; for(;;) { + if (maxlen >= 0 && i >= maxlen - 1) + break; + if (!chr) break; tmp.push_back(chr); chr = readUint16(); + i++; } utf8::utf16to8(tmp.begin(), tmp.end(), back_inserter(ret)); return ret; } -std::string BinaryReader::readString() +std::string BinaryReader::readString(atInt32 maxlen) { std::string ret = ""; atUint8 chr = readByte(); + atInt32 i = 0; while (chr != 0) { + if (maxlen >= 0 && i >= maxlen - 1) + break; + ret += chr; chr = readByte(); + i++; } return ret; diff --git a/src/Athena/FileReader.cpp b/src/Athena/FileReader.cpp index 5b7fd09..fc4ee00 100644 --- a/src/Athena/FileReader.cpp +++ b/src/Athena/FileReader.cpp @@ -298,21 +298,26 @@ bool FileReader::readBool() return (readByte() != 0); } -std::string FileReader::readString() +std::string FileReader::readString(atInt32 maxlen) { std::string ret = ""; atUint8 chr = readByte(); + atInt32 i = 0; while (chr != 0) { + if (maxlen >= 0 && i >= maxlen - 1) + break; + ret += chr; chr = readByte(); + i++; } return ret; } -std::string FileReader::readUnicode() +std::string FileReader::readUnicode(atInt32 maxlen) { if (!isOpen()) THROW_INVALID_OPERATION_EXCEPTION("File not open for reading"); @@ -320,13 +325,18 @@ std::string FileReader::readUnicode() std::string ret; std::vector tmp; + atInt32 i = 0; for(;;) { + if (maxlen >= 0 && i >= maxlen - 1) + break; + short chr = readUint16(); if (chr) tmp.push_back(chr); else break; + i++; }; utf8::utf16to8(tmp.begin(), tmp.end(), back_inserter(ret));