static_handler: implement basic version

Implement basic version that simply caches the information written to
it.  Later, error conditions will need addressing.

Change-Id: Ife27b72a6a685e500a0a16c55975c9f4469b206a
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/static_handler.cpp b/static_handler.cpp
index c4fa602..616a882 100644
--- a/static_handler.cpp
+++ b/static_handler.cpp
@@ -11,18 +11,65 @@
 bool StaticLayoutHandler::open(const std::string& path)
 {
     this->path = path;
-    return false;
+
+    if (stagedOutput.is_open())
+    {
+        /* This wasn't properly closed somehow.
+         * TODO: Throw an error or just reset the state?
+         */
+        return false;
+    }
+
+    /* using ofstream no need to set out */
+    stagedOutput.open(stagedFilename, std::ios::binary);
+    if (stagedOutput.bad())
+    {
+        /* TODO: Oh no! Care about this. */
+        return false;
+    }
+
+    /* We were able to open the file for staging.
+     * TODO: We'll need to do other stuff to eventually.
+     */
+    return true;
 }
 
 void StaticLayoutHandler::close()
 {
+    if (stagedOutput.is_open())
+    {
+        stagedOutput.close();
+    }
     return;
 }
 
 bool StaticLayoutHandler::write(std::uint32_t offset,
                                 const std::vector<std::uint8_t>& data)
 {
-    return false;
+    if (!stagedOutput.is_open())
+    {
+        return false;
+    }
+
+    /* We could track this, but if they write in a scattered method, this is
+     * easier.
+     */
+    stagedOutput.seekp(offset, std::ios_base::beg);
+    if (!stagedOutput.good())
+    {
+        /* the documentation wasn't super clear on fail vs bad in these cases,
+         * so let's only be happy with goodness.
+         */
+        return false;
+    }
+
+    stagedOutput.write(reinterpret_cast<const char*>(data.data()), data.size());
+    if (!stagedOutput.good())
+    {
+        return false;
+    }
+
+    return true;
 }
 
 } // namespace blobs