blob: 78e191ea3e401867274e465371d23a0d3db8e647 [file] [log] [blame]
Chris Masoned903c3b2011-05-12 15:35:46 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
Andrew de los Reyes785bc352010-05-26 12:34:53 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/simple_key_value_store.h"
6#include <map>
7#include <string>
8#include <vector>
Chris Masoned903c3b2011-05-12 15:35:46 -07009#include "base/string_split.h"
Andrew de los Reyes785bc352010-05-26 12:34:53 -070010#include "base/string_util.h"
11
12using std::map;
13using std::string;
14using std::vector;
15
16namespace chromeos_update_engine {
17namespace simple_key_value_store {
18
19// Parses a string.
20map<std::string, std::string> ParseString(const string& str) {
21 // Split along '\n', then along '='
22 std::map<std::string, std::string> ret;
23 vector<string> lines;
Chris Masoned903c3b2011-05-12 15:35:46 -070024 base::SplitStringDontTrim(str, '\n', &lines);
Andrew de los Reyes785bc352010-05-26 12:34:53 -070025 for (vector<string>::const_iterator it = lines.begin();
26 it != lines.end(); ++it) {
27 string::size_type pos = it->find('=');
28 if (pos == string::npos)
29 continue;
30 ret[it->substr(0, pos)] = it->substr(pos + 1);
31 }
32 return ret;
33}
34
35string AssembleString(const std::map<string, string>& data) {
36 string ret;
37 for (std::map<string, string>::const_iterator it = data.begin();
38 it != data.end(); ++it) {
39 ret += it->first;
40 ret += "=";
41 ret += it->second;
42 ret += "\n";
43 }
44 return ret;
45}
46
47} // namespace simple_key_value_store
48} // namespace chromeos_update_engine