blob: 266d2e44d3fbca12b96c1b008ad49887d262fac7 [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"
Alex Deymoa7658442014-04-17 15:49:37 -07006
Andrew de los Reyes785bc352010-05-26 12:34:53 -07007#include <map>
8#include <string>
9#include <vector>
Alex Deymoa7658442014-04-17 15:49:37 -070010
11#include <base/file_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070012#include <base/strings/string_split.h>
13#include <base/strings/string_util.h>
Andrew de los Reyes785bc352010-05-26 12:34:53 -070014
Alex Deymoa7658442014-04-17 15:49:37 -070015#include "update_engine/utils.h"
16
Andrew de los Reyes785bc352010-05-26 12:34:53 -070017using std::map;
18using std::string;
19using std::vector;
20
21namespace chromeos_update_engine {
Andrew de los Reyes785bc352010-05-26 12:34:53 -070022
Alex Deymoa7658442014-04-17 15:49:37 -070023bool KeyValueStore::Load(const string& filename) {
24 string file_data;
25 if (!base::ReadFileToString(base::FilePath(filename), &file_data))
26 return false;
27
Andrew de los Reyes785bc352010-05-26 12:34:53 -070028 // Split along '\n', then along '='
Andrew de los Reyes785bc352010-05-26 12:34:53 -070029 vector<string> lines;
Alex Deymoa7658442014-04-17 15:49:37 -070030 base::SplitStringDontTrim(file_data, '\n', &lines);
31 for (auto& it : lines) {
32 if (it.empty() || it[0] == '#')
33 continue;
34 string::size_type pos = it.find('=');
Andrew de los Reyes785bc352010-05-26 12:34:53 -070035 if (pos == string::npos)
36 continue;
Alex Deymoa7658442014-04-17 15:49:37 -070037 store_[it.substr(0, pos)] = it.substr(pos + 1);
Andrew de los Reyes785bc352010-05-26 12:34:53 -070038 }
Alex Deymoa7658442014-04-17 15:49:37 -070039 return true;
Andrew de los Reyes785bc352010-05-26 12:34:53 -070040}
41
Alex Deymoa7658442014-04-17 15:49:37 -070042bool KeyValueStore::Save(const string& filename) const {
43 string data;
44 for (auto& it : store_)
45 data += it.first + "=" + it.second + "\n";
46
47 return utils::WriteFile(filename.c_str(), data.c_str(), data.size());
Andrew de los Reyes785bc352010-05-26 12:34:53 -070048}
49
Alex Deymoa7658442014-04-17 15:49:37 -070050bool KeyValueStore::GetString(const string& key, string* value) const {
51 auto it = store_.find(key);
52 if (it == store_.end())
53 return false;
54 *value = it->second;
55 return true;
56}
57
58void KeyValueStore::SetString(const string& key, const string& value) {
59 store_[key] = value;
60}
61
62bool KeyValueStore::GetBoolean(const string& key, bool* value) const {
63 auto it = store_.find(key);
64 if (it == store_.end())
65 return false;
66 if (it->second == "true") {
67 *value = true;
68 return true;
69 } else if (it-> second == "false") {
70 *value = false;
71 return true;
72 }
73
74 return false;
75}
76
77void KeyValueStore::SetBoolean(const string& key, bool value) {
78 store_[key] = value ? "true" : "false";
79}
80
Andrew de los Reyes785bc352010-05-26 12:34:53 -070081} // namespace chromeos_update_engine