summaryrefslogtreecommitdiff
path: root/lib/common/Archive.h
diff options
context:
space:
mode:
authorChris Wilson <chris+github@qwirx.com>2015-06-10 19:46:15 +0000
committerChris Wilson <chris+github@qwirx.com>2015-06-10 19:46:15 +0000
commitcc5787c423774dd709eab97511fd8a2fbf3d31d2 (patch)
treeb50b937e3ef71caef425c5d877e149b1400feb50 /lib/common/Archive.h
parent8ee901c542865e320574941c3d2214c15ba1c7f9 (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/Archive.h')
-rw-r--r--lib/common/Archive.h27
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];