[Box Backup-commit] COMMIT r3578 - box/trunk/lib/common

subversion at boxbackup.org subversion at boxbackup.org
Wed Jun 10 20:46:15 BST 2015


Author: chris
Date: 2015-06-10 19:46:15 +0000 (Wed, 10 Jun 2015)
New Revision: 3578

Modified:
   box/trunk/lib/common/Archive.h
Log:
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.

Modified: box/trunk/lib/common/Archive.h
===================================================================
--- box/trunk/lib/common/Archive.h	2015-06-10 19:46:11 UTC (rev 3577)
+++ box/trunk/lib/common/Archive.h	2015-06-10 19:46:15 UTC (rev 3578)
@@ -158,10 +158,35 @@
 		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];




More information about the Boxbackup-commit mailing list