AU: Common code to parse simple key/value store files
We use a few files, like /etc/lsb-release, that have a simple
key/value store format: each line is a key/value pair and the
key/value are separated by '='. This change make some common code to
parse (and assemble) these files, which is used in one place now but
will be used in another place soon, too.
BUG=None
TEST=attached unittests
Review URL: http://codereview.chromium.org/2105016
diff --git a/SConstruct b/SConstruct
index 1cf837a..53004ab 100644
--- a/SConstruct
+++ b/SConstruct
@@ -213,6 +213,7 @@
omaha_response_handler_action.cc
postinstall_runner_action.cc
set_bootable_flag_action.cc
+ simple_key_value_store.cc
split_file_writer.cc
subprocess.cc
tarjan.cc
@@ -245,6 +246,7 @@
omaha_response_handler_action_unittest.cc
postinstall_runner_action_unittest.cc
set_bootable_flag_action_unittest.cc
+ simple_key_value_store_unittest.cc
split_file_writer_unittest.cc
subprocess_unittest.cc
tarjan_unittest.cc
diff --git a/omaha_request_prep_action.cc b/omaha_request_prep_action.cc
index 2b019b3..25772d0 100644
--- a/omaha_request_prep_action.cc
+++ b/omaha_request_prep_action.cc
@@ -5,10 +5,13 @@
#include "update_engine/omaha_request_prep_action.h"
#include <sys/utsname.h>
#include <errno.h>
+#include <map>
#include <string>
#include "base/string_util.h"
+#include "update_engine/simple_key_value_store.h"
#include "update_engine/utils.h"
+using std::map;
using std::string;
// This gathers local system information and prepares info used by the
@@ -102,19 +105,10 @@
string file_data;
if (!utils::ReadFileToString(root_ + files[i], &file_data))
continue;
- string::size_type pos = 0;
- if (!utils::StringHasPrefix(file_data, key + "=")) {
- pos = file_data.find(string("\n") + key + "=");
- if (pos != string::npos)
- pos++; // advance past \n
- }
- if (pos == string::npos)
- continue;
- pos += key.size() + 1; // advance past the key and the '='
- string::size_type endpos = file_data.find('\n', pos);
- string::size_type length =
- (endpos == string::npos ? string::npos : endpos - pos);
- return file_data.substr(pos, length);
+
+ map<string, string> data = simple_key_value_store::ParseString(file_data);
+ if (utils::MapContainsKey(data, key))
+ return data[key];
}
// not found
return default_value;
diff --git a/simple_key_value_store.cc b/simple_key_value_store.cc
new file mode 100644
index 0000000..0315045
--- /dev/null
+++ b/simple_key_value_store.cc
@@ -0,0 +1,47 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "update_engine/simple_key_value_store.h"
+#include <map>
+#include <string>
+#include <vector>
+#include "base/string_util.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) {
+ // Split along '\n', then along '='
+ std::map<std::string, std::string> ret;
+ vector<string> lines;
+ SplitStringDontTrim(str, '\n', &lines);
+ for (vector<string>::const_iterator it = lines.begin();
+ it != lines.end(); ++it) {
+ string::size_type pos = it->find('=');
+ if (pos == string::npos)
+ continue;
+ ret[it->substr(0, pos)] = it->substr(pos + 1);
+ }
+ return ret;
+}
+
+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;
+}
+
+} // namespace simple_key_value_store
+} // namespace chromeos_update_engine
diff --git a/simple_key_value_store.h b/simple_key_value_store.h
new file mode 100644
index 0000000..6453eee
--- /dev/null
+++ b/simple_key_value_store.h
@@ -0,0 +1,26 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// These functions can parse a blob of data that's formatted as a simple
+// key value store. Each key/value pair is stored on its own line and
+// separated by the first '=' on the line.
+
+#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_SIMPLE_KEY_VALUE_STORE_H__
+#define CHROMEOS_PLATFORM_UPDATE_ENGINE_SIMPLE_KEY_VALUE_STORE_H__
+
+#include <map>
+#include <string>
+
+namespace chromeos_update_engine {
+namespace simple_key_value_store {
+
+// Parses a string.
+std::map<std::string, std::string> ParseString(const std::string& str);
+
+std::string AssembleString(const std::map<std::string, std::string>& data);
+
+} // namespace simple_key_value_store
+} // namespace chromeos_update_engine
+
+#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_SIMPLE_KEY_VALUE_STORE_H__
diff --git a/simple_key_value_store_unittest.cc b/simple_key_value_store_unittest.cc
new file mode 100644
index 0000000..b8ca384
--- /dev/null
+++ b/simple_key_value_store_unittest.cc
@@ -0,0 +1,45 @@
+// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <map>
+#include <string>
+#include <gtest/gtest.h>
+#include "base/string_util.h"
+#include "update_engine/simple_key_value_store.h"
+
+using std::map;
+using std::string;
+
+namespace chromeos_update_engine {
+
+class SimpleKeyValueStoreTest : public ::testing::Test {};
+
+TEST(SimpleKeyValueStoreTest, SimpleTest) {
+ string blob = "A=B\nC=\n=\nFOO=BAR=BAZ\nBAR=BAX\nMISSING=NEWLINE";
+ map<string, string> parts = simple_key_value_store::ParseString(blob);
+ string combined = simple_key_value_store::AssembleString(parts);
+ map<string, string> combined_parts =
+ simple_key_value_store::ParseString(combined);
+ map<string, string>* maps[] = { &parts, &combined_parts };
+ for (size_t i = 0; i < arraysize(maps); i++) {
+ map<string, string>* test_map = maps[i];
+ EXPECT_EQ(6, test_map->size()) << "i = " << i;
+ EXPECT_EQ("B", (*test_map)["A"]) << "i = " << i;
+ EXPECT_EQ("", (*test_map)["C"]) << "i = " << i;
+ EXPECT_EQ("", (*test_map)[""]) << "i = " << i;
+ EXPECT_EQ("BAR=BAZ", (*test_map)["FOO"]) << "i = " << i;
+ EXPECT_EQ("BAX", (*test_map)["BAR"]) << "i = " << i;
+ EXPECT_EQ("NEWLINE", (*test_map)["MISSING"]) << "i = " << i;
+ }
+}
+
+TEST(SimpleKeyValueStoreTest, EmptyTest) {
+ map<string, string> empty_map = simple_key_value_store::ParseString("");
+ EXPECT_TRUE(empty_map.empty());
+ string str = simple_key_value_store::AssembleString(empty_map);
+ if (!str.empty())
+ EXPECT_EQ("\n", str); // Optionally may end in newline
+}
+
+} // namespace chromeos_update_engine