* Add maxlen to readString

This commit is contained in:
Phillip Stephens 2015-01-28 22:20:57 -08:00
parent e868c31253
commit 39f7ed55fc
5 changed files with 34 additions and 13 deletions

View File

@ -267,14 +267,14 @@ public:
* \return std::string The value at the current address * \return std::string The value at the current address
* \throw IOException when address is out of range * \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 /*! \brief Reads a string and advances the position in the file
* *
* \return std::string The value at the current address * \return std::string The value at the current address
* \throw IOException when address is out of range * \throw IOException when address is out of range
*/ */
std::string readString(); std::string readString(atInt32 maxlen = -1);
void setProgressCallback(std::function<void(int)> cb); void setProgressCallback(std::function<void(int)> cb);
protected: protected:

View File

@ -60,9 +60,9 @@ public:
double readDouble(); double readDouble();
float readFloat(); float readFloat();
bool readBool(); bool readBool();
std::string readString(); std::string readString(atInt32 maxlen = -1);
std::string readUnicode(); std::string readUnicode(atInt32 maxlen = -1);
private: protected:
std::string m_filename; std::string m_filename;
FILE* m_fileHandle; FILE* m_fileHandle;
Endian m_endian; Endian m_endian;
@ -74,9 +74,9 @@ private:
} // Athena } // Athena
#ifndef FILEREADER_BASE #ifndef FILEREADER_BASE
#define FILEREADER_BASE \ #define FILEREADER_BASE() \
private: \ private: \
typedef Athena::io::FileReader base; typedef Athena::io::FileReader base
#endif // FILEREADER_BASE #endif // FILEREADER_BASE

View File

@ -56,8 +56,8 @@ protected:
virtual double readDouble() {THROW_NOT_IMPLEMENTED_EXCEPTION();} virtual double readDouble() {THROW_NOT_IMPLEMENTED_EXCEPTION();}
virtual float readFloat() {THROW_NOT_IMPLEMENTED_EXCEPTION();} virtual float readFloat() {THROW_NOT_IMPLEMENTED_EXCEPTION();}
virtual bool readBool() {THROW_NOT_IMPLEMENTED_EXCEPTION();} virtual bool readBool() {THROW_NOT_IMPLEMENTED_EXCEPTION();}
virtual std::string readUnicode(){THROW_NOT_IMPLEMENTED_EXCEPTION();} virtual std::string readUnicode(atInt32){THROW_NOT_IMPLEMENTED_EXCEPTION();}
virtual std::string readString() {THROW_NOT_IMPLEMENTED_EXCEPTION();} virtual std::string readString(atInt32) {THROW_NOT_IMPLEMENTED_EXCEPTION();}
// Writing // Writing
virtual void writeBit (bool){THROW_NOT_IMPLEMENTED_EXCEPTION();} virtual void writeBit (bool){THROW_NOT_IMPLEMENTED_EXCEPTION();}
virtual void writeUByte (atUint8){THROW_NOT_IMPLEMENTED_EXCEPTION();} virtual void writeUByte (atUint8){THROW_NOT_IMPLEMENTED_EXCEPTION();}

View File

@ -403,34 +403,45 @@ bool BinaryReader::readBool()
return ret; return ret;
} }
std::string BinaryReader::readUnicode() std::string BinaryReader::readUnicode(atInt32 maxlen)
{ {
if (!m_data) if (!m_data)
loadData(); loadData();
std::string ret; std::string ret;
std::vector<short> tmp; std::vector<short> tmp;
atUint16 chr = readUint16(); atUint16 chr = readUint16();
atInt32 i = 0;
for(;;) for(;;)
{ {
if (maxlen >= 0 && i >= maxlen - 1)
break;
if (!chr) if (!chr)
break; break;
tmp.push_back(chr); tmp.push_back(chr);
chr = readUint16(); chr = readUint16();
i++;
} }
utf8::utf16to8(tmp.begin(), tmp.end(), back_inserter(ret)); utf8::utf16to8(tmp.begin(), tmp.end(), back_inserter(ret));
return ret; return ret;
} }
std::string BinaryReader::readString() std::string BinaryReader::readString(atInt32 maxlen)
{ {
std::string ret = ""; std::string ret = "";
atUint8 chr = readByte(); atUint8 chr = readByte();
atInt32 i = 0;
while (chr != 0) while (chr != 0)
{ {
if (maxlen >= 0 && i >= maxlen - 1)
break;
ret += chr; ret += chr;
chr = readByte(); chr = readByte();
i++;
} }
return ret; return ret;

View File

@ -298,21 +298,26 @@ bool FileReader::readBool()
return (readByte() != 0); return (readByte() != 0);
} }
std::string FileReader::readString() std::string FileReader::readString(atInt32 maxlen)
{ {
std::string ret = ""; std::string ret = "";
atUint8 chr = readByte(); atUint8 chr = readByte();
atInt32 i = 0;
while (chr != 0) while (chr != 0)
{ {
if (maxlen >= 0 && i >= maxlen - 1)
break;
ret += chr; ret += chr;
chr = readByte(); chr = readByte();
i++;
} }
return ret; return ret;
} }
std::string FileReader::readUnicode() std::string FileReader::readUnicode(atInt32 maxlen)
{ {
if (!isOpen()) if (!isOpen())
THROW_INVALID_OPERATION_EXCEPTION("File not open for reading"); THROW_INVALID_OPERATION_EXCEPTION("File not open for reading");
@ -320,13 +325,18 @@ std::string FileReader::readUnicode()
std::string ret; std::string ret;
std::vector<short> tmp; std::vector<short> tmp;
atInt32 i = 0;
for(;;) for(;;)
{ {
if (maxlen >= 0 && i >= maxlen - 1)
break;
short chr = readUint16(); short chr = readUint16();
if (chr) if (chr)
tmp.push_back(chr); tmp.push_back(chr);
else else
break; break;
i++;
}; };
utf8::utf16to8(tmp.begin(), tmp.end(), back_inserter(ret)); utf8::utf16to8(tmp.begin(), tmp.end(), back_inserter(ret));