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

subversion at boxbackup.org subversion at boxbackup.org
Mon Oct 22 21:47:08 BST 2012


Author: chris
Date: 2012-10-22 21:47:08 +0100 (Mon, 22 Oct 2012)
New Revision: 3121

Modified:
   box/trunk/lib/common/Archive.h
Log:
Add helper method to read a value that might not be present in an Archive
(end of Archive) to avoid duplicating this code many times.


Modified: box/trunk/lib/common/Archive.h
===================================================================
--- box/trunk/lib/common/Archive.h	2012-10-22 20:46:11 UTC (rev 3120)
+++ box/trunk/lib/common/Archive.h	2012-10-22 20:47:08 UTC (rev 3121)
@@ -81,7 +81,7 @@
 		int privItem;
 		Read(privItem);
 
-		if (privItem)
+		if(privItem)
 		{
 			rItemOut = true;
 		}
@@ -90,6 +90,12 @@
 			rItemOut = false;
 		}
 	}
+	void ReadIfPresent(bool &rItemOut, bool ValueIfNotPresent)
+	{
+		int privItem;
+		ReadIfPresent(privItem, ValueIfNotPresent ? 1 : 0);
+		rItemOut = privItem ? true : false;
+	}
 	void ReadExact(uint32_t &rItemOut) { Read((int&)rItemOut); }
 	void Read(int &rItemOut)
 	{
@@ -100,6 +106,25 @@
 		}
 		rItemOut = ntohl(privItem);
 	}
+	void ReadIfPresent(int &rItemOut, int ValueIfNotPresent)
+	{
+		int32_t privItem;
+		int bytesRead;
+		if(mrStream.ReadFullBuffer(&privItem, sizeof(privItem), &bytesRead))
+		{
+			rItemOut = ntohl(privItem);
+		}
+		else if(bytesRead == 0)
+		{
+			// item is simply not present
+			rItemOut = ValueIfNotPresent;
+		}
+		else
+		{
+			// bad number of remaining bytes
+			THROW_EXCEPTION(CommonException, ArchiveBlockIncompleteRead)
+		}
+	}
 	void Read(int64_t &rItemOut)
 	{
 		int64_t privItem;




More information about the Boxbackup-commit mailing list