* 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
* \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<void(int)> cb);
protected:

View File

@ -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

View File

@ -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();}

View File

@ -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<short> 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;

View File

@ -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<short> 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));