diff options
author | Chris Wilson <chris+github@qwirx.com> | 2015-06-10 19:46:15 +0000 |
---|---|---|
committer | Chris Wilson <chris+github@qwirx.com> | 2015-06-10 19:46:15 +0000 |
commit | cc5787c423774dd709eab97511fd8a2fbf3d31d2 (patch) | |
tree | b50b937e3ef71caef425c5d877e149b1400feb50 /lib/common | |
parent | 8ee901c542865e320574941c3d2214c15ba1c7f9 (diff) |
Add support for optional items in Archives.
If the item is not present (the end of the Archive is reached instead) then a
default value is returned instead of throwing an exception.
Diffstat (limited to 'lib/common')
-rw-r--r-- | lib/common/Archive.h | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/lib/common/Archive.h b/lib/common/Archive.h index 858d7f3a..76b069a0 100644 --- a/lib/common/Archive.h +++ b/lib/common/Archive.h @@ -158,10 +158,35 @@ public: Read(privItem); rItemOut = privItem; } + void ReadIfPresent(std::string &rItemOut, const std::string& ValueIfNotPresent) + { + Read(rItemOut, &ValueIfNotPresent); + } void Read(std::string &rItemOut) { + Read(rItemOut, NULL); + } +private: + void Read(std::string &rItemOut, const std::string* pValueIfNotPresent) + { int size; - Read(size, mTimeout); + int bytesRead; + if(!mrStream.ReadFullBuffer(&size, sizeof(size), &bytesRead, mTimeout)) + { + if(bytesRead == 0 && pValueIfNotPresent != NULL) + { + // item is simply not present + rItemOut = *pValueIfNotPresent; + return; + } + else + { + // bad number of remaining bytes + THROW_EXCEPTION(CommonException, + ArchiveBlockIncompleteRead) + } + } + size = ntohl(size); // Assume most strings are relatively small char buf[256]; |