From e641dbbe4b5f3106f8ab506a8192139bdc9e42be Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Fri, 19 Jun 2015 10:40:59 -1000 Subject: [PATCH] added seek directive to fixed-length string reads --- PKGBUILD | 2 +- atdna/PKGBUILD | 2 +- src/Athena/FileReader.cpp | 47 +++++++++++++++++++------------------ src/Athena/MemoryReader.cpp | 39 ++++++++++++++++-------------- 4 files changed, 47 insertions(+), 43 deletions(-) diff --git a/PKGBUILD b/PKGBUILD index 0a7b4c5..fc7f066 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -1,7 +1,7 @@ # PKGBUILD for libAthena _pkgname=libathena pkgname=$_pkgname-git -pkgver=1.1.0.35.gfd3db3e +pkgver=1.1.0.37.g3dfb001 pkgrel=1 pkgdesc="Basic cross platform IO library" arch=('i686' 'x86_64') diff --git a/atdna/PKGBUILD b/atdna/PKGBUILD index 02becd7..613e9db 100644 --- a/atdna/PKGBUILD +++ b/atdna/PKGBUILD @@ -1,7 +1,7 @@ # PKGBUILD for atdna _pkgname=atdna pkgname=$_pkgname-git -pkgver=1.1.0.36.g242a069 +pkgver=1.1.0.37.g3dfb001 pkgrel=1 pkgdesc="Companion DNA utility for libAthena" arch=('i686' 'x86_64') diff --git a/src/Athena/FileReader.cpp b/src/Athena/FileReader.cpp index 1e8fbb0..f45e41e 100644 --- a/src/Athena/FileReader.cpp +++ b/src/Athena/FileReader.cpp @@ -340,71 +340,72 @@ atVec4f FileReader::readVec4f() return val; } -std::string FileReader::readString(atInt32 maxlen) +std::string FileReader::readString(atInt32 fixedLen) { std::string ret; + atUint8 chr = readByte(); - - atInt32 i = 0; - - while (chr != 0) + atInt32 i; + for (i = 0 ; chr != 0 ; ++i) { - if (maxlen >= 0 && i >= maxlen - 1) + if (fixedLen >= 0 && i >= fixedLen - 1) break; ret += chr; chr = readByte(); - i++; } + if (fixedLen >= 0 && i < fixedLen) + seek(fixedLen - i); + return ret; } -std::wstring FileReader::readWString(atInt32 maxlen) +std::wstring FileReader::readWString(atInt32 fixedLen) { std::wstring ret; atUint16 chr = readUint16(); - atInt32 i = 0; - - while (chr != 0) + atInt32 i; + for (i = 0 ; chr != 0 ; ++i) { - if (maxlen >= 0 && i >= maxlen - 1) + if (fixedLen >= 0 && i >= fixedLen - 1) break; ret += chr; chr = readUint16(); - i++; } + if (fixedLen >= 0 && i < fixedLen) + seek(fixedLen - i); + return ret; } -std::string FileReader::readUnicode(atInt32 maxlen) +std::string FileReader::readUnicode(atInt32 fixedLen) { if (!isOpen()) THROW_INVALID_OPERATION_EXCEPTION_RETURN(std::string(), "File not open for reading"); std::string ret; - std::vector tmp; - - atInt32 i = 0; - - for (;;) + std::vector tmp; + atInt32 i; + for (i = 0 ;; ++i) { - if (maxlen >= 0 && i >= maxlen - 1) + if (fixedLen >= 0 && i >= fixedLen - 1) break; - short chr = readUint16(); + atUint16 chr = readUint16(); if (chr) tmp.push_back(chr); else break; - - i++; }; + if (fixedLen >= 0 && i < fixedLen) + seek(fixedLen - i); + utf8::utf16to8(tmp.begin(), tmp.end(), back_inserter(ret)); return ret; diff --git a/src/Athena/MemoryReader.cpp b/src/Athena/MemoryReader.cpp index 40dd369..7b0b630 100644 --- a/src/Athena/MemoryReader.cpp +++ b/src/Athena/MemoryReader.cpp @@ -493,7 +493,7 @@ atVec4f MemoryReader::readVec4f() return result; } -std::string MemoryReader::readUnicode(atInt32 maxlen) +std::string MemoryReader::readUnicode(atInt32 fixedLen) { if (!m_data) loadData(); @@ -502,11 +502,10 @@ std::string MemoryReader::readUnicode(atInt32 maxlen) std::vector tmp; atUint16 chr = readUint16(); - atInt32 i = 0; - - for (;;) + atInt32 i; + for (i = 0 ;; ++i) { - if (maxlen >= 0 && i >= maxlen - 1) + if (fixedLen >= 0 && i >= fixedLen - 1) break; if (!chr) @@ -514,50 +513,54 @@ std::string MemoryReader::readUnicode(atInt32 maxlen) tmp.push_back(chr); chr = readUint16(); - i++; } + if (fixedLen >= 0 && i < fixedLen) + seek(fixedLen - i); + utf8::utf16to8(tmp.begin(), tmp.end(), back_inserter(ret)); return ret; } -std::string MemoryReader::readString(atInt32 maxlen) +std::string MemoryReader::readString(atInt32 fixedLen) { std::string ret; atUint8 chr = readByte(); - atInt32 i = 0; - - while (chr != 0) + atInt32 i; + for (i = 0 ; chr != 0 ; ++i) { - if (maxlen >= 0 && i >= maxlen - 1) + if (fixedLen >= 0 && i >= fixedLen - 1) break; ret += chr; chr = readByte(); - i++; } + if (fixedLen >= 0 && i < fixedLen) + seek(fixedLen - i); + return ret; } -std::wstring MemoryReader::readWString(atInt32 maxlen) +std::wstring MemoryReader::readWString(atInt32 fixedLen) { std::wstring ret; atUint16 chr = readUint16(); - atInt32 i = 0; - - while (chr != 0) + atInt32 i; + for (i = 0 ; chr != 0 ; ++i) { - if (maxlen >= 0 && i >= maxlen - 1) + if (fixedLen >= 0 && i >= fixedLen - 1) break; ret += chr; chr = readUint16(); - i++; } + if (fixedLen >= 0 && i < fixedLen) + seek(fixedLen - i); + return ret; }