Revisit the simple_key_value_store into a class.

The simple_key_value_store was implemented as two separated functions
to parse and assemble a string containing several lines of key=value
pairs. The representation of that was passed to the caller as a
map<string, string> who would use the map operations to modify it.
Also, the inteded use for these strings was to parse and write text
files on the filesystem.

This key=value store is used to store strings and boolean values,
and will be reused for the policy manager config provider.

This patch reworks those functions as a class and adds support for
reading and writing boolean values and does the file read and write
operations as well.

BUG=chromium:359674
TEST=Unittest extended.

Change-Id: I4890c4a4ca81c1a4857e9893ea827c3fa7815aab
Reviewed-on: https://chromium-review.googlesource.com/195489
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
diff --git a/simple_key_value_store.cc b/simple_key_value_store.cc
index 24e9bc9..266d2e4 100644
--- a/simple_key_value_store.cc
+++ b/simple_key_value_store.cc
@@ -3,46 +3,79 @@
 // found in the LICENSE file.
 
 #include "update_engine/simple_key_value_store.h"
+
 #include <map>
 #include <string>
 #include <vector>
+
+#include <base/file_util.h>
 #include <base/strings/string_split.h>
 #include <base/strings/string_util.h>
 
+#include "update_engine/utils.h"
+
 using std::map;
 using std::string;
 using std::vector;
 
 namespace chromeos_update_engine {
-namespace simple_key_value_store {
 
-// Parses a string. 
-map<std::string, std::string> ParseString(const string& str) {
+bool KeyValueStore::Load(const string& filename) {
+  string file_data;
+  if (!base::ReadFileToString(base::FilePath(filename), &file_data))
+    return false;
+
   // Split along '\n', then along '='
-  std::map<std::string, std::string> ret;
   vector<string> lines;
-  base::SplitStringDontTrim(str, '\n', &lines);
-  for (vector<string>::const_iterator it = lines.begin();
-       it != lines.end(); ++it) {
-    string::size_type pos = it->find('=');
+  base::SplitStringDontTrim(file_data, '\n', &lines);
+  for (auto& it : lines) {
+    if (it.empty() || it[0] == '#')
+      continue;
+    string::size_type pos = it.find('=');
     if (pos == string::npos)
       continue;
-    ret[it->substr(0, pos)] = it->substr(pos + 1);
+    store_[it.substr(0, pos)] = it.substr(pos + 1);
   }
-  return ret;
+  return true;
 }
 
-string AssembleString(const std::map<string, string>& data) {
-  string ret;
-  for (std::map<string, string>::const_iterator it = data.begin();
-       it != data.end(); ++it) {
-    ret += it->first;
-    ret += "=";
-    ret += it->second;
-    ret += "\n";
-  }
-  return ret;
+bool KeyValueStore::Save(const string& filename) const {
+  string data;
+  for (auto& it : store_)
+    data += it.first + "=" + it.second + "\n";
+
+  return utils::WriteFile(filename.c_str(), data.c_str(), data.size());
 }
 
-}  // namespace simple_key_value_store
+bool KeyValueStore::GetString(const string& key, string* value) const {
+  auto it = store_.find(key);
+  if (it == store_.end())
+    return false;
+  *value = it->second;
+  return true;
+}
+
+void KeyValueStore::SetString(const string& key, const string& value) {
+  store_[key] = value;
+}
+
+bool KeyValueStore::GetBoolean(const string& key, bool* value) const {
+  auto it = store_.find(key);
+  if (it == store_.end())
+    return false;
+  if (it->second == "true") {
+    *value = true;
+    return true;
+  } else if (it-> second == "false") {
+    *value = false;
+    return true;
+  }
+
+  return false;
+}
+
+void KeyValueStore::SetBoolean(const string& key, bool value) {
+  store_[key] = value ? "true" : "false";
+}
+
 }  // namespace chromeos_update_engine